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

📄 componentbar.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.jmodel.mainGui;

import jmt.gui.common.resources.ImageLoader;
import jmt.gui.jmodel.controller.Mediator;
import jmt.gui.jmodel.controller.actions.SetInsertState;

import javax.swing.*;
import java.lang.reflect.Field;
import java.util.Enumeration;

/**
 * <p>Title: Component Toolbar</p>
 * <p>Description: A toolbar used to insert new stations in the graph, or to go
 * into "select mode" or "link mode". This class uses reflection on
 * <code>JMODELConstants</code>, in such way a future placement of new station
 * types will be easy.</p>
 * 
 * @author Bertoli Marco
 *         Date: 4-giu-2005
 *         Time: 14.13.55
 */
public class ComponentBar extends JToolBar {
    protected Mediator mediator;
    protected ButtonGroup buttongroup = new ButtonGroup();
    protected JToggleButton select, link, invisible;

    public ComponentBar (Mediator m) {
        super();
        mediator = m;
        setRollover(true);
        // Adds Select button
        select = SelectButton();
        add(new CustomSeparator());
        // Adds insert mode buttons.
        String[] stations = getStationList();
        for (int i=0; i<stations.length; i++)
            InsertButton(stations[i]);
        add(new CustomSeparator());
        // Adds link button
        link = LinkButton();
        //Adds an invisible button (as Java documentation says is the only way
        //to uncheck every button in a ButtonGroup)
        invisible = new JToggleButton();
        invisible.setVisible(false);
        buttongroup.add(invisible);

        BlockingRegionButton();

        // Disables all components button
        Enable(false);
    }

    /**
     * Enable/disable all buttons of this componenet
     * @param value iff true all components are enabled, iff false all components are disabled
     */
    public void Enable(boolean value) {
        Enumeration buttons = buttongroup.getElements();
        while(buttons.hasMoreElements())
          ((JToggleButton) buttons.nextElement()).setEnabled(value);
    }

    /**
     * Selects 'select' button, putting graph in selection mode
     */
    public void setSelect() {
        select.setEnabled(true);
        select.doClick();
    }

    /**
     * Enables or disables select button
     * @param value true to enable, false to disable
     */
    public void enableSelect(boolean value) {
        select.setEnabled(value);
    }

    /**
     * Enables or disables link button
     * @param value true to enable, false to disable
     */
    public void enableLink(boolean value) {
        link.setEnabled(value);
    }

    /**
     * Unsets all buttons (used before creating a new file)
     */
    public void unsetAll() {
        invisible.doClick();
    }

    /**
     * Finds all possible station types using reflection on <code>JMODELConstants</code>
     * In such way a future placement of new station types will be easy.
     * @return All station types element found
     */
    private String[] getStationList() {
        String path = "jmt.gui.jmodel.JGraphMod.";
        Field[] fields = jmt.gui.jmodel.JMODELConstants.class.getFields();
        String[] stationNames = new String[fields.length];
        int index = 0;
        try {
        for (int i=0; i<fields.length; i++)
            if (fields[i].getName().startsWith("STATION_TYPE_")) {
                // Checks for existance of graphic component
                String name = (String) fields[i].get(null)  + "Cell";
                try {
                    boolean enabled = Class.forName(path + name).getDeclaredField("canBePlaced").getBoolean(null);
                    if (enabled)
                        stationNames[index++] = name;
                } catch (ClassNotFoundException e) {
                    // Never Thrown
                    e.printStackTrace();
                } catch (NoSuchFieldException ex) {
                    // Never Thrown
                    ex.printStackTrace();
                }
            }
        } catch (IllegalAccessException ex) {
            System.out.println("A security manager has blocked reflection");
            ex.printStackTrace();
        }
        // Build *Cell array to be given as output
        String[] tmp = new String[index];
        for (int i=0; i<index;i++)
            tmp[i] = stationNames[i];
        return tmp;
    }

    /**
     * Puts graph in insert mode for given station. This method will assume the existance
     * of the following resources (For a class named 'className'):
     * - ClassName.gif   : Main image for the button
     * - ClassNameRO.gif : Button rollover image
     * - ClassNameP.gif  : Button pressed image
     * - ClassNameS.gif  : Button selected image
     * @param className class to be inserted by button
     * @return created button
     */
    private JToggleButton InsertButton(String className) {
        JToggleButton button;
        button = new JToggleButton();
        button.setAction(new SetInsertState(mediator, className));
        button.setText("");
        button.setFocusPainted(false);
        button.setIcon(ImageLoader.loadImage(className));
        button.setContentAreaFilled(false);
        button.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        button.setRolloverIcon(ImageLoader.loadImage(className+"RO"));
        button.setPressedIcon(ImageLoader.loadImage(className+"P"));
        button.setSelectedIcon(ImageLoader.loadImage(className+"S"));
        buttongroup.add(button);
        add(button);
        return button;
    }

    /**
     * Inserts a new button for "Select Mode"
     * @return created button
     */
    private JToggleButton SelectButton() {
        JToggleButton button;
        button = new JToggleButton(mediator.getSetSelect());
        button.setText("");
        button.setIcon(ImageLoader.loadImage("Select"));
        button.setFocusPainted(false);
        button.setContentAreaFilled(false);
        button.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        button.setRolloverIcon(ImageLoader.loadImage("SelectRO"));
        button.setPressedIcon(ImageLoader.loadImage("SelectP"));
        button.setSelectedIcon(ImageLoader.loadImage("SelectS"));
        buttongroup.add(button);
        add(button);
        return button;
    }

    /**
     * Insert a new button for "Link Mode"
     * @return created button
     */
    private JToggleButton LinkButton() {
        JToggleButton button;
        button = new JToggleButton(mediator.getSetConnect());
        button.setText("");
        button.setIcon(ImageLoader.loadImage("Link"));
        button.setFocusPainted(false);
        button.setContentAreaFilled(false);
        button.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        button.setRolloverIcon(ImageLoader.loadImage("LinkRO"));
        button.setPressedIcon(ImageLoader.loadImage("LinkP"));
        button.setSelectedIcon(ImageLoader.loadImage("LinkS"));
        buttongroup.add(button);
        add(button);
        return button;
    }

    /**
     * Adds a button used to group selected stations into a blocking region
     * @return created button
     */
    private JButton BlockingRegionButton() {
        // Button for blocking region insertion
        JButton blocking = new JButton(mediator.getAddBlockingRegion());
        blocking.setText("");
        blocking.setFocusPainted(false);
        blocking.setIcon(ImageLoader.loadImage("Blocking"));
        blocking.setContentAreaFilled(false);
        blocking.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        blocking.setRolloverIcon(ImageLoader.loadImage("BlockingRO"));
        blocking.setPressedIcon(ImageLoader.loadImage("BlockingP"));
        add(blocking);
        return blocking;
    }

}

⌨️ 快捷键说明

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