3
import java.awt.BorderLayout;
4
import java.awt.Dimension;
6
import java.awt.GridBagConstraints;
7
import java.awt.GridBagLayout;
8
import java.awt.Insets;
9
import java.awt.event.ActionEvent;
10
import java.awt.event.ActionListener;
11
import java.math.BigDecimal;
12
import java.math.MathContext;
13
import java.util.Hashtable;
15
import javax.swing.BorderFactory;
16
import javax.swing.JButton;
17
import javax.swing.JCheckBox;
18
import javax.swing.JComboBox;
19
import javax.swing.JDialog;
20
import javax.swing.JLabel;
21
import javax.swing.JPanel;
22
import javax.swing.JSlider;
24
import pipe.gui.widgets.EscapableDialog;
26
import dk.aau.cs.model.tapn.simulation.DelayMode;
27
import dk.aau.cs.model.tapn.simulation.ManualDelayMode;
28
import dk.aau.cs.model.tapn.simulation.RandomDelayMode;
29
import dk.aau.cs.model.tapn.simulation.ShortestDelayMode;
31
public class BlueTransitionControl extends JPanel{
33
private static final long serialVersionUID = -5735674907635981304L;
34
private static DelayMode defaultDelayMode = ShortestDelayMode.getInstance();
35
private static BigDecimal defaultGranularity = new BigDecimal("0.1");
36
private static boolean defaultIsRandomTrasition;
38
JLabel precitionLabel;
39
JSlider bluePrecision;
40
JLabel delayModeLabel;
44
private BlueTransitionControl() {
45
super(new GridBagLayout());
47
//0 corresponds to 0.00001, 5 corresponds to 1 ( thus x corresponds to 1/(10^(5−x)) )
48
bluePrecision = new JSlider(JSlider.HORIZONTAL, 0, 5, 4);
49
Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
50
labelTable.put(new Integer(0), new JLabel("0.00001"));
51
labelTable.put(new Integer(1), new JLabel("0.0001"));
52
labelTable.put(new Integer(2), new JLabel("0.001"));
53
labelTable.put(new Integer(3), new JLabel("0.01"));
54
labelTable.put(new Integer(4), new JLabel("0.1"));
55
labelTable.put(new Integer(5), new JLabel("1"));
57
bluePrecision.setLabelTable(labelTable);
58
bluePrecision.setSnapToTicks(true);
59
bluePrecision.setMajorTickSpacing(1);
60
bluePrecision.setPaintLabels(true);
61
bluePrecision.setPaintTicks(true);
62
bluePrecision.setPaintTrack(false);
63
bluePrecision.setPreferredSize(new Dimension(340, bluePrecision.getPreferredSize().height));
64
setValue(defaultGranularity);
65
//UIManager.put("Slider.paintValue", false);
66
//UIManager.getLookAndFeelDefaults().put("Slider.paintValue", false);
68
//TODO is this good? it's the only soloution I've found
69
//It makes sure the value of the slider is NOT written
71
/*Class<?> sliderUIClass;
73
sliderUIClass = Class.forName("javax.swing.plaf.synth.SynthSliderUI");
74
Field paintValue = sliderUIClass.getDeclaredField("paintValue");
75
paintValue.setAccessible(true);
76
paintValue.set(bluePrecision.getUI(), false);
77
} catch (Exception e) {
81
DelayMode[] items = {ShortestDelayMode.getInstance(), RandomDelayMode.getInstance(), ManualDelayMode.getInstance()};
82
delayMode = new JComboBox(items);
83
setDelayMode(defaultDelayMode);
85
randomMode = new JCheckBox("Choose next transition randomly");
86
setRandomTransitionMode(defaultIsRandomTrasition);
88
GridBagConstraints gbc = new GridBagConstraints();
90
gbc.anchor = GridBagConstraints.WEST;
91
gbc.fill = GridBagConstraints.HORIZONTAL;
95
add(precitionLabel = new JLabel("Set the delay granularity"), gbc);
97
gbc = new GridBagConstraints();
99
gbc.anchor = GridBagConstraints.WEST;
100
gbc.fill = GridBagConstraints.HORIZONTAL;
104
add(bluePrecision, gbc);
106
gbc = new GridBagConstraints();
107
gbc.anchor = GridBagConstraints.WEST;
108
gbc.fill = GridBagConstraints.HORIZONTAL;
111
add(delayModeLabel = new JLabel("Delay Mode:"), gbc);
113
gbc = new GridBagConstraints();
114
gbc.anchor = GridBagConstraints.WEST;
115
gbc.fill = GridBagConstraints.HORIZONTAL;
121
gbc = new GridBagConstraints();
122
gbc.anchor = GridBagConstraints.WEST;
126
add(randomMode, gbc);
128
setBorder(BorderFactory.createCompoundBorder(
129
BorderFactory.createTitledBorder("Delay controller"),
130
BorderFactory.createEmptyBorder(3, 3, 3, 3)));
133
private int getValueFromDecimal(BigDecimal decimal) {
134
if(new BigDecimal("0.00001").compareTo(decimal) == 0) return 0;
135
if(new BigDecimal("0.0001").compareTo(decimal) == 0) return 1;
136
if(new BigDecimal("0.001").compareTo(decimal) == 0) return 2;
137
if(new BigDecimal("0.01").compareTo(decimal) == 0) return 3;
138
if(new BigDecimal("0.1").compareTo(decimal) == 0) return 4;
139
if(new BigDecimal("1").compareTo(decimal) == 0) return 5;
143
//0 corresponds to 0.00001, 5 corresponds to 1 ( thus x corresponds to 1/(10^(5−x)) )
144
public BigDecimal getValue(){
145
return new BigDecimal(1.0/(Math.pow(10.0, (5.0-bluePrecision.getValue()))), new MathContext(Pipe.AGE_PRECISION));
148
public void setValue(BigDecimal value){
149
bluePrecision.setValue(getValueFromDecimal(value));
152
public DelayMode getDelayMode(){
153
return (DelayMode)delayMode.getSelectedItem();
156
public void setDelayMode(DelayMode delayMode){
157
this.delayMode.setSelectedItem(delayMode);
160
public boolean isRandomTransitionMode(){
161
if(SimulationControl.getInstance().randomSimulation()){
164
return randomMode.isSelected();
168
public void setRandomTransitionMode(boolean randomTransition){
169
randomMode.setSelected(randomTransition);
172
private static BlueTransitionControl instance;
173
private static JButton closeDialogButton;
174
private static JDialog dialog;
176
public static void showBlueTransitionDialog(){
177
JPanel contentPane = new JPanel(new GridBagLayout());
179
closeDialogButton = new JButton("Close");
180
closeDialogButton.addActionListener(new ActionListener() {
182
public void actionPerformed(ActionEvent arg0) {
183
dialog.setVisible(false);
187
GridBagConstraints gbc = new GridBagConstraints();
188
gbc.anchor = GridBagConstraints.NORTHWEST;
189
gbc.insets = new Insets(0, 3, 0, 3);
190
gbc.fill = GridBagConstraints.BOTH;
191
contentPane.add(getInstance(), gbc);
193
gbc = new GridBagConstraints();
194
gbc.anchor = GridBagConstraints.NORTHWEST;
195
gbc.insets = new Insets(3, 3, 0, 3);
197
gbc.fill = GridBagConstraints.HORIZONTAL;
198
contentPane.add(closeDialogButton, gbc);
200
dialog = new EscapableDialog(CreateGui.getApp(), "Delay controller", true);
201
dialog.setContentPane(contentPane);
203
dialog.setResizable(false);
204
dialog.setLocationRelativeTo(CreateGui.getApp());
205
dialog.setVisible(true);
208
public static BlueTransitionControl getInstance(){
209
if(instance == null){
210
instance = new BlueTransitionControl();
215
public static void setDefaultDelayMode(DelayMode delayMode){
216
defaultDelayMode = delayMode;
219
public static DelayMode getDefaultDelayMode(){
220
if(instance != null){
221
return getInstance().getDelayMode();
223
return defaultDelayMode;
227
public static void setDefaultGranularity(BigDecimal granularity){
228
defaultGranularity = granularity;
231
public static BigDecimal getDefaultGranularity(){
232
if(instance != null){
233
return getInstance().getValue();
235
return defaultGranularity;
239
public static void setDefaultIsRandomTransition(boolean blueTransitionIsRandomTransition) {
240
defaultIsRandomTrasition = blueTransitionIsRandomTransition;
243
public static boolean isRandomTransition(){
244
if(instance != null){
245
return getInstance().isRandomTransitionMode();
247
return defaultIsRandomTrasition;
252
public void setEnabled(boolean enabled) {
253
super.setEnabled(enabled);
254
precitionLabel.setEnabled(enabled);
255
bluePrecision.setEnabled(enabled);
256
delayModeLabel.setEnabled(enabled);
257
delayMode.setEnabled(enabled);
258
randomMode.setEnabled(enabled);