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

📄 blockingstationpanel.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        });
    }

    /**
     * Updates table and spinner values to reflect number of stations
     */
    protected void update() {
        // Fires a table change event
        stationTable.tableChanged(new TableModelEvent(stationTable.getModel()));
        // Updates spinner value
        stationNumSpinner.setValue(new Integer(stations.size()));
        // Check if stations can be added
        addStation.setEnabled(bd.getBlockableStationKeys().size() > 0);
    }

    /**
     * Adds a new station to current blocking region. Takes the first available station
     */
    protected void add() {
        if (bd.getBlockableStationKeys().size() > 0) {
            Object key = bd.getBlockableStationKeys().get(0);
            bd.addRegionStation(regionKey, key);
            stations.add(key);
        }
        else
            addStation.setEnabled(false);
    }

    /**
     * Table used to show stations inside a region
     */
    protected class StationTable extends JTable {
        /** Cell renderer for deletion */
        private ButtonCellEditor deleteRenderer;

        /**
         * Builds a new Blocking region Station Table
         */
        public StationTable() {
            super(new StationTableModel());
            setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            delete.setFocusable(false);
            deleteRenderer = new ButtonCellEditor(delete);
            getColumnModel().getColumn(1).setMaxWidth(ROW_HEIGHT);
            getColumnModel().setColumnSelectionAllowed(false);
            setRowHeight(ROW_HEIGHT);
        }

        /**
         * Returns an appropriate editor for the cell specified by
         * <code>row</code> and <code>column</code>. If the
         * <code>TableColumn</code> for this column has a non-null editor,
         * returns that.  If not, finds the class of the data in this
         * column (using <code>getColumnClass</code>)
         * and returns the default editor for this type of data.
         * <p/>
         * <b>Note:</b>
         * Throughout the table package, the internal implementations always
         * use this method to provide editors so that this default behavior
         * can be safely overridden by a subclass.
         *
         * @param row    the row of the cell to edit, where 0 is the first row
         * @param column the column of the cell to edit,
         *               where 0 is the first column
         * @return the editor for this cell;
         *         if <code>null</code> return the default editor for
         *         this type of cell
         * @see javax.swing.DefaultCellEditor
         */
        public TableCellEditor getCellEditor(int row, int column) {
            if (column == 1)
                return new ButtonCellEditor(delete);
            else {
                // Builds an ordered array with station keys that can be added and current one
                SortedSet tmp = new TreeSet(bd.getBlockableStationKeys());
                tmp.add(stations.get(row));
                return comboFactory.getEditor(tmp.toArray());
            }
        }

        /**
         * Returns an appropriate renderer for the cell specified by this row and
         * column. If the <code>TableColumn</code> for this column has a non-null
         * renderer, returns that.  If not, finds the class of the data in
         * this column (using <code>getColumnClass</code>)
         * and returns the default renderer for this type of data.
         * <p/>
         * <b>Note:</b>
         * Throughout the table package, the internal implementations always
         * use this method to provide renderers so that this default behavior
         * can be safely overridden by a subclass.
         *
         * @param row    the row of the cell to render, where 0 is the first row
         * @param column the column of the cell to render,
         *               where 0 is the first column
         * @return the assigned renderer; if <code>null</code>
         *         returns the default renderer
         *         for this type of object
         * @see javax.swing.table.DefaultTableCellRenderer
         * @see javax.swing.table.TableColumn#setCellRenderer
         * @see #setDefaultRenderer
         */
        public TableCellRenderer getCellRenderer(int row, int column) {
            if (column==1)
                return deleteRenderer;
            else
                return comboFactory.getRenderer();
        }

        /** Button to delete selected station */
        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 = stationTable.getSelectedRow();
                if(index >= 0 && index<stationTable.getRowCount()) {
                    Object key = stations.get(index);
                    stations.remove(key);
                    bd.removeRegionStation(regionKey, key);
                    BlockingStationPanel.this.update();
                }
            }
        });
    }

    /**
     * Model for table used to show stations inside a region
     */
    protected class StationTableModel extends AbstractTableModel {

        /**
         * Returns the number of columns in the model. A
         * <code>JTable</code> uses this method to determine how many columns it
         * should create and display by default.
         *
         * @return the number of columns in the model
         * @see #getRowCount
         */
        public int getColumnCount() {
            return 2;
        }

        /**
         * Returns the number of rows in the model. A
         * <code>JTable</code> uses this method to determine how many rows it
         * should display.  This method should be quick, as it
         * is called frequently during rendering.
         *
         * @return the number of rows in the model
         * @see #getColumnCount
         */
        public int getRowCount() {
            return stations.size();
        }

        /**
         * Returns true if the cell at <code>rowIndex</code> and
         * <code>columnIndex</code>
         * is editable.  Otherwise, <code>setValueAt</code> on the cell will not
         * change the value of that cell.
         *
         * @param    rowIndex    the row whose value to be queried
         * @param    columnIndex    the column whose value to be queried
         * @return true if the cell is editable
         * @see #setValueAt
         */
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex == 1 || bd.getBlockableStationKeys().size() > 0;
        }

        /**
         * Returns the value for the cell at <code>columnIndex</code> and
         * <code>rowIndex</code>.
         *
         * @param    rowIndex    the row whose value is to be queried
         * @param    columnIndex the column whose value is to be queried
         * @return the value Object at the specified cell
         */
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0)
                    return stations.get(rowIndex);
            else
                return null;
        }

        /**
         * Sets the value in the cell at <code>columnIndex</code> and
         * <code>rowIndex</code> to <code>aValue</code>.
         *
         * @param    aValue         the new value
         * @param    rowIndex     the row whose value is to be changed
         * @param    columnIndex the column whose value is to be changed
         */
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                // If value changed
                if (stations.get(rowIndex) != aValue) {
                    bd.removeRegionStation(regionKey, stations.get(rowIndex));
                    stations.set(rowIndex, aValue);
                    bd.addRegionStation(regionKey, aValue);
                }
            }
        }

        /**
         * Returns the name of the column at <code>columnIndex</code>.  This is used
         * to initialize the table's column header name.  Note: this name does
         * not need to be unique; two columns in a table can have the same name.
         *
         * @return the name of the column
         * @param    columnIndex    the index of the column
         */
        public String getColumnName(int columnIndex) {
            if (columnIndex == 0)
                return "Station name";
            else
                return "";
        }
    }


}

⌨️ 快捷键说明

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