📄 defaultseditor.java
字号:
/**
* Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package jmt.gui.common.editors;
import jmt.gui.common.Defaults;
import jmt.gui.common.distributions.Distribution;
import jmt.gui.common.layouts.SpringUtilities;
import jmt.gui.common.routingStrategies.RoutingStrategy;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
/**
* <p>Title: Defaults Editor</p>
* <p>Description: A modal or non-modal editor used to setup default values for every
* parameter of the model.</p>
*
* @author Bertoli Marco
* Date: 12-lug-2005
* Time: 16.18.35
*/
public class DefaultsEditor extends JDialog{
/**
* Constants used to select which parameters should be shown
*/
public static final int JMODEL = 0;
public static final int JSIM = 1;
protected static final int BORDERSIZE = 20;
protected static final int MINIMUM_TIME = 5; // Minimum simulation duration
protected int target;
// --- Constructors --------------------------------------------------------------------------------
/**
* Construct a new DefaultsEditor in a non-modal dialog
* @param target Used to specify to show specific parameters for JMODEL or JSIM
*/
public DefaultsEditor(int target) {
super();
initWindow(target);
}
/**
* Construct a new DefaultsEditor in a modal dialog
* @param owner owner Frame
* @param target Used to specify to show specific parameters for JMODEL or JSIM
*/
public DefaultsEditor(Frame owner, int target) {
super(owner, true);
initWindow(target);
}
/**
* Construct a new DefaultsEditor in a modal dialog
* @param owner owner Dialog
* @param target Used to specify to show specific parameters for JMODEL or JSIM
*/
public DefaultsEditor(Dialog owner, int target) {
super(owner, true);
initWindow(target);
}
/**
* Returns a new instance of DefaultsEditor, given parent container (used to find
* top level Dialog or Frame to create this dialog as modal)
* @param parent any type of container contained in a Frame or Dialog
* @param target Used to specify to show specific parameters for JMODEL or JSIM
* @return new instance of DefaultsEditor
*/
public static DefaultsEditor getInstance(Container parent, int target) {
// Finds top level Dialog or Frame to invoke correct costructor
while (!(parent instanceof Frame || parent instanceof Dialog))
parent = parent.getParent();
if (parent instanceof Frame)
return new DefaultsEditor((Frame)parent, target);
else
return new DefaultsEditor((Dialog)parent, target);
}
// -------------------------------------------------------------------------------------------------
// --- Actions performed by buttons and EventListeners ---------------------------------------------
// When okay button is pressed
protected AbstractAction okayAction = new AbstractAction("Okay") {
{
putValue(Action.SHORT_DESCRIPTION, "Closes this window and saves all changes");
}
public void actionPerformed(ActionEvent e) {
Defaults.save();
DefaultsEditor.this.dispose();
}
};
// When cancel button is pressed
protected AbstractAction cancelAction = new AbstractAction("Cancel") {
{
putValue(Action.SHORT_DESCRIPTION, "Closes this window discarding all changes");
}
public void actionPerformed(ActionEvent e) {
Defaults.reload();
DefaultsEditor.this.dispose();
}
};
// When reset button is pressed
protected AbstractAction resetAction = new AbstractAction("Reset") {
{
putValue(Action.SHORT_DESCRIPTION, "Reverts all values to original ones");
}
public void actionPerformed(ActionEvent e) {
// Unregister all stringListener to avoid strange random things
JTextField tmp;
while (!registeredStringListener.isEmpty()) {
tmp = (JTextField) registeredStringListener.remove(0);
tmp.removeFocusListener(stringListener);
tmp.removeKeyListener(stringListener);
}
Defaults.revertToDefaults();
DefaultsEditor.this.getContentPane().removeAll();
DefaultsEditor.this.initComponents(target);
DefaultsEditor.this.show();
}
};
/**
* Listener used to set parameters (associated to param_panel's JTextFields).
* Parameters are set when JTextField loses focus or ENTER key is pressed.
*/
protected class inputListener implements KeyListener, FocusListener {
/**
* Update values of Defaults fields
*/
protected void updateValues(Object source) {
JTextField src = (JTextField) source;
Defaults.set(src.getName(), src.getText());
}
public void focusLost(FocusEvent e) {
updateValues(e.getSource());
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
updateValues(e.getSource());
e.consume();
}
}
public void focusGained(FocusEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
protected inputListener stringListener = new inputListener();
// Vector that contains every compoent in which a StringListener has been registered
// It is used as the focus listener can do "random" things while reverting values
// to original ones
protected Vector registeredStringListener = new Vector();
// -------------------------------------------------------------------------------------------------
/**
* Initialize parameters of the window (size, title)... Then calls <code>initComponents</code>
* @param target target application (JMODEL or JSIM)
*/
protected void initWindow(int target) {
this.target = target;
// Sets default title, close operation and dimensions
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.setTitle("Editing Default Values...");
int width = 648, height=480;
// Centers this dialog on the screen
Dimension scrDim = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds((scrDim.width-width)/2,(scrDim.height-height)/2,width,height);
// If user closes this window, act as cancel and reloads saved parameters
this.addWindowStateListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Defaults.reload();
}
});
initComponents(target);
}
/**
* Initialize all graphic objects
* @param target target application (JMODEL or JSIM)
*/
protected void initComponents(int target) {
// Creates a main panel and adds margins to it
JPanel mainpanel = new JPanel(new BorderLayout());
mainpanel.setLayout(new BorderLayout());
mainpanel.setBorder(BorderFactory.createEmptyBorder(BORDERSIZE, BORDERSIZE, BORDERSIZE, BORDERSIZE));
this.getContentPane().add(mainpanel, BorderLayout.CENTER);
// Adds bottom_panel to contentpane
JPanel bottom_panel = new JPanel(new FlowLayout());
this.getContentPane().add((bottom_panel), BorderLayout.SOUTH);
// Adds Okay button to bottom_panel
JButton okaybutton = new JButton(okayAction);
bottom_panel.add(okaybutton);
// Adds Cancel button to bottom_panel
JButton cancelbutton = new JButton(cancelAction);
bottom_panel.add(cancelbutton);
// Adds Cancel button to bottom_panel
JButton resetbutton = new JButton(resetAction);
bottom_panel.add(resetbutton);
// Creates param_panel
JPanel param_panel = new JPanel(new GridLayout(1, 4));
mainpanel.add(new JScrollPane(param_panel), BorderLayout.CENTER);
Map tmpMap;
// Class Parameters
JPanel class_panel = new JPanel(new SpringLayout());
int classpanelnum = 0; // Counts all inserted elements
class_panel.setBorder(new TitledBorder(new EtchedBorder(), "Class parameters"));
// Name
addInputString("Name", "className", class_panel);
classpanelnum++;
// Type
tmpMap = new TreeMap();
tmpMap.put(""+Defaults.CLASS_TYPE_CLOSED, "Closed");
tmpMap.put(""+Defaults.CLASS_TYPE_OPEN, "Open");
addInputCombo("Type", "classType", class_panel, tmpMap);
classpanelnum++;
// Priority
addInputSpinner("Priority","classPriority", class_panel, 0);
classpanelnum++;
// Population
addInputSpinner("Population (closed classes)","classPopulation", class_panel, 1);
classpanelnum++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -