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

📄 stationspanel.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.StationDefinition;
import jmt.gui.common.editors.ImagedComboBoxCellEditorFactory;
import jmt.gui.common.resources.ImageLoader;
import jmt.gui.exact.table.DisabledCellRenderer;
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.event.TableModelListener;
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;
import java.util.Vector;

/**
 * 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 11-oct-2005, 10-apr-2006
 */
public class StationsPanel extends WizardPanel implements CommonConstants {
    //Table containg class-set data
    private StationTable stationTable;

    //Button that allows to add classes one by one
    private JButton addStation;

    //ComboBox editor for station type
    protected ImagedComboBoxCellEditorFactory comboEditor;

    //Enabled types of station for station editing
    Object[] stationTypes = new Object[]{STATION_TYPE_DELAY,
            STATION_TYPE_SERVER,
            STATION_TYPE_FORK,
            STATION_TYPE_JOIN,
            STATION_TYPE_ROUTER};

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

    //Interface linking to underlying implementation layer
    private StationDefinition data;

    //Interface linking to underlying implementation layer
    private ClassDefinition classData;


    //Index for temporary station name assignment
    private int stationNameIndex = 0;

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

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

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

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

    /**Creates a new instance of <code>ClassesPanel</code> given a model definition.*/
    public StationsPanel(StationDefinition sd, ClassDefinition cd){
        super();
        stationTable = new StationTable();
        comboEditor = new ImagedComboBoxCellEditorFactory();
        initComponents();
        //forbid column to be moved
        stationTable.getTableHeader().setReorderingAllowed(false);
        setData(sd, cd);
    }

    /**Sets data model for this panel.
     * Instantly all of the panel components are assigned their specific value.
     * @param sd: data for station definition.*/
    public void setData(StationDefinition sd, ClassDefinition cd){
        data = sd;
        classData = cd;
        stationTable.setModel(new StationTableModel());
        stationNumSpinner.setValue(new Integer(data.getStationKeys().size()));
        comboEditor.clearCache();
    }

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

    //Builds internal structure of the panel. Sets up layout of components
    private 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(STATIONS_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());
        addStation = new JButton(addNewStation);
        addStation.setMinimumSize(DIM_BUTTON_S);
        upRightPanel.add(addStation, BorderLayout.CENTER);

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

        //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(stationTable), 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 "Stations";
    }

    //adds a new class to the table and, simultaneously to the underlying model data structure
    private void addStation(){
        data.addStation(Defaults.get("stationName")+ (++stationNameIndex),
                Defaults.get("stationType"));
        refreshComponents();
    }

    //synchronizes components to display coherently global number of classes
    private void refreshComponents(){
        stationTable.tableChanged(new TableModelEvent(stationTable.getModel()));
        try{
            stationNumSpinner.setValue(new Integer(data.getStationKeys().size()));
        }catch(NumberFormatException nfe){
            // Nothing to be done
        }
        if(data.getStationKeys().size()>=MAX_NUMBER_OF_STATIONS) addStation.setEnabled(false);
        else addStation.setEnabled(true);
    }

    public void repaint(){
        if(data!=null){
            refreshComponents();
        }

⌨️ 快捷键说明

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