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

📄 whatifpanel.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        }
        catch (NumberFormatException ex) {
            // Do nothing
        }
        try {
            double to = Double.parseDouble(this.to.getText());
            if (to < 0)
                to = 0.0;
            int iterations = 2;
            if (this.iterations.getText() != null && !this.iterations.getText().equals(""))
                iterations = Integer.parseInt(this.iterations.getText());
            if (iterations < 2)
                iterations = 2;
            int classNum = ((Integer)classNames.get(className.getSelectedItem())).intValue();
            int stationNum = -1;
            // If from and to are expressed as percentage, divides them by 100
            if (classNum < 0) {
                from /= 100;
                to /= 100;
            }
            // Correct values for population mix what-if analysis
            if (type.equals(ExactModel.WHAT_IF_MIX)) {
                if (from > 1)
                    from = 0;
                if (to > 1)
                    to = 1;
            }

            if (stationName.getSelectedItem() != null)
                stationNum = ((Integer)stationNames.get(stationName.getSelectedItem())).intValue();
            values = data.generateWhatIfValues(type, from, to, iterations, classNum, stationNum);
        } catch (NumberFormatException e) {
            // Number format is wrong, skips this update.
        } catch (ArithmeticException ex) {
            JOptionPane.showMessageDialog(this, "Closed class with 0 customers found. They will be set to 1.", "Error", JOptionPane.ERROR_MESSAGE);
            double[]classData = (double[])data.getClassData().clone();
            for (int i=0; i<classData.length; i++)
                if (data.getClassTypes()[i] == ExactModel.CLASS_CLOSED && classData[i] < 1)
                    classData[i] = 1.0;
            data.setClassData(classData);
            updateFields();
        }
        setFromToIterations();
    }

    /**
     * Listener used to modify from, to and iterations fields
     * when JTextField loses focus or ENTER key is pressed.
     */
    protected class inputListener implements KeyListener, FocusListener {
        public void focusLost(FocusEvent e) {
            updateFields();
        }

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                updateFields();
                e.consume();
            }
         }

        public void focusGained(FocusEvent e) {}

        public void keyReleased(KeyEvent e) {}

        public void keyTyped(KeyEvent e) {}
    }

    /**
     * called by the Wizard when the user presses the help button
     */
    public void help() {
        JOptionPane.showMessageDialog(this, helpText, "Help", JOptionPane.INFORMATION_MESSAGE);
    }

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

    /**
     * called by the Wizard before when switching to another panel
     */
    public void lostFocus() {
        commitData();
    }


    /**
     * This method force a stream of data from application to GUI panel. This grants application
     * user to be working on perfectly updated data as far as <code>retrieveData()</code> was called.
     */
    public void retrieveData() {
        sync();
    }

    /**
     * This method force a stream of data from GUI panel to application. This must grant other GUI
     * panels to be working on the most recently updated version of the data this GUI panel was working
     * on, so that when other implementing classes call retrieveData() they will have coherent data with
     * this class.
     */
    public void commitData() {
        commit();
    }

    /**
     * This table is used to display informations on customer class (like population,
     * arrival rates and betas)
     */
    protected class ClassTable extends JTable {
        /**
         * Creates a new ClassTable with a ClassTableModel
         */
        public ClassTable() {
            super(new ClassTableModel());
        }

        /**
         * This method must be called each time analysis type or selected class is changed
         */
        public void update() {
            tableChanged(new TableModelEvent(classTable.getModel(), TableModelEvent.ALL_COLUMNS));
            this.getColumnModel().setColumnSelectionAllowed(false);
            this.getColumnModel().getColumn(0).setMaxWidth(40);
        }

        /**
         * Prepares the renderer by querying the data model for the
         * value and selection state
         * of the cell at <code>row</code>, <code>column</code>.
         * Returns the component (may be a <code>Component</code>
         * or a <code>JComponent</code>) under the event location.
         * <p/>
         *
         * This paints in red elements of selected classes
         *
         * @param renderer the <code>TableCellRenderer</code> to prepare
         * @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 <code>Component</code> under the event location
         */
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            Component comp = super.prepareRenderer(renderer, row, column);
            if (comp instanceof JLabel)
                ((JLabel)comp).setHorizontalAlignment(JLabel.CENTER);
            String name = (String) className.getSelectedItem();
            // Sets color of selected elements
            if ((name.equals(ALL_CLASSES) || getColumnName(column).equals(name)) && column != 0)
                comp.setForeground(Color.red);
            else
                comp.setForeground(getForeground());
            // Changes background and font of headers
            if (column == 0 || row == 0) {
                comp.setBackground(this.getTableHeader().getBackground());
                comp.setFont(comp.getFont().deriveFont(Font.BOLD));
            }
            else {
                comp.setBackground(Color.white);
                comp.setFont(comp.getFont().deriveFont(Font.PLAIN));
            }
            return comp;
        }

    }

    /**
     * Table model for ClasTable
     */
    protected class ClassTableModel 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() {
            String parameter = (String)type.getSelectedItem();
            if (parameter.equals(NO_ANALYSIS))
                return 1;
            else if (parameter.equals(WHAT_IF_CUSTOMERS) || parameter.equals(WHAT_IF_MIX))
                return closedClasses.size()+1;
            else if (parameter.equals(WHAT_IF_ARRIVAL))
                return openClasses.size()+1;
            else
                return data.getClasses()+1;
        }

        /**
         * 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() {
            String parameter = (String)type.getSelectedItem();
            if (parameter.equals(WHAT_IF_CUSTOMERS) || parameter.equals(WHAT_IF_MIX))
                return 3;
            else
                return 2;
        }

        /**
         * @return the object at (rowIndex, columnIndex)
         */
        public Object getValueAt(int rowIndex, int columnIndex) {
            String parameter = (String)type.getSelectedItem();
            int index;
            // First column is header
            if (columnIndex == 0) {
                switch(rowIndex) {
                    case 0:
                        return "";
                    case 1:
                        if (parameter.equals(WHAT_IF_CUSTOMERS) || parameter.equals(WHAT_IF_MIX))
                            return "N.job";
                         else if (parameter.equals(WHAT_IF_ARRIVAL))
                            return "\u03bbi";
                        else
                            return "Di";
                    case 2:
                        return "\u03b2i";

                }
            }

            switch (rowIndex) {
                case 0: // first row is header
                    return getColumnName(columnIndex);
                case 1: // customers or arrival rates
                    if (parameter.equals(WHAT_IF_CUSTOMERS) || parameter.equals(WHAT_IF_MIX))
                        index = ((Integer)closedClassNames.get(closedClasses.get(columnIndex-1))).intValue();
                     else if (parameter.equals(WHAT_IF_ARRIVAL))
                        index = ((Integer)openClassNames.get(openClasses.get(columnIndex-1))).intValue();
                    else
                        index = columnIndex-1;
                    // Retrives class data
                    double num;
                    if (parameter.equals(WHAT_IF_DEMANDS)) {
                        int station = ((Integer)stationNames.get(stationName.getSelectedItem())).intValue();
                        num = data.getServiceTimes()[station][index][0]*data.getVisits()[station][index];
                    }
                    else
                        num = data.getClassData()[index];

                    if (parameter.equals(WHAT_IF_CUSTOMERS) || parameter.equals(WHAT_IF_MIX))
                        return intFormatter.format(num);
                    else
                        return doubleFormatter.format(num);
                case 2: // betas
                    // This is available only for closed classes
                    index = ((Integer)closedClassNames.get(closedClasses.get(columnIndex-1))).intValue();
                    return doubleFormatter.format(data.getClassData()[index]/data.getMaxpop());
            }
            return null;
        }

		public String getColumnName(int index) {
            // First column is row header
            String parameter = (String)type.getSelectedItem();
            if (parameter.equals(NO_ANALYSIS) || index == 0)
                return "";
            if (parameter.equals(WHAT_IF_CUSTOMERS) || parameter.equals(WHAT_IF_MIX))
                return (String)closedClasses.get(index-1);
             else if (parameter.equals(WHAT_IF_ARRIVAL))
                return (String)openClasses.get(index-1);
            else
                return data.getClassNames()[index-1];
        }
    }
}

⌨️ 快捷键说明

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