projecthandler.java
字号:
}
return true;
}
/**
* Get the currently in-use file dialog. "Parent" may be null if a file dialog
* already exists (since it will be ignored).
* @param parent the parent frame for modal puposes
* @return the file dialog
*/
private FileDialog getFileDialog(Frame parent) {
if (fileDialog == null) fileDialog = new FileDialog(parent);
return fileDialog;
}
/**
* Anything that changes a project should call this method as soon as
* the change is made.
* If the project is subsequently closed, the user will be asked if they wish
* to save the changes made.
*/
public void projectChanged() { changed = true; }
private boolean makingEnquiry = false;
/**
* If an action occurs that may result in the current project being lost,
* a call to this method will ask the user whether they want to save or not.
* This method will save if the user says so, but if the user selects
* "cancel" it means they don't want to go through with the original action -
* this method will return false. It's up to the calling method to cancel
* the action in this case.
* @return true if the user selects "Yes" or "No", false if the user selects
* "Cancel"
*/
private boolean makeSaveEnquiry() {
if (changed && !makingEnquiry) {
boolean yesOrNo = true;
makingEnquiry = true;
JOptionPane pane = new JOptionPane(
new String[] {"The current project has not yet been saved.", "Do you wish to save?"},
JOptionPane.WARNING_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION);
JDialog dialog = pane.createDialog(null, "Please Confirm");
dialog.setResizable(false);
dialog.show();
//Get the button the user clicked
int answer = 0;
Object selectedValue = pane.getValue();
if(selectedValue == null) answer = JOptionPane.CLOSED_OPTION;
else answer = ((Integer)selectedValue).intValue();
switch (answer) {
case JOptionPane.YES_OPTION:
saveProject();
case JOptionPane.NO_OPTION:
break;
case JOptionPane.CANCEL_OPTION:
case JOptionPane.CLOSED_OPTION:
yesOrNo = false;
break;
}
makingEnquiry = false;
return yesOrNo;
} else return true;
}
/**
* Add a new user window to the project. The new window will be named and have
* the standard UserWindowListener attached, and will have been resized.
* @return the new UserWindow object that has been added to the project
*/
protected UserWindow newWindow() {
UserWindow newWindow = new UserWindow();
String name = project.addWindow(newWindow);
newWindow.setTitle(name);
newWindow.setBounds(210,120,400,320);
newWindow.show();
UserWindowListener.getInstance().listenOnFrame(newWindow);
projectChanged();
return newWindow;
}
/**
* Gets the project's gui namer
* @return the gui namer of the current project
*/
public PIYNamer getGuiNamer() { return project == null ? null : project.getGuiNamer(); }
}
/**
* Representation of the whole user project. From here we should be able to
* get information on all of the user windows and user components. Also, there
* should only ever be one instance of this class at any one time.
* @author David Vivash
* @version 1.0.1, 12/02/01
*/
class Project implements Serializable
{
//-------------------------------------------//
// PIY specific methods //
//-------------------------------------------//
private PIYNamer guiNamer = new PIYNamer(32);
private PIYNamer actionListNamer = new PIYNamer(8);
private PIYNamer memoryNamer = new PIYNamer(8);
private HashMap componentsToEventListeners = new HashMap(8);
private File fileName = null;
private JFrame mainWindow = null;
/**
* Gets the namer object used to name windows and componentns in this project.
* @return the object used to manage the names of the user windows
*/
protected PIYNamer getGuiNamer() {
return guiNamer;
}
/**
* Gets the namer object used to name action lists in this project.
* @return the object used to manage the names of the action lists
*/
protected PIYNamer getActionListNamer() {
return actionListNamer;
}
/**
* The event listener map returned for a component is a map from PIYEventListener classes
* (Class type) to a Hashmap, which maps Strings (event names) to PIYAction lists). Thus the
* hashmap returned by this method stores mappings to more hashmaps. This method sets up
* maps for components not recognised, and so does not return null. However, it will be
* initially set up with no eventListener->hashmap mappings, so this will need to be done
* to make the map useful. The policing of this map is currently done on faith - maybe
* a specific class which handles the maps itself could replace this system.
* @param component the component for which the hashmap should be retrieved
* @return the hashmap mapping PIYEventListeners to hashmaps, or a blank hashmap if none has
* been set up yet for the specified component
*/
protected HashMap getEventListeners(Object component) {
HashMap toReturn = (HashMap)componentsToEventListeners.get(component);
//if component not found in the mappings
if (toReturn == null) componentsToEventListeners.put(component, toReturn = new HashMap(2));
return toReturn;
}
/**
* Add a component to the project. UserWindows cannot have the same name as UserComponents.
* @param guiComponent a UserComponent
* @return the name of the component just added
*/
public String addComponent(UserComponent guiComponent) {
ProjectHandler.getInstance().projectChanged();
return guiNamer.add(guiComponent, "Component");
}
/**
* Add a window to the project. UserWindows cannot have the same name as UserComponents.
* @param window a UserWindow
* @return the name of the window just added
*/
public String addWindow(UserWindow window) {
ProjectHandler.getInstance().projectChanged();
return guiNamer.add(window, "Window");
}
/**
* Sets the name of a particular component within this project. This can be done more
* flexibly by retrieving the PIYNamer for guiComponents.
* @param component a UserComponent (or UserWindow) or an ActionList that exists within
* this project
* @param name the new name of the component
*/
public void setName(Object component, String name) {
ProjectHandler.getInstance().projectChanged();
if ( (component instanceof UserComponent) || (component instanceof UserWindow) )
guiNamer.renameValue(guiNamer.getName(component), name);
else if (component instanceof ActionList)
actionListNamer.renameValue(actionListNamer.getName(component), name);
}
/**
* Get the name of the specified component.
* @param component a UserComponent (or UserWindow) or ActionList that exists within this
* project
* @return the name of the specified component, or null if the component wasn't found in
* this project
*/
public String getName(Object component) {
if ( (component instanceof UserComponent) || (component instanceof UserWindow) )
return guiNamer.getName(component);
else if (component instanceof ActionList)
return actionListNamer.getName(component);
return "";
}
/**
* Gets the filename of this project, or null if this project hasn't been loaded/saved.
* There is no guarantee that the returned filename exists or represents this project.
* @return this Project's filename, or null
*/
protected File getFileName() { return fileName; }
/**
* Set the filename associated with this project. The filename should represent
* where the project was loaded from, or if it hasn't been loaded, this should represent
* where the project has been saved to.
* @param fileName the filename to set
*/
protected void setFileName(File fileName) {
ProjectHandler.getInstance().projectChanged();
this.fileName = fileName;
}
/**
* Remove the window with the specified name from the project. If the window is the
* last window, it will not be deleted. If the window is the main window, this method
* will automatically choose a replacement main window.
* @param name the name of the window to remove
* @return true if the window was deleted, false otherwise
*/
public boolean removeWindow(String name) {
ProjectHandler.getInstance().projectChanged();
UserWindow window = (UserWindow)guiNamer.getValue(name);
if (window != null) {
if (window == mainWindow) {
java.util.List windows = guiNamer.getNamesList(UserWindow.class); //get the list of windows
if (windows.size() > 1) {
//Set a different window to be the main window
boolean found = false;
for (int i=0; i<windows.size() && !found; i++)
if (windows.get(i) != name) {
setMainWindow((JFrame)guiNamer.getValue((String)windows.get(i)));
found = true;
}
guiNamer.removeValue(name);
return true;
} else {
JOptionPane pane = new JOptionPane(
"The last window cannot be removed from the project",
JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog(null, "Information");
dialog.setResizable(false);
dialog.show();
setMainWindow(window);
return false;
}
} else {
guiNamer.removeValue(name);
return true;
}
}
return false;
}
/**
* The window that acts as the "main" window. The main window is the one that is
* first displayed when the compiled project is run.
* @param mainWindow the window to set as main. If null is supplied as an argument,
* the old value for mainWindow is kept
*/
public void setMainWindow(JFrame mainWindow) {
ProjectHandler.getInstance().projectChanged();
if (mainWindow != null) this.mainWindow = mainWindow;
}
/**
* Get the reference to the window set as the mainWindow
* @return the current main window
*/
public JFrame getMainWindow() { return mainWindow; }
}
/**
* This pane is added to windows in place of the PIYGlassPane when removed.
* @author David Vivash
* @version 1.0, 26/04/01
*/
final class MiniGlassPane extends JComponent implements Serializable {
public boolean isOpaque() { return false; }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -