package com.mrbussy.ratp;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.swing.JInternalFrame;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import com.mrbussy.ratp.core.Resource;
import com.mrbussy.ratp.core.ResourceView;
import com.mrbussy.ratp.io.PlanningFileHandler;
/**
* Main window for the planning. This class is the planning window as well as
* the planning container.
*
* @author Rudi Middel
* @see javax.swing.JInternalFrame
* @see com.mrbussy.ratp.IRATPPlanning
*/
public class RATPPlanning extends JInternalFrame implements IRATPPlanning {
/**
* Version ID needed to distinguish versions
*/
private static final long serialVersionUID = 1L;
/**
* List of resources
*/
private Hashtable<String, Resource> resources = null;
/**
* Filename of the current planning
*/
private String filename = null;
/**
* Public constructor with the filename of the planning to open.
*
* @param filename
* @throws Exception
*/
public RATPPlanning(String filename) {
super("New planning", true, true, true, true);
this.setSize(new Dimension(400, 400));
this.filename = filename;
// Create menu
}
/**
* Empty constructor
*/
public RATPPlanning() {
this(null);
}
/*
* Implementation of the getResources function (non-Javadoc)
*
* @see com.mrbussy.ratp.IRATPPlanning#getResources() @return A list of
* resources
*/
public Hashtable<String, Resource> getResources() {
return this.resources;
}
/*
* (non-Javadoc)
*
* @see com.mrbussy.ratp.IRATPPlanning#setResources(java.util.Hashtable)
*/
public void setResources(Hashtable<String, Resource> resources) {
this.resources = resources;
}
/**
* Save the current planning into a planning file
*/
public void save(String filename) {
try {
if ((this.filename != filename) && (filename != null))
this.filename = filename;
// Create a new planning file handler
PlanningFileHandler pfh = new PlanningFileHandler(this);
// Open a new file output stream
OutputStream stream = new FileOutputStream(this.filename);
// Save the current planning to the stream
pfh.save(stream);
// Close the current stream
stream.close();
// Clean up
pfh = null;
stream = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Load the current planning from file
*
* @param filename
* of the planning to load
* @throws Exception
*/
public void Load(String filename) throws Exception {
// Begin with storing the filename for later use
this.filename = filename;
if (new File(filename).exists()) {
// file is a real file
// Create the PlanningFileHandler instance
PlanningFileHandler pfh = new PlanningFileHandler(this);
InputStream stream;
// Open the filestream with the file
stream = new FileInputStream(filename);
// Load the planning into this window
pfh.load(stream);
// Close the current stream
stream.close();
// When loaded: Discard the file handler;
pfh = null;
}
ShowResource();
}
private void ShowResource() {
// Display all information
ResourceView view = new ResourceView(this, resources);
view.setSize(200, this.getSize().height);
this.add(view, BorderLayout.WEST);
}
}