📄 ldstrategyeditor.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.CommonConstants;
import jmt.gui.common.distributions.Distribution;
import jmt.gui.common.resources.ImageLoader;
import jmt.gui.common.serviceStrategies.LDStrategy;
import jmt.gui.exact.utils.BrowserControl;
import jmt.gui.jsim.editors.ButtonCellEditor;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.HashMap;
/**
* <p>Title: Load Dependent Service Time Strategy Editor</p>
* <p>Description: An editor used to parametrize a load dependent service
* time strategy. This is a modal JDialog.</p>
*
* @author Bertoli Marco
* Date: 13-ott-2005
* Time: 11.49.53
*/
public class LDStrategyEditor extends JDialog implements CommonConstants {
protected static final int BORDERSIZE = 20;
protected static final int MAXRANGES = 1000;
protected static final String HELPFILE = "ParserHelp.html";
protected static HashMap distributions;
protected LDStrategy strategy;
protected JTable rangesTable;
// --- Static methods ------------------------------------------------------------------------------
/**
* Returns a new instance of LDStrategyEditor, 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 strategy LDStrategy to be modified
* @return new instance of LDStrategyEditor
*/
public static LDStrategyEditor getInstance(Container parent, LDStrategy strategy) {
// 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 LDStrategyEditor((Frame)parent, strategy);
else
return new LDStrategyEditor((Dialog)parent, strategy);
}
/**
* Uses reflection to return an HashMap of distributions suitable for LDStrategy.
* (With mean value specifiable) Search's key is distribution name and value is the
* Class of found distribution
* @return found distributions
*/
protected static HashMap findDistributions() {
Distribution[] all = Distribution.findAllWithMean();
HashMap tmp = new HashMap();
for (int i=0; i<all.length; i++)
tmp.put(all[i].getName(), all[i]);
return tmp;
}
// -------------------------------------------------------------------------------------------------
// --- Constructors --------------------------------------------------------------------------------
/**
* Builds a new LDStrategyEditor Dialog. This dialog is designed to be modal.
* @param parent owner Frame for this dialog.
* @param strategy Reference to LDStrategy to be edited
*/
public LDStrategyEditor(Frame parent, LDStrategy strategy) {
super(parent, true);
initData(strategy);
initGUI();
}
/**
* Builds a new LDStrategyEditor Dialog. This dialog is designed to be modal.
* @param parent owner Dialog for this dialog.
* @param strategy Reference to LDStrategy to be edited
*/
public LDStrategyEditor(Dialog parent, LDStrategy strategy) {
super(parent, true);
initData(strategy);
initGUI();
}
// -------------------------------------------------------------------------------------------------
// --- Actions performed by buttons and EventListeners ---------------------------------------------
// When okay button is pressed
protected AbstractAction okayAction = new AbstractAction("Okay") {
{
putValue(Action.SHORT_DESCRIPTION, "Accepts changes and closes this window");
}
public void actionPerformed(ActionEvent e) {
LDStrategyEditor.this.dispose();
}
};
// When Add Range button is pressed
protected AbstractAction addRangeAction = new AbstractAction("Add Range") {
{
putValue(Action.SHORT_DESCRIPTION, "Adds a new range into this service time strategy");
}
public void actionPerformed(ActionEvent e) {
addRange();
}
};
// deletion of one range
protected AbstractAction deleteRange = new AbstractAction("") {
{
putValue(Action.SHORT_DESCRIPTION, "Deletes this range from current service section");
putValue(Action.SMALL_ICON, ImageLoader.loadImage("Delete"));
}
public void actionPerformed(ActionEvent e) {
int index = rangesTable.getSelectedRow();
if(index>=0 && index<rangesTable.getRowCount())
deleteRange(index);
}
};
//Component responsible of setting global number of ranges at once
protected JSpinner rangesNumSpinner = new JSpinner(){
{
addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
//stop editing text inside spinner
try{
rangesNumSpinner.commitEdit();
}catch(ParseException pe){
//if string does not represent a number, return
return;
}
//new number of ranges
int x = -1;
try{
x = ((Integer)rangesNumSpinner.getValue()).intValue();
}catch(NumberFormatException nfe){
}catch(ClassCastException cce){}
//if new number is valid, proceed updating number
if(x !=- 1){
setNumberOfRanges(x);
}else{
//otherwise, reset to 0
rangesNumSpinner.setValue(new Integer(0));
}
}
});
}
};
// -------------------------------------------------------------------------------------------------
// --- Initialize data structure and layout --------------------------------------------------------
/**
* Initialize this dialog data structures
* @param strategy Reference to service strategy to be edited
*/
protected void initData(LDStrategy strategy) {
this.strategy = strategy;
// If distributions is not already set, sets it!
if (distributions == null)
distributions = findDistributions();
}
/**
* Inits all gui related stuff
*/
protected void initGUI() {
// Sets default title, close operation and dimensions
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.setTitle("Editing Load Dependent Service Strategy...");
int width = 640, 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);
JTabbedPane mainPanel = new JTabbedPane();
this.getContentPane().setLayout(new BorderLayout());
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);
// Creates the Edit panel
JPanel LDPanel = new JPanel(new BorderLayout());
LDPanel.setBorder(BorderFactory.createEmptyBorder(BORDERSIZE, BORDERSIZE, BORDERSIZE, BORDERSIZE));
JPanel upperPanel = new JPanel (new BorderLayout());
// Adds "Add Range" Button
JPanel upRightPanel = new JPanel(new BorderLayout());
JButton addRange = new JButton(addRangeAction);
addRange.setMinimumSize(DIM_BUTTON_S);
upRightPanel.add(addRange,BorderLayout.NORTH);
//build spinner panel
JPanel spinnerPanel = new JPanel();
JLabel spinnerDescrLabel = new JLabel("Ranges:");
rangesNumSpinner.setPreferredSize(DIM_BUTTON_XS);
spinnerPanel.add(spinnerDescrLabel);
spinnerPanel.add(rangesNumSpinner);
upRightPanel.add(spinnerPanel, BorderLayout.SOUTH);
upperPanel.add(upRightPanel, BorderLayout.EAST);
upperPanel.add(new JLabel(LDSERVICE_DESCRIPTION), BorderLayout.CENTER);
upperPanel.add(Box.createVerticalStrut(BORDERSIZE / 2), BorderLayout.SOUTH);
LDPanel.add(upperPanel, BorderLayout.NORTH);
// Adds StrategyTable
rangesTable = new LDStrategyTable();
LDPanel.add(new JScrollPane(rangesTable), BorderLayout.CENTER);
// Creates Help panel
System.out.println();
JEditorPane helpPanel = null;
try {
helpPanel = new JEditorPane(LDStrategyEditor.class.getResource(HELPFILE));
helpPanel.setContentType("text/html");
} catch (IOException e) {
helpPanel = new JEditorPane("text/plain", "Help not found, please check location of " +
"file \"" + HELPFILE + "\".");
}
helpPanel.setEditable(false);
helpPanel.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
BrowserControl.displayURL(e.getURL().toString());
}
}
});
helpPanel.setBorder(BorderFactory.createEmptyBorder(BORDERSIZE, BORDERSIZE, BORDERSIZE, BORDERSIZE));
// Adds Edit and Help tabs to mainPanel
mainPanel.addTab("Edit", LDPanel);
mainPanel.addTab("Help", new JScrollPane(helpPanel));
refreshComponents();
}
/**
* Sets number of ranges in this strategy
* @param newNumber new number of ranges
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -