⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classespanel.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**    
  * 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.jsim.panels;

import jmt.gui.common.CommonConstants;
import jmt.gui.common.Defaults;
import jmt.gui.common.definitions.ClassDefinition;
import jmt.gui.common.definitions.SimulationDefinition;
import jmt.gui.common.distributions.Distribution;
import jmt.gui.common.editors.DistributionsEditor;
import jmt.gui.common.editors.ImagedComboBoxCellEditorFactory;
import jmt.gui.common.resources.ImageLoader;
import jmt.gui.jsim.editors.ButtonCellEditor;
import jmt.gui.wizard.WizardPanel;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TableModelEvent;
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.awt.event.KeyEvent;
import java.text.ParseException;

/**
 * Created by IntelliJ IDEA.
 * User: OrsotronIII
 * Date: 13-mag-2005
 * Time: 14.43.32
 * This panel provides functionality of editing and visualizing data about model's
 * classes.
 * Modified by Bertoli Marco 9-jan-2006  --> ComboBoxCellEditor
 */
public class ClassesPanel extends WizardPanel implements CommonConstants {


    //Interface containing values of main parameters to be set by user
    /*TODO: to enable this functionality, just put a constructor accepting a customized
    *JSIMDefault class as parameter, and then put a form to allow user to generate instances
    *of this class.*/
    //protected Defaults defaults = new Defaults(){};

    //Table containg class-set data
    protected ClassTable classTable;

    //ComboBox editor for station type
    protected ImagedComboBoxCellEditorFactory comboEditor = new ImagedComboBoxCellEditorFactory();

    //Button that allows to add classes one by one
    protected JButton addClass;

    //Component responsible of setting global number of classes at once
    protected JSpinner classNumSpinner = new JSpinner(){
        {
            addChangeListener(new ChangeListener(){
                public void stateChanged(ChangeEvent e) {
                    //stop editing text inside spinner
                    try{
                        classNumSpinner.commitEdit();
                    }catch(ParseException pe){
                        //if string does not represent a number, return
                        return;
                    }
                    //new number of classes
                    int x = -1;
                    try{
                        x = ((Integer)classNumSpinner.getValue()).intValue();
                    }catch(NumberFormatException nfe){
                    }catch(ClassCastException cce){}
                    //if new number is valid, proceed updating number
                    if(x !=- 1){
                        setNumberOfClasses(x);
                    }else{
                        //otherwise, reset to 0
                        classNumSpinner.setValue(new Integer(0));
                    }
                }
            });
        }
    };

    //Interface linking to underlying implementation layer
    protected ClassDefinition data;

    //Index for temporary class name assignment
    protected int classNameIndex = 0;

    //deletion of classes after a multiple selection
    protected AbstractAction deleteSelectedClasses = new AbstractAction("Delete selected classes") {
            {
                putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false));
                putValue(Action.SHORT_DESCRIPTION, "Deletes selected classes from the system");
            }

            public void actionPerformed(ActionEvent e) {
                deleteSelectedClasses();
            }
        };

    //deletion of one class
    protected AbstractAction deleteClass = new AbstractAction("") {
            {
                putValue(Action.SHORT_DESCRIPTION, "Delete");
                putValue(Action.SMALL_ICON, ImageLoader.loadImage("Delete"));
            }

            public void actionPerformed(ActionEvent e) {
                int index = classTable.getSelectedRow();
                if(index>=0 && index<classTable.getRowCount())deleteClass(index);
            }
        };

    //addition of a class one by one
    protected AbstractAction addNewClass = new AbstractAction("Add Class") {
        {
            putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, ActionEvent.ALT_MASK));
            putValue(Action.SHORT_DESCRIPTION, "Adds a new class");
        }

        public void actionPerformed(ActionEvent e) {
            addClass();
        }
    };

    //editing of arrival time distribution
    protected AbstractAction editDistribution = new AbstractAction("Edit") {
            {
                putValue(Action.SHORT_DESCRIPTION, "Edits distribution parameters");
            }

            public void actionPerformed(ActionEvent e) {
                // ---- Bertoli Marco ----
                int index = classTable.getSelectedRow();
                if(index>=0 && index<classTable.getRowCount()) {
                    Object key = data.getClassKeys().elementAt(index);
                    DistributionsEditor editor = DistributionsEditor.getInstance(
                            ClassesPanel.this.getParent(),
                            (Distribution)data.getClassDistribution(key)
                    );
                    // Sets editor window title
                    editor.setTitle("Editing " + data.getClassName(key) + " distribution...");
                    // Shows editor window
                    editor.show();
                    // Sets new Distribution to selected class
                    data.setClassDistribution(editor.getResult(), key);

                    // Updates table view. This is needed as Distribution is not contained
                    // into edited cell (but in its left one)
                    classTable.repaint();
                }
                // ---- end ----
            }
        };

    /**Creates a new instance of <code>ClassesPanel</code> given a model definition.*/
    public ClassesPanel(ClassDefinition cd){
        super();
        classTable = new ClassTable();
        initComponents();
        //forbid column to be moved
        classTable.getTableHeader().setReorderingAllowed(false);
        setData(cd);
    }

    /**Default constructor, used to extend this panel for JModel*/
    public ClassesPanel() {
        super();
    }

    /**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 ClassTableModel());
        classNumSpinner.setValue(new Integer(data.getClassKeys().size()));
        comboEditor.clearCache();
    }

    /**Gets data model for this panel.
     * @return : data for class definition.*/
    public ClassDefinition getData(){
        return data;
    }

    /**
     * called by the Wizard when the panel becomes active
     */
    public void gotFocus() {
        comboEditor.clearCache();
    }

    //Builds internal structure of the panel. Sets up layout of components
    protected void initComponents(){
        //create margins for this panel.
        Box vBox = Box.createVerticalBox();
        Box hBox = Box.createHorizontalBox();
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(hBox);
        vBox.add(Box.createVerticalStrut(20));
        hBox.add(Box.createHorizontalStrut(20));

        //build central panel
        JPanel componentsPanel = new JPanel(new BorderLayout());
        //new BoxLayout(componentsPanel, BoxLayout.Y_AXIS);

        //build upper part of central panel
        JPanel upperPanel = new JPanel(new BorderLayout());
        JLabel descrLabel = new JLabel(CLASSES_DESCRIPTION);
        //descrLabel.setMaximumSize(new Dimension(300, 1000));
        upperPanel.add(descrLabel, BorderLayout.CENTER);

        //build upper right corner of the main panel
        JPanel upRightPanel = new JPanel(new BorderLayout());
        addClass = new JButton(addNewClass);
        addClass.setMinimumSize(DIM_BUTTON_S);
        upRightPanel.add(addClass, BorderLayout.CENTER);

        //build spinner panel
        JPanel spinnerPanel = new JPanel();
        JLabel spinnerDescrLabel = new JLabel("Classes:");
        //rangesNumSpinner = new JSpinner();
        classNumSpinner.setPreferredSize(DIM_BUTTON_XS);
        spinnerPanel.add(spinnerDescrLabel);
        spinnerPanel.add(classNumSpinner);

        //add all panels to the mail panel
        upRightPanel.add(spinnerPanel, BorderLayout.SOUTH);
        upperPanel.add(upRightPanel, BorderLayout.EAST);
        componentsPanel.add(upperPanel, BorderLayout.NORTH);
        componentsPanel.add(new JScrollPane(classTable), BorderLayout.CENTER);
        hBox.add(componentsPanel);
        hBox.add(Box.createHorizontalStrut(20));
        this.setLayout(new GridLayout(1,1));
        this.add(vBox);
    }

    //returns name to be displayed on the tab, when inserted in a wizard tabbed pane
    public String getName(){
        return "Classes";
    }

    //adds a new class to the table and, simultaneously to the underlying model data structure
    protected void addClass(){
        data.addClass(Defaults.get("className")+ (++classNameIndex),
                Defaults.getAsInteger("classType").intValue(),
                Defaults.getAsInteger("classPriority").intValue(),
                Defaults.getAsInteger("classPopulation"),
                Defaults.getAsNewInstance("classDistribution"));
        refreshComponents();

⌨️ 快捷键说明

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