piy.java
字号:
package piy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
/**
* The top level PIY class. Classes associated with the Project class should never
* use this class (It is not okay for actions to use this class, but it is okay for
* action descriptors etc.)
* @author David Vivash
* @version 1.0.3, 12/Mar/02
*/
public class PIY extends Observable implements ActionListener, Observer
{
public static final String NEW_PROJECT = "newProject";
public static final String OPEN_PROJECT = "openProject";
public static final String SAVE_PROJECT = "saveProject";
public static final String SAVE_PROJECT_AS = "saveProjectAs";
public static final String NEW_WINDOW = "newWindow";
public static final String VIEW_LIST_EDITOR = "viewListEditor";
public static final String VIEW_MEMORY_EDITOR = "viewMemoryEditor";
public static final String VIEW_PROPERTIES = "viewProperties";
public static final String RUN_PROJECT = "runProject";
public static final String STOP_PROJECT = "stopProject";
public static final String COMPILE_PROJECT = "compileProject";
public static final String ABOUT = "about";
public static final String EXIT = "exit";
/** constant denoting a component has been chosen in the MainWindow */
public static final String COMPONENT_CHOSEN = "componentChosen";
/** constant denoting a component has been unchosen in the MainWindow */
public static final String COMPONENT_UNCHOSEN = "componentUnChosen";
/** constant denoting a container has been chosen in the MainWindow */
public static final String CONTAINER_CHOSEN = "containerChosen";
/** constant denoting a container has been unchosen in the MainWindow */
public static final String CONTAINER_UNCHOSEN = "containerUnChosen";
/** constant denoting when a component/container has been selected on a user window */
public static final String COMPONENT_SELECTED = "componentSelected";
/** constant denoting when a component/container has been deselected on a user window */
public static final String COMPONENT_DESELECTED = "componentDeselected";
/** constant denoting when a component/container has been moved or resized on a user window */
public static final String COMPONENT_CHANGED = "componentChanged";
private static File rootDirectory = new File(System.getProperty("user.dir"));
public static String version = "0.4 alpha"; //version of PIY, not of this class
public static int selectedBorderWidth = 5;
//these represent all of the usercomponents and actions which PIY offers
private Descriptor[] componentDescriptors, containerDescriptors;
private ActionDescriptor[] actionDescriptors, comparatorDescriptors;
private Class[] listeners = null;
public Frame ultimateParent = null;
//these represent all of the property types supported by PIY
private Class[] classSupport;
//The application currently being run from within PIY
private Thread runningApplication = null;
/**
* Rather than access this variable, it is better to call getInstance() - since the
* result of calling getInstance() always returns a valid PIY Object, whereas this
* variable could point to null.
*/
protected static PIY instance = null;
private MainWindow mainWindow;
private PropertyEditorFrame propertyEditor;
private ActionListEditor actionListEditor;
private MemoryEditor memoryEditor;
/**
* Constructs the PIY instance, which will collect all valid userComponents and actions. If
* the usercomponent or the action directory are not found, this instantiation will cause
* the jvm to exit.
*/
private PIY() {
PIY.instance = this;
// load in usercomponent and action descriptor classes
try {
componentDescriptors = DescriptorClassLoader.getDescriptors(new File("piy" + File.separatorChar + "usercomponent"), "piy.", java.awt.Component.class, UserComponent.class, true);
containerDescriptors = DescriptorClassLoader.getDescriptors(new File("piy" + File.separatorChar + "usercontainer"), "piy.", java.awt.Container.class, UserContainer.class, true);
actionDescriptors = ActionDescriptorClassLoader.getDescriptors(new File("piy" + File.separatorChar + "action"), "piy.", PIYAction.class, null, true);
comparatorDescriptors = ActionDescriptorClassLoader.getDescriptors(new File("piy" + File.separatorChar + "comparator"), "piy.", PIYComparator.class, null, true);
classSupport = PIYClassLoader.getClasses(new File("piy" + File.separatorChar + "support"),"piy.", ClassSupport.class, null, true);
listeners = PIYClassLoader.getClasses(new File("piy" + File.separatorChar + "listeners"), "piy.", PiyEventListener.class, null, true);
} catch (java.io.FileNotFoundException e) {
System.out.println("Unable to load usercomponents, actions or support type classes");
e.printStackTrace();
System.exit(0);
}
ultimateParent = mainWindow; //This window will count as the parent of all others (for model dialog purposes)
mainWindow = new MainWindow("Piy v" + version, componentDescriptors, containerDescriptors);
propertyEditor = new PropertyEditorFrame(new PropertyManager(classSupport), new ListenerManager(listeners));
actionListEditor = new ActionListEditor(actionDescriptors, null);
memoryEditor = new MemoryEditor(classSupport);
propertyEditor.setBounds(0,120,220,600);
memoryEditor.setBounds(640,0,180,200);
actionListEditor.setBounds(220,320,500,350);
mainWindow.show();
propertyEditor.show();
}
/**
* Runs PIY.
* @param args currently ignored by PIY
*/
public static void main(String[] args)
{
try{
if (args.length == 2) //Use the system look and feel
if (args[0].equalsIgnoreCase("-lf") && args[1].equalsIgnoreCase("System"))
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
//carry on regardless - ie. use standard look and feel
}
try{
System.setErr(new PrintStream(new FileOutputStream("Error.log")));
} catch (Exception e) {
//carry on regardless - ie. use standard error output
}
getInstance();
}
/**
* Used to determine the directory from which the java virtual machine
* was initialised. It is assumed within piy that the jvm is initialised in
* the <i>parent</i> directory of PIY. If it is not, PIY will not function
* correctly
* @return the directory from which the java virtual machine was initialised
*/
public static File getRootDirectory() {
return rootDirectory;
}
/**
* All actions in the windows get referred back to here since the windows have
* no direct intercommunication.
* @param e an action event such as a button click that PIY can act upon
*/
public void actionPerformed(ActionEvent e) {
setChanged();
String command = e.getActionCommand();
if (command == NEW_WINDOW) {
UserWindowListener.getInstance().setCurrentWindow(ProjectHandler.getInstance().newWindow());
UserWindowListener.getInstance().selectComponent(null);
} else if (command == NEW_PROJECT) {
ProjectHandler.getInstance().newProject();
notifyObservers(new ActionEvent(this, 1, NEW_PROJECT));
UserWindowListener.getInstance().setCurrentWindow(ProjectHandler.getInstance().getProject().getMainWindow());
UserWindowListener.getInstance().selectComponent(null);
} else if (command == SAVE_PROJECT) {
ProjectHandler.getInstance().saveProject();
} else if (command == SAVE_PROJECT_AS) {
ProjectHandler.getInstance().saveProjectAs();
} else if (command == OPEN_PROJECT) {
ProjectHandler.getInstance().loadProject();
notifyObservers(new ActionEvent(this, 1, OPEN_PROJECT));
UserWindowListener.getInstance().setCurrentWindow(ProjectHandler.getInstance().getProject().getMainWindow());
UserWindowListener.getInstance().selectComponent(null);
} else if (command == VIEW_LIST_EDITOR) {
actionListEditor.show();
} else if (command == VIEW_MEMORY_EDITOR) {
memoryEditor.show();
} else if (command == VIEW_PROPERTIES) {
propertyEditor.show();
} else if (command == RUN_PROJECT) {
if (runningApplication == null) {
runningApplication = ProjectHandler.getInstance().compileProject();
ApplicationManager.getInstance().setApplication((Application)runningApplication);
if (runningApplication != null) {
runningApplication.start();
new Thread(new ApplicationMonitor((Application)runningApplication, (Observer)this)).start();
}
}
} else if (command == COMPILE_PROJECT) {
runningApplication = ProjectHandler.getInstance().compileProject();
ApplicationManager.getInstance().setApplication((Application)runningApplication);
new ApplicationPackager((Application)runningApplication).saveApplication(mainWindow, getClasses(actionDescriptors), getClasses(componentDescriptors), getClasses(containerDescriptors), listeners, getClasses(comparatorDescriptors));
runningApplication = null;
} else if (command == STOP_PROJECT) {
if (runningApplication != null) ((Application)runningApplication).running = false;
} else if (command == EXIT) {
if (ProjectHandler.getInstance().closeProject(true)) { //check user wants to close project
System.exit(0);
}
} else if (command == ABOUT) {
AboutBox.getInstance().show();
}
notifyObservers(e);
clearChanged();
}
/**
* Returns the only instantiation of PIY. If no instantiation of PIY exists,
* a new one will be constructed and returned.
* @return the PIY instance
*/
public static PIY getInstance() {
return (PIY.instance == null) ? PIY.instance = new PIY() : PIY.instance;
}
/**
* Adds the component (which is currently selected in the main window) to the specified
* user window.
* @param window the window in which to add the component
* param component the component to add, or null if the component should be
* retrieved from the main window
* @param x the x position of the component (relative to bottom of title bar)
* @param y the y position of the component (relative to right of window border)
* @param width the width of the component
* @param height the height of the component
* @return the component object that was added to the window
*/
protected JComponent addComponent(UserContainer container, Component component, int x, int y, int width, int height) {
java.awt.Insets border = ((Container)container).getInsets();
if (component == null) {
try{
if (mainWindow.isComponentSelected())
component = (Component)componentDescriptors[mainWindow.getSelectedUserComponent()].getDescribedClass().newInstance();
else //add a container instead (a container is a subclass of a component)
component = (Component)containerDescriptors[mainWindow.getSelectedUserContainer()].getDescribedClass().newInstance();
container.add(component);
component.setBounds(x-border.right, y-border.top, width, height);
} catch (Exception e) {
System.err.println("Error adding user component or container to container");
e.printStackTrace();
}
}
component.setBounds(x-border.right, y-border.top, width, height);
ProjectHandler.getInstance().getProject().addComponent((UserComponent)component);
if (component instanceof UserContainer) {
((Container)component).setLayout(new AlignmentLayout());
}
ProjectHandler.getInstance().projectChanged();
return (JComponent)component;
}
/**
* Get the support for all valid property types.
* @return the class support array
*/
public Class[] getClassSupport() { return classSupport; }
/**
* Get the comparator descriptors, which describe comparators that can be added to a
* boolean expression.
* @return the comparator descriptors as an ActionDescriptor array.
*/
public ActionDescriptor[] getComparators() { return comparatorDescriptors; }
/**
* Get the comparator descriptors, which describe comparators that can be added to a
* boolean expression.
* @return the comparator descriptors as an ActionDescriptor array.
*/
public ActionDescriptor[] getActionDescriptors() { return actionDescriptors; }
/**
* Gets the classes being described by the descriptors in the specified array.
* @param descriptors the descriptor classes
* @return the classes being described by the descriptors
*/
private Class[] getClasses(Descriptor[] descriptors) {
Class[] toReturn = new Class[descriptors.length];
for (int i=0; i<toReturn.length; i++)
toReturn[i] = descriptors[i].getDescribedClass();
return toReturn;
}
//--- Observer method ---
//this will be called when the running application terminates
public void update(Observable o, Object arg) {
((Application)runningApplication).closeWindows();
runningApplication = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -