📄 mainframe.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * web: http://yale.cs.uni-dortmund.de/ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */package edu.udo.cs.yale.gui;import edu.udo.cs.yale.operator.Operator;import edu.udo.cs.yale.operator.OperatorChain;import edu.udo.cs.yale.operator.IOContainer;import edu.udo.cs.yale.Yale;import edu.udo.cs.yale.Experiment;import edu.udo.cs.yale.BreakpointListener;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.tools.ParameterService;import edu.udo.cs.yale.tools.xml.XMLException;import edu.udo.cs.yale.tools.Tools;import javax.swing.*;import javax.swing.event.*;import java.awt.Component;import java.awt.BorderLayout;import java.awt.Image;import java.awt.event.*;import java.awt.print.*;import java.io.*;import javax.imageio.ImageIO;import java.util.List;import java.util.LinkedList;import java.util.Iterator;import org.freehep.graphicsio.exportchooser.ExportDialog;/** The main GUI class. The class holds a lot of Actions that can be used for the tool bar and for the menu bar. * MainFrame has methods for handling the experiment (saving, opening, creating new). It keeps track of the state of the * experiment and enables/disables buttons. It must be notified whenever the experiment changes and propagates this * event to its children. * Most of the code is enclosed within the Actions. * * @version $Id: MainFrame.java,v 2.9 2003/09/03 14:27:27 fischer Exp $ */public class MainFrame extends JFrame implements WindowListener, ChangeListener, BreakpointListener { private static final String TITLE = "YALE"; private class ValidateExperiment extends ConditionalAction { private ValidateExperiment() { super("Validate experiment", new ImageIcon(Tools.getResource("icons/icon_check_experiment.gif"))); putValue(SHORT_DESCRIPTION, "Validate input/output and experiment structure"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_V)); putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0)); setCondition(EXPERIMENT_RUNNING, DISALLOWED); } public void actionPerformed(ActionEvent e) { Yale.getExperiment().checkExperiment(); treePanel.getOperatorTree().refresh(); } } private class WizardAction extends AbstractAction { private WizardAction() { super("Wizard..."); putValue(SHORT_DESCRIPTION, "Start the YALE wizard"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_W)); } public void actionPerformed(ActionEvent e) { new WizardDialog(MainFrame.this).show(); } } private class RunAction extends ConditionalAction { private RunAction() { super("Run", new ImageIcon(Tools.getResource("icons/icon_run.gif"))); putValue(SHORT_DESCRIPTION, "Run experiment"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_R)); putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0)); setCondition(EXPERIMENT_RUNNING, DISALLOWED); } public void actionPerformed(ActionEvent e) { if (changed || (Yale.getExperiment().getExperimentFile() == null)) { if (JOptionPane.showConfirmDialog(MainFrame.this, "Save experiment file?", "Save?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) save(); } experimentThread = new ExperimentThread(Yale.getExperiment()); try { LogService.initGUI(); resultDisplay.clear(); experimentThread.start(); } catch (Throwable t) { SwingTools.showSimpleErrorMessage("Cannot start experiment!", t); } finally { enableActions(); } } } private class ResumeAction extends ConditionalAction { private ResumeAction() { super("Resume", new ImageIcon(Tools.getResource("icons/icon_resume.gif"))); putValue(SHORT_DESCRIPTION, "Resume experiment"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_U)); setCondition(EXPERIMENT_RUNNING, MANDATORY); } public void actionPerformed(ActionEvent e) { synchronized (experimentThread) { experimentThread.notify(); } } } private class StopAction extends ConditionalAction { private StopAction() { super("Stop", new ImageIcon(Tools.getResource("icons/icon_stop.gif"))); putValue(SHORT_DESCRIPTION, "Stop experiment execution"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_S)); putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F5, java.awt.event.InputEvent.CTRL_MASK)); setCondition(EXPERIMENT_RUNNING, MANDATORY); } public void actionPerformed(ActionEvent e) { experimentThread.stopExperiment(); try { LogService.logMessage("Waiting for experiment thread to die.", LogService.STATUS); experimentThread.join(); LogService.logMessage("Experiment thread died.", LogService.STATUS); } catch (InterruptedException ex) { SwingTools.showErrorMessage("While waiting for death of experiment thread:", ex); } enableActions(); } } //////////////////////////////////////////////////////////////////////////////// private class NewAction extends AbstractAction { private NewAction() { super("New", new ImageIcon(Tools.getResource("icons/icon_new.gif"))); putValue(SHORT_DESCRIPTION, "Create empty experiment"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_N)); putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK)); } public void actionPerformed(ActionEvent e) { if (close()) setExperiment(new Experiment()); } } private class OpenAction extends AbstractAction { private OpenAction() { super("Open...", new ImageIcon(Tools.getResource("icons/icon_open.gif"))); putValue(SHORT_DESCRIPTION, "Open experiment file"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_O)); putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK)); } public void actionPerformed(ActionEvent e) { open(); } } private class SaveAction extends AbstractAction { private SaveAction() { super("Save", new ImageIcon(Tools.getResource("icons/icon_save.gif"))); setEnabled(false); putValue(SHORT_DESCRIPTION, "Save experiment file"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_S)); putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK)); } public void actionPerformed(ActionEvent e) { save(); } } private class SaveAsAction extends AbstractAction { private SaveAsAction() { super("Save As...", new ImageIcon(Tools.getResource("icons/icon_saveas.gif"))); putValue(SHORT_DESCRIPTION, "Save experiment file as..."); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_A)); } public void actionPerformed(ActionEvent e) { File file = SwingTools.chooseFile(MainFrame.this, null, false); if (file != null) { Yale.getExperiment().setExperimentFile(file); save(); } } } private class PrintAction extends AbstractAction { private PrintAction() { super("Print...", new ImageIcon(Tools.getResource("icons/icon_print.gif"))); putValue(SHORT_DESCRIPTION, "Print experiment"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_P)); } public void actionPerformed(ActionEvent e) { printerJob.setPrintable(new ComponentPrinter(((ExperimentEditor)editorTabs.getSelectedComponent()).getMainComponent())); if (printerJob.printDialog()) { try { printerJob.print(); } catch (PrinterException pe) { SwingTools.showSimpleErrorMessage("Printer error", pe); //pe.printStackTrace(); } } } } private class ExportAction extends AbstractAction { private ExportAction() { super("Export...", new ImageIcon(Tools.getResource("icons/icon_export.gif"))); putValue(SHORT_DESCRIPTION, "Export experiment"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_E)); } public void actionPerformed(ActionEvent e) { ExportDialog exportDialog = new ExportDialog("YALE"); exportDialog.showExportDialog(MainFrame.this, ((ExperimentEditor)editorTabs.getSelectedComponent()).getMainComponent()); } } private class ExitAction extends AbstractAction { private ExitAction() { super("Exit"); putValue(SHORT_DESCRIPTION, "Exit Yale"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_X)); } public void actionPerformed(ActionEvent e) { exit(); } } // -------------------------------------------------------------------------------- public final Action NEW_ACTION = new NewAction(); public final Action OPEN_ACTION = new OpenAction(); public final Action SAVE_ACTION = new SaveAction(); public final Action SAVE_AS_ACTION = new SaveAsAction(); public final Action PRINT_ACTION = new PrintAction(); public final Action EXPORT_ACTION = new ExportAction(); public final Action EXIT_ACTION = new ExitAction(); public final Action RUN_ACTION = new RunAction(); public final Action RESUME_ACTION = new ResumeAction(); public final Action STOP_ACTION = new StopAction(); public final Action VALIDATE_ACTION = new ValidateExperiment(); public final Action WIZARD_ACTION = new WizardAction(); private XMLEditor editor = new XMLEditor(this); private TreePanel treePanel = new TreePanel(this); private ExperimentPanel experimentPanel = new ExperimentPanel(); private JTabbedPane editorTabs = new JTabbedPane(); private ResultDisplay resultDisplay = new ResultDisplay(); private MonitorPanel monitorPanel = new MonitorPanel(); private MessageViewer messageViewer = new MessageViewer(); private StatusBar statusBar = new StatusBar(); private JMenu recentFilesMenu = new JMenu("Recent Files"); private List conditionalActions = new LinkedList(); private PrinterJob printerJob = PrinterJob.getPrinterJob(); private PageFormat pageFormat = printerJob.defaultPage(); private boolean changed = false; private ExperimentThread experimentThread = null; private ExperimentEditor currentExperimentEditor = treePanel; private JSplitPane splitPaneV; public MainFrame() { super(TITLE); try { setIconImage(ImageIO.read(Tools.getResource("yale_logo.gif"))); } catch (IOException e) { e.printStackTrace(); } Yale.setExperiment(new Experiment()); Yale.getExperiment().addBreakpointListener(MainFrame.this); setTitle(); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(this); setOperator(Yale.getExperiment().getRootOperator()); editorTabs.addChangeListener(this); editorTabs.add(treePanel, "Tree"); editorTabs.add(editor, "XML"); editorTabs.add(experimentPanel, "Experiment"); editorTabs.add(resultDisplay, "Results"); editorTabs.add(monitorPanel, "Monitor"); splitPaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT, editorTabs, messageViewer); splitPaneV.setOneTouchExpandable(true); getContentPane().add(splitPaneV, BorderLayout.CENTER); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); fileMenu.add(NEW_ACTION); fileMenu.add(OPEN_ACTION); fileMenu.add(new JSeparator()); fileMenu.add(SAVE_ACTION); fileMenu.add(SAVE_AS_ACTION); fileMenu.add(new JSeparator()); fileMenu.add(PRINT_ACTION); fileMenu.add(EXPORT_ACTION); fileMenu.add(new JSeparator()); updateRecentFileList(); fileMenu.add(recentFilesMenu); fileMenu.add(new JSeparator()); fileMenu.add(WIZARD_ACTION); fileMenu.add(new JSeparator()); fileMenu.add(EXIT_ACTION); menuBar.add(fileMenu); menuBar.add(treePanel.getOperatorTree().createOperatorMenu()); JMenu expMenu = new JMenu("Experiment"); expMenu.setMnemonic(KeyEvent.VK_X); expMenu.add(RUN_ACTION); expMenu.add(RESUME_ACTION); expMenu.add(STOP_ACTION); expMenu.add(new JSeparator()); expMenu.add(VALIDATE_ACTION); expMenu.add(new JSeparator()); expMenu.add(new JSeparator()); expMenu.add(messageViewer.CLEAR_MESSAGE_VIEWER_ACTION);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -