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

📄 allblockingregionspanel.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.ClassDefinition;
import jmt.gui.common.definitions.StationDefinition;
import jmt.gui.common.definitions.BlockingRegionDefinition;
import jmt.gui.common.CommonConstants;
import jmt.gui.common.Defaults;
import jmt.gui.common.editors.GrayCellRenderer;
import jmt.gui.common.resources.ImageLoader;
import jmt.gui.jsim.editors.ButtonCellEditor;

import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ParseException;

/**
 * <p>Title: All Blocking Region Panel</p>
 * <p>Description: This is the tabbed pane shown in JSIM for the specification of blocking
 * regions.</p>
 *
 * @author Bertoli Marco
 *         Date: 5-mag-2006
 *         Time: 15.41.01
 */
public class AllBlockingRegionsPanel extends WizardPanel implements CommonConstants {
    private ClassDefinition cd;
    private StationDefinition sd;
    private BlockingRegionDefinition bd;
    // Global components
    private JSpinner regionsNumSpinner;
    private JButton addRegion;
    private JTable regions;
    private int counter = 1; // Used for name generation
    private BlockingStationPanel blockingPanel;
    private JPanel emptyPanel = new JPanel();

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

    /**
     * called by the Wizard when the panel becomes active
     */
    public void gotFocus() {
        if (blockingPanel != null)
            blockingPanel.gotFocus();
        // Selects first blocking region if none selected and if present
        if (regions.getRowCount() > 0 && regions.getSelectedRow() < 0)
            regions.getSelectionModel().setSelectionInterval(0,0);
    }

    /**
     * @return the panel's name
     */
    public String getName() {
        return "Finite Capacity Regions";
    }

    /**
     * Creates a new BlockingRegionsPanel with given data structures
     * @param cd class definition data structure
     * @param sd station definition data structure
     * @param bd blocking region definition data structure
     */
    public AllBlockingRegionsPanel(ClassDefinition cd, StationDefinition sd, BlockingRegionDefinition bd) {
        this.cd = cd;
        this.sd = sd;
        this.bd = bd;
        initComponents();
        initActions();
    }

    /**
     * Changes data structure references of this panel
     * @param cd
     * @param sd
     * @param bd
     */
    public void setData(ClassDefinition cd, StationDefinition sd, BlockingRegionDefinition bd) {
        this.cd = cd;
        this.sd = sd;
        this.bd = bd;
        counter = 1;
        // Updates number of regions shown
        update();
    }

    /**
     * Initialize all components of this panel
     */
    private void initComponents(){
        setLayout(new BorderLayout(5,5));
        this.setBorder(new EmptyBorder(20,20,20,20));
        // Builds upper panel
        JPanel upperPanel = new JPanel(new BorderLayout());
        JLabel description = new JLabel(BLOCKING_DESCRIPTION);
        upperPanel.add(description, BorderLayout.CENTER);

        //build upper right corner of the main panel
        JPanel upRightPanel = new JPanel(new BorderLayout());
        addRegion = new JButton("Add Region");
        addRegion.setMinimumSize(DIM_BUTTON_S);
        upRightPanel.add(addRegion, BorderLayout.CENTER);
        upperPanel.add(upRightPanel, BorderLayout.EAST);

        //build spinner panel
        JPanel spinnerPanel = new JPanel();
        JLabel spinnerDescrLabel = new JLabel("Regions:");
        regionsNumSpinner = new JSpinner();
        regionsNumSpinner.setPreferredSize(DIM_BUTTON_XS);
        spinnerPanel.add(spinnerDescrLabel);
        spinnerPanel.add(regionsNumSpinner);
        upRightPanel.add(spinnerPanel,BorderLayout.SOUTH);

        add(upperPanel, BorderLayout.NORTH);

        // Creates blocking regions list
        regions = new RegionTable();
        regions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        JScrollPane jsp = new JScrollPane(regions,
        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jsp.setPreferredSize(new Dimension(220,200));
        add(jsp, BorderLayout.WEST);
        update();
    }

    /**
     * Initialize actions for every component of this window
     */
    private void initActions() {
        // Sets add button action
        addRegion.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addRegion();
                update();
            }
        });

        // Sets spinner actions
        regionsNumSpinner.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                //stop editing text inside spinner
                try{
                    regionsNumSpinner.commitEdit();
                }catch(ParseException pe){
                    //if string does not represent a number, return
                    return;
                }
                Object value = regionsNumSpinner.getValue();
                if (value instanceof Integer) {
                    int val = ((Integer)value).intValue();
                    // Avoid placement of thousands of regions
                    if (val > MAX_NUMBER_OF_REGIONS)
                        val = MAX_NUMBER_OF_REGIONS;
                    // If value is valid
                    if (val >= 0) {
                        int diff = val - bd.getRegionKeys().size();
                        // Avoid ciclic updates whenever no differences are inserted
                        if (diff == 0)
                            return;

                        while (diff > 0) {
                            // Add new region
                            addRegion();
                            diff--;
                        }
                        while (diff < 0) {
                            // Removes last region
                            bd.deleteBlockingRegion(bd.getRegionKeys().lastElement());
                            diff++;
                        }
                    }
                    update();
                }
            }
        });

        // Sets table selection action
        regions.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            /**
             * Called whenever the value of the selection changes.
             *
             * @param e the event that characterizes the change.
             */
            public void valueChanged(ListSelectionEvent e) {
                // Retrives blocking region key
                Object key = regions.getValueAt(regions.getSelectedRow(), 0);
                // If no elements are selected, removes old blocking panel

                if (key == null) {
                    if (blockingPanel != null) {
                        blockingPanel.lostFocus();
                        // Hides blocking panel before removing it to avoid validation bugs under Windows
                        blockingPanel.setVisible(false);
                        AllBlockingRegionsPanel.this.remove(blockingPanel);
                        AllBlockingRegionsPanel.this.add(emptyPanel);
                        blockingPanel = null;
                        AllBlockingRegionsPanel.this.validate();
                    }
                    return;
                }

                // A new blocking panel has to be created
                if (blockingPanel == null){
                    AllBlockingRegionsPanel.this.remove(emptyPanel);
                    blockingPanel = new BlockingStationPanel(cd, sd, bd, key);
                    AllBlockingRegionsPanel.this.add(blockingPanel, BorderLayout.CENTER);
                    blockingPanel.gotFocus();
                    AllBlockingRegionsPanel.this.validate();
                }
                // Modify existing panel
                else {
                    // Lose focus on old data
                    blockingPanel.lostFocus();
                    blockingPanel.setData(cd, sd, bd, key);
                    // Got focus for new data
                    blockingPanel.gotFocus();
                }
            }
        });
    }

    /**
     * Adds a new blocking region to current model
     */
    private void addRegion() {
        if (bd.getRegionKeys().size() < MAX_NUMBER_OF_REGIONS)
            bd.addBlockingRegion(Defaults.get("blockingRegionName") + counter++, Defaults.get("blockingRegionType"));
    }
    /**
     * Updates region spinner and region list to reflect current blocking regions
     */
    private void update() {
        regionsNumSpinner.setValue(new Integer(bd.getRegionKeys().size()));
        regions.tableChanged(new TableModelEvent(regions.getModel()));
        regions.repaint();
        if (bd.getRegionKeys().size() >= MAX_NUMBER_OF_REGIONS)
            addRegion.setEnabled(false);
        else
            addRegion.setEnabled(true);

        // Selects first blocking region if none selected and if present
        if (regions.getSelectedRow() < 0)
            regions.getSelectionModel().setSelectionInterval(0,0);
    }

    protected class RegionTable extends JTable {
        /** Cell renderer for region names */
        private BlockingElementRenderer blockRenderer;
        /** Cell renderer for deletion */
        private ButtonCellEditor deleteRenderer;
        /** Cell renderer for capacity */
        private TableCellRenderer gray = new GrayCellRenderer();

        /** Button to delete selected region */
        private JButton delete = new JButton(new AbstractAction() {
            {
                putValue(Action.SHORT_DESCRIPTION, "Delete");
                putValue(Action.SMALL_ICON, ImageLoader.loadImage("Delete"));
            }
            /**
             * Invoked when an action occurs.
             */
            public void actionPerformed(ActionEvent e) {
                int index = regions.getSelectedRow();
                if(index >= 0 && index<regions.getRowCount()) {
                    Object key = regions.getValueAt(index, 0);
                    bd.deleteBlockingRegion(key);
                    AllBlockingRegionsPanel.this.update();
                    // Select next or prev element
                    if (regions.getRowCount() > index)
                        regions.getSelectionModel().setSelectionInterval(index, index);

⌨️ 快捷键说明

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