projecthandler.java
字号:
package piy;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.lang.reflect.*;
/**
* Handles project loading/saving. Only one of these can exist at once - its instance can be
* retrieved through the getInstance() static method.
* @author David Vivash
* @version 1.0.3, 29/04/01
*/
public class ProjectHandler {
private Project project = null;
private static ProjectHandler instance = null;
private FileDialog fileDialog = null;
//set to true as soon as a change is made to the project
private boolean changed = false;
/**
* Get the only instance of ProjectHandler.
*/
public static ProjectHandler getInstance() {
if (instance == null) instance = new ProjectHandler();
return instance;
}
/**
* Instantiate the ProjectHandler. No setup code required.
*/
private ProjectHandler() {
}
/**
* Compile the current project into an application.
* @return the compiled project as an application
*/
public Application compileProject() {
//Create a clone of the project via serialization
File oldName = project.getFileName();
project.setFileName(new File("temp.out"));
Project oldProject = project;
saveProject();
loadProject(new File("temp.out"));
//Get rid of the piy glass panes
java.util.List win = project.getGuiNamer().getValues();
for (int i=0; i<win.size(); i++)
if (win.get(i) instanceof UserWindow)
((UserWindow)win.get(i)).setGlassPane(new MiniGlassPane());
//Create our new application
Application toReturn = new Application();
toReturn.setMainWindow(project.getMainWindow());
//Add all windows to the app
java.util.List windows = project.getGuiNamer().getValues();
for (int i=0; i<windows.size(); i++)
if (windows.get(i) instanceof UserWindow)
toReturn.addWindow((UserWindow)windows.get(i));
PIYNamer guiNamer = project.getGuiNamer();
String[] components = guiNamer.getNames();
//set up the event listeners for each component.
for (int i=0; i<components.length; i++) {
Object component = guiNamer.getValue(components[i]);
if (component instanceof Component) {
HashMap listeners = project.getEventListeners(component);
toReturn.addComponentMapping((Component)component, listeners);
}
}
//restore the project to its old state
project = oldProject;
project.setFileName(oldName);
//return the compiled application
return toReturn;
}
/**
* Construct a new project for editting in PIY.
* @return a new project object
*/
public void newProject() {
if (makeSaveEnquiry()) { //ask if user want to save
closeProject(false);
project = new Project();
project.setMainWindow(newWindow());
changed = false;
}
}
/**
* Retrieves the Project currently being editted. If no project is currently being editted
* this method first calls newProject(), which ensures that this method never returns null.
* @return the project currently being editted
*/
public Project getProject() {
if (project == null) newProject();
return project;
}
/**
* Save the Project currently being editted. If the project does not yet have a filename
* this method will ask the user to supply one.
*/
public void saveProject() {
if (project.getFileName() == null) saveProjectAs();
if (project.getFileName() != null) {
UserWindowListener.getInstance().setCurrentWindow(null);
UserWindowListener.getInstance().selectComponent(null);
//Get rid of glass panes
java.util.List win = project.getGuiNamer().getValues();
for (int i=0; i<win.size(); i++)
if (win.get(i) instanceof UserWindow)
((UserWindow)win.get(i)).setGlassPane(new MiniGlassPane());
boolean error = false;
String message = "";
try{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(project.getFileName()));
oos.writeObject(project);
oos.flush();
oos.close();
} catch (InvalidClassException e) {
System.err.println("Error saving project = invalid class exception");
error = true;
} catch (NotSerializableException e) {
System.err.println("Error saving project! = class not serializable " + e.getMessage());
error = true;
message = e.getMessage() + "could not be added";
} catch (Exception e) {
System.err.println("Error saving project");
e.printStackTrace();
error = true;
}
//inform the user of any error
if (error) {
JOptionPane pane = new JOptionPane(
new String[] {"There was an error saving the project", message},
JOptionPane.ERROR_MESSAGE);
JDialog dialog = pane.createDialog(null, "Cannot Save");
dialog.setResizable(false);
dialog.show();
}
//Add glass panes back to all windows
for (int i=0; i<win.size(); i++)
if (win.get(i) instanceof UserWindow)
UserWindowListener.getInstance().listenOnFrame((UserWindow)win.get(i));
changed = false;
}
}
/**
* Saves the project currently being editted under a different filename, which the user will
* be asked for.
*/
public void saveProjectAs() {
//get a valid filename, then call saveProject
fileDialog = getFileDialog(new Frame());
fileDialog.setMode(FileDialog.SAVE);
try{
fileDialog.show();
if (fileDialog.getFile() != null)
project.setFileName(new File(fileDialog.getDirectory(), fileDialog.getFile()));
if (project.getFileName() != null) saveProject();
} catch (Exception e) {
//inform the user of the error
JOptionPane pane = new JOptionPane("There was an error saving the project", JOptionPane.ERROR_MESSAGE);
JDialog dialog = pane.createDialog(null, "Cannot Save");
dialog.setResizable(false);
dialog.show();
}
}
/**
* Loads a project. Implicitly closes the project currently being editted, via a call to
* closeProject().
* @param fileName the filename of the project to be loaded.
*/
private void loadProject(File fileName) {
if ((fileName == null) || !fileName.exists()) {
JOptionPane pane = new JOptionPane("The specified project filename doesn't exist", JOptionPane.ERROR_MESSAGE);
JDialog dialog = pane.createDialog(null, "Cannot Load");
dialog.setResizable(false);
dialog.show();
return;
}
//don't need save enquiry - this method is called privately
closeProject(false);
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
project = (Project)ois.readObject();
ois.close();
} catch (Exception e) {
JOptionPane pane = new JOptionPane("There was an error loading the project", JOptionPane.ERROR_MESSAGE);
JDialog dialog = pane.createDialog(null, "Cannot Load");
dialog.setResizable(false);
dialog.show();
e.printStackTrace();
changed = false;
newProject();
}
}
/**
* Asks the user which project to load, and then loads it. If the user does not
* specify a file, this method will not cause the project currently being editted to
* close, and will return back to its previous state.
*/
public void loadProject() {
if (makeSaveEnquiry()) { //ask the user to save the current project if it has changed
//get a valid filename, then call loadProject
fileDialog = getFileDialog(new Frame());
fileDialog.setMode(FileDialog.LOAD);
try {
fileDialog.show();
if (fileDialog.getFile() == null) return; //the user selected "Cancel"
File loadFileName = new File(fileDialog.getDirectory(), fileDialog.getFile());
if (loadFileName != null) loadProject(loadFileName);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "There was an error loading the project", "Cannot Load", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
changed = false;
newProject();
}
//Add glass panes to all windows
java.util.List win = project.getGuiNamer().getValues();
for (int i=0; i<win.size(); i++)
if (win.get(i) instanceof UserWindow)
UserWindowListener.getInstance().listenOnFrame((UserWindow)win.get(i));
changed = false;
}
}
/**
* Close the current project.
* @param enquiry set this to true if the call to this method should first make
* a call to makeSaveEnquiry. For most purposes this is true.
* @return true if the project was closed, false if the user decided to cancel the
* close operation. Calling methods should cancel their course of action if this
* method returns false.
*/
public boolean closeProject(boolean enquiry) {
if (project != null) {
if (enquiry ? makeSaveEnquiry() : true) {
//Dispose of all windows
java.util.List win = project.getGuiNamer().getValues();
for (int i=0; i<win.size(); i++)
if (win.get(i) instanceof UserWindow)
((UserWindow)win.get(i)).dispose();
project = null;
return true;
} else return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -