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

📄 connectionspanel.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
字号:
/**    
  * 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.definitions.StationDefinition;
import jmt.gui.common.panels.WarningScrollTable;
import jmt.gui.exact.table.DisabledCellRenderer;
import jmt.gui.exact.table.ExactTable;
import jmt.gui.exact.table.ExactTableModel;
import jmt.gui.wizard.WizardPanel;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: orsotronIII
 * Date: 21-giu-2005
 * Time: 16.44.47
 * To change this template use Options | File Templates.
 */
public class ConnectionsPanel extends WizardPanel implements CommonConstants {


    private StationDefinition data;
    private ConnectionTable connTab;



    public ConnectionsPanel(StationDefinition sd) {
        data = sd;
        initComponents();
    }



    /**
     * Set up the panel contents and layout
     */
    private void initComponents() {

        connTab = new ConnectionTable();

        //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(0,5));
        //new BoxLayout(componentsPanel, BoxLayout.Y_AXIS);

        //build upper part of central panel
        JLabel descrLabel = new JLabel(CONNECTIONS_DESCRIPTION);

        //add all panels to the mail panel
        componentsPanel.add(descrLabel, BorderLayout.NORTH);
        componentsPanel.add(new WarningScrollTable(connTab, WARNING_STATIONS), BorderLayout.CENTER);
        hBox.add(componentsPanel);
        hBox.add(Box.createHorizontalStrut(20));
        this.setLayout(new GridLayout(1,1));
        this.add(vBox);
    }


    public String getName() {
        return "Connections";
    }

    public void repaint(){
        if(connTab!=null)connTab.updateStructure();
        super.repaint();
    }

    public void setData(StationDefinition sd){
        this.data = sd;
        repaint();
    }



    private class ConnectionTable extends ExactTable {

        ConnectionTable() {
            super(new ConnTableModel());
            autoResizeMode = AUTO_RESIZE_OFF;

            createRenderers();
            createEditors();
            setDisplaysScrollLabels(true);

            setRowSelectionAllowed(false);
            setColumnSelectionAllowed(false);
            setClipboardTransferEnabled(false);

        }

        protected void createRenderers(){
            final TableCellRenderer boolRend = getDefaultRenderer(Boolean.class);
            setDefaultRenderer(Boolean.class, new DisabledCellRenderer(){
                public Component getTableCellRendererComponent(JTable table, Object value,
                                                               boolean isSelected, boolean hasFocus,
                                                               int row, int column) {
                    Component cbox = boolRend.getTableCellRendererComponent(
                                table, value, isSelected, hasFocus, row, column);
                    cbox.setEnabled(true);
                    if(value == null){
                        Component disabled = super.getTableCellRendererComponent(
                            table, value, isSelected, hasFocus, row, column);
                        cbox.setBackground(disabled.getBackground());
                        cbox.setEnabled(false);
                    }
                    return cbox;
                }
            });
        }

        protected void createEditors(){
            setDefaultEditor(Boolean.class, new DefaultCellEditor(new JCheckBox()){
                public Component getTableCellEditorComponent(JTable table, Object value,
                                                             boolean isSelected,
                                                             int row, int column) {
                    Component c = super.getTableCellEditorComponent(
                            table, value, isSelected, row, column
                    );
                    if(value instanceof Boolean){
                        c.setBackground(Color.WHITE);
                        c.setVisible(false);
                    }
                    return c;
                }
            });
        }
    }

    /**
     * the model backing the service times table.
     * Rows represent source stations, columns target stations.
     */
    private class ConnTableModel extends ExactTableModel {

        ConnTableModel() {
            prototype = "Station10000";
            rowHeaderPrototype = "Station10000";
        }

        public int getRowCount() {
            return data.getStationKeys().size();
        }

        public int getColumnCount() {
            return data.getStationKeys().size();
        }


        public Class getColumnClass(int columnIndex){
            if(columnIndex >= 0) return Boolean.class;
            else return super.getColumnClass(columnIndex);
        }

        public String getColumnName(int index) {
            if(index >= 0 && data.getStationKeys().size()>0) {
                return data.getStationName(getStationKey(index));
            }
            else return "";
        }

        public Object getPrototype(int i){
            if(i==-1) return rowHeaderPrototype;
            else return prototype;
        }

        protected Object getValueAtImpl(int rowIndex, int columnIndex) {
            Object row = getStationKey(rowIndex),
                    col = getStationKey(columnIndex);
            boolean areConnected = data.areConnected(row, col);
            boolean areConnectable = data.areConnectable(row, col);
            if(!areConnectable) return null;
            return new Boolean(areConnected);
        }

        protected Object getRowName(int rowIndex) {
            return data.getStationName(getStationKey(rowIndex));
        }

        //returns search key of a station given its index in table
        private Object getStationKey(int index){
            return data.getStationKeys().get(index);
        }

        public void setValueAt(Object value, int rowIndex, int columnIndex) {
            if(value instanceof Boolean){
                boolean b = ((Boolean)value).booleanValue();
                data.setConnected(getStationKey(rowIndex),
                        getStationKey(columnIndex),
                        b);
            }
        }

        public void clear(int row, int col) {
            data.setConnected(getStationKey(row),
                    getStationKey(col),
                    false);
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            Object row = getStationKey(rowIndex),
                    col = getStationKey(columnIndex);
            boolean areConnectable = data.areConnectable(row, col);
            if(!areConnectable) return false;
            else return true;
        }

    }

}

⌨️ 快捷键说明

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