From: <mar...@us...> - 2007-10-19 00:51:28
|
Revision: 74 http://gridsim.svn.sourceforge.net/gridsim/?rev=74&view=rev Author: marcos_dias Date: 2007-10-18 17:51:28 -0700 (Thu, 18 Oct 2007) Log Message: ----------- This update is to allow the user to change the default Graphical User Interface used by GridSim and provide his/her own. Removed Paths: ------------- branches/gridsim4.0-branch3/source/gridsim/gui/GridSimVisualizer.java Deleted: branches/gridsim4.0-branch3/source/gridsim/gui/GridSimVisualizer.java =================================================================== --- branches/gridsim4.0-branch3/source/gridsim/gui/GridSimVisualizer.java 2007-10-18 06:20:07 UTC (rev 73) +++ branches/gridsim4.0-branch3/source/gridsim/gui/GridSimVisualizer.java 2007-10-19 00:51:28 UTC (rev 74) @@ -1,407 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2005-2007 - * - * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - * - *****************************************************************************/ -package gridsim.gui; - -import gridsim.GridResource; -import gridsim.GridSim; -import gridsim.ParameterException; -import gridsim.ResourceCharacteristics; - -import java.awt.BorderLayout; -import java.awt.GridLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; - -import javax.swing.BorderFactory; -import javax.swing.DefaultListModel; -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JList; -import javax.swing.JMenu; -import javax.swing.JMenuBar; -import javax.swing.JMenuItem; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.border.Border; -import javax.swing.border.EtchedBorder; -import javax.swing.border.TitledBorder; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; - -/** - * {@link GridSimVisualiser} is the class that represents the main window. - * used by the visualisation tool. From this window it is possible to - * start the simulation, run step by step or run it in slow motion. <br> - * <b>NOTE:</b> This visualisation tool should be used for debugging - * purposes only. It is useful if you want to evaluate a new allocation - * policy for example. A real experiment is not meant to have any - * visualisation features. This interface was initially created for - * another simulator called PajFit available at <b>http://pajfit.sourceforge.net/</b>. - * - * @author Marco A. S. Netto (created this visualisation tool) - * @author Marcos Dias de Assuncao (modified this class to be - * used by GridSim.) - * @since GridSim Turbo Alpha 0.1 - * - * @see gridsim.GridSim#startGridSimulation(boolean) - * @see gridsim.GridSim#startGridSimulation() - * @see gridsim.turbo.ParallelSpaceShared - * @see gridsim.turbo.ARTParallelSpaceShared - */ -public class GridSimVisualizer extends JFrame - implements ActionListener, ListSelectionListener { - - private static final long serialVersionUID = 2059324063853260682L; - public static final int WINDOW_WIDTH = 400; - public static final int WINDOW_HEIGHT = 350; - - private JButton stepButton_; - private JButton runButton_; - private JButton slowMotionButton_; - private JList resourceJList_; - private JList resourceInfoJList_; - - // the thread responsible for starting the simulation. This is needed - // because we do not want to buttons to remain blocked during the whole - // simulation process - private Thread simThread_; - - // indicates whether this is the first time the user - // is clicking a button of the graphical user interface - private boolean firstButtonClick_; - - // a table containing references to the resource windows. That is, - // each resource has a window that displays the content of the - // scheduling queue and the allocation actions performed - private HashMap<String, ResourceWindow> resourceWindows_ = null; - private ArrayList<GridResource> resources_ = null; - - // a list of all the allocation listeners in the system - private static LinkedHashMap<Integer, AllocationListener> listeners_; - - // constants to indicate the time unit to be used for displaying - // information - public static final int TIME_UNIT_SECOND = 0; - public static final int TIME_UNIT_MINUTE = 1; - public static final int TIME_UNIT_HOUR = 2; - - static { - listeners_ = new LinkedHashMap<Integer, AllocationListener>(); - } - - /** - * Creates the main window of the visualiser. - * @param resources a list of the {@link GridResource} entities created - * during the simulation process - * @throws ParameterException if the simulation has been started - * as debug mode and no resources have been created - */ - public GridSimVisualizer(ArrayList<GridResource> resources) - throws ParameterException { - - resources_ = resources; - resourceWindows_ = new HashMap<String,ResourceWindow>(); - initResourceWindows(); - - super.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); - super.setLocation(10, 10); - super.setTitle("GridSim Turbo Alpha 0.1 Visualizer"); - - JPanel mainPanel = new JPanel(new GridLayout(0, 1)); - JPanel simulationPanel = new JPanel(new GridLayout(0, 2)); - - Border simulationBorder = BorderFactory.createTitledBorder( - BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "Simulation"); - simulationPanel.setBorder(simulationBorder); - - JPanel executionPanel = new JPanel(new GridLayout(3,0)); - executionPanel.setBorder(new TitledBorder("Execution")); - - stepButton_ = new JButton("Step"); - runButton_ = new JButton("Run"); - slowMotionButton_ = new JButton("Slow Motion"); - - stepButton_.addActionListener(this); - runButton_.addActionListener(this); - slowMotionButton_.addActionListener(this); - - executionPanel.add(stepButton_); - executionPanel.add(runButton_); - executionPanel.add(slowMotionButton_); - - ArrayList<String> resourceNames = new ArrayList<String>(); - for(GridResource resource : resources_){ - resourceNames.add(resource.get_name()); - } - - resourceJList_ = new JList(); - resourceJList_.setListData(resourceNames.toArray()); - resourceJList_.setBackground(this.getBackground()); - resourceJList_.addListSelectionListener(this); - - JPanel resourcePanel = new JPanel(new GridLayout(0, 1)); - resourcePanel.setBorder(new TitledBorder("Resources")); - JScrollPane scrollResourcePanel = new JScrollPane(resourceJList_); - resourcePanel.add(scrollResourcePanel); - - simulationPanel.add(executionPanel); - simulationPanel.add(resourcePanel); - - resourceInfoJList_ = new JList(); - resourceInfoJList_.setBackground(this.getBackground()); - - JPanel resourceInfoPanel = new JPanel(new GridLayout(0, 1)); - resourceInfoPanel.setBorder(new TitledBorder("Resource Details")); - resourceInfoPanel.add(resourceInfoJList_); - - Border resourceInfoBorder = BorderFactory.createTitledBorder( - BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "Resource Details"); - resourceInfoPanel.setBorder(resourceInfoBorder); - - mainPanel.add(simulationPanel); - mainPanel.add(resourceInfoPanel); - - createMenuBar(); - firstButtonClick_ = true; - - super.setLocation(0, 0); - super.getContentPane().add(mainPanel, BorderLayout.CENTER); - super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - - // Creates a thread that will be responsible for starting the simulation - // I don't want to block the buttons during the whole simulation because - // they will be required for other things - simThread_ = new Thread(){ - public void run(){ - // here in fact, start the simulation - GridSim.startGridSimulation(); - } - }; - } - - // ------------------------- PUBLIC METHODS ------------------------- - - /** - * Notifies a listener about the action performed - * @param action the action performed - * @param shouldPause indicates whether the simulation should be paused after - * notifying the listener. <tt>true</tt> indicates that it should pause and - * <tt>false</tt> means that it should not. - * @see AllocationAction#ITEM_ARRIVED - * @see AllocationAction#ITEM_SCHEDULED - * @see AllocationAction#ITEM_CANCELLED - * @see AllocationAction#ITEM_COMPLETED - * @see AllocationAction#ITEM_STATUS_CHANGED - * @see AllocationAction#SCHEDULE_CHANGED - */ - public static void notifyListeners(AllocationAction action, boolean shouldPause) { - AllocationListener listener = listeners_.get(action.getSubject()); - if(listener != null) { - listener.allocationActionPerformed(action); - informListenersAboutTime(); - - if(shouldPause){ - if(GridSim.isStepByStepEnabled()){ - GridSim.pauseSimulation(); - } - else if(GridSim.isSlowMotionModeEnabled()){ - GridSim.smallPause(); - } - } - } - } - - /** - * Adds a listener to the list of listeners interested in the events - * generated by a given entity - * @param entityId the id of the entity in which the listener is interested - * @param listener the listener to be registered - * @return <tt>true</tt> if success or <tt>false</tt> otherwise - */ - public static boolean addAllocationListener(int entityId, - AllocationListener listener) { - if(listeners_.containsKey(entityId)) - return false; - - listeners_.put(entityId, listener); - return true; - } - - /** - * Unregisters a listener from the list of listeners - * @param listener the listener to be unregistered - * @return <tt>true</tt> if success or <tt>false</tt> otherwise - */ - public static boolean removeAllocationListener(AllocationListener listener) { - if(!listeners_.containsValue(listener)){ - return false; - } - listeners_.remove(listener); - return true; - } - - /** - * Handles events triggered by the list of resource - */ - public void valueChanged(ListSelectionEvent ev) { - String selectedResource = (String) resourceJList_.getSelectedValue(); - updateResourceDetails(selectedResource); - } - - /** - * Handles the option that the user selected. - */ - public void actionPerformed(ActionEvent e){ - String cmd = e.getActionCommand(); - - if(cmd.equals("Step")){ - GridSim.enableStepByStepMode(); - GridSim.disableSlowMotionMode(); - - checkFirstClick(); - GridSim.resumeSimulation(); - } - else if(cmd.equals("Run")){ - GridSim.disableStepByStepMode(); - GridSim.disableSlowMotionMode(); - - if(!checkFirstClick()) { - GridSim.resumeSimulation(); - } - } - else if(cmd.equals("Slow Motion")){ - GridSim.disableStepByStepMode(); - GridSim.enableSlowMotionMode(); - - if(!checkFirstClick()) { - GridSim.resumeSimulation(); - } - } - else if(cmd.equals("Exit") ){ - System.exit(0); - } - else { - String resourceName = cmd; - ResourceWindow window = resourceWindows_.get(resourceName); - window.setVisible(true); - } - } - - // ------------------------- PRIVATE METHODS ------------------------- - - /** - * Informs all the listeners about the change in the - * simulation time - */ - private static void informListenersAboutTime() { - Iterator<AllocationListener> iterListener = listeners_.values().iterator(); - while(iterListener.hasNext()) { - AllocationListener listener = iterListener.next(); - AllocationAction action = - new AllocationAction(AllocationAction.SIMULATION_TIME_CHANGED); - listener.allocationActionPerformed(action); - } - } - - /** - * This method initialises the resource windows - */ - private void initResourceWindows() { - int windowId = 0; - for(GridResource resource : resources_){ - GridResource tresource = (GridResource)resource; - ResourceWindow window = new ResourceWindow(resource, windowId); - resourceWindows_.put(resource.get_name(), window); - - // registers the window as an allocation listener - // of the allocation policy. This means that the window will be - // notified of the allocation actions performed by the resource - // allocation policy - listeners_.put(tresource.getAllocationPolicy().get_id(), window); - windowId++; - } - } - - /** - * Creates the menu bar of the main window - */ - private void createMenuBar() { - JMenuBar menuBar = new JMenuBar(); - JMenu menuCommand = new JMenu("Start"); - JMenuItem item; - - for(GridResource rs : resources_){ - item = new JMenuItem(rs.get_name()); - item.addActionListener(this); - menuCommand.add(item); - } - - menuCommand.addSeparator(); - item = new JMenuItem("Exit"); - item.addActionListener(this); - menuCommand.add(item); - menuBar.add(menuCommand); - setJMenuBar(menuBar); - } - - /** - * This method checks whether this is the first time that a - * button is clicked. If so, then the simulation has to be - * started. - * @return <tt>true</tt> if it was the first click or - * <tt>false</tt> otherwise. - */ - private boolean checkFirstClick() { - if(firstButtonClick_){ - firstButtonClick_ = false; - simThread_.start(); - return true; - } - return false; - } - - /** - * Updates the details about the resource - */ - private void updateResourceDetails(String resourceName) { - GridResource resource = null; - Iterator<GridResource> iterResources = resources_.iterator(); - while(iterResources.hasNext()) { - resource = iterResources.next(); - if(resource.get_name().equals(resourceName)) - break; - } - - if(resource == null) - return; - - DefaultListModel model = new DefaultListModel(); - ResourceCharacteristics charact = resource.getResourceCharacteristics(); - model.addElement("Resource ID: " + charact.getResourceID()); - model.addElement("Number of PEs: " + charact.getNumPE()); - model.addElement("Allocation Policy: " + charact.getResourceAllocationPolicyName()); - model.addElement("Time Zone: " + charact.getResourceTimeZone()); - model.addElement("Rating per PE: " + charact.getMIPSRatingOfOnePE() + " MIPS"); - resourceInfoJList_.setModel(model); - } -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |