📄 mainframe.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* 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.operator.parameter.*;
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.Tools;
import edu.udo.cs.yale.tools.XMLException;
import edu.udo.cs.yale.tools.plugin.Plugin;
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.util.export.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.35 2004/08/27 11:57:33 ingomierswa Exp $
*/
public class MainFrame extends JFrame implements WindowListener, ChangeListener, BreakpointListener {
static {
Yale.registerYaleProperty(new ParameterTypeInt("yale.gui.attributeeditor.rowlimit", "Maximum number of examples to use for the attribute editor. -1 for no limit.", -1, Integer.MAX_VALUE, 50));
Yale.registerYaleProperty(new ParameterTypeBoolean("yale.gui.beep.success", "Beep on experiment success?", false));
Yale.registerYaleProperty(new ParameterTypeBoolean("yale.gui.beep.error", "Beep on error?", false));
Yale.registerYaleProperty(new ParameterTypeBoolean("yale.gui.beep.breakpoint", "Beep when breakpoint reached?", false));
Yale.registerYaleProperty(new ParameterTypeInt("yale.gui.resultviewer.attributelimit", "Limit number of displayed attributes to this number. -1 for no limit.", -1, Integer.MAX_VALUE, 100));
Yale.registerYaleProperty(new ParameterTypeInt("yale.gui.resultviewer.examplelimit", "Limit number of displayed examples in plot view to this number. -1 for no limit.", -1, Integer.MAX_VALUE, 1000));
Yale.registerYaleProperty(new ParameterTypeBoolean("yale.gui.experimentinfo.show", "Shows experiment info screen after loading?", true));
}
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)) && !tutorialMode) {
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();
synchronized (experimentThread) {
experimentThread.notify();
}
try {
LogService.logMessage("Waiting for experiment thread to die.", LogService.STATUS);
if (experimentThread != null)
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 SaveAsTemplateAction extends AbstractAction {
private SaveAsTemplateAction() {
super("Save as Template...");
putValue(SHORT_DESCRIPTION, "Save experiment file as template...");
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_T));
}
public void actionPerformed(ActionEvent e) {
Operator rootOperator = Yale.getExperiment().getRootOperator();
SaveAsTemplateDialog dialog = new SaveAsTemplateDialog(MainFrame.this, rootOperator);
dialog.show();
if (dialog.isOk()) {
Template template = dialog.getTemplate(rootOperator);
String name = template.getName();
try {
File templateFile = ParameterService.getUserConfigFile(name + ".template");
template.save(templateFile);
File templateXmlFile = ParameterService.getUserConfigFile(name + ".xml");
Yale.getExperiment().save(templateXmlFile);
} catch (IOException ioe) {
SwingTools.showSimpleErrorMessage("Cannot write template files:", ioe);
}
}
}
}
private class ManageTemplatesAction extends AbstractAction {
private ManageTemplatesAction() {
super("Manage Templates...");
putValue(SHORT_DESCRIPTION, "Manage previously saved templates...");
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_M));
}
public void actionPerformed(ActionEvent e) {
ManageTemplatesDialog dialog = new ManageTemplatesDialog(MainFrame.this);
dialog.show();
}
}
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")));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -