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

📄 whatifpanel.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**
 * 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.exact.panels;

import jmt.gui.wizard.WizardPanel;
import jmt.gui.exact.ExactConstants;
import jmt.gui.exact.ExactWizard;
import jmt.gui.exact.ExactModel;
import jmt.gui.help.HoverHelp;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.TreeMap;
import java.util.Iterator;
import java.text.DecimalFormat;

/**
 * <p>Title: What-If Analysis Panel</p>
 * <p>Description: This panel is used to show and parametrize what if analysis
 * for jMVA. This is really complex as I have to cope with horrible JMVA data structure
 * and many controls must be performed to correctly parametrize analysis.</p>
 *
 * @author Bertoli Marco
 *         Date: 26-mag-2006
 *         Time: 13.48.45
 */
public class WhatIfPanel extends WizardPanel implements ExactConstants, ForceUpdatablePanel{
    private ExactWizard wizard;
    private HoverHelp help;
    private JLabel description, stationLabel, classLabel, fromLabel, toLabel, iterationLabel;

    public static final String helpText = "<html>In this panel you can select a <em>control parameter</em> " +
            "that will be used to performa a what-if analysis.<br>Model solution is iterated by changing selected " +
            "parameter and graphical results will be shown.</html>";

    private static final String NO_ANALYSIS = "-- Disabled --";
    private static final String ALL_CLASSES = "-- All classes proportionally --";

    private static final String FROM_ALL = "From (%) : ";
    private static final String FROM_ARRIVAL = "From (job/s) : ";
    private static final String FROM_CUSTOMERS = "From (Ni) : ";
    private static final String FROM_DEMANDS = "From (s) : ";
    private static final String FROM_MIX = "From (\u03b2i) : ";
    private static final String TO_ALL = "To (%) : ";
    private static final String TO_ARRIVAL = "To (job/s) : ";
    private static final String TO_CUSTOMERS = "To (Ni) : ";
    private static final String TO_DEMANDS = "To (s) : ";
    private static final String TO_MIX = "To (\u03b2i) : ";

    private JComboBox type, stationName, className;
    private JTextField from, to, iterations;
    private ClassTable classTable;
    private JPanel tablePanel;

    private DecimalFormat intFormatter = new DecimalFormat("#"); // This is used to display population
    private DecimalFormat doubleFormatter = new DecimalFormat("#0.0###"); // This is used to display doubles

    // Data structures
    private ExactModel data;
    private TreeMap classNames; // A map className --> index in source array
    private TreeMap closedClassNames, openClassNames;
    private TreeMap stationNames; // A map stationName --> index in source array without LD stations
    private double[] values;
    private String currentType, currentClass;
    private Vector openClasses, closedClasses; // All open classes and closed classes names
    private Vector modes = new Vector(); // Available what-if types
    // Current value ('from' field)
    private double current;

    /**
     * @return the panel's name
     */
    public String getName() {
        return "What-if";
    }

    /**
     * Creates a new What-If Panel, given parent ExactWizard
     * @param ex
     */
    public WhatIfPanel(ExactWizard ex) {
        this.wizard = ex;
        help = ex.getHelp();
        initComponents();
        sync();
    }

