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

📄 servicesectionpanel.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.common.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.distributions.Distribution;
import jmt.gui.common.editors.ComboBoxCellEditor;
import jmt.gui.common.editors.DistributionsEditor;
import jmt.gui.common.editors.LDStrategyEditor;
import jmt.gui.common.editors.ImagedComboBoxCellEditorFactory;
import jmt.gui.common.serviceStrategies.LDStrategy;
import jmt.gui.common.serviceStrategies.ZeroStrategy;
import jmt.gui.jsim.editors.ButtonCellEditor;
import jmt.gui.wizard.WizardPanel;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * Created by IntelliJ IDEA.
 * User: orsotronIII
 * Date: 30-giu-2005
 * Time: 14.01.23
 * Modified by Bertoli Marco 7-oct-2005  --> Added load Dependent
 *                           9-jan-2006  --> ComboBoxCellEditor
 */
public class ServiceSectionPanel extends WizardPanel implements CommonConstants {

    protected Object stationKey;
    protected StationDefinition data;
    protected ClassDefinition classData;

    private ServiceTable serviceTable;
    private JSpinner serversNumSpinner;

    /** Used to display classes with icon */
    protected ImagedComboBoxCellEditorFactory classEditor;

    //editing of arrival time distribution
    protected AbstractAction editDistribution = new AbstractAction("Edit") {
            {
                putValue(Action.SHORT_DESCRIPTION, "Edits service section parameters");
            }

            public void actionPerformed(ActionEvent e) {
                // ---- Bertoli Marco ----
                int index = serviceTable.getSelectedRow();
                if(index>=0 && index<serviceTable.getRowCount()) {
                    Object key = classData.getClassKeys().elementAt(index);
                    Object service = data.getServiceTimeDistribution(stationKey, key);
                    // If it's a Distribution, shows Distribution Editor
                    if (service instanceof Distribution) {
                        DistributionsEditor editor = DistributionsEditor.getInstance(
                                ServiceSectionPanel.this.getParent(),
                                (Distribution)service);
                        // Sets editor window title
                        editor.setTitle("Editing " + classData.getClassName(key) + " distribution...");
                        // Shows editor window
                        editor.show();
                        // Sets new Distribution to selected class
                        data.setServiceTimeDistribution(stationKey, key, editor.getResult());

                        // Updates table view. This is needed as Distribution is not contained
                        // into edited cell (but in its left one)
                        serviceTable.repaint();
                    }
                    // Otherwise shows LDStrategy Editor
                    else {
                            LDStrategyEditor editor = LDStrategyEditor.getInstance(
                                    ServiceSectionPanel.this.getParent(),
                                    (LDStrategy)service);
                            // Sets editor window title
                            editor.setTitle("Editing " + classData.getClassName(key) + " Load Dependent service strategy...");
                            // Shows editor window
                            editor.show();
                            serviceTable.repaint();
                    }
                }
                // ---- end ----
            }
        };


    public ServiceSectionPanel(StationDefinition sd, ClassDefinition cd, Object stationKey){
        super();
        classEditor = new ImagedComboBoxCellEditorFactory(cd);
        setData(sd, cd, stationKey);
    }

    private void initComponents(){
        //building mainPanel
        this.setLayout(new BorderLayout(5,5));
        this.setBorder(new EmptyBorder(5,5,5,5));
        //layout of main panel
        JPanel serversNumPanel = new JPanel();
        serversNumPanel.setBorder(new TitledBorder(new EtchedBorder(), "Number of Servers"));
        serversNumPanel.add(new JLabel("Number:"));
        serversNumPanel.add(serversNumSpinner);
        this.add(serversNumPanel, BorderLayout.NORTH);
        WarningScrollTable ServiceSectionTable = new WarningScrollTable(serviceTable, WARNING_CLASS);
        ServiceSectionTable.setBorder(new TitledBorder(new EtchedBorder(), "Service Time Distributions"));
        this.add(ServiceSectionTable, BorderLayout.CENTER);
    }

    public void setData(StationDefinition sd, ClassDefinition cd, Object stationKey){
        this.removeAll();
        this.stationKey = stationKey;
        data = sd;
        classData = cd;
        serviceTable = new ServiceTable();
        serversNumSpinner = new JSpinner();
        serversNumSpinner.setPreferredSize(DIM_BUTTON_XS);
        classEditor.setData(cd);
        initComponents();
        updateSpinner();
        addManagers();
    }

    private void updateSpinner(){
        serversNumSpinner.setValue(
                data.getStationNumberOfServers(stationKey));
        if(STATION_TYPE_DELAY.equals(data.getStationType(stationKey))){
            serversNumSpinner.setValue(new Float(Float.POSITIVE_INFINITY));
            serversNumSpinner.setEnabled(false);
        }
        else serversNumSpinner.setEnabled(true);
    }

