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

📄 distributionseditor.java

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

import jmt.gui.common.distributions.Distribution;
import jmt.gui.common.distributions.Exponential;
import jmt.gui.common.layouts.SpringUtilities;
import jmt.gui.common.panels.ImagePanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;

/**
 * <p>Title: Distributions' Editor</p>
 * <p>Description: A modal dialog used to choose a specific distribution for a class or station service
 * and to enter its parameters. Users will enter owner Frame or Dialog and initial Distribution
 * (can be null) and will collect chosen distribution with <code>getResult()</code> method.</p>
 * 
 * @author Bertoli Marco
 *         Date: 29-giu-2005
 *         Time: 11.31.07
 */
public class DistributionsEditor extends JDialog {
    // Internal data structure
    protected Distribution initial, current, target;

    /**
     * This variable will be initialized only once.
     * It will contains every distribution that can be inserted
     */
    protected static HashMap distributions;

    // Constants
    protected static final int BORDERSIZE = 20;

    // Components
    protected JComboBox choser = new JComboBox();
    protected ImagePanel iconpanel = new ImagePanel();
    protected JPanel param_panel = new JPanel(new SpringLayout());
    protected JPanel mean_c_panel = new JPanel(new SpringLayout());

// --- Static methods ------------------------------------------------------------------------------
    /**
     * Returns a new instance of DistributionsEditor, given parent container (used to find
     * top level Dialog or Frame to create this dialog as modal)
     * @param parent any type of container contained in a Frame or Dialog
     * @param initial initial distribution to be set
     * @return new instance of DistributionsEditor
     */
    public static DistributionsEditor getInstance(Container parent, Distribution initial) {
        // Finds top level Dialog or Frame to invoke correct costructor
        while (!(parent instanceof Frame || parent instanceof Dialog))
            parent = parent.getParent();

        if (parent instanceof Frame)
            return new DistributionsEditor((Frame)parent, initial);
        else
            return new DistributionsEditor((Dialog)parent, initial);
    }

    /**
     * Uses reflection to return an HashMap of distributions. Search's key is distribution name and
     * value is the Class of found distribution
     * @return found distributions
     */
    protected static HashMap findDistributions() {
        Distribution[] all = Distribution.findAll();
        HashMap tmp = new HashMap();
        for (int i=0; i<all.length; i++)
            tmp.put(all[i].getName(), all[i].getClass());
        return tmp;
    }
// -------------------------------------------------------------------------------------------------

// --- Method to collect results -------------------------------------------------------------------
    /**
     * Returns Distribution selected in this dialog or initial one if cancel button was pressed
     * @return Selected distribution if okay button was pressed, initial otherwise. If this
     * dialog has not been shown yet, returns initial value too.
     */
    public Distribution getResult() {
        return target;
    }
// -------------------------------------------------------------------------------------------------

// --- Constructors to create modal dialog ---------------------------------------------------------
    /**
     * Builds a new Distribution Editor Dialog. This dialog is designed to be modal.
     * @param owner owner Dialog for this dialog.
     * @param initial Reference to initial distribution to be shown
     */
    public DistributionsEditor (Dialog owner, Distribution initial) {
        super(owner, true);
        initData(initial);
        initComponents();
    }

    /**
     * Builds a new Distribution Editor Dialog. This dialog is
     *  designed to be modal.
     * @param owner owner Frame for this dialog.
     * @param initial Reference to initial distribution to be shown
     */
    public DistributionsEditor (Frame owner, Distribution initial) {
        super(owner, true);
        initData(initial);
        initComponents();
    }
// -------------------------------------------------------------------------------------------------


// --- Actions performed by buttons and EventListeners ---------------------------------------------
    // When okay button is pressed
    protected AbstractAction okayAction = new AbstractAction("OK") {
        {
            putValue(Action.SHORT_DESCRIPTION, "Closes this window and apply changes");
        }
        public void actionPerformed(ActionEvent e) {
            // Checks if distribution parameters are correct
            if (current.checkValue()) {
                target = current;
                DistributionsEditor.this.dispose();
            }
            else
                JOptionPane.showMessageDialog(DistributionsEditor.this,
                        "Error in distribution parameters: " + current.getPrecondition(),
                        "Wrong parameters error",
                        JOptionPane.ERROR_MESSAGE);
        }
    };

    // When cancel button is pressed
    protected AbstractAction cancelAction = new AbstractAction("Cancel") {
        {
            putValue(Action.SHORT_DESCRIPTION, "Closes this window discarding all changes");
        }
        public void actionPerformed(ActionEvent e) {
            target = initial;
            DistributionsEditor.this.dispose();
        }
    };

    /**
     * Listener used to set parameters (associated to param_panel's JTextFields).
     * Parameters are set when JTextField loses focus or ENTER key is pressed.
     */
    protected class inputListener implements KeyListener, FocusListener {
        /**
         * Update values into current Distribution, given source JTextField
         * @param source source JTextField for catched event.
         */
        protected void updateValues(Object source) {
            // Finds parameter number
            JTextField sourcefield = (JTextField) source;
            int num = Integer.parseInt(sourcefield.getName());
            current.getParameter(num).setValue(sourcefield.getText());
            current.updateCM();
            refreshValues();
        }

        public void focusLost(FocusEvent e) {
            updateValues(e.getSource());
        }

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

        public void focusGained(FocusEvent e) {
        }

        public void keyReleased(KeyEvent e) {
        }

        public void keyTyped(KeyEvent e) {
        }
    }

    protected inputListener parameterListener = new inputListener();

    /**
     * Listener that listens on Mean and C variations and updates parameters
     */
    protected inputListener cmListener = new inputListener(){
        /**
         * Update values into current Distribution, given source JTextField
         * @param source source JTextField for catched event.
         */
        protected void updateValues(Object source) {
            // Finds parameter number
            JTextField sourcefield = (JTextField) source;
            try {
                if (sourcefield.getName().equals("mean")) {
                    current.setMean(Double.parseDouble(sourcefield.getText()));
                }else if (sourcefield.getName().equals("c")) {
                    current.setC(Double.parseDouble(sourcefield.getText()));
                }
            } catch (NumberFormatException e) { // Do nothing
            }
            refreshValues();
        }
    };

    /**
     * Listener for choser ComboBox to instantiate a new distributions data object when

⌨️ 快捷键说明

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