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

📄 forksectionpanel.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.common.panels;

import jmt.gui.common.CommonConstants;
import jmt.gui.common.Defaults;
import jmt.gui.common.definitions.ClassDefinition;
import jmt.gui.common.definitions.StationDefinition;
import jmt.gui.wizard.WizardPanel;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * <p>Title: ForkSection Panel</p>
 * <p>Description: This panelis used to parametrize fork special behaviour</p>
 *
 * @author Bertoli Marco
 *         Date: 15-mar-2006
 *         Time: 14.25.36
 */
public class ForkSectionPanel extends WizardPanel implements CommonConstants {
    protected StationDefinition sd;
    protected ClassDefinition cd;
    protected Object stationKey;
    protected JSpinner numForkSpinner, blockSpinner;

    /**
     * Sets data for this panel
     * @param sd station definition data structure
     * @param cd class definition data structure
     * @param stationKey search's key for input station
     */
    public void setData(StationDefinition sd, ClassDefinition cd, Object stationKey){
        this.removeAll();
        this.stationKey = stationKey;
        this.sd = sd;
        this.cd = cd;
        updateSpinners();
        initComponents();
    }

    /**
     * Builds a new panel to specify fork section parameters
     * @param sd station definition data structure
     * @param cd class definition data structure
     * @param stationKey search's key for input station
     */
    public ForkSectionPanel(StationDefinition sd, ClassDefinition cd, Object stationKey) {
        super();
        setData(sd, cd, stationKey);
    }

    /**
     * Initialize layout
     */
    private void initComponents() {
        //building mainPanel
        this.setLayout(new GridLayout(2,1));
        this.setBorder(new EmptyBorder(5,5,5,5));

        //layout of fragNum panel
        JPanel fragnum = new JPanel();
        fragnum.setBorder(new TitledBorder(new EtchedBorder(), "Number of tasks"));
        this.add(fragnum);
        JLabel text = new JLabel("Number of tasks to be generated on each output link for each input job:");
        text.setLabelFor(numForkSpinner);
        fragnum.add(text);
        fragnum.add(numForkSpinner);

        //layout of block panel
        JPanel block = new JPanel(new BorderLayout(10,10));
        block.setBorder(new TitledBorder(new EtchedBorder(), "Blocking property"));
        this.add(block);

        // Adds a checkbox to block panel to select block function
        final JCheckBox check = new JCheckBox();
        check.setText("Enables blocking: sets the maximum number of jobs inside a fork-join section");
        check.setToolTipText("Sets the maximum allowed number of jobs inside a fork-join section. Following jobs will be queued.");

        // Adds a spinner to block panel to select block number
        JLabel label = new JLabel("Fork-join section capacity (max number of jobs - NOT tasks): ");
        label.setLabelFor(blockSpinner);


        // Initial values
        if (sd.getForkBlock(stationKey).intValue() < 0) {
            check.setSelected(false);
            blockSpinner.setValue(new Float(Float.POSITIVE_INFINITY));
            blockSpinner.setEnabled(false);
        }
        else {
            check.setSelected(true);
            blockSpinner.setValue(sd.getForkBlock(stationKey));
            blockSpinner.setEnabled(true);
        }

        // Adds action listeners
        check.addActionListener(new ActionListener() {

            /**
             * Toggles block property
             */
            public void actionPerformed(ActionEvent e) {
                if (check.isSelected()) {
                    if (Defaults.getAsInteger("forkBlock").intValue() > 0)
                        sd.setForkBlock(stationKey, Defaults.getAsInteger("forkBlock"));
                    else
                        sd.setForkBlock(stationKey, new Integer(1));
                    blockSpinner.setValue(sd.getForkBlock(stationKey));
                    blockSpinner.setEnabled(true);
                }
                else {
                    sd.setForkBlock(stationKey, new Integer(-1));
                    blockSpinner.setValue(new Float(Float.POSITIVE_INFINITY));
                    blockSpinner.setEnabled(false);
                }
            }
        });

        // Creates a temp panel for blockSpinner and its label
        JPanel tmp = new JPanel();
        tmp.add(label);
        tmp.add(blockSpinner);

        block.add(check, BorderLayout.NORTH);
        block.add(tmp, BorderLayout.CENTER);
    }

    /**
     * Creates spinner to choose number of splits for each output link
     * and to choose blocking region number of jobs
     */
    private void updateSpinners(){
        numForkSpinner = new JSpinner();
        numForkSpinner.setPreferredSize(DIM_BUTTON_XS);
        numForkSpinner.setValue(sd.getStationNumberOfServers(stationKey));
        numForkSpinner.setToolTipText("Number of tasks created for each input job on each output link");
        numForkSpinner.addChangeListener(new ChangeListener(){
            public void stateChanged(ChangeEvent e) {
                if (numForkSpinner.getValue() instanceof Integer) {
                    Integer serverNum = (Integer)numForkSpinner.getValue();
                    if(serverNum.intValue()<1){
                        serverNum = new Integer(1);
                        numForkSpinner.setValue(serverNum);
                    }
                    sd.setStationNumberOfServers(serverNum, stationKey);
                }
            }
        });

        blockSpinner = new JSpinner();
        blockSpinner.setPreferredSize(DIM_BUTTON_XS);
        blockSpinner.setToolTipText("Maximum allowed number of jobs inside a fork-join section. Following jobs will be queued.");
        blockSpinner.addChangeListener(new ChangeListener(){
            public void stateChanged(ChangeEvent e) {
                if (blockSpinner.getValue() instanceof Integer) {
                    Integer blockNum = (Integer)blockSpinner.getValue();
                    if(blockNum.intValue()<1){
                        blockNum = new Integer(1);
                        blockSpinner.setValue(blockNum);
                    }
                    sd.setForkBlock(stationKey, blockNum);
                }
            }
        });

    }


    /**
     * @return the panel's name
     */
    public String getName() {
        return "Fork Section";
    }
}

⌨️ 快捷键说明

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