    private void addManagers(){
        serversNumSpinner.addChangeListener(new ChangeListener(){
            public void stateChanged(ChangeEvent e) {
                if (serversNumSpinner.getValue() instanceof Integer) {
                    Integer serverNum = (Integer)serversNumSpinner.getValue();
                    if(serverNum.intValue()<1 && serversNumSpinner.isEnabled()){
                        serverNum = new Integer(1);
                        serversNumSpinner.setValue(serverNum);
                    }
                data.setStationNumberOfServers(serverNum, stationKey);
                }
            }
        });
    }

    public void repaint(){
        if(serviceTable!=null)serviceTable.tableChanged(new TableModelEvent(serviceTable.getModel()));
        super.repaint();
    }

    /**
     * @return the panel's name
     */
    public String getName() {
        return "Service Section";
    }

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

    /**
     * called by the Wizard when the panel becomes active
     */
    public void gotFocus() {
        classEditor.clearCache();
    }

    protected class ServiceTable extends JTable{
        /**
         * This field is used to initialize elements shown on Service type selection - Bertoli Marco
         */
        protected Object[] serviceType =
                new Object[] {SERVICE_LOAD_INDIPENDENT, SERVICE_LOAD_DIPENDENT, SERVICE_ZERO};

        JButton editButton = new JButton(){
            {
                setText("Edit");
            }
        };

        int[] columnSizes = new int[]{90, 60, 150, 30};

        public ServiceTable(){
            super();
            setModel(new ServiceTableModel());
            sizeColumns();
            setRowHeight(ROW_HEIGHT);
        }

        public TableCellRenderer getCellRenderer(int row, int column){
            if(column == columnSizes.length-1 && getValueAt(row, 1) != SERVICE_ZERO)
                return new ButtonCellEditor(editButton);
            else if (column == 1)
                return ComboBoxCellEditor.getRendererInstance();
            else if (column == 0)
                return classEditor.getRenderer();
            else return super.getCellRenderer(row, column);
        }

        public TableCellEditor getCellEditor(int row, int column){
            if (column == 1)
                return ComboBoxCellEditor.getEditorInstance(serviceType);
            else if(column == columnSizes.length-1){
                return new ButtonCellEditor(new JButton(editDistribution));
            }else return super.getCellEditor(row, column);
        }

        private void sizeColumns(){
            for (int i = 0; i < columnSizes.length && i < getColumnCount(); i++) {
                this.getColumnModel().getColumn(i).setPreferredWidth(columnSizes[i]);
            }
        }
    }

    protected class ServiceTableModel extends AbstractTableModel{

        private Class[] columnClasses = new Class[]{
            String.class, JComboBox.class, String.class, Object.class
        };
        private String[] columnNames = new String[]{
            "Class", "Strategy","Service Time Distribution", ""
        };

        public int getRowCount() {
            return classData.getClassKeys().size();
        }

        public int getColumnCount() {
            return 4;
        }

        public String getColumnName(int columnIndex) {
            return columnNames[columnIndex];
        }

        public Class getColumnClass(int columnIndex) {
            return columnClasses[columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return ((columnIndex == columnNames.length-1  && getValueAt(rowIndex, 1) != SERVICE_ZERO) || columnIndex == 1);
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            Object classKey = classData.getClassKeys().get(rowIndex);
            switch(columnIndex){
                case(0):
                    return classKey;
                case (1):
                    // Checks if current service section is load dependent or indipendent
                    if (data.getServiceTimeDistribution(stationKey, classKey) instanceof LDStrategy)
                        return SERVICE_LOAD_DIPENDENT;
                    else if (data.getServiceTimeDistribution(stationKey, classKey) instanceof ZeroStrategy)
                        return SERVICE_ZERO;
                    else
                        return SERVICE_LOAD_INDIPENDENT;
                case(2):
                    return data.getServiceTimeDistribution(stationKey, classKey);
            }
            return null;
        }

        /**Puts edited values to the underlying data structure for model implementation*/
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            Object classKey = classData.getClassKeys().get(rowIndex);
            switch (columnIndex) {
                // Load dependency
                case (1):
                    if (SERVICE_LOAD_DIPENDENT.equals(aValue)) {
                        // Puts a LDService strategy only if previously it was different
                        if (!(data.getServiceTimeDistribution(stationKey, classKey) instanceof LDStrategy))
                            data.setServiceTimeDistribution(stationKey, classKey, new LDStrategy());
                    }
                    else if (SERVICE_ZERO.equals(aValue))
                        data.setServiceTimeDistribution(stationKey, classKey, new ZeroStrategy());
                    else {
                        // Puts default service strategy, only if previously a load dependent was chosen
                        if (!(data.getServiceTimeDistribution(stationKey, classKey) instanceof Distribution))
                            data.setServiceTimeDistribution(stationKey, classKey, Defaults.getAsNewInstance("stationServiceStrategy"));
                    }
                    repaint();
            }
        }


    }


}

⌨️ 快捷键说明

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