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

📄 blockingstationpanel.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.wizard.WizardPanel;
import jmt.gui.common.definitions.BlockingRegionDefinition;
import jmt.gui.common.definitions.ClassDefinition;
import jmt.gui.common.definitions.StationDefinition;
import jmt.gui.common.panels.BlockingRegionParameterPanel;
import jmt.gui.common.panels.WarningScrollTable;
import jmt.gui.common.CommonConstants;
import jmt.gui.common.editors.ImagedComboBoxCellEditorFactory;
import jmt.gui.common.resources.ImageLoader;
import jmt.gui.jsim.editors.ButtonCellEditor;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * <p>Title: Blocking Region Station Definition Panel</p>
 * <p>Description: This panel is used to specify stations to be inserted into a
 * blocking region. This panel includes BlockingRegionParameterPanel to
 * specify class dependent constraints.</p>
 *
 * @author Bertoli Marco
 *         Date: 10-mag-2006
 *         Time: 16.53.41
 */
public class BlockingStationPanel extends WizardPanel implements CommonConstants {
    /** Blocking region definition data structure */
    protected BlockingRegionDefinition bd;
    /** Class definition data structure */
    protected ClassDefinition cd;
    /** Station definition data structure */
    protected StationDefinition sd;
    /** Current region key */
    protected Object regionKey;
    /** Inner panel to specify class constraints */
    protected BlockingRegionParameterPanel blockingPanel;

    /** Panel used to display stations */
    protected JPanel stationPanel;

    /** Table used to display stations */
    protected JTable stationTable;

    /** Spinner used to select number of stations */
    protected JSpinner stationNumSpinner;

    /** Button used to add a stations */
    protected JButton addStation;

    /** Vector used to store stations in a given region. This is needed as
     * data structure uses a set which is sorted */
    protected Vector stations;

    /** Used for station selection renderers */
    protected ImagedComboBoxCellEditorFactory comboFactory;

    /**
     * @return the panel's name
     */
    public String getName() {
        return "Blocking Region Station Definition";
    }

    /**
     * called by the Wizard before when switching to another panel
     */
    public void lostFocus() {
        if (blockingPanel != null)
            blockingPanel.lostFocus();
        // Aborts editing of table
        TableCellEditor editor = stationTable.getCellEditor();
        if (editor != null)
            editor.stopCellEditing();

    }

    /**
     * called by the Wizard when the panel becomes active
     */
    public void gotFocus() {
        if (blockingPanel != null)
            blockingPanel.gotFocus();
        stations.clear();
        stations.addAll(bd.getBlockingRegionStations(regionKey));
        comboFactory.clearCache();
        update();
    }

    /**
     * @param cd class definition data structure
     * @param brd blocking region definition data structure
     * @param key search's key for given blocking region
     */
    public BlockingStationPanel(ClassDefinition cd, StationDefinition sd, BlockingRegionDefinition brd, Object key) {
        this.cd = cd;
        this.bd = brd;
        this.sd = sd;
        this.regionKey = key;
        stations = new Vector();
        comboFactory = new ImagedComboBoxCellEditorFactory(sd);
        initComponent();
        addActions();
    }

    /**
     * Sets data for this panel
     * @param cd class definition data structure
     * @param brd blocking region definition data structure
     * @param key search's key for given blocking region
     */
    public void setData(ClassDefinition cd, StationDefinition sd, BlockingRegionDefinition brd, Object key) {
        this.cd = cd;
        this.sd = sd;
        this.bd = brd;
        this.regionKey = key;
        comboFactory.setData(sd);
        blockingPanel.setData(cd, brd, key);
        stationPanel.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(), "Stations in "+bd.getRegionName(regionKey)));
    }

    /**
     * Initialize all gui related stuff
     */
    private void initComponent() {
        setLayout(new GridLayout(2,1));
        stationPanel = new JPanel(new BorderLayout(5,5));
        stationPanel.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(), "Stations in "+bd.getRegionName(regionKey)));

        // Creates panel with add station button and spinner
        JPanel addPanel = new JPanel(new BorderLayout());
        addStation = new JButton("Add Station");
        addStation.setToolTipText("Adds a station to selected blocking region");
        addStation.setMinimumSize(DIM_BUTTON_S);
        addPanel.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);
        addPanel.add(spinnerPanel, BorderLayout.SOUTH);
        // Creates a tmp panel to put addStation panel on the northeast corner
        JPanel tmpPanel = new JPanel(new BorderLayout());
        tmpPanel.add(addPanel, BorderLayout.NORTH);
        stationPanel.add(tmpPanel,BorderLayout.EAST);
        // Creates table to display stations
        stationTable = new StationTable();
        WarningScrollTable warning = new WarningScrollTable(stationTable, WARNING_STATIONS);
        warning.addCheckVector(sd.getStationKeysNoSourceSink());
        stationPanel.add(warning);
        add(stationPanel);

        // Creates blocking region panel
        blockingPanel = new BlockingRegionParameterPanel(cd, bd, regionKey);
        // Hides unneeded global properties (specified by table)
        blockingPanel.setGlobalVisible(false);
        add(blockingPanel);
    }

    /**
     * Adds acrion listeners to gui elements
     */
    private void addActions() {
        // Add button
        addStation.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                add();
                update();
            }
        });

        stationNumSpinner.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                try {
                    int i = ((Integer)stationNumSpinner.getValue()).intValue();
                    // Avoid useless computations
                    if (i == stations.size())
                        return;

                    // Limit to maximum number of insertable stations
                    if (i - stations.size() > bd.getBlockableStationKeys().size())
                        i = bd.getBlockableStationKeys().size() + stations.size();

                    if (i < stations.size() && i >= 0) {
                        while (i < stations.size()) {
                            // Removes last element.
                            Object o = stations.remove(stations.size() - 1);
                            bd.removeRegionStation(regionKey, o);
                        }
                    }
                    else if (i > stations.size())
                        while (i>stations.size())
                            add();
                    update();
                } catch (ClassCastException e1) {
                    e1.printStackTrace();
                }
            }

⌨️ 快捷键说明

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