📄 stationspanel.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.exact.panels;
import jmt.gui.common.resources.ImageLoader;
import jmt.gui.exact.ExactConstants;
import jmt.gui.exact.ExactModel;
import jmt.gui.exact.ExactWizard;
import jmt.gui.exact.table.*;
import jmt.gui.exact.utils.ArrayUtils;
import jmt.gui.help.HoverHelp;
import jmt.gui.wizard.WizardPanel;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author alyf (Andrea Conti)
* Date: 11-set-2003
* Time: 23.48.19
*/
/**
* 2nd panel: stations number, names, types
*/
public final class StationsPanel extends WizardPanel implements ExactConstants, ForceUpdatablePanel {
private ExactWizard ew;
private HoverHelp help;
private static final String helpText = "<html>In this panel you can define the number of stations in the system and their properties.<br><br>" +
" To edit values, single-click on the desired cell" +
" and start typing.<br> To select stations click or drag on the row headers.<br> <b>For a list of the available operations right-click" +
" on the table</b>.<br>" +
" Pressing DELETE removes all selected stations from the system.</html>";
private int stations;
private int pop;
//if true, LD stations are allowed
private boolean LD_enabled_system;
private String[] stationNames;
private int[] stationTypes;
private int nameCounter = 1;
private List stationOps;
private boolean hasDeletes;
private boolean deleting = false;
private JSpinner stationSpinner = new JSpinner(new SpinnerNumberModel(1, 1, MAX_STATIONS, 1));
private StationTable stationTable;
private ChangeListener spinnerListener = new ChangeListener() {
public void stateChanged(ChangeEvent ce) {
if (!deleting) updateSizes();
}
};
private AbstractAction deleteStation = new AbstractAction("Delete selected stations") {
{
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false));
putValue(Action.SHORT_DESCRIPTION, "Deletes selected stations from the system");
}
public void actionPerformed(ActionEvent e) {
deleteSelectedStations();
}
};
private AbstractAction deleteOneStation = new AbstractAction("") {
{
putValue(Action.SHORT_DESCRIPTION, "Deletes this station");
putValue(Action.SMALL_ICON, ImageLoader.loadImage("Close"));
}
public void actionPerformed(ActionEvent e) {
}
};
private AbstractAction addStation = new AbstractAction("New Station") {
{
putValue(Action.SHORT_DESCRIPTION, "Adds a new Station to Model");
}
public void actionPerformed(ActionEvent e) {
addStation();
}
};
public StationsPanel(ExactWizard ew) {
this.ew = ew;
help = ew.getHelp();
stationOps = new ArrayList();
sync();
makeNames();
initComponents();
}
private void sync() {
hasDeletes = false;
stationOps.clear();
/* sync status with data object */
/* arrays are copied to ensure data object consistency is preserved */
ExactModel data = ew.getData();
synchronized (data) {
stations = data.getStations();
pop = data.getMaxpop();
//OLD
// closedSystem = data.isClosed();
//NEW
//@author Stefano Omini
//if true, load dependent stations are allowed
//(at the moment, only single class closed models allow LD stations)
LD_enabled_system = data.isClosed() && !data.isMultiClass();
//end NEW
//TODO: sostituire LD??
stationNames = ArrayUtils.copy(data.getStationNames());
stationTypes = ArrayUtils.copy(data.getStationTypes());
}
stationSpinner.setValue(new Integer(stations));
}
public void gotFocus() {
sync();
stationTable.update();
}
public void lostFocus() {
commit();
//release();
}
/**
* make up names for null entries
*/
private void makeNames() {
for (int i = 0; i < stationNames.length; i++) {
if (stationNames[i] == null) {
stationNames[i] = "Station" + (++nameCounter);
}
}
}
/**
* resize internal data structures according to new values. intended to be called from a listener.
*/
private void updateSizes() {
setNumberOfStations(((Integer) stationSpinner.getValue()).intValue());
}
private void addStation(){
setNumberOfStations(stations+1);
}
private void setNumberOfStations(int number){
stationTable.stopEditing();
stations = number;
stationNames = ArrayUtils.resize(stationNames, stations, null);
makeNames();
stationTypes = ArrayUtils.resize(stationTypes, stations, STATION_LI);
stationTable.update();
if (!deleting) stationOps.add(ListOp.createResizeOp(stations));
stationSpinner.setValue(new Integer(stations));
stationTable.updateDeleteCommand();
}
/**
* Set up the panel contents and layout
*/
private void initComponents() {
stationSpinner.addChangeListener(spinnerListener);
stationTable = new StationTable();
/* and now some Box black magic */
Box stationSpinnerBox = Box.createHorizontalBox();
//OLD
//DEK (Federico Granata) 26-09-2003
//JLabel spinnerLabel = new JLabel("<html><font size=\"4\">Set the number of stations (1-" + MAX_STATIONS + "):</font></html>");
//NEW
//@author Stefano
JLabel spinnerLabel = new JLabel(DESCRIPTION_STATIONS);
//spinnerLabel.setMaximumSize(new Dimension(300, 18));
stationSpinnerBox.add(spinnerLabel);
stationSpinnerBox.add(Box.createHorizontalStrut(10));
Box numberBox = Box.createVerticalBox();
JPanel spinnerPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JLabel numberLabel = new JLabel("Number:");
stationSpinner.setMaximumSize(new Dimension(600, 18));
spinnerPanel.add(numberLabel);
spinnerPanel.add(stationSpinner);
numberBox.add(spinnerPanel);
numberBox.add(new JButton(addStation));
numberBox.setMaximumSize(new Dimension(150,50));
stationSpinnerBox.add(numberBox);
//END
Box stationBox = Box.createVerticalBox();
stationBox.add(Box.createVerticalStrut(20));
stationBox.add(stationSpinnerBox);
stationBox.add(Box.createVerticalStrut(10));
JScrollPane stationTablePane = new JScrollPane(stationTable);
stationTablePane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
stationTablePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
stationBox.add(stationTablePane);
stationBox.add(Box.createRigidArea(new Dimension(10, 20)));
Box totalBox = Box.createHorizontalBox();
totalBox.add(Box.createHorizontalStrut(20));
totalBox.add(stationBox);
totalBox.add(Box.createHorizontalStrut(20));
setLayout(new BorderLayout());
add(totalBox, BorderLayout.CENTER);
}
public String getName() {
return "Stations";
}
private void commit() {
if (stationSpinner.getEditor().getComponent(0).hasFocus()) {
try {
stationSpinner.commitEdit();
updateSizes();
} catch (java.text.ParseException e) {
}
}
stationTable.stopEditing();
ExactModel data = ew.getData();
synchronized (data) {
if (hasDeletes) {
playbackStationOps(data); //play back ops on the data object
} else {
data.resize(stations, data.getClasses());
}
data.setStationNames(stationNames);
data.setStationTypes(stationTypes);
//NEW
//@author Stefano Omini
sync();
//end NEW
}
}
public boolean canFinish() {
return checkLD() && !areThereDuplicates();
}
private void deleteSelectedStations() {
int[] selectedRows = stationTable.getSelectedRows();
int nrows = selectedRows.length;
if (nrows == 0) return;
int left = stationTable.getRowCount() - nrows;
if (left < 1) {
stationTable.removeRowSelectionInterval(selectedRows[nrows - 1], selectedRows[nrows - 1]);
deleteSelectedStations();
return;
}
deleteStations(selectedRows);
}
private void deleteStations(int[] idx) {
deleting = true;
Arrays.sort(idx);
for (int i = idx.length-1; i >=0 ; i--) {
deleteStation(idx[i]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -