📄 guiframe.java
字号:
package pipe.gui;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.io.File;import java.util.Observable;import java.util.Observer;import javax.swing.Action;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTabbedPane;import javax.swing.JToggleButton;import javax.swing.JToolBar;import javax.swing.KeyStroke;import javax.swing.UIManager;import javax.swing.border.BevelBorder;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import pipe.dataLayer.DataLayer;import pipe.gui.widgets.FileBrowser;public class GuiFrame extends JFrame implements Constants, ActionListener, Observer{ // HAK private String frameTitle; //Frame title private DataLayer appModel; private GuiFrame appGui; private GuiView appView; private int mode, prev_mode; // *** mode WAS STATIC *** private int newNameCounter=1; private JTabbedPane appTab; private StatusBar statusBar; private JMenuBar menuBar; private FileAction createAction, openAction, closeAction, saveAction,saveAsAction, exitAction, printAction, exportPNGAction, exportPSAction; private GridAction toggleGrid; private DeleteAction deleteAction; private TypeAction annotationAction, arcAction, placeAction, transAction, timedtransAction, tokenAction, selectAction, deleteTokenAction; private AnimateAction startAction, stepforwardAction, stepbackwardAction, randomAction, randomAnimateAction; public GuiFrame(String title) { // HAK-arrange for frameTitle to be initialized and the default file name to be appended to basic window title frameTitle = title; setTitle(null); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception exc) { System.err.println("Error loading L&F: " + exc); } this.setIconImage(new ImageIcon(CreateGui.imgPath+"icon.png").getImage()); Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); this.setSize(screenSize.width*80/100,screenSize.height*80/100); this.setLocationRelativeTo(null); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); // Build menus menuBar=new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic('F'); addMenuItem(fileMenu, createAction = new FileAction("New", "Create a new Petri net","ctrl N")); addMenuItem(fileMenu, openAction = new FileAction("Open", "Open","ctrl O")); addMenuItem(fileMenu, closeAction = new FileAction("Close", "Close the current tab","ctrl F4")); fileMenu.addSeparator(); addMenuItem(fileMenu, saveAction = new FileAction("Save", "Save","ctrl S")); addMenuItem(fileMenu, saveAsAction = new FileAction("Save as","Save as...","F12")); // Export menu JMenu exportMenu=new JMenu("Export"); exportMenu.setIcon(new ImageIcon(CreateGui.imgPath+"Export.png")); addMenuItem(exportMenu, exportPNGAction = new FileAction("PNG", "Export the net to PNG format","")); addMenuItem(exportMenu, exportPSAction = new FileAction("PostScript", "Export the net to PostScript format","")); fileMenu.add(exportMenu); fileMenu.addSeparator(); addMenuItem(fileMenu, printAction = new FileAction("Print", "Print","ctrl P")); fileMenu.addSeparator(); // Example files menu try { File[] nets=new File(CreateGui.appPath+"Example nets").listFiles(); if(nets.length>0) { JMenu exampleMenu=new JMenu("Example nets"); exampleMenu.setIcon(new ImageIcon(CreateGui.imgPath+"Example.png")); for (int i = 0; i < nets.length; i++) if(nets[i].getName().toLowerCase().endsWith(".xml")) addMenuItem(exampleMenu,new ExampleFileAction(nets[i])); fileMenu.add(exampleMenu); fileMenu.addSeparator(); } } catch (Exception e) { System.err.println("Error getting example files:"+e); } addMenuItem(fileMenu, exitAction = new FileAction("Exit", "Close the program","alt F4")); JMenu viewMenu = new JMenu("View"); viewMenu.setMnemonic('V'); addMenuItem(viewMenu, toggleGrid = new GridAction("Cycle grid", "Change the grid size", "G")); JMenu drawMenu = new JMenu("Draw"); drawMenu.setMnemonic('D'); addMenuItem(drawMenu, placeAction = new TypeAction("Place", PLACE, "Add a place","P",true)); addMenuItem(drawMenu, transAction = new TypeAction("Immediate transition",IMMTRANS, "Add an immediate transition","I",true)); addMenuItem(drawMenu, timedtransAction = new TypeAction("Timed transition", TIMEDTRANS,"Add a timed transition","T",true)); addMenuItem(drawMenu, arcAction = new TypeAction("Arc", ARC, "Add an arc","A",true)); addMenuItem(drawMenu, annotationAction = new TypeAction("Annotation", ANNOTATION, "Add an annotation","N",true)); drawMenu.addSeparator(); addMenuItem(drawMenu, tokenAction = new TypeAction("Add token", ADDTOKEN, "Add a token","ADD",true)); addMenuItem(drawMenu, deleteTokenAction = new TypeAction("Delete token", DELTOKEN, "Delete a token","SUBTRACT",true)); drawMenu.addSeparator(); addMenuItem(drawMenu, selectAction = new TypeAction("Select", SELECT, "Select components","S",true)); addMenuItem(drawMenu, deleteAction = new DeleteAction("Delete", "Delete selection","DELETE")); JMenu animateMenu = new JMenu("Animate"); animateMenu.setMnemonic('A'); addMenuItem(animateMenu, startAction = new AnimateAction("Animation mode",START,"Toggle Animation Mode",true)); animateMenu.addSeparator(); addMenuItem(animateMenu, stepbackwardAction = new AnimateAction("Back", STEPBACKWARD,"Step backward a firing")); addMenuItem(animateMenu, stepforwardAction = new AnimateAction("Forward",STEPFORWARD, "Step forward a firing")); addMenuItem(animateMenu, randomAction = new AnimateAction("Random", RANDOM, "Randomly fire a transition")); addMenuItem(animateMenu, randomAnimateAction= new AnimateAction("Animate",ANIMATE, "Randomly fire a number of transitions",true)); JMenu helpMenu = new JMenu("Help"); helpMenu.setMnemonic('H'); HelpBox helpAction=new HelpBox("Help","View documentation","F1","index.htm"); addMenuItem(helpMenu,helpAction); JMenuItem aboutItem = helpMenu.add("About PIPE"); aboutItem.addActionListener(this); // Help - About is implemented differently aboutItem.setIcon(new ImageIcon(CreateGui.imgPath+"About.png")); menuBar.add(fileMenu); menuBar.add(viewMenu); menuBar.add(drawMenu); menuBar.add(animateMenu); menuBar.add(helpMenu); setJMenuBar(menuBar); // Status bar... statusBar = new StatusBar(); getContentPane().add(statusBar, BorderLayout.PAGE_END); // Create the toolbar JToolBar toolBar=new JToolBar(); toolBar.setFloatable(false);//Inhibit toolbar floating addButton(toolBar,createAction); addButton(toolBar,openAction); addButton(toolBar,saveAction); addButton(toolBar,saveAsAction); //toolBar.add(printAction); addButton(toolBar,closeAction); toolBar.addSeparator(); addButton(toolBar,printAction); toolBar.addSeparator(); addButton(toolBar,placeAction);//Add Draw Menu Buttons addButton(toolBar,transAction); addButton(toolBar,timedtransAction); addButton(toolBar,arcAction); addButton(toolBar,annotationAction); addButton(toolBar,tokenAction); addButton(toolBar,deleteTokenAction); addButton(toolBar,selectAction); toolBar.addSeparator(); addButton(toolBar,toggleGrid); toolBar.addSeparator(); addButton(toolBar,startAction); addButton(toolBar,stepbackwardAction); addButton(toolBar,stepforwardAction); addButton(toolBar,randomAction); addButton(toolBar,randomAnimateAction); toolBar.addSeparator(); addButton(toolBar,helpAction); for(int i=0;i<toolBar.getComponentCount();i++) toolBar.getComponent(i).setFocusable(false); getContentPane().add(toolBar,BorderLayout.PAGE_START); enableActions(true); addWindowListener(new WindowHandler()); // Set selection mode at startup selectAction.actionPerformed(null); } private void addButton(JToolBar toolBar,GuiAction action) { if (action.getValue("selected")!=null) toolBar.add(new ToggleButton(action)); else toolBar.add(action); } /** * A JToggleButton that watches an Action for selection change * @author Maxim * * Selection must be stored in the action using putValue("selected",Boolean); */ class ToggleButton extends JToggleButton implements PropertyChangeListener { public ToggleButton(Action a) { super(a); if(a.getValue(Action.SMALL_ICON)!=null) setText(null); // toggle buttons like to have images *and* text, nasty a.addPropertyChangeListener(this); } public void propertyChange(PropertyChangeEvent evt) { if(evt.getPropertyName()=="selected") { Boolean b=(Boolean)evt.getNewValue(); if(b!=null) setSelected(b.booleanValue()); } } } private JMenuItem addMenuItem(JMenu menu, Action action){ JMenuItem item = menu.add(action); KeyStroke keystroke = (KeyStroke)action.getValue(Action.ACCELERATOR_KEY); if(keystroke != null) item.setAccelerator(keystroke); return item; } /* sets all buttons to enabled or disabled according to status. */ public void enableActions(boolean status){// createAction.setEnabled(status);// openAction.setEnabled(status);// closeAction.setEnabled(status); saveAction.setEnabled(status); saveAsAction.setEnabled(status);// printAction.setEnabled(status);// exportPNGAction.setEnabled(status);// exportPSAction.setEnabled(status);// exitAction.setEnabled(status); placeAction.setEnabled(status); arcAction.setEnabled(status); annotationAction.setEnabled(status); transAction.setEnabled(status); timedtransAction.setEnabled(status); tokenAction.setEnabled(status); deleteAction.setEnabled(status); selectAction.setEnabled(status); deleteTokenAction.setEnabled(status);// toggleGrid.setEnabled(status); if(status) { startAction.setSelected(false); randomAnimateAction.setSelected(false); } randomAction.setEnabled(!status); randomAnimateAction.setEnabled(!status); stepbackwardAction.setEnabled(!status); stepforwardAction.setEnabled(!status); } public StatusBar getStatusBar(){ return statusBar; } //HAK set current objects in Frame public void setObjects(){ appModel = CreateGui.getModel(); appView = CreateGui.getView(); } //set frame objects by array place public void setObjects(int place){ appModel = CreateGui.getModel(place); appView = CreateGui.getView(place); } public void setObjectsNull(int index){ CreateGui.removeTab(index); } // set tabbed pane properties and add change listener that updates tab with linked model and view public void setTab(){ appTab =CreateGui.getTab(); appTab.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ int index = appTab.getSelectedIndex(); setObjects(index); if(appView!=null) { appView.setVisible(true); appView.repaint(); enableActions(true); //renables all non-animation buttons CreateGui.getAnimator().restoreModel(); CreateGui.removeAnimationHistory(); setTitle(appTab.getTitleAt(index)); } else { setTitle(null); } } }); appGui = CreateGui.getApp(); } // Less sucky yet far, far simpler to code About dialogue public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(this, "Imperial College DoC MSc Group Project\n" + "Original version (c) 2003 by Jamie Bloom, Clare Clark, Camilla Clifford, Alex Duncan, Haroun Khan and Manos Papantoniou\n" + "MLS(tm) Edition (c) 2004 by Tom Barnwell, Michael Camacho, Matthew Cook, Maxim Gready, Peter Kyme and Michail Tsouchlaris\n" + "http://petri-net.sf.net", "About PIPE", JOptionPane.INFORMATION_MESSAGE); } // HAK Method called by netModel object when it changes public void update(Observable o, Object obj){ if(mode!=CREATING && !CreateGui.getView().animationmode) appView.netChanged = true; } private void saveOperation(boolean forceSaveAs){ if(appView==null) return; File modelFile=CreateGui.getFile(); if(!forceSaveAs && modelFile!=null) { // ordinary save if(!appView.netChanged) return; saveNet(modelFile); } else { // save as String path=null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -