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

📄 visitspanel.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.jaba.panels;

import jmt.gui.exact.panels.ForceUpdatablePanel;
import jmt.gui.exact.table.ExactTable;
import jmt.gui.exact.table.ExactTableModel;
import jmt.gui.exact.utils.ArrayUtils;
import jmt.gui.help.HoverHelp;
import jmt.gui.jaba.JabaConstants;
import jmt.gui.jaba.JabaModel;
import jmt.gui.jaba.JabaWizard;
import jmt.gui.wizard.WizardPanel;

import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;

/**

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

 */

/**
 * 3rd panel: visits
 */
public final class VisitsPanel extends WizardPanel implements JabaConstants, ForceUpdatablePanel {
    // Bertoli Marco - Used to show only two decimal digits
    private static DecimalFormat formatter = new DecimalFormat("#0.00");

	private JabaWizard ew;
	private HoverHelp help;
	private static final String helpText = "<html>In this panel you can edit the station visits for each class.<br><br>" +
	        " To enter values, single-click on the desired cell" +
	        " and start typing.<br> To select multiple cells drag the mouse on them; click or drag on" +
	        " row/column headers to select whole rows/columns.<br> <b>For a list of the available operations right-click" +
	        " on the table</b>; all operations except pasting affect selected cells.<br>" +
	        " To copy one value to multiple cells click on the cell containing the value, select the" +
	        " target cells by dragging and select <b>\"Fill\"</b>.<br><br></html>";

	private int classes, stations;
	private String[] classNames;
	private String[] stationNames;
	private double[][] visits;

	private VisitTable visitTable;


	public VisitsPanel(JabaWizard ew) {
		this.ew = ew;
		help = ew.getHelp();

		/* sync status with data object */
		sync();
		initComponents();
	}

	/**
	 * gets status from data object
	 */
	private void sync() {
		/* arrays are copied to ensure data object consistency is preserved */
		JabaModel data = ew.getData();
		synchronized (data) {
			classes = data.getClasses();
			stations = data.getStations();

			classNames = data.getClassNames();
			stationNames = data.getStationNames();
			visits = ArrayUtils.copy2(data.getVisits());
		}
	}

	/**
	 * Set up the panel contents and layout
	 */
	private void initComponents() {

		visitTable = new VisitTable();

        JPanel totalBox = new JPanel(new BorderLayout(10,10));

        //Horizontal box containing Description label and buttons
        JLabel descrLabel = new JLabel(DESCRIPTION_VISITS);
        JPanel descrBox = new JPanel(new BorderLayout());
        descrBox.setPreferredSize(new Dimension(200,1000));
        descrBox.add(descrLabel, BorderLayout.NORTH);

        JScrollPane visitTablePane = new JScrollPane(visitTable);
        visitTablePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		visitTablePane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        totalBox.add(visitTablePane, BorderLayout.CENTER);
        totalBox.add(descrBox, BorderLayout.WEST);

		setLayout(new BorderLayout());
        add(totalBox, BorderLayout.CENTER);
        add(Box.createVerticalStrut(20), BorderLayout.NORTH);
        add(Box.createVerticalStrut(20), BorderLayout.SOUTH);
        add(Box.createHorizontalStrut(20), BorderLayout.EAST);
        add(Box.createHorizontalStrut(20), BorderLayout.WEST);

	}


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

	private void commit() {

		visitTable.stopEditing();

		JabaModel data = ew.getData();
		synchronized (data) {
			data.setVisits(visits);
		}
	}

	public void gotFocus() {
		sync();
		visitTable.updateStructure();
	}

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

	public void help() {
		JOptionPane.showMessageDialog(this, helpText, "Help", JOptionPane.INFORMATION_MESSAGE);
	}

    /**{@see ForceUpdatablePanel} for further details*/
    public void retrieveData() {
        this.sync();
    }

    /**{@see ForceUpdatablePanel} for further details*/
    public void commitData() {
        this.commit();
    }

    private class VisitTable extends ExactTable {

		VisitTable() {
			super(new VisitTableModel());
			autoResizeMode = AUTO_RESIZE_OFF;

			setDisplaysScrollLabels(true);
			help.addHelp(this, "Click or drag to select cells; to edit data single-click and start typing. Right-click for a list of available operations");
			help.addHelp(moreColumnsLabel, "There are more classes: scroll right to see them");
			help.addHelp(moreRowsLabel, "There are more stations: scroll down to see them");
			help.addHelp(selectAllButton, "Click to select all cells");
			tableHeader.setToolTipText(null);
			help.addHelp(tableHeader, "Click, SHIFT-click or drag to select columns");
			rowHeader.setToolTipText(null);
			help.addHelp(rowHeader, "Click, SHIFT-click or drag to select rows");

			setRowSelectionAllowed(true);
			setColumnSelectionAllowed(true);
			setClipboardTransferEnabled(true);

		}

	}

	/**
	 * the model backing the visit table.
	 * Rows represent stations, columns classes.
	 */
	private class VisitTableModel extends ExactTableModel {

		VisitTableModel() {
			prototype = new Double(1000);
			rowHeaderPrototype = "Station1000";
		}

		public int getRowCount() {
			return stations;
		}

		public int getColumnCount() {
			return classes;
		}

		protected Object getRowName(int rowIndex) {
			return stationNames[rowIndex];
		}

		public String getColumnName(int index) {
			return classNames[index];
		}

		protected Object getValueAtImpl(int rowIndex, int columnIndex) {
			return  formatter.format(visits[rowIndex][columnIndex]);
		}

		public void setValueAt(Object value, int rowIndex, int columnIndex) {
			try {
				double newVal = Double.parseDouble((String) value);
                if (newVal <= 0.01)
                    visits[rowIndex][columnIndex] = 0.01;
                else
				    visits[rowIndex][columnIndex] = newVal;
			} catch (NumberFormatException e) {
			}
		}

		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return true;
		}

		public void clear(int row, int col) {
			visits[row][col] = 0.01;
		}

		/**
		 * Copy the contents of a cell to an area. Works directly on the data set.
		 */
		public void copyCellToArea(int sourceRow, int sourceCol, int rowFrom, int rowTo, int colFrom, int colTo) {
			double source = visits[sourceRow][sourceCol];

			for (int row = rowFrom; row <= rowTo; row++) {
				for (int col = colFrom; col <= colTo; col++) {
					visits[row][col] = source;
				}
			}
		}

	}
}

⌨️ 快捷键说明

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