diff options
| author | Paul Buetow <paul@buetow.org> | 2008-05-30 00:52:51 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2008-05-30 00:52:51 +0000 |
| commit | b82536ec35d01fd1195a11733203be7c1c008ad6 (patch) | |
| tree | 55b69f0d6d2a1ba2dcedd57a2cac7d92ea784393 /sources/simulator | |
| parent | 19c666a62379a94e22acc96b9ed81660fcb1e53f (diff) | |
new package has been documented.
Diffstat (limited to 'sources/simulator')
| -rw-r--r-- | sources/simulator/2DOCUMENT | 0 | ||||
| -rw-r--r-- | sources/simulator/VSLogging.java | 48 | ||||
| -rw-r--r-- | sources/simulator/VSMain.java | 24 | ||||
| -rw-r--r-- | sources/simulator/VSMenuItemStates.java | 8 | ||||
| -rw-r--r-- | sources/simulator/VSSimulator.java | 724 | ||||
| -rw-r--r-- | sources/simulator/VSSimulatorCanvas.java | 652 | ||||
| -rw-r--r-- | sources/simulator/VSSimulatorFrame.java | 247 |
7 files changed, 870 insertions, 833 deletions
diff --git a/sources/simulator/2DOCUMENT b/sources/simulator/2DOCUMENT deleted file mode 100644 index e69de29..0000000 --- a/sources/simulator/2DOCUMENT +++ /dev/null diff --git a/sources/simulator/VSLogging.java b/sources/simulator/VSLogging.java index b89751c..b7ce221 100644 --- a/sources/simulator/VSLogging.java +++ b/sources/simulator/VSLogging.java @@ -10,11 +10,14 @@ import javax.swing.*; import utils.*; -// TODO: Auto-generated Javadoc /** - * The Class VSLogging. + * The Class VSLogging. An object of this class is responsible for the logging + * of text messages into the simulator's logging window. + * + * @author Paul C. Buetow */ public class VSLogging { + /** The serial version uid */ private static final long serialVersionUID = 1L; /** The logging area. */ @@ -23,7 +26,9 @@ public class VSLogging { /** The filter text. */ private String filterText; - /** The pause lines. */ + /** The pause lines. Used for cacheing the logging if the logging is + * deactivated for a while + */ private ArrayList<StringBuffer> pauseLines; /** The logging lines. */ @@ -32,17 +37,17 @@ public class VSLogging { /** The simulation canvas. */ private VSSimulatorCanvas simulationCanvas; - /** The is filtered. */ + /** The logging messages are filtered. */ private boolean isFiltered; - /** The is paused. */ + /** The logging is paused. */ private boolean isPaused; /** The filter pattern. */ private Pattern filterPattern; /** - * Instantiates a new lang.process.removelogging. + * Instantiates a new VSLogging object. */ public VSLogging() { loggingArea = new JTextArea(0, 0); @@ -57,7 +62,7 @@ public class VSLogging { /** * Sets the simulation canvas. * - * @param simulationCanvas the new simulation canvas + * @param simulationCanvas the simulation canvas */ public void setSimulationCanvas(VSSimulatorCanvas simulationCanvas) { this.simulationCanvas = simulationCanvas; @@ -73,7 +78,7 @@ public class VSLogging { } /** - * Logg. + * Loggs a message using the global time. * * @param message the message */ @@ -85,7 +90,7 @@ public class VSLogging { } /** - * Logg. + * Loggs a message using the specified time. * * @param message the message * @param time the time @@ -103,9 +108,9 @@ public class VSLogging { } /** - * Checks if is paused. + * Sets if the logging is paused. * - * @param isPaused the is paused + * @param isPaused true, if the logging is paused */ public synchronized void isPaused(boolean isPaused) { this.isPaused = isPaused; @@ -119,26 +124,29 @@ public class VSLogging { } /** - * Logg filtered. + * If the logging is filtered, it's using the pattern matching. * - * @param buffer the buffer + * @param buffer the logging buffer to filter */ private void loggFiltered(StringBuffer buffer) { loggingLines.add(buffer); if (!isFiltered) { loggingArea.append(buffer.toString()+"\n"); - loggingArea.setCaretPosition(loggingArea.getDocument().getLength()); + loggingArea.setCaretPosition( + loggingArea.getDocument().getLength()); - } else if (filterPattern != null && filterPattern.matcher(buffer).find()) { + } else if (filterPattern != null && + filterPattern.matcher(buffer).find()) { loggingArea.append(buffer.toString()+"\n"); - loggingArea.setCaretPosition(loggingArea.getDocument().getLength()); + loggingArea.setCaretPosition( + loggingArea.getDocument().getLength()); } } /** - * Checks if is filtered. + * Checks if the logging is filtered. * - * @param isFiltered the is filtered + * @param isFiltered true, if the logging is filtered */ public synchronized void isFiltered(boolean isFiltered) { this.isFiltered = isFiltered; @@ -160,7 +168,7 @@ public class VSLogging { } /** - * Clear. + * Clears the logging. */ public synchronized void clear() { loggingLines.clear(); @@ -169,7 +177,7 @@ public class VSLogging { } /** - * Filter. + * Filters the logging. */ private void filter() { try { diff --git a/sources/simulator/VSMain.java b/sources/simulator/VSMain.java index f8a6df8..b8aa84a 100644 --- a/sources/simulator/VSMain.java +++ b/sources/simulator/VSMain.java @@ -11,15 +11,18 @@ import events.*; import prefs.*; import prefs.editors.*; -// TODO: Auto-generated Javadoc /** - * The Class VSMain. + * The Class VSMain. This class contains the static main method. The simulator + * starts here! + * + * @author Paul C. Buetow */ public class VSMain { + /** The serial version uid */ private static final long serialVersionUID = 1L; /** - * Instantiates a new lang.process.removemain. + * Instantiates a new VSMain object. * * @param prefs the prefs */ @@ -28,24 +31,27 @@ public class VSMain { } /** - * Instantiates a new lang.process.removemain. + * Instantiates a new VSMain object * * @param prefs the prefs - * @param relativeTo the relative to + * @param relativeTo the component to open the window relative to */ public VSMain(VSPrefs prefs, Component relativeTo) { init(prefs, relativeTo); } /** - * Inits the. + * Inits the VSMain object. * * @param prefs the prefs - * @param relativeTo the relative to + * @param relativeTo the component to open the window relative to */ private void init(VSPrefs prefs, Component relativeTo) { - VSSimulatorFrame simulatorFrame = new VSSimulatorFrame(prefs, relativeTo); - new VSEditorFrame(prefs, relativeTo, new VSSimulatorEditor(prefs, simulatorFrame)); + VSSimulatorFrame simulatorFrame = + new VSSimulatorFrame(prefs, relativeTo); + + new VSEditorFrame(prefs, relativeTo, + new VSSimulatorEditor(prefs, simulatorFrame)); } /** diff --git a/sources/simulator/VSMenuItemStates.java b/sources/simulator/VSMenuItemStates.java index ba901de..b751b29 100644 --- a/sources/simulator/VSMenuItemStates.java +++ b/sources/simulator/VSMenuItemStates.java @@ -7,8 +7,11 @@ package simulator; /** * The Class VSMenuItemStates. Used by the VSSimulation to update the * "simulation" bar of the VSSimulationFrame. + * + * @author Paul C. Buetow */ public class VSMenuItemStates { + /** The serial version uid */ private static final long serialVersionUID = 1L; /** The pause state. */ @@ -24,14 +27,15 @@ public class VSMenuItemStates { private volatile boolean start; /** - * Instantiates a new lang.process.removemenu item states. + * Instantiates a new VSMenuItemStates object. * * @param pause the pause state * @param replay the replay state * @param reset the reset state * @param start the start state */ - public VSMenuItemStates(boolean pause, boolean replay, boolean reset, boolean start) { + public VSMenuItemStates(boolean pause, boolean replay, boolean reset, + boolean start) { this.pause = pause; this.replay = replay; this.reset = reset; diff --git a/sources/simulator/VSSimulator.java b/sources/simulator/VSSimulator.java index 3497dd1..be368fd 100644 --- a/sources/simulator/VSSimulator.java +++ b/sources/simulator/VSSimulator.java @@ -19,10 +19,15 @@ import prefs.editors.*; import utils.*; /** - * The Class VSSimulator. + * The Class VSSimulator. An object of this class represents a whole simulation. + * It may be, that several parallel simulations exist. They are independent + * fron each other. + * + * @author Paul C. Buetow */ public class VSSimulator extends JPanel { - private static final long serialVersionUID = 1L; + /** the serial version uid */ + private static final long serialversionuid = 1l; /** The global text fields. */ private ArrayList<String> globalTextFields; @@ -30,7 +35,7 @@ public class VSSimulator extends JPanel { /** The local text fields. */ private ArrayList<String> localTextFields; - /** The create tasks. */ + /** The create tasks array list. */ private ArrayList<VSCreateTask> createTasks; /** The filter active check box. */ @@ -117,12 +122,15 @@ public class VSSimulator extends JPanel { /** The task manager local model. */ private VSTaskManagerTableModel taskManagerLocalModel; - /** The has started. */ + /** The simulation has started. */ private boolean hasStarted = false; /** The last selected process num. */ private int lastSelectedProcessNum; + /** The last expert state. */ + private boolean lastExpertState; + /** The simulation counter. */ private static int simulationCounter; @@ -130,7 +138,222 @@ public class VSSimulator extends JPanel { private static int simulationNum; /** - * Instantiates a new lang.process.removesimulator. + * The Class VSTaskManagerTableModel. An object of this class handles + * the task manager's JTable. + */ + private class VSTaskManagerTableModel extends AbstractTableModel + implements MouseListener { + /** the serial version uid */ + private static final long serialversionuid = 1l; + + /** The Constant LOCAL. */ + public static final boolean LOCAL = true; + + /** The Constant GLOBAL. */ + public static final boolean GLOBAL = false; + + /** The Constant ALL_PROCESSES. */ + public static final boolean ALL_PROCESSES = true; + + /** The Constant ONE_PROCESS. */ + public static final boolean ONE_PROCESS = false; + + /** The all processes. */ + public boolean allProcesses; + + /** The tasks. */ + private VSPriorityQueue<VSTask> tasks; + + /** The column names. */ + private String columnNames[]; + + /** The num columns. */ + private int numColumns; + + /** The table. */ + private JTable table; + + /** + * Instantiates a new VSTaskManagerTableModel object + * + * @param process the process + * @param localTask true, if this table manages the local task. false, + * if this table manages the global tasks. + */ + public VSTaskManagerTableModel(VSProcess process, boolean localTask) { + set(process, localTask, ONE_PROCESS); + columnNames = new String[3]; + columnNames[0]= prefs.getString("lang.time") + " (ms)"; + columnNames[1] = prefs.getString("lang.process.id"); + columnNames[2] = prefs.getString("lang.event"); + numColumns = 3; + } + + /** + * Sets the table. + * + * @param table the table + */ + public void setTable(JTable table) { + this.table = table; + } + + /** + * Sets new values. + * + * @param process the process + * @param localTasks true, if this table manages the local tasks. false + * if this table manages the global tasks. + * @param allProcesses true, if this table shows tasks of all processes. + * false, if this table only shows tasks of the specified process. + */ + public void set(VSProcess process, boolean localTasks, + boolean allProcesses) { + this.allProcesses = allProcesses; + + if (allProcesses) { + this.tasks = localTasks + ? taskManager.getLocalTasks() + : taskManager.getGlobalTasks(); + } else { + this.tasks = localTasks + ? taskManager.getProcessLocalTasks(process) + : taskManager.getProcessGlobalTasks(process); + } + + fireTableDataChanged(); + } + + /* (non-Javadoc) + * @see javax.swing.table.AbstractTableModel#getColumnName(int) + */ + public String getColumnName(int col) { + return columnNames[col]; + } + + /* (non-Javadoc) + * @see javax.swing.table.TableModel#getRowCount() + */ + public int getRowCount() { + return tasks == null ? 0 : tasks.size(); + } + + /* (non-Javadoc) + * @see javax.swing.table.TableModel#getColumnCount() + */ + public int getColumnCount() { + return numColumns; + } + + /* (non-Javadoc) + * @see javax.swing.table.TableModel#getValueAt(int, int) + */ + public Object getValueAt(int row, int col) { + VSTask task = tasks.get(row); + + switch (col) { + case 0: + return task.getTaskTime(); + case 1: + return task.getProcess().getProcessID(); + } + + return task.getEvent().getShortname(); + } + + /* (non-Javadoc) + * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int) + */ + public boolean isCellEditable(int row, int col) { + return false; + } + + /* (non-Javadoc) + * @see javax.swing.table.AbstractTableModel#setValueAt( + * java.lang.Object, int, int) + */ + public void setValueAt(Object value, int row, int col) { + fireTableDataChanged(); + } + + /** + * Adds the task. + * + * @param task the task + */ + public void addTask(VSTask task) { + tasks.add(task); + fireTableDataChanged(); + } + + /** + * Removes the task at a specified row. + * + * @param row the row + */ + private void removeTaskAtRow(int row) { + VSTask task = tasks.get(row); + tasks.remove(task); + taskManager.removeTask(task); + fireTableDataChanged(); + } + + /* (non-Javadoc) + * @see java.awt.event.MouseListener#mouseClicked( + * java.awt.event.MouseEvent) + */ + public void mouseClicked(MouseEvent me) { + JTable source = (JTable) me.getSource(); + final int row = source.rowAtPoint(me.getPoint()); + final int col = source.columnAtPoint(me.getPoint()); + + if (SwingUtilities.isRightMouseButton(me)) { + ActionListener actionListener = new ActionListener() { + public void actionPerformed(ActionEvent ae) { + String command = ae.getActionCommand(); + if (command.equals(prefs.getString("lang.remove"))) { + removeTaskAtRow(row); + } + } + }; + + JPopupMenu popup = new JPopupMenu(); + JMenuItem item = new JMenuItem(prefs.getString("lang.remove")); + item.addActionListener(actionListener); + popup.add(item); + + popup.show(me.getComponent(), me.getX(), me.getY()); + } + } + + /* (non-Javadoc) + * @see java.awt.event.MouseListener#mouseEntered( + * java.awt.event.MouseEvent) + */ + public void mouseEntered(MouseEvent me) { } + + /* (non-Javadoc) + * @see java.awt.event.MouseListener#mouseExited( + * java.awt.event.MouseEvent) + */ + public void mouseExited(MouseEvent me) { } + + /* (non-Javadoc) + * @see java.awt.event.MouseListener#mousePressed( + * java.awt.event.MouseEvent) + */ + public void mousePressed(MouseEvent me) { } + + /* (non-Javadoc) + * @see java.awt.event.MouseListener#mouseReleased( + * java.awt.event.MouseEvent) + */ + public void mouseReleased(MouseEvent me) { } + } + + + /** + * Instantiates a new VSSimulator object. * * @param prefs the prefs * @param simulatorFrame the simulator frame @@ -171,7 +394,7 @@ public class VSSimulator extends JPanel { } /** - * Fill content pane. + * Fills the content pane. */ private void fillContentPane() { loggingArea = logging.getLoggingArea(); @@ -195,7 +418,7 @@ public class VSSimulator extends JPanel { loggingPanel.setPreferredSize(new Dimension(200, 1)); splitPaneH.setOrientation(JSplitPane.HORIZONTAL_SPLIT); - splitPaneH.setLeftComponent(createProcessPane()); + splitPaneH.setLeftComponent(createProcessPanel()); splitPaneH.setRightComponent(canvasPanel); splitPaneH.setContinuousLayout(true); splitPaneH.setOneTouchExpandable(true); @@ -208,25 +431,24 @@ public class VSSimulator extends JPanel { this.add(splitPaneV); } - /** The last expert state. */ - private boolean lastExpertState; - /** * Creates the tools panel. * - * @return the j panel + * @return the panel */ private JPanel createToolsPanel() { JPanel toolsPanel = new JPanel(); boolean expertMode = prefs.getBoolean("sim.mode.expert"); toolsPanel.setLayout(new BoxLayout(toolsPanel, BoxLayout.X_AXIS)); + JCheckBox expertActiveCheckBox = + new JCheckBox(prefs.getString("lang.mode.expert")); - JCheckBox expertActiveCheckBox = new JCheckBox(prefs.getString("lang.mode.expert")); expertActiveCheckBox.setSelected(expertMode); expertActiveCheckBox.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { - AbstractButton abstractButton = (AbstractButton) ce.getSource(); + AbstractButton abstractButton = + (AbstractButton) ce.getSource(); ButtonModel buttonModel = abstractButton.getModel(); boolean newState = buttonModel.isSelected(); if (lastExpertState != newState) { @@ -239,11 +461,13 @@ public class VSSimulator extends JPanel { toolsPanel.add(expertActiveCheckBox); if (expertMode) { - lamportActiveCheckBox = new JCheckBox(prefs.getString("lang.time.lamport")); + lamportActiveCheckBox = new JCheckBox( + prefs.getString("lang.time.lamport")); lamportActiveCheckBox.setSelected(false); lamportActiveCheckBox.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { - AbstractButton abstractButton = (AbstractButton) ce.getSource(); + AbstractButton abstractButton = + (AbstractButton) ce.getSource(); ButtonModel buttonModel = abstractButton.getModel(); simulationCanvas.showLamport(buttonModel.isSelected()); if (buttonModel.isSelected()) @@ -252,11 +476,13 @@ public class VSSimulator extends JPanel { }); toolsPanel.add(lamportActiveCheckBox); - vectorTimeActiveCheckBox = new JCheckBox(prefs.getString("lang.time.vector")); + vectorTimeActiveCheckBox = new JCheckBox( + prefs.getString("lang.time.vector")); vectorTimeActiveCheckBox.setSelected(false); vectorTimeActiveCheckBox.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { - AbstractButton abstractButton = (AbstractButton) ce.getSource(); + AbstractButton abstractButton = + (AbstractButton) ce.getSource(); ButtonModel buttonModel = abstractButton.getModel(); simulationCanvas.showVectorTime(buttonModel.isSelected()); if (buttonModel.isSelected()) @@ -265,11 +491,13 @@ public class VSSimulator extends JPanel { }); toolsPanel.add(vectorTimeActiveCheckBox); - JCheckBox antiAliasing = new JCheckBox(prefs.getString("lang.antialiasing")); + JCheckBox antiAliasing = new JCheckBox( + prefs.getString("lang.antialiasing")); antiAliasing.setSelected(false); antiAliasing.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { - AbstractButton abstractButton = (AbstractButton) ce.getSource(); + AbstractButton abstractButton = + (AbstractButton) ce.getSource(); ButtonModel buttonModel = abstractButton.getModel(); simulationCanvas.isAntiAliased(buttonModel.isSelected()); } @@ -277,11 +505,13 @@ public class VSSimulator extends JPanel { toolsPanel.add(antiAliasing); } - JCheckBox loggingActiveCheckBox = new JCheckBox(prefs.getString("lang.logging.active")); + JCheckBox loggingActiveCheckBox = new JCheckBox( + prefs.getString("lang.logging.active")); loggingActiveCheckBox.setSelected(true); loggingActiveCheckBox.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { - AbstractButton abstractButton = (AbstractButton) ce.getSource(); + AbstractButton abstractButton = + (AbstractButton) ce.getSource(); ButtonModel buttonModel = abstractButton.getModel(); logging.isPaused(!buttonModel.isSelected()); } @@ -289,11 +519,13 @@ public class VSSimulator extends JPanel { toolsPanel.add(loggingActiveCheckBox); if (expertMode) { - filterActiveCheckBox = new JCheckBox(prefs.getString("lang.filter")); + filterActiveCheckBox = new JCheckBox( + prefs.getString("lang.filter")); filterActiveCheckBox.setSelected(false); filterActiveCheckBox.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { - AbstractButton abstractButton = (AbstractButton) ce.getSource(); + AbstractButton abstractButton = + (AbstractButton) ce.getSource(); ButtonModel buttonModel = abstractButton.getModel(); logging.isFiltered(buttonModel.isSelected()); if (buttonModel.isSelected()) @@ -303,26 +535,27 @@ public class VSSimulator extends JPanel { toolsPanel.add(filterActiveCheckBox); filterTextField = new JTextField(); - filterTextField.getDocument().addDocumentListener(new DocumentListener() { + filterTextField.getDocument().addDocumentListener( + new DocumentListener() { public void insertUpdate(DocumentEvent de) { logging.setFilterText(filterTextField.getText()); } - public void removeUpdate(DocumentEvent de) { logging.setFilterText(filterTextField.getText()); } - public void changedUpdate(DocumentEvent de) { logging.setFilterText(filterTextField.getText()); } }); toolsPanel.add(filterTextField); - JButton clearButton = new JButton(prefs.getString("lang.logging.clear")); + JButton clearButton = new JButton( + prefs.getString("lang.logging.clear")); clearButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { - String actionCommand = ae.getActionCommand(); - if (actionCommand.equals(prefs.getString("lang.logging.clear"))) { + String command = ae.getActionCommand(); + if (command.equals( + prefs.getString("lang.logging.clear"))) { logging.clear(); } } @@ -334,11 +567,11 @@ public class VSSimulator extends JPanel { } /** - * Creates the process pane. + * Creates the process panel. * - * @return the j panel + * @return the panel */ - private JPanel createProcessPane() { + private JPanel createProcessPanel() { JPanel editPanel = new JPanel(new GridBagLayout()); boolean expertMode = prefs.getBoolean("sim.mode.expert"); editPanel.setLayout(new BoxLayout(editPanel, BoxLayout.Y_AXIS)); @@ -362,7 +595,8 @@ public class VSSimulator extends JPanel { localPIDComboBox.addItem(prefs.getString("lang.all")); globalPIDComboBox.addItem(prefs.getString("lang.all")); - tabbedPane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT); + tabbedPane = new JTabbedPane(JTabbedPane.TOP, + JTabbedPane.WRAP_TAB_LAYOUT); localPanel = createTaskLabel(VSTaskManagerTableModel.LOCAL); JPanel globalPanel = createTaskLabel(VSTaskManagerTableModel.GLOBAL); @@ -380,8 +614,10 @@ public class VSSimulator extends JPanel { processesComboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { - localTextFields.set(lastSelectedProcessNum, localTextField.getText()); - globalTextFields.set(lastSelectedProcessNum, globalTextField.getText()); + localTextFields.set(lastSelectedProcessNum, + localTextField.getText()); + globalTextFields.set(lastSelectedProcessNum, + globalTextField.getText()); updateTaskManagerTable(); int processNum = getSelectedProcessNum(); @@ -405,8 +641,10 @@ public class VSSimulator extends JPanel { if (processNum != simulationCanvas.getNumProcesses()) { VSProcess process = getSelectedProcess(); - VSProcessEditor processEditor = new VSProcessEditor(prefs, process); - tabbedPane.setComponentAt(1, processEditor.getContentPane()); + VSProcessEditor processEditor = + new VSProcessEditor(prefs, process); + tabbedPane.setComponentAt(1, + processEditor.getContentPane()); } } }); @@ -424,7 +662,7 @@ public class VSSimulator extends JPanel { * * @param text the text * - * @return the j panel + * @return the panel */ private JPanel createLabelPanel(String text) { JPanel panel = new JPanel(); @@ -437,9 +675,10 @@ public class VSSimulator extends JPanel { /** * Creates the task label. * - * @param localTasks the local tasks + * @param localTasks true, if the local task label has to get created. + * false, if the global task label has to get created. * - * @return the j panel + * @return the panel */ private JPanel createTaskLabel(boolean localTasks) { JPanel panel = new JPanel(new GridBagLayout()); @@ -462,346 +701,17 @@ public class VSSimulator extends JPanel { } /** - * The Class VSCreateTask. - */ - private class VSCreateTask { - - /** The event classname. */ - private String eventClassname; - - /** The protocol classname. */ - private String protocolClassname; - - /** The shortname. */ - private String shortname; - - /* Those 3 values are for VSProtocolEvent events */ - /** The is protocol activation. */ - private boolean isProtocolActivation; - - /** The is protocol deactivation. */ - private boolean isProtocolDeactivation; - - /** The is client protocol. */ - private boolean isClientProtocol; - - /* Those values are for ProtocolClient onStart events */ - /** The is client request. */ - private boolean isRequest; - - /** - * Instantiates a new lang.process.removecreate task. - * - * @param eventClassname the event classname - */ - public VSCreateTask(String eventClassname) { - this.eventClassname = eventClassname; - } - - /** - * Checks if is protocol activation. - * - * @param isProtocolActivation the is protocol activation - */ - public void isProtocolActivation(boolean isProtocolActivation) { - this.isProtocolActivation = isProtocolActivation; - - if (isProtocolActivation) - isProtocolDeactivation(false); - } - - /** - * Checks if is protocol deactivation. - * - * @param isProtocolDeactivation the is protocol deactivation - */ - public void isProtocolDeactivation(boolean isProtocolDeactivation) { - this.isProtocolDeactivation = isProtocolDeactivation; - - if (isProtocolDeactivation) - isProtocolActivation(false); - } - - /** - * Checks if is client protocol. - * - * @param isClientProtocol the is client protocol - */ - public void isClientProtocol(boolean isClientProtocol) { - this.isClientProtocol = isClientProtocol; - } - - /** - * Checks if is client request. - * - * @param isRequest the is client request - */ - public void isRequest(boolean isRequest) { - this.isRequest = isRequest; - } - - /** - * Sets the protocol classname. - * - * @param protocolClassname the new protocol classname - */ - public void setProtocolClassname(String protocolClassname) { - this.protocolClassname = protocolClassname; - } - - /** - * Sets the shortname. - * - * @param shortname the new shortname - */ - public void setShortname(String shortname) { - this.shortname = shortname; - } - - /** - * Creates the task. - * - * @param process the process - * @param time the time - * @param localTimedTask the local timed task - * - * @return the lang.process.removetask - */ - public VSTask createTask(VSProcess process, long time, boolean localTimedTask) { - VSAbstractEvent event = null; - - if (isRequest) { - event = process.getProtocolObject(eventClassname); - - } else { - event = VSRegisteredEvents.createEventInstanceByClassname(eventClassname, process); - } - - event.init(process); - if (shortname != null) - event.setShortname(shortname); - - if (isProtocolActivation || isProtocolDeactivation) { - VSProtocolEvent protocolEvent = (VSProtocolEvent) event; - protocolEvent.setProtocolClassname(protocolClassname); - protocolEvent.isProtocolActivation(isProtocolActivation); - protocolEvent.isClientProtocol(isClientProtocol); - } - - return new VSTask(time, process, event, localTimedTask); - } - } - - /** - * The Class VSTaskManagerTableModel. - */ - private class VSTaskManagerTableModel extends AbstractTableModel implements MouseListener { - - /** The Constant LOCAL. */ - public static final boolean LOCAL = true; - - /** The Constant GLOBAL. */ - public static final boolean GLOBAL = false; - - /** The Constant ALL_PROCESSES. */ - public static final boolean ALL_PROCESSES = true; - - /** The Constant ONE_PROCESS. */ - public static final boolean ONE_PROCESS = false; - - /** The all processes. */ - public boolean allProcesses; - - /** The tasks. */ - private VSPriorityQueue<VSTask> tasks; - - /** The column names. */ - private String columnNames[]; - - /** The num columns. */ - private int numColumns; - - /** The table. */ - private JTable table; - - /** - * Instantiates a new lang.process.removetask manager table model. - * - * @param process the process - * @param localTask the local task - */ - public VSTaskManagerTableModel(VSProcess process, boolean localTask) { - set(process, localTask, ONE_PROCESS); - columnNames = new String[3]; - columnNames[0]= prefs.getString("lang.time") + " (ms)"; - columnNames[1] = prefs.getString("lang.process.id"); - columnNames[2] = prefs.getString("lang.event"); - numColumns = 3; - } - - /** - * Sets the table. - * - * @param table the new table - */ - public void setTable(JTable table) { - this.table = table; - } - - /** - * Sets the. - * - * @param process the process - * @param localTasks the local tasks - * @param allProcesses the all processes - */ - public void set(VSProcess process, boolean localTasks, boolean allProcesses) { - this.allProcesses = allProcesses; - - if (allProcesses) { - this.tasks = localTasks - ? taskManager.getLocalTasks() - : taskManager.getGlobalTasks(); - } else { - this.tasks = localTasks - ? taskManager.getProcessLocalTasks(process) - : taskManager.getProcessGlobalTasks(process); - } - - fireTableDataChanged(); - } - - /* (non-Javadoc) - * @see javax.swing.table.AbstractTableModel#getColumnName(int) - */ - public String getColumnName(int col) { - return columnNames[col]; - } - - /* (non-Javadoc) - * @see javax.swing.table.TableModel#getRowCount() - */ - public int getRowCount() { - return tasks == null ? 0 : tasks.size(); - } - - /* (non-Javadoc) - * @see javax.swing.table.TableModel#getColumnCount() - */ - public int getColumnCount() { - return numColumns; - } - - /* (non-Javadoc) - * @see javax.swing.table.TableModel#getValueAt(int, int) - */ - public Object getValueAt(int row, int col) { - VSTask task = tasks.get(row); - - switch (col) { - case 0: - return task.getTaskTime(); - case 1: - return task.getProcess().getProcessID(); - } - - return task.getEvent().getShortname(); - } - - /* (non-Javadoc) - * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int) - */ - public boolean isCellEditable(int row, int col) { - return false; - } - - /* (non-Javadoc) - * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int) - */ - public void setValueAt(Object value, int row, int col) { - fireTableDataChanged(); - } - - /** - * Adds the task. - * - * @param task the task - */ - public void addTask(VSTask task) { - tasks.add(task); - fireTableDataChanged(); - } - - /** - * Removes the task at row. - * - * @param row the row - */ - private void removeTaskAtRow(int row) { - VSTask task = tasks.get(row); - tasks.remove(task); - taskManager.removeTask(task); - fireTableDataChanged(); - } - - /* (non-Javadoc) - * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent) - */ - public void mouseClicked(MouseEvent me) { - JTable source = (JTable) me.getSource(); - final int row = source.rowAtPoint(me.getPoint()); - final int col = source.columnAtPoint(me.getPoint()); - - if (SwingUtilities.isRightMouseButton(me)) { - ActionListener actionListener = new ActionListener() { - public void actionPerformed(ActionEvent ae) { - String actionCommand = ae.getActionCommand(); - if (actionCommand.equals(prefs.getString("lang.remove"))) { - removeTaskAtRow(row); - } - } - }; - - JPopupMenu popup = new JPopupMenu(); - JMenuItem item = new JMenuItem(prefs.getString("lang.remove")); - item.addActionListener(actionListener); - popup.add(item); - - popup.show(me.getComponent(), me.getX(), me.getY()); - } - } - - /* (non-Javadoc) - * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent) - */ - public void mouseEntered(MouseEvent me) { } - - /* (non-Javadoc) - * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent) - */ - public void mouseExited(MouseEvent me) { } - - /* (non-Javadoc) - * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent) - */ - public void mousePressed(MouseEvent me) { } - - /* (non-Javadoc) - * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent) - */ - public void mouseReleased(MouseEvent me) { } - } - - /** * Creates the task table. * - * @param localTasks the local tasks + * @param localTasks true, if the local task label has to get created. + * false, if the global task label has to get created. * - * @return the j table + * @return the table */ private JTable createTaskTable(boolean localTasks) { VSProcess process = getSelectedProcess(); - VSTaskManagerTableModel model = new VSTaskManagerTableModel(process, localTasks); + VSTaskManagerTableModel model = + new VSTaskManagerTableModel(process, localTasks); if (localTasks) taskManagerLocalModel = model; @@ -831,9 +741,10 @@ public class VSSimulator extends JPanel { * Inits the add panel. * * @param panel the panel - * @param localTasks the local tasks + * @param localTasks true, if the local task label has to get created. + * false, if the global task label has to get created. * - * @return the j panel + * @return the panel */ private JPanel initAddPanel(JPanel panel, final boolean localTasks) { JPanel addPanel = new JPanel(); @@ -863,7 +774,6 @@ public class VSSimulator extends JPanel { JButton takeoverButton = new JButton(prefs.getString("lang.takeover")); takeoverButton.addActionListener(new ActionListener() { private boolean isRed; - public void actionPerformed(ActionEvent ae) { String textValue = textField.getText(); Long longValue = null; @@ -914,13 +824,16 @@ public class VSSimulator extends JPanel { if (createTask == null) return false; - ArrayList<VSProcess> processes = getConcernedProcesses(localTasks); + ArrayList<VSProcess> processes = + getConcernedProcesses(localTasks); for (VSProcess process : processes) { - VSTask task = createTask.createTask(process, time, localTasks); + VSTask task = createTask.createTask(process, time, + localTasks); taskManager.addTask(task, VSTaskManager.PROGRAMMED); - if (selectedProcess == null || process.equals(selectedProcess)) { + if (selectedProcess == null || + process.equals(selectedProcess)) { if (localTasks) taskManagerLocalModel.addTask(task); else @@ -937,15 +850,18 @@ public class VSSimulator extends JPanel { boolean createTaskFlag = createTasks == null; if (createTaskFlag) createTasks = new ArrayList<VSCreateTask>(); - Vector<String> eventClassnames = VSRegisteredEvents.getNonProtocolClassnames(); + Vector<String> eventClassnames = + VSRegisteredEvents.getNonProtocolClassnames(); comboBox.setMaximumRowCount(20); - comboBox.addItem("----- " + prefs.getString("lang.events.process") + " -----"); + comboBox.addItem("----- " + prefs.getString("lang.events.process") + + " -----"); if (createTaskFlag) createTasks.add(null); for (String eventClassname : eventClassnames) { - String eventShortname = VSRegisteredEvents.getShortnameByClassname(eventClassname); + String eventShortname = + VSRegisteredEvents.getShortnameByClassname(eventClassname); comboBox.addItem(eventShortname); if (createTaskFlag) createTasks.add(new VSCreateTask(eventClassname)); @@ -963,14 +879,15 @@ public class VSSimulator extends JPanel { eventClassnames = VSRegisteredEvents.getProtocolClassnames(); for (String eventClassname : eventClassnames) { - String eventShortname_ = VSRegisteredEvents.getShortnameByClassname(eventClassname); + String eventShortname_ = + VSRegisteredEvents.getShortnameByClassname(eventClassname); String eventShortname = null; - comboBox.addItem("----- " + eventShortname_ + " " + protocol + " -----"); + comboBox.addItem("----- " + eventShortname_ + " " + + protocol + " -----"); if (createTaskFlag) createTasks.add(null); - if (VSRegisteredEvents.isOnServerStartProtocol(eventClassname)) eventShortname = eventShortname_ + " " + serverRequest; else @@ -987,7 +904,8 @@ public class VSSimulator extends JPanel { eventShortname = eventShortname_ + " " + client + " " + activate; comboBox.addItem(eventShortname); if (createTaskFlag) { - VSCreateTask createTask = new VSCreateTask(protocolEventClassname); + VSCreateTask createTask = + new VSCreateTask(protocolEventClassname); createTask.isProtocolActivation(true); createTask.isClientProtocol(true); createTask.setProtocolClassname(eventClassname); @@ -998,7 +916,8 @@ public class VSSimulator extends JPanel { eventShortname = eventShortname_ + " " + client + " " + deactivate; comboBox.addItem(eventShortname); if (createTaskFlag) { - VSCreateTask createTask = new VSCreateTask(protocolEventClassname); + VSCreateTask createTask = + new VSCreateTask(protocolEventClassname); createTask.isProtocolDeactivation(true); createTask.isClientProtocol(true); createTask.setProtocolClassname(eventClassname); @@ -1009,7 +928,8 @@ public class VSSimulator extends JPanel { eventShortname = eventShortname_ + " " + server + " " + activate; comboBox.addItem(eventShortname); if (createTaskFlag) { - VSCreateTask createTask = new VSCreateTask(protocolEventClassname); + VSCreateTask createTask = + new VSCreateTask(protocolEventClassname); createTask.isProtocolActivation(true); createTask.isClientProtocol(false); createTask.setProtocolClassname(eventClassname); @@ -1020,7 +940,8 @@ public class VSSimulator extends JPanel { eventShortname = eventShortname_ + " " + server + " " + deactivate; comboBox.addItem(eventShortname); if (createTaskFlag) { - VSCreateTask createTask = new VSCreateTask(protocolEventClassname); + VSCreateTask createTask = + new VSCreateTask(protocolEventClassname); createTask.isProtocolDeactivation(true); createTask.isClientProtocol(false); createTask.setProtocolClassname(eventClassname); @@ -1075,7 +996,8 @@ public class VSSimulator extends JPanel { /** * Gets the concerned processes. * - * @param localTasks the local tasks + * @param localTasks true, if this table manages the local tasks. false + * if this table manages the global tasks. * * @return the concerned processes */ @@ -1099,12 +1021,14 @@ public class VSSimulator extends JPanel { public void updateTaskManagerTable() { VSProcess process = getSelectedProcess(); boolean allProcesses = process == null; - taskManagerLocalModel.set(process, VSTaskManagerTableModel.LOCAL, allProcesses); - taskManagerGlobalModel.set(process, VSTaskManagerTableModel.GLOBAL, allProcesses); + taskManagerLocalModel.set(process, + VSTaskManagerTableModel.LOCAL, allProcesses); + taskManagerGlobalModel.set(process, + VSTaskManagerTableModel.GLOBAL, allProcesses); } /** - * Finish. + * The simulation has finished. */ public void finish() { menuItemStates.setStart(false); @@ -1159,7 +1083,7 @@ public class VSSimulator extends JPanel { } /** - * Removes the process at index. + * Removes the process at a specified index. * * @param index the index */ @@ -1180,7 +1104,7 @@ public class VSSimulator extends JPanel { } /** - * Adds the process at index. + * Adds the process at a specified index. * * @param index the index */ @@ -1199,7 +1123,7 @@ public class VSSimulator extends JPanel { } /** - * Fire expert mode changed. + * Fire expert mode changed. Tell, that the expert mode has changed. */ public void fireExpertModeChanged() { boolean expertMode = prefs.getBoolean("sim.mode.expert"); @@ -1209,7 +1133,8 @@ public class VSSimulator extends JPanel { if (expertMode) { tabbedPane.remove(localPanel); - tabbedPane.insertTab(prefs.getString("lang.events"), null, splitPane1, null, 0); + tabbedPane.insertTab(prefs.getString("lang.events"), null, + splitPane1, null, 0); splitPane1.setTopComponent(localPanel); //splitPane1.setDividerLocation((int) (getPaintSize()/2) - 20); @@ -1218,7 +1143,8 @@ public class VSSimulator extends JPanel { } else { tabbedPane.remove(splitPane1); - tabbedPane.insertTab(prefs.getString("lang.events"), null, localPanel, null, 0); + tabbedPane.insertTab(prefs.getString("lang.events"), null, + localPanel, null, 0); /* addPanel */ localAddPanel.remove(2); diff --git a/sources/simulator/VSSimulatorCanvas.java b/sources/simulator/VSSimulatorCanvas.java index 95d7e2c..924c08f 100644 --- a/sources/simulator/VSSimulatorCanvas.java +++ b/sources/simulator/VSSimulatorCanvas.java @@ -18,11 +18,17 @@ import events.internal.*; import prefs.*; import prefs.editors.*; -// TODO: Auto-generated Javadoc /** - * The Class VSSimulatorCanvas. + * The Class VSSimulatorCanvas. An instance of this object represents the + * graphical paint area of a simulation. It contains all graphic calculations. + * Also the simulation thread takes place in this class in a loop! This class + * is probably the most cryptic of the whole simulator source code. This is + * this way in order to gain more performance of the painting area! + * + * @author Paul C. Buetow */ -public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionListener, MouseListener, HierarchyBoundsListener { +public class VSSimulatorCanvas extends Canvas implements Runnable { + /** The serial version uid */ private static final long serialVersionUID = 1L; /** The highlighted process. */ @@ -46,31 +52,31 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi /** The thread sleep. */ private int threadSleep; - /** The until time. */ + /** The until time. Until then goes the simulation? */ private long untilTime; - /** The is paused. */ + /** The simulation is paused. */ private volatile boolean isPaused = true; - /** The is thread stopped. */ - private volatile boolean isThreadStopped = false; + /** The simulation thread is stopped. */ + private volatile boolean hasThreadStopped = false; - /** The is finished. */ + /** The simulation is finished. */ private volatile boolean isFinished = false; - /** The is resetted. */ + /** The simulation is resetted. */ private volatile boolean isResetted = false; - /** The is anti aliased. */ + /** The simulation is anti aliased. */ private volatile boolean isAntiAliased = false; - /** The is anti aliased changed. */ + /** The simulation's anti aliasing has changed. */ private volatile boolean isAntiAliasedChanged = false; - /** The show lamport. */ + /** The simulation shows the lamport time. */ private volatile boolean showLamport = false; - /** The show vector time. */ + /** The simulation shows the vector time. */ private volatile boolean showVectorTime = false; /** The pause time. */ @@ -79,10 +85,10 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi /** The start time. */ private volatile long startTime; - /** The time. */ + /** The global time. */ private volatile long time; - /** The last time. */ + /** The last global time. */ private volatile long lastTime; /** The task manager. */ @@ -103,14 +109,49 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi /** The simulation time. */ private long simulationTime; + /** The x paint size. */ + double xPaintSize; + + /** The paint size. */ + double paintSize; + + /** The y distance. */ + double yDistance; + + /** The global time x position. */ + double globalTimeXPosition; + + /** The xoffset_plus_xpaintsize. */ + int xoffset_plus_xpaintsize; + + /** The xpaintsize_dividedby_untiltime. */ + double xpaintsize_dividedby_untiltime; + + /** The paint processes offset. */ + int paintProcessesOffset; + + /** The paint secondlines seconds. */ + int paintSecondlinesSeconds; + + /** The paint secondlines line. */ + int paintSecondlinesLine[] = new int[4]; + + /** The paint secondlines y string pos1. */ + int paintSecondlinesYStringPos1; + + /** The paint secondlines y string pos2. */ + int paintSecondlinesYStringPos2; + + /** The paint global time y position. */ + int paintGlobalTimeYPosition; + /* GFX buffering */ - /** The strategy. */ + /** The strategy for buffering. */ private BufferStrategy strategy; - /** The g. */ + /** The graphics object to paint at. */ private Graphics2D g; - /* Static constats */ /** The Constant LINE_WIDTH. */ private static final int LINE_WIDTH = 5; @@ -161,9 +202,14 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi private long messageLineCounter; /** - * The Class VSMessageLine. + * The Class VSMessageLine. An object of this class represents a message + * line drawn into the painting area. + * + * @author Paul C. Buetow */ public class VSMessageLine { + /** The serial version uid */ + private static final long serialVersionUID = 1L; /** The receiver process. */ private VSProcess receiverProcess; @@ -189,10 +235,10 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi /** The offset2. */ private int offset2; - /** The is arrived. */ + /** The message has arrived. */ private boolean isArrived; - /** The is lost. */ + /** The message is lost. */ private boolean isLost; /** The x1. */ @@ -226,7 +272,7 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi private VSTask task; /** - * Instantiates a new lang.process.removemessage line. + * Instantiates a new VSMessageLine object. * * @param receiverProcess the receiver process * @param sendTime the send time @@ -284,9 +330,9 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Draw. + * Draws the message line. * - * @param g the g + * @param g the grpahics object to draw at * @param globalTime the global time */ public void draw(final Graphics2D g, final long globalTime) { @@ -320,11 +366,13 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Removes the process at index. + * Called if a process within the simulation has been removed at a + * specified index. * * @param index the index * - * @return true, if successful + * @return true, if the sender or the receiver of the message has been + * removed from the simulation. Else false is returned. */ public boolean removedAProcessAtIndex(int index) { if (index == receiverNum || index == senderNum) @@ -351,11 +399,12 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Equals. + * Checks one line to another if they equal (have the same message line + * id) * - * @param line the line + * @param line the line to compare against * - * @return true, if successful + * @return true, if they equal */ public boolean equals(VSMessageLine line) { return messageLineNum == line.getMessageLineNum(); @@ -372,13 +421,14 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Instantiates a new lang.process.removesimulator canvas. + * Instantiates a new VSSimulatorCanvas object. * * @param prefs the prefs * @param simulation the simulation * @param logging the logging */ - public VSSimulatorCanvas(VSPrefs prefs, VSSimulator simulation, VSLogging logging) { + public VSSimulatorCanvas(VSPrefs prefs, VSSimulator simulation, + VSLogging logging) { this.prefs = prefs; this.simulation = simulation; this.logging = logging; @@ -393,50 +443,167 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi for (int i = 0; i < numProcesses; ++i) processes.add(createProcess(i)); - addMouseListener(this); - addMouseMotionListener(this); - addHierarchyBoundsListener(this); - } + final VSPrefs finalPrefs = prefs; + addMouseListener(new MouseListener() { + public void mouseClicked(MouseEvent me) { + final VSProcess process = getProcessAtYPos(me.getY()); + + if (SwingUtilities.isRightMouseButton(me)) { + ActionListener actionListener = new ActionListener() { + public void actionPerformed(ActionEvent ae) { + String command = ae.getActionCommand(); + if (command.equals( + finalPrefs.getString("lang.process.edit"))) { + editProcess(process); + + } else if (command.equals( + finalPrefs.getString("lang.process.crash"))) { + VSAbstractEvent event = + new VSProcessCrashEvent(); + + taskManager.addTask(new VSTask( + process.getGlobalTime(), + process, event, + VSTask.GLOBAL)); + + } else if (command.equals( + finalPrefs.getString("lang.process.recover"))) { + VSAbstractEvent event = + new VSProcessRecoverEvent(); + + taskManager.addTask(new VSTask( + process.getGlobalTime(), + process, event, + VSTask.GLOBAL)); + + } else if (command.equals( + finalPrefs.getString("lang.process.remove"))) { + removeProcess(process); + + } else if (command.equals( + finalPrefs.getString("lang.process.add.new"))) { + addProcess(); + } + } + }; + + JPopupMenu popup = new JPopupMenu(); + JMenuItem item = new JMenuItem( + finalPrefs.getString("lang.process.edit")); + if (process == null) + item.setEnabled(false); + else + item.addActionListener(actionListener); + popup.add(item); - /** The x paint size. */ - double xPaintSize; + item = new JMenuItem( + finalPrefs.getString("lang.process.crash")); - /** The paint size. */ - double paintSize; + if (process == null || process.isCrashed() || isPaused || + time == 0 || isFinished) + item.setEnabled(false); + else + item.addActionListener(actionListener); + popup.add(item); - /** The y distance. */ - double yDistance; + item = new JMenuItem( + finalPrefs.getString("lang.process.recover")); - /** The global time x position. */ - double globalTimeXPosition; + if (process == null || !process.isCrashed() || isPaused || + time == 0 || isFinished) + item.setEnabled(false); + else + item.addActionListener(actionListener); + popup.add(item); - /** The xoffset_plus_xpaintsize. */ - int xoffset_plus_xpaintsize; + item = new JMenuItem( + finalPrefs.getString("lang.process.remove")); - /** The xpaintsize_dividedby_untiltime. */ - double xpaintsize_dividedby_untiltime; + if (process == null) + item.setEnabled(false); + else + item.addActionListener(actionListener); + popup.add(item); - /** The paint processes offset. */ - int paintProcessesOffset; + popup.addSeparator(); - /** The paint secondlines seconds. */ - int paintSecondlinesSeconds; + item = new JMenuItem( + finalPrefs.getString("lang.process.add.new")); - /** The paint secondlines line. */ - int paintSecondlinesLine[] = new int[4]; + item.addActionListener(actionListener); + popup.add(item); - /** The paint secondlines y string pos1. */ - int paintSecondlinesYStringPos1; - /** The paint secondlines y string pos2. */ - int paintSecondlinesYStringPos2; + popup.show(me.getComponent(), me.getX(), me.getY()); - /** The paint global time y position. */ - int paintGlobalTimeYPosition; + } else { + editProcess(process); + } + } + + public void mouseExited(MouseEvent e) { + if (highlightedProcess != null) { + highlightedProcess.highlightOff(); + highlightedProcess = null; + paint(); + } + } + + public void mouseEntered(MouseEvent e) { } + + public void mousePressed(MouseEvent e) { } + + public void mouseReleased(MouseEvent e) { } + + }); + addMouseMotionListener(new MouseMotionListener() { + public void mouseDragged(MouseEvent e) { } + + public void mouseMoved(MouseEvent e) { + VSProcess p = getProcessAtYPos(e.getY()); + + if (p == null) { + if (highlightedProcess != null) { + highlightedProcess.highlightOff(); + highlightedProcess = null; + } + + if (isPaused) + paint(); + + return; + } + + if (highlightedProcess != null) { + if (highlightedProcess.getProcessID() != p.getProcessID()) { + highlightedProcess.highlightOff(); + highlightedProcess = p; + p.highlightOn(); + } + } else { + highlightedProcess = p; + p.highlightOn(); + } + + if (isPaused) + paint(); + } + + }); + + addHierarchyBoundsListener(new HierarchyBoundsListener() { + public void ancestorMoved(HierarchyEvent e) { } + + public void ancestorResized(HierarchyEvent e) { + recalcOnChange(); + } + }); + } - /* This method contains very ugly code. But this has to be in order to gain performance! */ /** - * Recalc on change. + * This method gets called if the window border of the simulator canvas + * has changed. This method contains very ugly code. But this has to be in + * order to gain performance! */ private void recalcOnChange() { processlineColor = prefs.getColor("col.process.line"); @@ -448,8 +615,10 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi backgroundColor = prefs.getColor("col.background"); paintSize = simulation.getPaintSize(); - xPaintSize = simulation.getWidth() - (3 * XOFFSET + simulation.getSplitSize()); - yDistance = (simulation.getPaintSize() - 2 * (YOFFSET + YOUTER_SPACEING))/ numProcesses; + xPaintSize = simulation.getWidth() - + (3 * XOFFSET + simulation.getSplitSize()); + yDistance = (simulation.getPaintSize() - + 2 * (YOFFSET + YOUTER_SPACEING))/ numProcesses; xpaintsize_dividedby_untiltime = xPaintSize / (double) untilTime; for (VSMessageLine messageLine : messageLines) @@ -459,14 +628,20 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi { xoffset_plus_xpaintsize = XOFFSET + (int) xPaintSize; if (numProcesses > 1) - paintProcessesOffset = (int) ((paintSize-2*(YOFFSET+YOUTER_SPACEING+YSEPLINE_SPACEING))/(numProcesses-1)); + paintProcessesOffset = + (int) ((paintSize-2* (YOFFSET+ + YOUTER_SPACEING+YSEPLINE_SPACEING))/ + (numProcesses-1)); else - paintProcessesOffset = (int) ((paintSize-2*(YOFFSET+YOUTER_SPACEING+YSEPLINE_SPACEING))); + paintProcessesOffset = + (int) ((paintSize-2*(YOFFSET+ + YOUTER_SPACEING+YSEPLINE_SPACEING))); } /* paintSecondlines optimization, precalc things */ { - int yMax = YOFFSET + YOUTER_SPACEING + (int) (numProcesses * yDistance); + int yMax = YOFFSET + YOUTER_SPACEING + + (int) (numProcesses * yDistance); paintSecondlinesSeconds = (int) untilTime / 1000; paintSecondlinesLine[1] = YOFFSET; paintSecondlinesLine[3] = yMax; @@ -476,7 +651,8 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi /* paitnGlobalTime optimization, precalc things */ { - paintGlobalTimeYPosition = YOFFSET + YOUTER_SPACEING + (int) (numProcesses * yDistance); + paintGlobalTimeYPosition = YOFFSET + YOUTER_SPACEING + + (int) (numProcesses * yDistance); } if (strategy != null) { @@ -484,22 +660,23 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi g = (Graphics2D) strategy.getDrawGraphics(); g.setColor(backgroundColor); if (isAntiAliased) - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); } } } /** - * Update simulation. + * Updates the simulation. * * @param globalTime the global time * @param lastGlobalTime the last global time */ - private void updateSimulation(final long globalTime, final long lastGlobalTime) { + private void updateSimulation(long globalTime, long lastGlobalTime) { if (isPaused || isFinished) return; - final long lastSimulationTime = simulationTime; + long lastSimulationTime = simulationTime; long offset = globalTime - lastGlobalTime; clockOffset += offset * clockSpeed; @@ -524,7 +701,7 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Paint. + * Paints the simulation. */ public void paint() { while (getBufferStrategy() == null) { @@ -540,14 +717,16 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi synchronized (strategy) { if (isAntiAliasedChanged) { if (isAntiAliased) - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); else - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_OFF); isAntiAliasedChanged = false; } g.fillRect(0, 0, getWidth(), getHeight()); - final long globalTime = simulationTime; + long globalTime = simulationTime; globalTimeXPosition = getTimeXPosition(globalTime); paintSecondlines(g); @@ -560,15 +739,14 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } g.setColor(backgroundColor); - strategy.show(); } } /** - * Paint processes. + * Paints the processes. * - * @param g the g + * @param g the graphics object * @param globalTime the global time */ private void paintProcesses(Graphics2D g, long globalTime) { @@ -576,8 +754,12 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi * Second paint the processes */ final int yOffset = YOFFSET + YOUTER_SPACEING + YSEPLINE_SPACEING; - final int xPoints[] = { XOFFSET, xoffset_plus_xpaintsize, xoffset_plus_xpaintsize, XOFFSET, XOFFSET }; - final int yPoints[] = { yOffset, yOffset, yOffset + LINE_WIDTH, yOffset + LINE_WIDTH, yOffset }; + final int xPoints[] = { XOFFSET, xoffset_plus_xpaintsize, + xoffset_plus_xpaintsize, XOFFSET, XOFFSET + }; + final int yPoints[] = { yOffset, yOffset, yOffset + LINE_WIDTH, + yOffset + LINE_WIDTH, yOffset + }; synchronized (processes) { for (VSProcess process : processes) { @@ -592,23 +774,28 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi final int length = crashHistory.length; for (int i = 0; i < length; i += 2) { - final int crashStartPos = (int) getTimeXPosition(crashHistory[i].longValue()); + final int crashStartPos = + (int) getTimeXPosition( + crashHistory[i].longValue()); int crashEndPos; if (i == length - 1) crashEndPos = xoffset_plus_xpaintsize; else - crashEndPos = (int) getTimeXPosition(crashHistory[i+1].longValue()); + crashEndPos = (int) getTimeXPosition( + crashHistory[i+1].longValue()); - final int xPointsCrashed[] = { crashStartPos, crashEndPos, - crashEndPos, crashStartPos, crashStartPos - }; + final int xPointsCrashed[] = { + crashStartPos, crashEndPos, + crashEndPos, crashStartPos, crashStartPos + }; g.fillPolygon(xPointsCrashed, yPoints, 5); } } g.setColor(process.getColor()); - g.drawString("P" + process.getProcessID() + ":", XOFFSET - 30, yPoints[0] + LINE_WIDTH); + g.drawString("P" + process.getProcessID() + ":", XOFFSET - 30, + yPoints[0] + LINE_WIDTH); final long tmp = localTime > untilTime ? untilTime : localTime; final int xPos = 1 + (int) getTimeXPosition(tmp); @@ -620,9 +807,11 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi g.drawString(localTime+"ms", xPos + 2, yStart + TEXT_SPACEING); if (showLamport) - paintTime(g, process.getLamportTimeArray(), process, yStart, 25); + paintTime(g, process.getLamportTimeArray(), process, + yStart, 25); else if (showVectorTime) - paintTime(g, process.getVectorTimeArray(), process, yStart, 20 * numProcesses); + paintTime(g, process.getVectorTimeArray(), process, + yStart, 20 * numProcesses); for (int i = 0; i < 5; ++i) yPoints[i] += paintProcessesOffset; @@ -631,16 +820,17 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Paint time. + * Paints the time. (e.g. lamport time or vector time) * - * @param g the g + * @param g the graphics object * @param times the times * @param process the process * @param yStart the y start * @param distance the distance */ - private void paintTime(final Graphics2D g, final VSTime times[], final VSProcess process, - final int yStart, final int distance) { + private void paintTime(final Graphics2D g, final VSTime times[], + final VSProcess process, final int yStart, + final int distance) { final int lastPos[] = { -1, -1, -1, -1 }; @@ -673,23 +863,26 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi if (bestRow[1] != -1) xPos += distance - bestRow[1]; - g.drawString(time.toString(), xPos + 2, yStart + 3 * TEXT_SPACEING + row * ROW_HEIGHT); + g.drawString(time.toString(), xPos + 2, yStart + 3 * + TEXT_SPACEING + row * ROW_HEIGHT); lastPos[row] = xPos; } } /** - * Paint secondlines. + * Paint the second lines. * - * @param g the g + * @param g the graphics object */ private void paintSecondlines(Graphics2D g) { g.setColor(processSecondlineColor); int i; for (i = 0; i <= paintSecondlinesSeconds; i += secondsSpaceing) { - paintSecondlinesLine[0] = paintSecondlinesLine[2] = (int) getTimeXPosition(i*1000); - g.drawLine(paintSecondlinesLine[0], paintSecondlinesLine[1], paintSecondlinesLine[2], paintSecondlinesLine[3]); + paintSecondlinesLine[0] = paintSecondlinesLine[2] = + (int) getTimeXPosition(i*1000); + g.drawLine(paintSecondlinesLine[0], paintSecondlinesLine[1], + paintSecondlinesLine[2], paintSecondlinesLine[3]); final int xStringPos = paintSecondlinesLine[0] - 5; g.drawString(i+"s", xStringPos, paintSecondlinesYStringPos1); @@ -698,31 +891,39 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } if (i > paintSecondlinesSeconds) { - paintSecondlinesLine[0] = paintSecondlinesLine[2] = (int) getTimeXPosition(untilTime); - g.drawLine(paintSecondlinesLine[0], paintSecondlinesLine[1], paintSecondlinesLine[2], paintSecondlinesLine[3]); + paintSecondlinesLine[0] = paintSecondlinesLine[2] = + (int) getTimeXPosition(untilTime); + g.drawLine(paintSecondlinesLine[0], paintSecondlinesLine[1], + paintSecondlinesLine[2], paintSecondlinesLine[3]); } } /** - * Paint global time. + * Paints the global time. * - * @param g the g + * @param g the graphics object * @param globalTime the global time */ private void paintGlobalTime(Graphics2D g, long globalTime) { g.setColor(processSeplineColor); final int xOffset = (int) globalTimeXPosition; - final int xPoints[] = { xOffset, xOffset + SEPLINE_WIDTH, xOffset + SEPLINE_WIDTH, xOffset, xOffset }; + final int xPoints[] = { + xOffset, xOffset + SEPLINE_WIDTH, + xOffset + SEPLINE_WIDTH, xOffset, xOffset + }; final int yOffset = YOFFSET - 8; - final int yPoints[] = { yOffset, yOffset, paintGlobalTimeYPosition, paintGlobalTimeYPosition, yOffset }; + final int yPoints[] = { + yOffset, yOffset, paintGlobalTimeYPosition, + paintGlobalTimeYPosition, yOffset + }; g.fillPolygon(xPoints, yPoints, 5); g.drawString(globalTime+"ms", xPoints[1]+1, yPoints[0]+TEXT_SPACEING); } /** - * Gets the process at y pos. + * Gets the process at a specified y pos. * * @param yPos the y pos * @@ -733,12 +934,15 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi int y = YOFFSET + YOUTER_SPACEING + YSEPLINE_SPACEING; int yOffset = numProcesses > 1 - ? (int) ((paintSize-2*(YOFFSET+YOUTER_SPACEING+YSEPLINE_SPACEING))/(numProcesses-1)) - : (int) ((paintSize-2*(YOFFSET+YOUTER_SPACEING+YSEPLINE_SPACEING))); + ? (int) ((paintSize-2* + (YOFFSET+YOUTER_SPACEING+YSEPLINE_SPACEING))/ + (numProcesses-1)) + : (int) ((paintSize-2* + (YOFFSET+YOUTER_SPACEING+YSEPLINE_SPACEING))); - //System.out.println("JO " + yPos + " " + yDistance + " " + yOffset); for (int i = 0; i < numProcesses; ++i) { - if (yPos < y + reachDistance && yPos > y - reachDistance - LINE_WIDTH) + if (yPos < y + reachDistance && yPos > y - reachDistance - + LINE_WIDTH) return processes.get(i); y += yOffset; } @@ -760,7 +964,7 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi /** * Gets the process y position. * - * @param i the i + * @param i the process num * * @return the process y position */ @@ -769,7 +973,8 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi if (numProcesses > 1) y = (int) ((paintSize - - 2 * (YOFFSET + YOUTER_SPACEING + YSEPLINE_SPACEING))/ (numProcesses-1)); + 2 * (YOFFSET + YOUTER_SPACEING + YSEPLINE_SPACEING))/ + (numProcesses-1)); else y = (int) ((paintSize - 2 * (YOFFSET + YOUTER_SPACEING + YSEPLINE_SPACEING))); @@ -814,18 +1019,18 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Gets the num processes. + * Gets the num of processes. * - * @return the num processes + * @return the num of processes */ public int getNumProcesses() { return numProcesses; } /** - * Gets the process. + * Gets the specified process. * - * @param processNum the process num + * @param processNum the process num to get the process of * * @return the process */ @@ -841,19 +1046,21 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi */ public void run() { while (true) { - while (!isThreadStopped && (isPaused || isFinished || isResetted)) { + while (!hasThreadStopped && (isPaused || isFinished || + isResetted)) { try { Thread.sleep(100); paint(); + } catch (Exception e) { System.out.println(e); } } - if (isThreadStopped) + if (hasThreadStopped) break; /* Exit the thread */ - while (!isPaused && !isThreadStopped) { + while (!isPaused && !hasThreadStopped) { try { Thread.sleep(threadSleep); } catch (Exception e) { @@ -870,7 +1077,6 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi paint(); lastTime = time; time = System.currentTimeMillis() - startTime; - } updateSimulation(time, lastTime); @@ -879,7 +1085,7 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Play. + * Starts/plays the simulation. */ public void play() { logging.logg(prefs.getString("lang.simulation.started")); @@ -910,7 +1116,7 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Finish. + * Called if the simulation has finished. */ public void finish() { synchronized (processes) { @@ -920,13 +1126,12 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi simulation.finish(); isFinished = true; - logging.logg(prefs.getString("lang.simulation.finished")); paint(); } /** - * Pause. + * Call this, in order to pause the simulation. */ public void pause() { isPaused = true; @@ -936,13 +1141,12 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } pauseTime = System.currentTimeMillis(); - logging.logg(prefs.getString("lang.simulation.paused")); paint(); } /** - * Reset. + * Call this, in order to reset the simulation. */ public void reset() { if (!isResetted) { @@ -962,7 +1166,8 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi process.reset(); } - /* Reset the task manager AFTER the processes, for the programmed tasks */ + /* Reset the task manager AFTER the processes, for the programmed + tasks */ taskManager.reset(); synchronized (processes) { @@ -980,25 +1185,26 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Stop thread. + * Stops the thread of the simulation. */ public void stopThread() { - isThreadStopped = true; + hasThreadStopped = true; } /** - * Checks if is thread stopped. + * Checks if the thread has been stopped. * - * @return true, if is thread stopped + * @return true, if is thread has stopped */ - public boolean isThreadStopped() { - return isThreadStopped; + public boolean hasThreadStopped() { + return hasThreadStopped; } /** - * Show lamport. + * Sets, if the the lamport time should be shown. It implicitly disables + * the vector time. * - * @param showLamport the show lamport + * @param showLamport true, if the lamport time should be shown */ public void showLamport(boolean showLamport) { this.showLamport = showLamport; @@ -1007,9 +1213,10 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Show vector time. + * Sets, if the vector time should be shown. It implicitly disables the + * lamport time. * - * @param showVectorTime the show vector time + * @param showVectorTime true, if the vector time should be shown */ public void showVectorTime(boolean showVectorTime) { this.showVectorTime = showVectorTime; @@ -1018,9 +1225,9 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Checks if is anti aliased. + * Sets if the simulation graphics are anti aliased. * - * @param isAntiAliased the is anti aliased + * @param isAntiAliased true, if the simulation is anti aliased */ public void isAntiAliased(boolean isAntiAliased) { this.isAntiAliased = isAntiAliased; @@ -1030,7 +1237,7 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Send message. + * Sends a message. * * @param message the message */ @@ -1054,7 +1261,8 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } else { durationTime = sendingProcess.getDurationTime(); - deliverTime = sendingProcess.getGlobalTime() + durationTime; + deliverTime = sendingProcess.getGlobalTime() + + durationTime; if (prefs.getBoolean("sim.message.prob.mean")) outageTime = sendingProcess.getARandomMessageOutageTime( @@ -1086,80 +1294,6 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } } - /* (non-Javadoc) - * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent) - */ - public void mouseClicked(MouseEvent me) { - final VSProcess process = getProcessAtYPos(me.getY()); - - if (SwingUtilities.isRightMouseButton(me)) { - ActionListener actionListener = new ActionListener() { - public void actionPerformed(ActionEvent ae) { - String actionCommand = ae.getActionCommand(); - if (actionCommand.equals(prefs.getString("lang.process.edit"))) { - editProcess(process); - - } else if (actionCommand.equals(prefs.getString("lang.process.crash"))) { - VSAbstractEvent event = new VSProcessCrashEvent(); - taskManager.addTask(new VSTask(process.getGlobalTime(), process, event, VSTask.GLOBAL)); - - } else if (actionCommand.equals(prefs.getString("lang.process.recover"))) { - VSAbstractEvent event = new VSProcessRecoverEvent(); - taskManager.addTask(new VSTask(process.getGlobalTime(), process, event, VSTask.GLOBAL)); - - } else if (actionCommand.equals(prefs.getString("lang.process.remove"))) { - removeProcess(process); - - } else if (actionCommand.equals(prefs.getString("lang.process.add.new"))) { - addProcess(); - } - } - }; - - - JPopupMenu popup = new JPopupMenu(); - JMenuItem item = new JMenuItem(prefs.getString("lang.process.edit")); - if (process == null) - item.setEnabled(false); - else - item.addActionListener(actionListener); - popup.add(item); - - item = new JMenuItem(prefs.getString("lang.process.crash")); - if (process == null || process.isCrashed() || isPaused || time == 0 || isFinished) - item.setEnabled(false); - else - item.addActionListener(actionListener); - popup.add(item); - - item = new JMenuItem(prefs.getString("lang.process.recover")); - if (process == null || !process.isCrashed() || isPaused || time == 0 || isFinished) - item.setEnabled(false); - else - item.addActionListener(actionListener); - popup.add(item); - - item = new JMenuItem(prefs.getString("lang.process.remove")); - if (process == null) - item.setEnabled(false); - else - item.addActionListener(actionListener); - popup.add(item); - - popup.addSeparator(); - - item = new JMenuItem(prefs.getString("lang.process.add.new")); - item.addActionListener(actionListener); - popup.add(item); - - - popup.show(me.getComponent(), me.getX(), me.getY()); - - } else { - editProcess(process); - } - } - /** * Edits the process. * @@ -1183,85 +1317,6 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } } - /* (non-Javadoc) - * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent) - */ - public void mouseEntered(MouseEvent e) { } - - /* (non-Javadoc) - * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent) - */ - public void mouseExited(MouseEvent e) { - if (highlightedProcess != null) { - highlightedProcess.highlightOff(); - highlightedProcess = null; - paint(); - } - } - - /* (non-Javadoc) - * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent) - */ - public void mousePressed(MouseEvent e) { - } - - /* (non-Javadoc) - * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent) - */ - public void mouseReleased(MouseEvent e) { - } - - /* (non-Javadoc) - * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent) - */ - public void mouseDragged(MouseEvent e) { - } - - /* (non-Javadoc) - * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent) - */ - public void mouseMoved(MouseEvent e) { - VSProcess p = getProcessAtYPos(e.getY()); - - if (p == null) { - if (highlightedProcess != null) { - highlightedProcess.highlightOff(); - highlightedProcess = null; - } - - if (isPaused) - paint(); - - return; - } - - if (highlightedProcess != null) { - if (highlightedProcess.getProcessID() != p.getProcessID()) { - highlightedProcess.highlightOff(); - highlightedProcess = p; - p.highlightOn(); - } - } else { - highlightedProcess = p; - p.highlightOn(); - } - - if (isPaused) - paint(); - } - - /* (non-Javadoc) - * @see java.awt.event.HierarchyBoundsListener#ancestorMoved(java.awt.event.HierarchyEvent) - */ - public void ancestorMoved(HierarchyEvent e) { } - - /* (non-Javadoc) - * @see java.awt.event.HierarchyBoundsListener#ancestorResized(java.awt.event.HierarchyEvent) - */ - public void ancestorResized(HierarchyEvent e) { - recalcOnChange(); - } - /** * Gets the processes array. * @@ -1279,7 +1334,8 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Update from prefs. + * Updates from the prefs. Called by the VSSimulatorEditor if values + * have been saved. */ public void updateFromPrefs() { untilTime = prefs.getInteger("sim.seconds") * 1000; @@ -1297,7 +1353,7 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Removes the message line. + * Removes a specific message line from the painting area. * * @param messageLine the message line to remove */ @@ -1308,7 +1364,7 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Removes the process. + * Removes a process from the simulation. * * @param process the process */ @@ -1352,11 +1408,11 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Creates the process. + * Creates a process with the specified num. * * @param processNum the process num * - * @return the lang.process.removeprocess + * @return the new process */ private VSProcess createProcess(int processNum) { VSProcess process = new VSProcess(prefs, processNum, this, logging); @@ -1365,7 +1421,7 @@ public class VSSimulatorCanvas extends Canvas implements Runnable, MouseMotionLi } /** - * Adds the process. + * Adds a new process to the simulation. */ private void addProcess() { numProcesses = processes.size() + 1; diff --git a/sources/simulator/VSSimulatorFrame.java b/sources/simulator/VSSimulatorFrame.java index 14e4136..bf68fbd 100644 --- a/sources/simulator/VSSimulatorFrame.java +++ b/sources/simulator/VSSimulatorFrame.java @@ -15,11 +15,15 @@ import prefs.*; import prefs.editors.*; import utils.*; -// TODO: Auto-generated Javadoc /** - * The Class VSSimulatorFrame. + * The Class VSSimulatorFrame. An object of this class represents a window + * of the simulator. The window can have several tabs. Each tab contains + * an independent simulation. + * + * @author Paul C. Buetow */ -public class VSSimulatorFrame extends VSFrame implements ActionListener { +public class VSSimulatorFrame extends VSFrame { + /** The serial version uid */ private static final long serialVersionUID = 1L; /** The pause item. */ @@ -69,19 +73,110 @@ public class VSSimulatorFrame extends VSFrame implements ActionListener { /** The tabbed pane. */ private JTabbedPane tabbedPane; - //private JSlider speedSlider; + + /** The action listener */ + private ActionListener actionListener; /** - * Instantiates a new lang.process.removesimulator frame. + * Instantiates a new VSSimulatorFrame object. * * @param prefs the prefs - * @param relativeTo the relative to + * @param relativeTo the component to open the window relative to */ public VSSimulatorFrame(VSPrefs prefs, Component relativeTo) { super(prefs.getString("lang.name"), relativeTo); this.prefs = prefs; this.simulations = new Vector<VSSimulator>(); + final VSPrefs finalPrefs = this.prefs; + actionListener = new ActionListener() { + public void actionPerformed(ActionEvent e) { + Object source = e.getSource(); + String sourceText = null; + + if (source instanceof JMenuItem) + sourceText = ((JMenuItem) source).getText(); + else + sourceText = ((ImageIcon) ((JButton) source).getIcon()). + getDescription(); + + if (sourceText.equals( + finalPrefs.getString("lang.simulation.close"))) { + removeCurrentSimulation(); + + } else if (sourceText.equals( + finalPrefs.getString("lang.simulation.new"))) { + VSPrefs newPrefs = VSDefaultPrefs.init(); + VSSimulatorEditor simulatorEditor = + new VSSimulatorEditor(newPrefs, VSSimulatorFrame.this); + new VSEditorFrame(newPrefs, VSSimulatorFrame.this, + simulatorEditor); + + } else if (sourceText.equals( + finalPrefs.getString("lang.window.new"))) { + new VSMain(VSDefaultPrefs.init(), + VSSimulatorFrame.this); + + } else if (sourceText.equals( + finalPrefs.getString("lang.window.close"))) { + dispose(); + + } else if (sourceText.equals( + finalPrefs.getString("lang.about"))) { + new VSAbout(finalPrefs, VSSimulatorFrame.this); + + } else if (sourceText.equals( + finalPrefs.getString("lang.quit"))) { + System.exit(0); + + } else if (sourceText.equals( + finalPrefs.getString("lang.start"))) { + VSMenuItemStates menuItemState = + currentSimulation.getMenuItemStates(); + menuItemState.setStart(false); + menuItemState.setPause(true); + menuItemState.setReset(false); + menuItemState.setReplay(true); + currentSimulation.getSimulatorCanvas().play(); + updateSimulationMenu(); + + } else if (sourceText.equals( + finalPrefs.getString("lang.pause"))) { + VSMenuItemStates menuItemState = + currentSimulation.getMenuItemStates(); + menuItemState.setStart(true); + menuItemState.setPause(false); + menuItemState.setReset(true); + menuItemState.setReplay(true); + currentSimulation.getSimulatorCanvas().pause(); + updateSimulationMenu(); + + } else if (sourceText.equals( + finalPrefs.getString("lang.reset"))) { + VSMenuItemStates menuItemState = + currentSimulation.getMenuItemStates(); + menuItemState.setStart(true); + menuItemState.setPause(false); + menuItemState.setReset(false); + menuItemState.setReplay(false); + currentSimulation.getSimulatorCanvas().reset(); + updateSimulationMenu(); + + } else if (sourceText.equals( + finalPrefs.getString("lang.replay"))) { + VSMenuItemStates menuItemState = + currentSimulation.getMenuItemStates(); + menuItemState.setStart(false); + menuItemState.setPause(true); + menuItemState.setReset(false); + menuItemState.setReplay(true); + currentSimulation.getSimulatorCanvas().reset(); + currentSimulation.getSimulatorCanvas().play(); + updateSimulationMenu(); + } + } + }; + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setSize(prefs.getInteger("div.window.xsize"), @@ -116,7 +211,7 @@ public class VSSimulatorFrame extends VSFrame implements ActionListener { menuItem.setAccelerator(KeyStroke.getKeyStroke( prefs.getInteger("keyevent.new"), ActionEvent.ALT_MASK)); - menuItem.addActionListener(this); + menuItem.addActionListener(actionListener); menuFile.add(menuItem); menuItem = new JMenuItem( @@ -124,28 +219,28 @@ public class VSSimulatorFrame extends VSFrame implements ActionListener { menuItem.setAccelerator(KeyStroke.getKeyStroke( prefs.getInteger("keyevent.close"), ActionEvent.ALT_MASK)); - menuItem.addActionListener(this); + menuItem.addActionListener(actionListener); menuFile.add(menuItem); menuFile.addSeparator(); menuItem = new JMenuItem(prefs.getString("lang.window.new")); - menuItem.addActionListener(this); + menuItem.addActionListener(actionListener); menuFile.add(menuItem); menuItem = new JMenuItem(prefs.getString("lang.window.close")); - menuItem.addActionListener(this); + menuItem.addActionListener(actionListener); menuFile.add(menuItem); menuFile.addSeparator(); menuItem = new JMenuItem(prefs.getString("lang.about")); - menuItem.addActionListener(this); + menuItem.addActionListener(actionListener); menuFile.add(menuItem); menuItem = new JMenuItem(prefs.getString("lang.quit")); - menuItem.addActionListener(this); + menuItem.addActionListener(actionListener); menuFile.add(menuItem); /* Edit menu */ @@ -164,11 +259,12 @@ public class VSSimulatorFrame extends VSFrame implements ActionListener { resetItem.setAccelerator(KeyStroke.getKeyStroke( prefs.getInteger("keyevent.reset"), ActionEvent.ALT_MASK)); - resetItem.addActionListener(this); + resetItem.addActionListener(actionListener); resetItem.setEnabled(false); menuSimulation.add(resetItem); - resetButton = new JButton(getImageIcon("reset.png", prefs.getString("lang.reset"))); - resetButton.addActionListener(this); + resetButton = new JButton(getImageIcon("reset.png", + prefs.getString("lang.reset"))); + resetButton.addActionListener(actionListener); toolBar.add(resetButton); replayItem = new JMenuItem( @@ -176,32 +272,35 @@ public class VSSimulatorFrame extends VSFrame implements ActionListener { replayItem.setAccelerator(KeyStroke.getKeyStroke( prefs.getInteger("keyevent.replay"), ActionEvent.ALT_MASK)); - replayItem.addActionListener(this); + replayItem.addActionListener(actionListener); replayItem.setEnabled(false); menuSimulation.add(replayItem); - replayButton = new JButton(getImageIcon("replay.png", prefs.getString("lang.replay"))); - replayButton.addActionListener(this); + replayButton = new JButton( + getImageIcon("replay.png", prefs.getString("lang.replay"))); + replayButton.addActionListener(actionListener); toolBar.add(replayButton); pauseItem = new JMenuItem(prefs.getString("lang.pause")); pauseItem.setAccelerator(KeyStroke.getKeyStroke( prefs.getInteger("keyevent.pause"), ActionEvent.ALT_MASK)); - pauseItem.addActionListener(this); + pauseItem.addActionListener(actionListener); menuSimulation.add(pauseItem); pauseItem.setEnabled(false); - pauseButton = new JButton(getImageIcon("pause.png", prefs.getString("lang.pause"))); - pauseButton.addActionListener(this); + pauseButton = new JButton(getImageIcon("pause.png", + prefs.getString("lang.pause"))); + pauseButton.addActionListener(actionListener); toolBar.add(pauseButton); startItem = new JMenuItem(prefs.getString("lang.start")); startItem.setAccelerator(KeyStroke.getKeyStroke( prefs.getInteger("keyevent.start"), ActionEvent.ALT_MASK)); - startItem.addActionListener(this); + startItem.addActionListener(actionListener); menuSimulation.add(startItem); - startButton = new JButton(getImageIcon("start.png", prefs.getString("lang.start"))); - startButton.addActionListener(this); + startButton = new JButton(getImageIcon("start.png", + prefs.getString("lang.start"))); + startButton.addActionListener(actionListener); toolBar.add(startButton); @@ -220,7 +319,9 @@ public class VSSimulatorFrame extends VSFrame implements ActionListener { */ private Container createContentPane() { Container pane = getContentPane(); - tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM, JTabbedPane.SCROLL_TAB_LAYOUT); + tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM, + JTabbedPane.SCROLL_TAB_LAYOUT); + tabbedPane.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { JTabbedPane pane = (JTabbedPane) ce.getSource(); @@ -238,19 +339,22 @@ public class VSSimulatorFrame extends VSFrame implements ActionListener { } /** - * Update edit menu. + * Updates the edit menu. Called if another simulator tab has been selected + * or if processes have been added or removed. */ public void updateEditMenu() { menuEdit.removeAll(); - JMenuItem globalPrefsItem = new JMenuItem(prefs.getString("lang.prefs")); + JMenuItem globalPrefsItem = new JMenuItem( + prefs.getString("lang.prefs")); globalPrefsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { VSPrefs simulationPrefs = currentSimulation.getPrefs(); VSSimulatorEditor.TAKEOVER_BUTTON = true; VSSimulatorEditor simulationEditor = new VSSimulatorEditor( simulationPrefs, VSSimulatorFrame.this, currentSimulation); - new VSEditorFrame(prefs, VSSimulatorFrame.this, simulationEditor); + new VSEditorFrame(prefs, VSSimulatorFrame.this, + simulationEditor); } }); menuEdit.add(globalPrefsItem); @@ -260,29 +364,33 @@ public class VSSimulatorFrame extends VSFrame implements ActionListener { return; final String processString = prefs.getString("lang.process"); - final ArrayList<VSProcess> arr = currentSimulation.getSimulatorCanvas().getProcessesArray(); + final ArrayList<VSProcess> arr = + currentSimulation.getSimulatorCanvas().getProcessesArray(); final int numProcesses = arr.size(); int processNum = 0; for (VSProcess process : arr) { int processID = process.getProcessID(); - JMenuItem processItem = new JMenuItem(processString + " " + processID); + JMenuItem processItem = new JMenuItem(processString + " " + + processID); if (processNum < 10) - processItem.setAccelerator(KeyStroke.getKeyStroke(0x31+processNum, + processItem.setAccelerator( + KeyStroke.getKeyStroke(0x31+processNum, ActionEvent.ALT_MASK)); final int finalProcessNum = processNum++; processItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { - currentSimulation.getSimulatorCanvas().editProcess(finalProcessNum); + currentSimulation.getSimulatorCanvas().editProcess( + finalProcessNum); } }); menuEdit.add(processItem); } } - /* updateSimulationMenu can be called from concurrent threads */ /** - * Update simulation menu. + * Updates the simulation menu. Called if the simulator state has changed + * (e.g. start/play/stop/replay etc) */ public synchronized void updateSimulationMenu() { VSMenuItemStates menuItemState = currentSimulation.getMenuItemStates(); @@ -309,76 +417,6 @@ public class VSSimulatorFrame extends VSFrame implements ActionListener { super.dispose(); } - /* (non-Javadoc) - * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) - */ - public void actionPerformed(ActionEvent e) { - Object source = e.getSource(); - String sourceText = null; - - if (source instanceof JMenuItem) - sourceText = ((JMenuItem) source).getText(); - else - sourceText = ((ImageIcon) ((JButton) source).getIcon()).getDescription(); - - if (sourceText.equals(prefs.getString("lang.simulation.close"))) { - removeCurrentSimulation(); - - } else if (sourceText.equals(prefs.getString("lang.simulation.new"))) { - VSPrefs newPrefs = VSDefaultPrefs.init(); - new VSEditorFrame(newPrefs, this, new VSSimulatorEditor(newPrefs, this)); - - } else if (sourceText.equals(prefs.getString("lang.window.new"))) { - new VSMain(VSDefaultPrefs.init(), this); - - } else if (sourceText.equals(prefs.getString("lang.window.close"))) { - dispose(); - - } else if (sourceText.equals(prefs.getString("lang.about"))) { - new VSAbout(prefs, this); - - } else if (sourceText.equals(prefs.getString("lang.quit"))) { - System.exit(0); - - } else if (sourceText.equals(prefs.getString("lang.start"))) { - VSMenuItemStates menuItemState = currentSimulation.getMenuItemStates(); - menuItemState.setStart(false); - menuItemState.setPause(true); - menuItemState.setReset(false); - menuItemState.setReplay(true); - currentSimulation.getSimulatorCanvas().play(); - updateSimulationMenu(); - - } else if (sourceText.equals(prefs.getString("lang.pause"))) { - VSMenuItemStates menuItemState = currentSimulation.getMenuItemStates(); - menuItemState.setStart(true); - menuItemState.setPause(false); - menuItemState.setReset(true); - menuItemState.setReplay(true); - currentSimulation.getSimulatorCanvas().pause(); - updateSimulationMenu(); - - } else if (sourceText.equals(prefs.getString("lang.reset"))) { - VSMenuItemStates menuItemState = currentSimulation.getMenuItemStates(); - menuItemState.setStart(true); - menuItemState.setPause(false); - menuItemState.setReset(false); - menuItemState.setReplay(false); - currentSimulation.getSimulatorCanvas().reset(); - updateSimulationMenu(); - - } else if (sourceText.equals(prefs.getString("lang.replay"))) { - VSMenuItemStates menuItemState = currentSimulation.getMenuItemStates(); - menuItemState.setStart(false); - menuItemState.setPause(true); - menuItemState.setReset(false); - menuItemState.setReplay(true); - currentSimulation.getSimulatorCanvas().reset(); - currentSimulation.getSimulatorCanvas().play(); - updateSimulationMenu(); - } - } - /** * Adds the simulation. * @@ -450,5 +488,4 @@ public class VSSimulatorFrame extends VSFrame implements ActionListener { return new ImageIcon(imageURL, descr); } - } |
