📄 classespanel.java
字号:
}
//synchronizes components to display coherently global number of classes
protected void refreshComponents(){
classTable.tableChanged(new TableModelEvent(classTable.getModel()));
try{
classNumSpinner.setValue(new Integer(data.getClassKeys().size()));
}catch(NumberFormatException nfe){}
if(data.getClassKeys().size()>=MAX_NUMBER_OF_CLASSES) addClass.setEnabled(false);
else addClass.setEnabled(true);
}
/*delete a class from model given the index the class to be deleted is displayed at
inside the table.*/
protected void deleteClass(int index){
data.deleteClass(data.getClassKeys().get(index));
refreshComponents();
}
/*Multiple deletion of classes. Indexes to ship call to precedent method, are retrieved
through classtable methods (get selected rows)*/
protected void deleteSelectedClasses(){
int[] rows = classTable.getSelectedRows();
for(int i=rows.length-1; i>=0; i--){
deleteClass(rows[i]);
}
}
/*Modify global number of classes for this model all at once.*/
protected void setNumberOfClasses(int newNumber){
/*If new number is greater than a certain number, don't do anything and cancel
number modification inside spinner*/
if(newNumber > MAX_NUMBER_OF_CLASSES){
setNumberOfClasses(MAX_NUMBER_OF_CLASSES);
return;
}
/*If new number is not valid, reset to 0*/
if(newNumber < 0){
setNumberOfClasses(0);
return;
}
int oldNumber = data.getClassKeys().size();
/*If new number is greater than former one, just add */
if(newNumber > oldNumber){
for(int i = oldNumber; i<newNumber; i++){
addClass();
}
}else if(newNumber < oldNumber){
/*otherwise, just delete*/
for(int i = oldNumber-1; i >= newNumber; i--){
deleteClass(i);
}
}
refreshComponents();
}
//---------------------------- Table containing classes parameters --------------------------
/*Table that must display all of data about user classes. Customization of table settings is
obtained via inheritation of <code>JTable</code> Class.*/
protected class ClassTable extends JTable{
/* for selection of class types. Must be inserted in ComboBoxCellEditor*/
protected Object[] classTypes = new Object[]
{"Closed", "Open"};
/*This button allow a single userclass to be deleted directly from the table.
Corresponding value contained into cell must be zero.*/
public JButton deleteButton = new JButton(){
{
setAction(deleteClass);
setFocusable(false);
}
};
/*This button activates the distribution editor. Returning value must be sent
to underlying model definition, and then diplayed in another column contained*/
protected JButton editDistributionButton= new JButton(){
{
setText("Edit");
}
};
/*Set of column dimensions*/
protected int[] columnSizes = new int[]{120,50,50,50,150,38,18};
//Sets a table model for visualization and editing of data
public void setModel(ClassTableModel tabMod){
super.setModel(tabMod);
sizeColumnsAndRows();
setDefaultRenderer(String.class, new jmt.gui.exact.table.DisabledCellRenderer());
setDefaultEditor(Object.class, new jmt.gui.exact.table.ExactCellEditor());
setRowHeight(ROW_HEIGHT);
}
//returns a component to be contained inside a table column(or cell)
public TableCellRenderer getCellRenderer(int row, int column){
if(column == 1)
return comboEditor.getRenderer();
else if(column == 5){
/*if distribution column contains null value, no editor must be displayed,
as this class is a closed one (e.g. described by population)*/
if(getValueAt(row, column-1) != null){
return new ButtonCellEditor(editDistributionButton);
}else{
return getDefaultRenderer(String.class);
}
}
//Addition of column that contains delete buttons
if(column == 6) return new ButtonCellEditor(deleteButton);
else return getDefaultRenderer(getModel().getColumnClass(column));
}
/*returns customized editor for table cells.*/
public TableCellEditor getCellEditor(int row, int column){
if(column == 1){
return comboEditor.getEditor(classTypes);
}else if(column == 5 && getValueAt(row, column-1) != null){
return new ButtonCellEditor(new JButton(editDistribution));
}else if(column == 6){
return new ButtonCellEditor(new JButton(deleteClass));
}
else return super.getCellEditor(row, column);
}
//set sizes for columns and rows of this table.
protected void sizeColumnsAndRows(){
for (int i = 0; i < columnSizes.length && i < getColumnCount(); i++) {
this.getColumnModel().getColumn(i).setPreferredWidth(columnSizes[i]);
if(i==columnSizes.length-1){
//delete button and containing table cells as well, must be square
this.getColumnModel().getColumn(i).setMaxWidth(columnSizes[i]);
this.setRowHeight(columnSizes[i]);
}
}
}
}
//------------------------------------Table model for classes panel --------------------------
/*Table data model to implement customized data editing*/
protected class ClassTableModel extends AbstractTableModel{
//Names of columns contained in table. Columns containing buttons have empty names
protected String[] columnNames = new String[]{"Name", "Type", "Priority",
"Population", "Arrival Time Distribution", "", ""};
//Class declarations for this table's columns.
protected Class[] colClasses = new Class[]{String.class, JComboBox.class, String.class,
String.class, String.class, Object.class,
JButton.class};
/**Creates a new instance of class table model*/
public ClassTableModel(){
super();
}
/**returns number of rows to be displayed in the table. In this case, global
* number of classes*/
public int getRowCount() {
if(data.getClassKeys()!=null) return data.getClassKeys().size();
else return 0;
}
/**Returns total number of columns*/
public int getColumnCount() {
return columnNames.length;
}
/**Returns name for each column (given its index) to be displayed
* inside table header*/
public String getColumnName(int columnIndex) {
if(columnIndex<columnNames.length) return columnNames[columnIndex];
else return null;
}
/**Returns class describing data contained in specific column.*/
public Class getColumnClass(int columnIndex) {
if(columnIndex < colClasses.length) return colClasses[columnIndex];
else return Object.class;
}
/**Tells wether data contained in a specific cell(given row and column index)
* is editable or not. In this case distribution column is not editable, as
* editing functionality is implemented via edit button*/
public boolean isCellEditable(int rowIndex, int columnIndex) {
if(columnIndex == 3 && (getValueAt(rowIndex, 1).equals("Open"))) return false;
if(columnIndex == 5 && (getValueAt(rowIndex, 1).equals("Closed"))) return false;
if(columnIndex == 4) return false;
return true;
}
/**retrieves value to be displayed in table cell from the underlying model
* data structure implementation.*/
public Object getValueAt(int rowIndex, int columnIndex) {
Object key = data.getClassKeys().get(rowIndex);
switch(columnIndex){
case(0):{
return data.getClassName(key);
}case(1):{
int type = data.getClassType(key);
if(type == CLASS_TYPE_CLOSED) return "Closed";
else if(type == CLASS_TYPE_OPEN) return "Open";
}case(2):{
return new Integer(data.getClassPriority(key));
}case(3):{
return data.getClassPopulation(key);
}case(4):{
return data.getClassDistribution(key);
}default:{
return null;
}
}
}
/**Puts edited values to the underlying data structure for model implementation*/
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Object key = data.getClassKeys().get(rowIndex);
switch(columnIndex){
case(0):{
data.setClassName((String)aValue, key);
break;
}case(1):{
if((aValue).equals("Closed")) data.setClassType(CLASS_TYPE_CLOSED, key);
else if((aValue).equals("Open")) data.setClassType(CLASS_TYPE_OPEN, key);
break;
}case(2):{
try{
int priority = Integer.parseInt((String)aValue);
if(priority >= 0)data.setClassPriority(priority, key);
}catch(NumberFormatException nfe){}
break;
}case(3):{
try{
Integer population = Integer.decode((String)aValue);
if(population.intValue() > 0)data.setClassPopulation(population, key);
}catch(NumberFormatException nfe){}
break;
}/*case(5):{
data.setClassDistribution(aValue, key);
break;
}*/
}
/*if editing cell belongs to class type column, i must update population and
distribution cells*/
if(columnIndex == 1)repaint();
}
}
/**
* called by the Wizard before when switching to another panel
*/
public void lostFocus() {
// Aborts editing of table
TableCellEditor editor = classTable.getCellEditor();
if (editor != null)
editor.stopCellEditing();
// Preloads jobs
((SimulationDefinition)data).manageJobs();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -