📄 stationspanel.java
字号:
}
private void deleteStation(int i) {
stations--;
stationSpinner.setValue(new Integer(stations));
stationNames = ArrayUtils.delete(stationNames, i);
stationTypes = ArrayUtils.delete(stationTypes, i);
stationOps.add(ListOp.createDeleteOp(i));
hasDeletes = true;
}
private void playbackStationOps(JabaModel data) {
for (int i = 0; i < stationOps.size(); i++) {
ListOp lo = (ListOp) stationOps.get(i);
if (lo.isDeleteOp()) {
data.deleteStation(lo.getData());
}
if (lo.isResizeOp()) {
data.resize(lo.getData(), data.getClasses());
}
}
}
public boolean canGoBack() {
checkLD();
if(areThereDuplicates()) return false;
return true;
}
public boolean canGoForward() {
checkLD();
if(areThereDuplicates()) return false;
return true;
}
/**
* @return true if the system contains LD stations
*/
private boolean isLD() {
for (int i = 0; i < stations; i++) {
if (stationTypes[i] == STATION_LD) {
return true;
}
}
return false;
}
//checks population of classes for ld stations
private boolean checkLD() {
stationTable.stopEditing();
if (pop < 1 && isLD()) {
JOptionPane.showMessageDialog(this, "<html><center>A system with zero customers cannot have load dependent stations.<br>Increase the number of customers or remove all load dependent stations.</center></html>", "Warning", JOptionPane.WARNING_MESSAGE);
return false;
}
return true;
}
//checks for presence of classes with same name
private boolean areThereDuplicates(){
boolean thereAreDupl = false;
for(int i=0; i<stationNames.length; i++){
for(int j=i+1; j<stationNames.length; j++){
thereAreDupl = thereAreDupl || stationNames[i].equalsIgnoreCase(stationNames[j]);
}
}
if(thereAreDupl){
JOptionPane.showMessageDialog(this, "<html><center>Two or more stations in this system are identified by the same name.<br>Please modify names.</center></html>", "Warning", JOptionPane.WARNING_MESSAGE);
return true;
}else return false;
}
public void help() {
JOptionPane.showMessageDialog(this, helpText, "Help", JOptionPane.INFORMATION_MESSAGE);
}
//NEW Federico Dall'Orso
//Methods added to implement forcing of data refresh
public void retrieveData() {
sync();
}
public void commitData() {
commit();
}
//END
/* ------------------------------------------------------------------
The StationTable is a fairly complex object that would be probably better of as an outer class.
However, it is very specialized and it needs access to the data structures of the StationsPanel,
so having it as an inner class is *much* more practical
------------------------------------------------------------------
*/
/**
* the nifty station table
*/
private class StationTable extends ExactTable {
private TableCellEditor LD_disabled_StationTypeEditor, LD_enabled_StationTypeEditor;
//BEGIN Federico Dall'Orso 8/3/2005
//NEW
private ComboBoxCell LD_disabled_StationTypeCell, LD_enabled_StationTypeCell;
//14/3/2005
private JButton deleteButton;
private ButtonCellRenderer deleteButtonCellRenderer;
//END Federico Dall'Orso
StationTable() {
super(new StationTableModel());
//BEGIN Federico Dall'Orso 8/3/2005
//station type cell renderers
//NEW
LD_disabled_StationTypeCell = new ComboBoxCell(STATION_TYPENAMES);
LD_disabled_StationTypeCell.getComboBox().setEnabled(false);
LD_enabled_StationTypeCell = new ComboBoxCell(STATION_TYPENAMES_LD_ENABLED);
LD_enabled_StationTypeCell.getComboBox().setEnabled(false);
//14/3/2005
deleteButton=new JButton(deleteOneStation);
deleteButtonCellRenderer = new ButtonCellRenderer(deleteButton);
enableDeletes();
rowHeader.setRowHeight(18);
setRowHeight(18);
//END Federico Dall'Orso
/* a station type cell editor for open/mixed systems */
JComboBox stationTypeBox = new JComboBox(STATION_TYPENAMES);
stationTypeBox.setEditable(false);
stationTypeBox.setEnabled(false);
LD_disabled_StationTypeEditor = new DefaultCellEditor(stationTypeBox);
stationTypeBox.setEnabled(false);
/* a station type cell editor for closed systems */
JComboBox LD_enabled_StationTypeBox = new JComboBox(STATION_TYPENAMES_LD_ENABLED);
LD_enabled_StationTypeBox.setEditable(false);
LD_enabled_StationTypeBox.setEnabled(false);
LD_enabled_StationTypeEditor = new DefaultCellEditor(LD_enabled_StationTypeBox);
LD_enabled_StationTypeBox.setEnabled(false);
setColumnSelectionAllowed(false);
setRowSelectionAllowed(true);
setDisplaysScrollLabels(true);
installKeyboardAction(getInputMap(), getActionMap(), deleteStation);
mouseHandler = new ExactTable.MouseHandler(makeMouseMenu());
mouseHandler.install();
help.addHelp(this, "Click or drag to select stations; to edit data single-click and start typing. Right-click for a list of available operations");
help.addHelp(moreRowsLabel, "There are more stations: scroll down to see them");
help.addHelp(selectAllButton, "Click to select all stations");
tableHeader.setToolTipText(null);
rowHeader.setToolTipText(null);
help.addHelp(rowHeader, "Click, SHIFT-click or drag to select stations");
}
//BEGIN Federico Dall'Orso 14/3/2005
/*enables deleting operations with last column's button*/
private void enableDeletes(){
deleteOneStation.setEnabled(stations>1);
/*It seems the only way to implement row deletion...*/
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if((columnAtPoint(e.getPoint())==getColumnCount()-1)&&getRowCount()>1){
setRowSelectionInterval(rowAtPoint(e.getPoint()), rowAtPoint(e.getPoint()));
deleteSelectedStations();
}
}
});
getColumnModel().getColumn(getColumnCount()-1).setMinWidth(20);
getColumnModel().getColumn(getColumnCount()-1).setMaxWidth(20);
}
//END Federico Dall'Orso 14/3/2005
protected void installKeyboard() {
}
protected void installMouse() {
}
protected JPopupMenu makeMouseMenu() {
JPopupMenu menu = new JPopupMenu();
menu.add(deleteStation);
return menu;
}
/**
* Overridden to ensure proper handling of station type column
*/
public TableCellEditor getCellEditor(int row, int column) {
if (column == 1) { //station type
/* select the right editor */
if (LD_enabled_system) {
return LD_enabled_StationTypeEditor;
} else {
return LD_disabled_StationTypeEditor;
}
} else {
return super.getCellEditor(row, column);
}
}
//BEGIN Federico Dall'Orso 8/3/2005
//NEW
/**Returns combobox-styled cellrenderer if a multiple choice cell is to be rendered.
* @return cell renderer*/
public TableCellRenderer getCellRenderer(int row, int column){
//if this is type column, I must render it as a combo box instead of a jtextfield
if(column==1){
if(LD_enabled_system)
return LD_enabled_StationTypeCell;
else
return LD_disabled_StationTypeCell;
}
else if(column==getColumnCount()-1) return deleteButtonCellRenderer;
else return new DefaultTableCellRenderer();
}
//END Federico Dall'Orso 8/3/2005
//BEGIN Federico Dall'Orso 14/3/2005
//NEW
//Updates appearence of last column's buttons
void updateDeleteCommand(){
deleteOneStation.setEnabled(stations>1);
getColumnModel().getColumn(getColumnCount()-1).setMinWidth(20);
getColumnModel().getColumn(getColumnCount()-1).setMaxWidth(20);
}
//END Federico Dall'Orso 14/3/2005
protected void updateActions() {
deleteStation.setEnabled(stations > 1 && getSelectedRowCount() > 0);
deleteOneStation.setEnabled(stations>1);
}
}
/**
* the model backing the station table
*/
private class StationTableModel extends ExactTableModel {
private Object[] prototypes = {"10000",
new String(new char[15]),
new String(new char[15]),
""
};
public Object getPrototype(int columnIndex) {
return prototypes[columnIndex + 1];
}
public int getRowCount() {
return stations;
}
public int getColumnCount() {
return 3;
}
public String getColumnName(int index) {
switch (index) {
case 0:
return "Name";
case 1:
return "Type";
default:
return null;
}
}
protected Object getRowName(int rowIndex) {
return new Integer(rowIndex + 1);
}
protected Object getValueAtImpl(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0://name
return stationNames[rowIndex];
case 1://type
return STATION_TYPENAMES_LD_ENABLED[stationTypes[rowIndex]];
default:
return null;
}
}
public void setValueAt(Object value, int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0: //name
stationNames[rowIndex] = (String) value;
break;
case 1: //type
for (int i = 0; i < STATION_TYPENAMES_LD_ENABLED.length; i++) {
if (value == STATION_TYPENAMES_LD_ENABLED[i]) { //literal strings are canonical objects, hence == is ok
stationTypes[rowIndex] = i;
break;
}
}
break;
default:
}
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return true;
case 1:
return false;
default:
return false;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -