jmodelclassespanel.java

来自「一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!」· Java 代码 · 共 172 行

JAVA
172
字号
/**    
  * 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.jmodel.panels;

import jmt.gui.common.definitions.ClassDefinition;
import jmt.gui.common.editors.ColorCellEditor;
import jmt.gui.common.editors.ImagedComboBoxCellEditorFactory;
import jmt.gui.jmodel.JMODELConstants;
import jmt.gui.jmodel.definitions.JmodelClassDefinition;
import jmt.gui.jmodel.definitions.JmodelStationDefinition;
import jmt.gui.jsim.editors.ButtonCellEditor;
import jmt.gui.jsim.panels.ClassesPanel;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

/**
 * <p>Title: JModel User Classes Panel</p>
 * <p>Description: Panel used to edit user classes properties. Extends JSIM ClassesPanel.
 * In some points it is a bit tricky, as original ClassesPanel was not meant to be extended.
 * I preferred not to change ClassesPanel source code, nor copy and modificate it as
 * at the time of writing it was in early development stage and I didn't want to collide
 * with the author.</p>
 * 
 * @author Bertoli Marco
 *         Date: 14-giu-2005
 *         Time: 9.42.15
 */
public class jmodelClassesPanel extends ClassesPanel implements JMODELConstants {
    protected JmodelClassDefinition classdefinition;
    protected JmodelStationDefinition stationdefinition;

    protected ImagedComboBoxCellEditorFactory stations;

    /**
     * Construct a new jmodelClassesPanel
     * @param cd a reference to the underlayng data structure
     */
    public jmodelClassesPanel(JmodelClassDefinition cd, JmodelStationDefinition sd) {
        super();
        classdefinition = cd;
        stationdefinition = sd;
        stations = new ImagedComboBoxCellEditorFactory(sd);
        classTable = new jmodelClassTable();
        initComponents();
        classTable.getTableHeader().setReorderingAllowed(false);
        setData(cd);
    }

    /**Sets data model for this panel.
     * Instantly all of the panel components are assigned their specific value.
     * @param cd: data for class definition.*/
    public void setData(ClassDefinition cd){
        data = cd;
        classTable.setModel(new jmodelClassTableModel());
        classNumSpinner.setValue(new Integer(data.getClassKeys().size()));
    }

    /**
     * Adds a new class
     */
    protected void addClass(){
        classdefinition.addClass();
        refreshComponents();
    }

    /**
     * Overrides ClassTable. It is a table containing class parameters.
     * Added suppport for "Color" column and rererence station combobox
     */
    protected class jmodelClassTable extends ClassTable{
        public jmodelClassTable() {
            super();
            columnSizes = new int[]{8,100,60,28,38,140,39,90,18};
        }

        public TableCellRenderer getCellRenderer(int row, int column){
            if (column == 0)
                return new ColorCellEditor();
            else if (column == 2)
                return super.getCellRenderer(row, 1);
            else if (column == 6){
                /*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);
                }
            }
            // Column for refercence source
            else if (column == 7)
                return stations.getRenderer();
            // Addition of column that contains delete buttons
            else if(column == 8) return new ButtonCellEditor(deleteButton);
            else return getDefaultRenderer(getModel().getColumnClass(column));
        }

        public TableCellEditor getCellEditor(int row, int column){
            if (column == 0)
                return new ColorCellEditor();
            else if(column == 6 && getValueAt(row, column-1) != null){
                return new ButtonCellEditor(new JButton(editDistribution));
            }
            else if (column == 7 && getValueAt(row, 2).equals("Open"))
                return stations.getEditor(stationdefinition.getStationKeysSource());
            else if (column == 7 && getValueAt(row, 2).equals("Closed"))
                return stations.getEditor(stationdefinition.getStationKeysNoSourceSink());
            else if (column == 8)
                return new ButtonCellEditor(new JButton(deleteClass));
            else return super.getCellEditor(row, column - 1);
        }
    }

    /**
     * Define a custom table model. Overrides ClassTableModel to add support for "Color" column
     */
    protected class jmodelClassTableModel extends ClassTableModel{
        public jmodelClassTableModel() {
            super();
            columnNames = new String[]{"Color","Name", "Type", "Priority",
                                            "Population", "Arrival Time Distribution", "",
                                            "Reference Station", ""};
            colClasses = new Class[]{Color.class, String.class, JComboBox.class, String.class,
                                         String.class, String.class, Object.class,
                                         JComboBox.class, JButton.class};
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0)
                return classdefinition.getClassColor(classdefinition.getClassKeys().get(rowIndex));
            else if (columnIndex == 7)
                return classdefinition.getClassRefStation(classdefinition.getClassKeys().get(rowIndex));
            else
                return super.getValueAt(rowIndex, columnIndex - 1);
        }
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            if (columnIndex == 0)
                classdefinition.setClassColor(classdefinition.getClassKeys().get(rowIndex), (Color) aValue);
            else if (columnIndex == 7) {
                classdefinition.setClassRefStation(classdefinition.getClassKeys().get(rowIndex), aValue);
            }
            else
                super.setValueAt(aValue, rowIndex, columnIndex - 1);
        }
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            if(columnIndex == 4 && (getValueAt(rowIndex, 2).equals("Open"))) return false;
            if(columnIndex == 6 && (getValueAt(rowIndex, 2).equals("Closed"))) return false;
            if(columnIndex == 5) return false;
            return true;
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?