Browse Source
New/improved simulation path plotting functionality.
New/improved simulation path plotting functionality.
For -simpath switch: * paths are generated on-the-fly for efficiency where possible * rewards are not displayed by default, use 'rewards' option to show * new 'snapshot' option to only show states at certain time-points For GUI simulator: * basic functionality to plot existing paths or create new plots directly git-svn-id: https://www.prismmodelchecker.org/svn/prism/prism/trunk@5384 bbc10eb1-c90d-0410-af57-cb519fbb1720master
11 changed files with 1413 additions and 222 deletions
-
293prism/src/simulator/GenerateSimulationPath.java
-
34prism/src/simulator/Path.java
-
166prism/src/simulator/PathDisplayer.java
-
220prism/src/simulator/PathFull.java
-
30prism/src/simulator/PathFullPrefix.java
-
44prism/src/simulator/PathOnTheFly.java
-
121prism/src/simulator/PathToGraph.java
-
272prism/src/simulator/PathToText.java
-
25prism/src/simulator/SimulatorEngine.java
-
191prism/src/userinterface/simulator/GUIPathPlotDialog.java
-
239prism/src/userinterface/simulator/GUISimulator.java
@ -0,0 +1,166 @@ |
|||||
|
//============================================================================== |
||||
|
// |
||||
|
// Copyright (c) 2002- |
||||
|
// Authors: |
||||
|
// * Dave Parker <d.a.parker@cs.bham.ac.uk> (University of Birmingham/Oxford) |
||||
|
// |
||||
|
//------------------------------------------------------------------------------ |
||||
|
// |
||||
|
// This file is part of PRISM. |
||||
|
// |
||||
|
// PRISM 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. |
||||
|
// |
||||
|
// PRISM 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 PRISM; if not, write to the Free Software Foundation, |
||||
|
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||||
|
// |
||||
|
//============================================================================== |
||||
|
|
||||
|
package simulator; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
import parser.State; |
||||
|
|
||||
|
/** |
||||
|
* Abstract class for classes that "display" a simulation path. |
||||
|
*/ |
||||
|
public abstract class PathDisplayer |
||||
|
{ |
||||
|
/** Should we display (timed) snapshots, rather than steps? */ |
||||
|
protected boolean showSnapshots = false; |
||||
|
/** If we are displaying snapshots, how often? */ |
||||
|
protected double snapshotTimeStep = 0.0; |
||||
|
/** If we are displaying snapshots, when is the next one due? */ |
||||
|
protected double nextTime = 0.0; |
||||
|
|
||||
|
/** Indices of variables to show (null = all) */ |
||||
|
protected List<Integer> varsToShow = null; |
||||
|
|
||||
|
/** Should we display rewards? */ |
||||
|
protected boolean showRewards = false; |
||||
|
|
||||
|
// Getters for config |
||||
|
|
||||
|
/** |
||||
|
* Should we display (timed) snapshots, rather than steps? |
||||
|
*/ |
||||
|
public boolean getShowSnapshots() |
||||
|
{ |
||||
|
return showSnapshots; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* If we are displaying snapshots, how often? |
||||
|
*/ |
||||
|
public double getSnapshotTimeStep() |
||||
|
{ |
||||
|
return snapshotTimeStep; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Set (indices of) vars to show values of (null = all). |
||||
|
*/ |
||||
|
public void setVarsToShow(List<Integer> varsToShow) |
||||
|
{ |
||||
|
// Take a copy of var index list |
||||
|
this.varsToShow = varsToShow == null ? null : new ArrayList<Integer>(varsToShow); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Should we display rewards? |
||||
|
*/ |
||||
|
public boolean getShowRewards() |
||||
|
{ |
||||
|
return showRewards; |
||||
|
} |
||||
|
|
||||
|
// Setters for config |
||||
|
|
||||
|
/** |
||||
|
* Set to display (timed) snapshots, rather than steps. |
||||
|
*/ |
||||
|
public void setToShowSteps() |
||||
|
{ |
||||
|
this.showSnapshots = false; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Set to display steps, rather than (timed) snapshots. |
||||
|
*/ |
||||
|
public void setToShowSnapShots(double timeStep) |
||||
|
{ |
||||
|
this.showSnapshots = true; |
||||
|
this.snapshotTimeStep = timeStep; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Set whether we we display rewards. |
||||
|
*/ |
||||
|
public void setShowRewards(boolean showRewards) |
||||
|
{ |
||||
|
this.showRewards = showRewards; |
||||
|
} |
||||
|
|
||||
|
// Methods called by path owner |
||||
|
|
||||
|
public void start(State initialState, double[] initialStateRewards) |
||||
|
{ |
||||
|
startDisplay(initialState, initialStateRewards); |
||||
|
if (showSnapshots) { |
||||
|
nextTime = snapshotTimeStep; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void step(double timeSpent, double timeCumul, Object action, double[] transitionRewards, State newState, double[] newStateRewards) |
||||
|
{ |
||||
|
if (showSnapshots) { |
||||
|
if (timeCumul < nextTime) { |
||||
|
return; |
||||
|
} else { |
||||
|
while (timeCumul >= nextTime) { |
||||
|
displaySnapshot(nextTime, newState, newStateRewards); |
||||
|
nextTime += snapshotTimeStep; |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
displayStep(timeSpent, timeCumul, action, transitionRewards, newState, newStateRewards); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void end() |
||||
|
{ |
||||
|
endDisplay(); |
||||
|
} |
||||
|
|
||||
|
// Display methods to be implemented by subclasses |
||||
|
|
||||
|
/** |
||||
|
* Start displaying a path beginning with state {@code initialState}. |
||||
|
*/ |
||||
|
public abstract void startDisplay(State initialState, double[] initialStateRewards); |
||||
|
|
||||
|
/** |
||||
|
* Displaying a step of a path. |
||||
|
*/ |
||||
|
public abstract void displayStep(double timeSpent, double timeCumul, Object action, double[] transitionRewards, State newState, double[] newStateRewards); |
||||
|
|
||||
|
/** |
||||
|
* Displaying a snapshot of a path at a particular time instant. |
||||
|
*/ |
||||
|
public abstract void displaySnapshot(double timeCumul, State newState, double[] newStateRewards); |
||||
|
|
||||
|
/** |
||||
|
* Finish displaying a path.. |
||||
|
*/ |
||||
|
public abstract void endDisplay(); |
||||
|
} |
||||
@ -0,0 +1,121 @@ |
|||||
|
//============================================================================== |
||||
|
// |
||||
|
// Copyright (c) 2002- |
||||
|
// Authors: |
||||
|
// * Dave Parker <d.a.parker@cs.bham.ac.uk> (University of Birmingham/Oxford) |
||||
|
// |
||||
|
//------------------------------------------------------------------------------ |
||||
|
// |
||||
|
// This file is part of PRISM. |
||||
|
// |
||||
|
// PRISM 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. |
||||
|
// |
||||
|
// PRISM 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 PRISM; if not, write to the Free Software Foundation, |
||||
|
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||||
|
// |
||||
|
//============================================================================== |
||||
|
|
||||
|
package simulator; |
||||
|
|
||||
|
import org.jfree.data.xy.XYDataItem; |
||||
|
|
||||
|
import parser.State; |
||||
|
import parser.ast.ModulesFile; |
||||
|
import userinterface.graph.Graph; |
||||
|
import userinterface.graph.Graph.SeriesKey; |
||||
|
|
||||
|
/** |
||||
|
* Class to display a simulation path in text form, sending to a PrismLog. |
||||
|
*/ |
||||
|
public class PathToGraph extends PathDisplayer |
||||
|
{ |
||||
|
/** Graph on which to plot path */ |
||||
|
private Graph graphModel = null; |
||||
|
private SeriesKey seriesKeys[] = null; |
||||
|
|
||||
|
// Model info |
||||
|
private ModulesFile modulesFile; |
||||
|
private int numVars; |
||||
|
private int numRewardStructs; |
||||
|
|
||||
|
// Displayer state |
||||
|
/** Step counter */ |
||||
|
private double lastTime; |
||||
|
private State lastState; |
||||
|
|
||||
|
/** |
||||
|
* Construct a {@link PathToGraph} object |
||||
|
* @param graphModel Graph on which to plot path |
||||
|
* @param modulesFile Model associated with path |
||||
|
*/ |
||||
|
public PathToGraph(Graph graphModel, ModulesFile modulesFile) |
||||
|
{ |
||||
|
this.graphModel = graphModel; |
||||
|
this.modulesFile = modulesFile; |
||||
|
|
||||
|
// Get model info |
||||
|
numVars = modulesFile.getNumVars(); |
||||
|
numRewardStructs = modulesFile.getNumRewardStructs(); |
||||
|
} |
||||
|
|
||||
|
// Display methods |
||||
|
|
||||
|
@Override |
||||
|
public void startDisplay(State initialState, double[] stateRewards) |
||||
|
{ |
||||
|
// Configure axes |
||||
|
graphModel.getXAxisSettings().setHeading("Time"); |
||||
|
graphModel.getYAxisSettings().setHeading("Value"); |
||||
|
|
||||
|
// Create series |
||||
|
seriesKeys = new SeriesKey[numVars]; |
||||
|
for (int j = 0; j < numVars; j++) { |
||||
|
seriesKeys[j] = graphModel.addSeries(modulesFile.getVarName(j)); |
||||
|
} |
||||
|
|
||||
|
// Display initial state |
||||
|
lastState = new State(initialState.varValues.length); |
||||
|
displayState(0.0, initialState, true); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void displayStep(double timeSpent, double timeCumul, Object action, double[] transitionRewards, State newState, double[] newStateRewards) |
||||
|
{ |
||||
|
displayState(timeCumul, newState, false); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void displaySnapshot(double timeCumul, State newState, double[] newStateRewards) |
||||
|
{ |
||||
|
displayState(timeCumul, newState, false); |
||||
|
} |
||||
|
|
||||
|
private void displayState(double time, State state, boolean force) |
||||
|
{ |
||||
|
for (int j = 0; j < numVars; j++) { |
||||
|
// TODO: other var types? |
||||
|
if (force || !state.varValues[j].equals(lastState.varValues[j])) { |
||||
|
graphModel.addPointToSeries(seriesKeys[j], new XYDataItem(time, ((Integer) state.varValues[j]).intValue())); |
||||
|
} |
||||
|
} |
||||
|
lastTime = time; |
||||
|
lastState.copy(state); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void endDisplay() |
||||
|
{ |
||||
|
// Always display last points to ensure complete plot lines |
||||
|
// (it's OK to overwrite points) |
||||
|
displayState(lastTime, lastState, true); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,272 @@ |
|||||
|
//============================================================================== |
||||
|
// |
||||
|
// Copyright (c) 2002- |
||||
|
// Authors: |
||||
|
// * Dave Parker <d.a.parker@cs.bham.ac.uk> (University of Birmingham/Oxford) |
||||
|
// |
||||
|
//------------------------------------------------------------------------------ |
||||
|
// |
||||
|
// This file is part of PRISM. |
||||
|
// |
||||
|
// PRISM 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. |
||||
|
// |
||||
|
// PRISM 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 PRISM; if not, write to the Free Software Foundation, |
||||
|
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||||
|
// |
||||
|
//============================================================================== |
||||
|
|
||||
|
package simulator; |
||||
|
|
||||
|
import parser.State; |
||||
|
import parser.ast.ModulesFile; |
||||
|
import prism.PrismLog; |
||||
|
|
||||
|
/** |
||||
|
* Class to display a simulation path in text form, sending to a PrismLog. |
||||
|
*/ |
||||
|
public class PathToText extends PathDisplayer |
||||
|
{ |
||||
|
/** Log to display path to */ |
||||
|
private PrismLog log; |
||||
|
|
||||
|
// Model info |
||||
|
private ModulesFile modulesFile; |
||||
|
private int numVars; |
||||
|
private int numRewardStructs; |
||||
|
private boolean contTime; |
||||
|
|
||||
|
// Config |
||||
|
private boolean showTimeCumul = true; |
||||
|
private boolean showTimeSpent = false; |
||||
|
private String colSep = " "; |
||||
|
|
||||
|
// Displayer state |
||||
|
/** Step counter */ |
||||
|
private int step; |
||||
|
/** Is the next column the first? */ |
||||
|
private boolean firstCol; |
||||
|
/** Last state */ |
||||
|
private State lastState; |
||||
|
/** Last state rewards */ |
||||
|
private double[] lastStateRewards; |
||||
|
|
||||
|
/** |
||||
|
* Construct a {@link PathToText} object |
||||
|
* @param log Log to output path to |
||||
|
* @param modulesFile Model associated with path |
||||
|
*/ |
||||
|
public PathToText(PrismLog log, ModulesFile modulesFile) |
||||
|
{ |
||||
|
this.log = log; |
||||
|
this.modulesFile = modulesFile; |
||||
|
|
||||
|
// Get model info |
||||
|
numVars = modulesFile.getNumVars(); |
||||
|
numRewardStructs = modulesFile.getNumRewardStructs(); |
||||
|
contTime = modulesFile.getModelType().continuousTime(); |
||||
|
} |
||||
|
|
||||
|
// Setters |
||||
|
|
||||
|
/** |
||||
|
* Set whether we show cumulative time (for continuous models only) |
||||
|
*/ |
||||
|
public void setShowTimeCumul(boolean showTimeCumul) |
||||
|
{ |
||||
|
this.showTimeCumul = showTimeCumul; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Set whether we show time spent in each state (for continuous models only) |
||||
|
*/ |
||||
|
public void setShowTimeSpent(boolean showTimeSpent) |
||||
|
{ |
||||
|
this.showTimeSpent = showTimeSpent; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Set the column separator for the table of path contents |
||||
|
*/ |
||||
|
public void setColSep(String colSep) |
||||
|
{ |
||||
|
this.colSep = colSep; |
||||
|
} |
||||
|
|
||||
|
// Display methods |
||||
|
|
||||
|
@Override |
||||
|
public void startDisplay(State initialState, double[] initialStateRewards) |
||||
|
{ |
||||
|
int j; |
||||
|
|
||||
|
// Display header |
||||
|
firstCol = true; |
||||
|
if (!getShowSnapshots()) { |
||||
|
log.print(getColSep() + "action"); |
||||
|
log.print(getColSep() + "step"); |
||||
|
} |
||||
|
if (contTime && showTimeCumul) |
||||
|
log.print(getColSep() + "time"); |
||||
|
if (varsToShow == null) |
||||
|
for (j = 0; j < numVars; j++) |
||||
|
log.print(getColSep() + modulesFile.getVarName(j)); |
||||
|
else |
||||
|
for (int v : varsToShow) |
||||
|
log.print(getColSep() + modulesFile.getVarName(v)); |
||||
|
if (getShowRewards()) { |
||||
|
if (numRewardStructs == 1) { |
||||
|
log.print(getColSep() + "state_reward"); |
||||
|
if (!getShowSnapshots()) |
||||
|
log.print(getColSep() + "transition_reward"); |
||||
|
} else { |
||||
|
for (j = 0; j < numRewardStructs; j++) { |
||||
|
log.print(getColSep() + "state_reward" + (j + 1)); |
||||
|
if (!getShowSnapshots()) |
||||
|
log.print(getColSep() + "transition_reward" + (j + 1)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if (!getShowSnapshots()) { |
||||
|
if (contTime && showTimeSpent) |
||||
|
log.print(getColSep() + "time_in_state"); |
||||
|
} |
||||
|
log.println(); |
||||
|
|
||||
|
// Display initial step |
||||
|
step = 0; |
||||
|
firstCol = true; |
||||
|
if (!getShowSnapshots()) { |
||||
|
log.print(getColSep() + "-" + getColSep() + "0"); |
||||
|
} |
||||
|
if (contTime && showTimeCumul) |
||||
|
log.print(getColSep() + "0.0"); |
||||
|
lastState = new State(initialState.varValues.length); |
||||
|
displayState(initialState); |
||||
|
if (getShowRewards()) { |
||||
|
lastStateRewards = explicit.Utils.cloneDoubleArray(initialStateRewards); |
||||
|
} |
||||
|
if (getShowSnapshots()) { |
||||
|
log.println(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void displayStep(double timeSpent, double timeCumul, Object action, double[] transitionRewards, State newState, double[] newStateRewards) |
||||
|
{ |
||||
|
step++; |
||||
|
|
||||
|
// (if required) see if relevant vars have changed |
||||
|
if (varsToShow != null && step > 0) { |
||||
|
boolean changed = false; |
||||
|
for (int v : varsToShow){ |
||||
|
if (!newState.varValues[v].equals(lastState.varValues[v])) { |
||||
|
changed = true; |
||||
|
continue; |
||||
|
} |
||||
|
} |
||||
|
if (!changed) { |
||||
|
return; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// display rewards for last state |
||||
|
if (getShowRewards()) { |
||||
|
for (int j = 0; j < numRewardStructs; j++) { |
||||
|
log.print(getColSep() + lastStateRewards[j]); |
||||
|
log.print(getColSep() + transitionRewards[j]); |
||||
|
} |
||||
|
explicit.Utils.copyDoubleArray(newStateRewards, lastStateRewards); |
||||
|
} |
||||
|
// display time spent in state |
||||
|
if (contTime && showTimeSpent) |
||||
|
log.print(getColSep() + timeSpent); |
||||
|
log.println(); |
||||
|
|
||||
|
firstCol = true; |
||||
|
|
||||
|
// display action |
||||
|
log.print(getColSep() + action); |
||||
|
// display state index |
||||
|
log.print(getColSep() + step); |
||||
|
// display cumulative time |
||||
|
if (contTime && showTimeCumul) |
||||
|
log.print(getColSep() + timeCumul); |
||||
|
// display state |
||||
|
displayState(newState); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void displaySnapshot(double timeCumul, State newState, double[] newStateRewards) |
||||
|
{ |
||||
|
step++; |
||||
|
firstCol = true; |
||||
|
|
||||
|
// display cumulative time |
||||
|
if (contTime && showTimeCumul) |
||||
|
log.print(getColSep() + timeCumul); |
||||
|
// display state |
||||
|
displayState(newState); |
||||
|
// display state rewards |
||||
|
if (getShowRewards()) { |
||||
|
for (int j = 0; j < numRewardStructs; j++) { |
||||
|
log.print(getColSep() + newStateRewards[j]); |
||||
|
} |
||||
|
} |
||||
|
log.println(); |
||||
|
} |
||||
|
|
||||
|
private void displayState(State state) |
||||
|
{ |
||||
|
int j; |
||||
|
if (varsToShow == null) { |
||||
|
for (j = 0; j < numVars; j++) { |
||||
|
log.print(getColSep()); |
||||
|
log.print(state.varValues[j]); |
||||
|
} |
||||
|
} else { |
||||
|
for (int v : varsToShow) { |
||||
|
log.print(getColSep()); |
||||
|
log.print(state.varValues[v]); |
||||
|
} |
||||
|
} |
||||
|
lastState.copy(state); |
||||
|
} |
||||
|
|
||||
|
private String getColSep() |
||||
|
{ |
||||
|
if (firstCol) { |
||||
|
firstCol = false; |
||||
|
return ""; |
||||
|
} else { |
||||
|
return colSep; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void endDisplay() |
||||
|
{ |
||||
|
if (!getShowSnapshots()) { |
||||
|
// display state rewards for last state |
||||
|
// (transition rewards unknown because no outgoing transition) |
||||
|
if (getShowRewards()) { |
||||
|
for (int j = 0; j < numRewardStructs; j++) { |
||||
|
log.print(getColSep() + lastStateRewards[j]); |
||||
|
log.print(getColSep() + "?"); |
||||
|
} |
||||
|
} |
||||
|
// display (zero) time spent in state |
||||
|
if (contTime && showTimeSpent) |
||||
|
log.print(getColSep() + 0.0); |
||||
|
log.println(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,191 @@ |
|||||
|
//============================================================================== |
||||
|
// |
||||
|
// Copyright (c) 2002- |
||||
|
// Authors: |
||||
|
// * Dave Parker <d.a.parker@cs.bham.ac.uk> (University of Birmingham/Oxford) |
||||
|
// |
||||
|
//------------------------------------------------------------------------------ |
||||
|
// |
||||
|
// This file is part of PRISM. |
||||
|
// |
||||
|
// PRISM 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. |
||||
|
// |
||||
|
// PRISM 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 PRISM; if not, write to the Free Software Foundation, |
||||
|
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||||
|
// |
||||
|
//============================================================================== |
||||
|
|
||||
|
package userinterface.simulator; |
||||
|
|
||||
|
import javax.swing.*; |
||||
|
import userinterface.*; |
||||
|
|
||||
|
public class GUIPathPlotDialog extends javax.swing.JDialog |
||||
|
{ |
||||
|
public static final int OK = 0; |
||||
|
public static final int CANCELLED = 1; |
||||
|
|
||||
|
static double time = 0.0; |
||||
|
static boolean first = true; |
||||
|
|
||||
|
private boolean cancelled = true; |
||||
|
|
||||
|
/** Call this static method to construct a new GUIPathPlotDialog to get a time value. */ |
||||
|
public static int requestTime(GUIPrism parent) |
||||
|
{ |
||||
|
return new GUIPathPlotDialog(parent).requestTime(); |
||||
|
} |
||||
|
|
||||
|
public int requestTime() |
||||
|
{ |
||||
|
setVisible(true); |
||||
|
return cancelled ? CANCELLED : OK; |
||||
|
} |
||||
|
|
||||
|
public static double getTime() |
||||
|
{ |
||||
|
return time; |
||||
|
} |
||||
|
|
||||
|
/** Creates new form GUIPathPlotDialog */ |
||||
|
public GUIPathPlotDialog(java.awt.Frame parent) |
||||
|
{ |
||||
|
super(parent, "Define time", true); |
||||
|
initComponents(); |
||||
|
this.getRootPane().setDefaultButton(okayButton); |
||||
|
setLocationRelativeTo(getParent()); // centre |
||||
|
if (!first) |
||||
|
timeField.setText("" + time); |
||||
|
} |
||||
|
|
||||
|
/** This method is called from within the constructor to |
||||
|
* initialize the form. |
||||
|
* WARNING: Do NOT modify this code. The content of this method is |
||||
|
* always regenerated by the Form Editor. |
||||
|
*/ |
||||
|
private void initComponents() |
||||
|
{//GEN-BEGIN:initComponents |
||||
|
java.awt.GridBagConstraints gridBagConstraints; |
||||
|
|
||||
|
jPanel1 = new javax.swing.JPanel(); |
||||
|
jPanel2 = new javax.swing.JPanel(); |
||||
|
jPanel3 = new javax.swing.JPanel(); |
||||
|
jPanel4 = new javax.swing.JPanel(); |
||||
|
jPanel5 = new javax.swing.JPanel(); |
||||
|
jLabel1 = new javax.swing.JLabel(); |
||||
|
timeField = new javax.swing.JTextField(); |
||||
|
jPanel6 = new javax.swing.JPanel(); |
||||
|
okayButton = new javax.swing.JButton(); |
||||
|
cancelButton = new javax.swing.JButton(); |
||||
|
|
||||
|
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); |
||||
|
jPanel1.setLayout(new java.awt.GridBagLayout()); |
||||
|
|
||||
|
gridBagConstraints = new java.awt.GridBagConstraints(); |
||||
|
gridBagConstraints.gridx = 0; |
||||
|
gridBagConstraints.gridy = 0; |
||||
|
jPanel1.add(jPanel2, gridBagConstraints); |
||||
|
|
||||
|
gridBagConstraints = new java.awt.GridBagConstraints(); |
||||
|
gridBagConstraints.gridx = 2; |
||||
|
gridBagConstraints.gridy = 0; |
||||
|
jPanel1.add(jPanel3, gridBagConstraints); |
||||
|
|
||||
|
gridBagConstraints = new java.awt.GridBagConstraints(); |
||||
|
gridBagConstraints.gridx = 0; |
||||
|
gridBagConstraints.gridy = 2; |
||||
|
jPanel1.add(jPanel4, gridBagConstraints); |
||||
|
|
||||
|
gridBagConstraints = new java.awt.GridBagConstraints(); |
||||
|
gridBagConstraints.gridx = 0; |
||||
|
gridBagConstraints.gridy = 4; |
||||
|
jPanel1.add(jPanel5, gridBagConstraints); |
||||
|
|
||||
|
jLabel1.setText("Please specify a time limit for simulation:"); |
||||
|
gridBagConstraints = new java.awt.GridBagConstraints(); |
||||
|
gridBagConstraints.gridx = 1; |
||||
|
gridBagConstraints.gridy = 1; |
||||
|
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; |
||||
|
jPanel1.add(jLabel1, gridBagConstraints); |
||||
|
|
||||
|
timeField.setColumns(10); |
||||
|
gridBagConstraints = new java.awt.GridBagConstraints(); |
||||
|
gridBagConstraints.gridx = 1; |
||||
|
gridBagConstraints.gridy = 3; |
||||
|
jPanel1.add(timeField, gridBagConstraints); |
||||
|
|
||||
|
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); |
||||
|
|
||||
|
jPanel6.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT)); |
||||
|
|
||||
|
okayButton.setText("Okay"); |
||||
|
okayButton.addActionListener(new java.awt.event.ActionListener() |
||||
|
{ |
||||
|
public void actionPerformed(java.awt.event.ActionEvent evt) |
||||
|
{ |
||||
|
okayButtonActionPerformed(evt); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
jPanel6.add(okayButton); |
||||
|
|
||||
|
cancelButton.setText("Cancel"); |
||||
|
cancelButton.addActionListener(new java.awt.event.ActionListener() |
||||
|
{ |
||||
|
public void actionPerformed(java.awt.event.ActionEvent evt) |
||||
|
{ |
||||
|
cancelButtonActionPerformed(evt); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
jPanel6.add(cancelButton); |
||||
|
|
||||
|
getContentPane().add(jPanel6, java.awt.BorderLayout.SOUTH); |
||||
|
|
||||
|
pack(); |
||||
|
}//GEN-END:initComponents |
||||
|
|
||||
|
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) |
||||
|
{//GEN-FIRST:event_cancelButtonActionPerformed |
||||
|
dispose(); |
||||
|
}//GEN-LAST:event_cancelButtonActionPerformed |
||||
|
|
||||
|
private void okayButtonActionPerformed(java.awt.event.ActionEvent evt) |
||||
|
{//GEN-FIRST:event_okayButtonActionPerformed |
||||
|
double d = 0.0; |
||||
|
try { |
||||
|
d = Double.parseDouble(timeField.getText()); |
||||
|
if (d < 0) |
||||
|
throw new NumberFormatException(); |
||||
|
} catch (NumberFormatException e) { |
||||
|
JOptionPane.showMessageDialog(this, "Error: Invalid time value.", "Error", JOptionPane.ERROR_MESSAGE); |
||||
|
return; |
||||
|
} |
||||
|
time = d; |
||||
|
first = false; |
||||
|
cancelled = false; |
||||
|
dispose(); |
||||
|
}//GEN-LAST:event_okayButtonActionPerformed |
||||
|
|
||||
|
// Variables declaration - do not modify//GEN-BEGIN:variables |
||||
|
private javax.swing.JButton cancelButton; |
||||
|
private javax.swing.JLabel jLabel1; |
||||
|
private javax.swing.JPanel jPanel1; |
||||
|
private javax.swing.JPanel jPanel2; |
||||
|
private javax.swing.JPanel jPanel3; |
||||
|
private javax.swing.JPanel jPanel4; |
||||
|
private javax.swing.JPanel jPanel5; |
||||
|
private javax.swing.JPanel jPanel6; |
||||
|
private javax.swing.JButton okayButton; |
||||
|
private javax.swing.JTextField timeField; |
||||
|
// End of variables declaration//GEN-END:variables |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue