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

📄 classespanel.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.exact.panels;

import jmt.gui.common.resources.ImageLoader;
import jmt.gui.exact.ExactConstants;
import jmt.gui.exact.ExactModel;
import jmt.gui.exact.ExactWizard;
import jmt.gui.exact.table.*;
import jmt.gui.exact.utils.ArrayUtils;
import jmt.gui.help.HoverHelp;
import jmt.gui.wizard.WizardPanel;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**

 * @author alyf (Andrea Conti)
 * Date: 11-set-2003
 * Time: 23.48.19

 * Modifyed by Bertoli Marco

 */

/**
 * 1st panel: classes number, names, types and data
 */
public final class ClassesPanel extends WizardPanel implements ExactConstants, ForceUpdatablePanel{

	private HoverHelp help;
	private static final String helpText = "<html>In this panel you can define the number of stations in the system and their properties.<br><br>" +
	        " To edit values, single-click on the desired cell" +
	        " and start typing.<br> To select classes click or drag on the row headers.<br> <b>For a list of the available operations right-click" +
	        " on the table</b>.<br>" +
	        " Pressing DELETE removes all selected classes from the system.</html>";

	private ExactWizard ew;
	private boolean isLd;
	private int classes;
	private String[] classNames;
	private int[] classTypes;
	private double[] classData;
	private int nameCounter = 1;

	private List classOps;
	private boolean hasDeletes;
	private boolean deleting = false;

    private JSpinner classSpinner = new JSpinner(new SpinnerNumberModel(1, 1, MAX_CLASSES, 1));

	private ClassTable classTable;

	private ChangeListener spinnerListener = new ChangeListener() {
		public void stateChanged(ChangeEvent ce) {
			if (!deleting) updateSizes();
		}
	};

    private AbstractAction deleteClass = new AbstractAction("Delete selected classes") {
        {
            putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false));
            putValue(Action.SHORT_DESCRIPTION, "Deletes selected classes from the system");
        }

        public void actionPerformed(ActionEvent e) {
            deleteSelectedClasses();
        }
    };

    private AbstractAction deleteOneClass = new AbstractAction("") {
        {
            putValue(Action.SHORT_DESCRIPTION, "Delete This Class");
            putValue(Action.SMALL_ICON, ImageLoader.loadImage("Close"));
        }

        public void actionPerformed(ActionEvent e) {
        }
    };


    private AbstractAction addClass = new AbstractAction("New Class") {
        {
            putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C,KeyEvent.ALT_MASK));
            putValue(Action.SHORT_DESCRIPTION, "Adds a new Class to Model");
        }

        public void actionPerformed(ActionEvent e) {
            addClass();
        }
    };


	public ClassesPanel(ExactWizard ew) {
		this.ew = ew;
		help = ew.getHelp();
		classOps = new ArrayList();

		sync();

		initComponents();
		makeNames();

	}

	private void sync() {

		hasDeletes = false;
		classOps.clear();

		/* sync status with data object */
		/* arrays are copied to ensure data object consistency is preserved */
		ExactModel data = ew.getData();
		synchronized (data) {
			classes = data.getClasses();
			isLd = data.isLd();
			classNames = ArrayUtils.copy(data.getClassNames());
			classTypes = ArrayUtils.copy(data.getClassTypes());
			classData = ArrayUtils.copy(data.getClassData());

            //TODO
            //NEW
            //@author Stefano Omini
            if (isLd) {
                //if load dependent is no more supported after class changes
                //remove all load dependent stations
                if (!(data.isClosed() && !data.isMultiClass())) {
                    JOptionPane.showMessageDialog(this, "<html><center>jMVA allows Load Dependent stations only for single class closed model. <br> Load Dependent stations will be replaced with Load Independent stations.</center></html>", "Warning", JOptionPane.WARNING_MESSAGE);
                    data.removeLD();
                    isLd = false;
                }
            }
            //end NEW
		}
		classSpinner.setValue(new Integer(classes));
	}

	/**
	 * make up names for null entries
	 */
	private void makeNames() {
		for (int i = 0; i < classNames.length; i++) {
			if (classNames[i] == null) {
				classNames[i] = "Class" + (++nameCounter);
			}
		}
	}

	/**
	 * resize internal data structures according to new values. intended to be called from a listener.
	 */
	private void updateSizes() {
        setNumberOfClasses(((Integer) classSpinner.getValue()).intValue());
	}

    private void addClass(){
        setNumberOfClasses(classes+1);
    }

    private void setNumberOfClasses(int number){
        classTable.stopEditing();
        classes = number;

        classNames = ArrayUtils.resize(classNames, classes, null);
        makeNames();

        classTypes = ArrayUtils.resize(classTypes, classes, CLASS_CLOSED);
        classData = ArrayUtils.resize(classData, classes, 0.0);

        classTable.updateStructure();
        if (!deleting) classOps.add(ListOp.createResizeOp(classes));

        classSpinner.setValue(new Integer(classes));
        classTable.updateDeleteCommand();
    }


	/**
	 * Set up the panel contents and layout
	 */
	private void initComponents() {
		classSpinner.addChangeListener(spinnerListener);
		help.addHelp(classSpinner, "Enter the number of classes for this system");

		classTable = new ClassTable();

		/* and now some Box black magic */
		//DEK (Federico Granata) 26-09-2003
		Box classSpinnerBox = Box.createHorizontalBox();
		//OLD
        //JLabel spinnerLabel = new JLabel("<html><font size=\"4\">Set the Number of classes (1-" + MAX_CLASSES + "):</font></html>");
        //NEW
        //@author Stefano
        JLabel spinnerLabel = new JLabel(DESCRIPTION_CLASSES);

		classSpinnerBox.add(spinnerLabel);
        //END
		//BEGIN Federico Dall'Orso 9/3/2005
        //OLD
        /*
        classSpinnerBox.add(Box.createGlue());
        */
        //NEW
        classSpinnerBox.add(Box.createHorizontalStrut(10));
        Box numberBox = Box.createVerticalBox();

        JPanel spinnerPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JLabel numberLabel = new JLabel("Number:");
        classSpinner.setMaximumSize(new Dimension(600, 18));
        spinnerPanel.add(numberLabel);
        spinnerPanel.add(classSpinner);
        numberBox.add(spinnerPanel);

        numberBox.add(new JButton(addClass));

        numberBox.setMaximumSize(new Dimension(150,50));

        classSpinnerBox.add(numberBox);
        //END  Federico Dall'Orso 9/3/2005

		Box classBox = Box.createVerticalBox();
		classBox.add(Box.createVerticalStrut(20));
		classBox.add(classSpinnerBox);
		classBox.add(Box.createVerticalStrut(10));
		JScrollPane classTablePane = new JScrollPane(classTable);
		classTablePane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		classTablePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		classBox.add(classTablePane);
		classBox.add(Box.createVerticalStrut(20));

		Box totalBox = Box.createHorizontalBox();
		totalBox.add(Box.createHorizontalStrut(20));
		totalBox.add(classBox);
		totalBox.add(Box.createHorizontalStrut(20));

		setLayout(new BorderLayout());
		add(totalBox, BorderLayout.CENTER);

	}



	/**
	 * @return the total number of customers in the system
	 */
	private int sumPop() {
        int pop = 0;
		for (int i = 0; i < classes; i++) {
			if (classTypes[i] == CLASS_CLOSED) pop += classData[i];
		}
		return pop;
	}

    /**
     * Checks if a class was not initialized
     * @return false if a class was not inizialized, true otherwise
     */
    private boolean checkData() {
        for (int i=0; i<classes; i++)
            if (classData[i] == 0.0)
                return false;
        return true;
    }

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

	public void lostFocus() {
		commit();
		//release();
	}

	public void gotFocus() {
		sync();
		classTable.update();
    }

	public boolean canFinish() {
		return checkPop()&& !areThereDuplicates();
	}

	public boolean canGoBack() {
		checkPop();
        if(areThereDuplicates()) return false;
        return true; //so that the user can correct errors
	}

	public boolean canGoForward() {
		checkPop();
        if(areThereDuplicates()) return false;

        if (!checkData()) {
            JOptionPane.showMessageDialog(this, "<html><center>Please provide correct, not null, values for<br>'No. of Customers' and 'Arrival Rate' for each class.</center></html>", "Warning", JOptionPane.WARNING_MESSAGE);
            return false;
        }

        return true; // so that the user can correct errors
	}

    //checks wether population is greater than 0
	private boolean checkPop() {
		classTable.stopEditing();
		if (isLd && sumPop() < 1) {
			JOptionPane.showMessageDialog(this, "<html><center>A system with load dependent stations cannot have zero customers.<br>Increase the number of customers or remove all load dependent stations.</center></html>", "Warning", JOptionPane.WARNING_MESSAGE);
			return false;
		}
		return true;
	}

    //checks for presence of classes with same name
    private boolean areThereDuplicates(){
        boolean thereAreDupl = false;
        for(int i=0; i<classNames.length; i++){
            for(int j=i+1; j<classNames.length; j++){
                thereAreDupl = thereAreDupl || classNames[i].equalsIgnoreCase(classNames[j]);
            }
        }
        if(thereAreDupl){
            JOptionPane.showMessageDialog(this, "<html><center>Two or more classes in this system are identified by the same name.<br>Please modify names.</center></html>", "Warning", JOptionPane.WARNING_MESSAGE);
            return true;
        }else return false;
    }

	private void commit() {
		/* stop any editing in progress */
		if (classSpinner.getEditor().getComponent(0).hasFocus()) {
            //disgusting. there must be a better way...
			try {
				classSpinner.commitEdit();
				updateSizes();
			} catch (java.text.ParseException e) {
			}
		}

⌨️ 快捷键说明

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