    /**
     * retrive current data structure and updates shown components
     */
    private void sync() {
        /* retrive current data structure */
        data = wizard.getData();

        // Adds all classes to classNames and closedClassNames/openClassNames arrays
        closedClassNames = new TreeMap();
        closedClasses = new Vector();
        closedClassNames.put(ALL_CLASSES, new Integer(-1));
        openClassNames = new TreeMap();
        openClassNames.put(ALL_CLASSES, new Integer(-1));
        openClasses = new Vector();
        classNames = new TreeMap();
        classNames.put(ALL_CLASSES, new Integer(-1));
        for (int i=0; i<data.getClasses(); i++) {
            String name = data.getClassNames()[i];
            Integer index = new Integer(i);
            classNames.put(name, index);
            if (data.getClassTypes()[i] == ExactModel.CLASS_OPEN) {
                openClassNames.put(name, index);
                openClasses.add(name);
            }
            else {
                closedClassNames.put(name, index);
                closedClasses.add(name);
            }
        }

        // Removes LD stations
        stationNames = new TreeMap();
        for (int i=0; i<data.getStations(); i++)
            if (data.getStationTypes()[i] != ExactModel.STATION_LD)
                stationNames.put(data.getStationNames()[i], new Integer(i));

        // Finds available analysis types
        modes.clear();
        type.removeAllItems();
        modes.add(NO_ANALYSIS);
        type.addItem(NO_ANALYSIS);
        // If model has some closed classes
        if (data.isClosed() || data.isMixed()) {
            modes.add(WHAT_IF_CUSTOMERS);
            type.addItem(WHAT_IF_CUSTOMERS);
        }
        else
            type.addItem(GRAY_S+WHAT_IF_CUSTOMERS+" (only closed classes)"+GRAY_E);

        // If model has some open classes
        if (data.isOpen() || data.isMixed()) {
            modes.add(WHAT_IF_ARRIVAL);
            type.addItem(WHAT_IF_ARRIVAL);
        }
        else
            type.addItem(GRAY_S+WHAT_IF_ARRIVAL+" (only open classes)"+GRAY_E);

        // If model has two closed classes, allow population mix
        if (closedClasses.size() == 2) {
            modes.add(WHAT_IF_MIX);
            type.addItem(WHAT_IF_MIX);
        }
        else
            type.addItem(GRAY_S+WHAT_IF_MIX+" (only 2 closed classes)"+GRAY_E);

        // If model has at least one not LD station, allows service demands
        if (stationNames.size() > 0) {
            modes.add(WHAT_IF_DEMANDS);
            type.addItem(WHAT_IF_DEMANDS);
        }
        else
            type.addItem(GRAY_S+WHAT_IF_DEMANDS+" (only LI or Delay)"+GRAY_E);

        // Selects correct type
        if (data.getWhatIfType() == null || !modes.contains(data.getWhatIfType()))
            type.setSelectedItem(NO_ANALYSIS);
        else
            type.setSelectedItem(data.getWhatIfType());

        // Selects correct class
        if (data.getWhatIfClass() < 0 || data.getWhatIfClass() >= data.getClasses())
            className.setSelectedItem(ALL_CLASSES);
        else
            className.setSelectedItem(data.getClassNames()[data.getWhatIfClass()]);

        // Selects correct station
        if (data.getWhatIfStation() >= 0 && data.getWhatIfStation() < data.getStations())
            stationName.setSelectedItem(data.getStationNames()[data.getWhatIfStation()]);
        else if (stationName.getItemCount() > 0)
            stationName.setSelectedIndex(0);
        if (data.getWhatIfValues() != null)
            values = data.getWhatIfValues();
        setFromToIterations();
    }

    /**
     * Sets values for from, to and iteration fields
     */
    private void setFromToIterations() {
        // Gets type
        String type = (String) this.type.getSelectedItem();

        // Sets from and to values
        if (values != null && !type.equals(NO_ANALYSIS)) {
            // Percentage values
            if (((Integer)classNames.get(className.getSelectedItem())).intValue() < 0) {
                from.setText(doubleFormatter.format(values[0]*100));
                to.setText(doubleFormatter.format(values[values.length-1]*100));
            }
            // Normal values
            else {
                if (!type.equals(ExactModel.WHAT_IF_CUSTOMERS)) {
                    from.setText(doubleFormatter.format(values[0]));
                    to.setText(doubleFormatter.format(values[values.length-1]));
                }
                else { // Integer values
                    from.setText(intFormatter.format(values[0]));
                    to.setText(intFormatter.format(values[values.length-1]));
                }
            }
            iterations.setText(""+values.length);
        }
    }

    /**
     * Commits changes to data structure
     */
    private void commit() {
        if (type.getSelectedItem().equals(NO_ANALYSIS)) {
            data.setWhatIfType(null);
            data.setWhatIfClass(-1);
            data.setWhatIfStation(-1);
            data.setWhatIfValues(null);
        }
        else {
            data.setWhatIfType((String) type.getSelectedItem());
            data.setWhatIfValues(values);
            // Sets selected class
            data.setWhatIfClass(((Integer)classNames.get(className.getSelectedItem())).intValue());

            // Sets selected station if Service Demands is selected
            if (!data.getWhatIfType().equals(ExactModel.WHAT_IF_DEMANDS))
                data.setWhatIfStation(-1);
            else {
                data.setWhatIfStation(((Integer)stationNames.get(stationName.getSelectedItem())).intValue());
            }
        }
    }

    /**
     * Initialize gui of this panel
     */
    private void initComponents() {
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        setLayout(new BorderLayout(5,5));

        // Listener for From, To and Iteration fields
        inputListener listener = new inputListener();

        // Description label
        description = new JLabel(DESCRIPTION_WHATIF_NONE);
        description.setPreferredSize(new Dimension(220,200));
        description.setVerticalAlignment(JLabel.NORTH);
        add(description, BorderLayout.WEST);

        // Builds panel with parameters
        JPanel paramPanel = new JPanel(new GridLayout(6,2,5,5));
        // Control parameter
        paramPanel.add(new JLabel("Control Parameter :", JLabel.RIGHT));
        type = new JComboBox(new String[]{NO_ANALYSIS});
        type.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {

⌨️ 快捷键说明

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