1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
package simulator;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
import core.*;
import events.*;
import events.implementations.*;
import prefs.*;
import prefs.editors.*;
import protocols.*;
import utils.*;
public class VSSimulatorFrame extends VSFrame implements ActionListener {
private JMenuItem pauseItem;
private JMenuItem replayItem;
private JMenuItem resetItem;
private JMenuItem startItem;
private JButton pauseButton;
private JButton replayButton;
private JButton resetButton;
private JButton startButton;
private JMenu menuEdit;
private JMenu menuFile;
private JMenu menuSimulation;
private JToolBar toolBar;
private VSPrefs prefs;
private Vector<VSSimulation> simulations;
private VSSimulation currentSimulation;
private JTabbedPane tabbedPane;
//private JSlider speedSlider;
public VSSimulatorFrame(VSPrefs prefs, Component relativeTo) {
super(prefs.getString("lang.name"), relativeTo);
this.prefs = prefs;
this.simulations = new Vector<VSSimulation>();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(prefs.getInteger("div.window.xsize"),
prefs.getInteger("div.window.ysize"));
setJMenuBar(createMenuBar());
setLayout(new BorderLayout());
setContentPane(createContentPane());
setVisible(true);
pauseButton.setEnabled(false);
replayButton.setEnabled(false);
resetButton.setEnabled(false);
startButton.setEnabled(false);
menuEdit.setEnabled(false);
menuFile.setEnabled(false);
menuSimulation.setEnabled(false);
}
private JMenuBar createMenuBar() {
/* File menu */
menuFile = new JMenu(prefs.getString("lang.file"));
menuFile.setMnemonic(prefs.getInteger("keyevent.file"));
JMenuItem menuItem;
menuItem = new JMenuItem(prefs.getString("lang.simulation.new"));
menuItem.setAccelerator(KeyStroke.getKeyStroke(
prefs.getInteger("keyevent.new"),
ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menuFile.add(menuItem);
menuItem = new JMenuItem(
prefs.getString("lang.simulation.close"));
menuItem.setAccelerator(KeyStroke.getKeyStroke(
prefs.getInteger("keyevent.close"),
ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menuFile.add(menuItem);
menuFile.addSeparator();
menuItem = new JMenuItem(prefs.getString("lang.window.new"));
menuItem.addActionListener(this);
menuFile.add(menuItem);
menuItem = new JMenuItem(prefs.getString("lang.window.close"));
menuItem.addActionListener(this);
menuFile.add(menuItem);
menuFile.addSeparator();
menuItem = new JMenuItem(prefs.getString("lang.about"));
menuItem.addActionListener(this);
menuFile.add(menuItem);
menuItem = new JMenuItem(prefs.getString("lang.quit"));
menuItem.addActionListener(this);
menuFile.add(menuItem);
/* Edit menu */
menuEdit = new JMenu(
prefs.getString("lang.edit"));
menuEdit.setMnemonic(prefs.getInteger("keyevent.edit"));
updateEditMenu();
/* Simulation menu */
toolBar = new JToolBar();
menuSimulation = new JMenu(
prefs.getString("lang.simulation"));
menuSimulation.setMnemonic(prefs.getInteger("keyevent.simulation"));
resetItem = new JMenuItem(prefs.getString("lang.reset"));
resetItem.setAccelerator(KeyStroke.getKeyStroke(
prefs.getInteger("keyevent.reset"),
ActionEvent.ALT_MASK));
resetItem.addActionListener(this);
resetItem.setEnabled(false);
menuSimulation.add(resetItem);
resetButton = new JButton(getImageIcon("reset.png", prefs.getString("lang.reset")));
resetButton.addActionListener(this);
toolBar.add(resetButton);
replayItem = new JMenuItem(
prefs.getString("lang.replay"));
replayItem.setAccelerator(KeyStroke.getKeyStroke(
prefs.getInteger("keyevent.replay"),
ActionEvent.ALT_MASK));
replayItem.addActionListener(this);
replayItem.setEnabled(false);
menuSimulation.add(replayItem);
replayButton = new JButton(getImageIcon("replay.png", prefs.getString("lang.replay")));
replayButton.addActionListener(this);
toolBar.add(replayButton);
pauseItem = new JMenuItem(prefs.getString("lang.pause"));
pauseItem.setAccelerator(KeyStroke.getKeyStroke(
prefs.getInteger("keyevent.pause"),
ActionEvent.ALT_MASK));
pauseItem.addActionListener(this);
menuSimulation.add(pauseItem);
pauseItem.setEnabled(false);
pauseButton = new JButton(getImageIcon("pause.png", prefs.getString("lang.pause")));
pauseButton.addActionListener(this);
toolBar.add(pauseButton);
startItem = new JMenuItem(prefs.getString("lang.start"));
startItem.setAccelerator(KeyStroke.getKeyStroke(
prefs.getInteger("keyevent.start"),
ActionEvent.ALT_MASK));
startItem.addActionListener(this);
menuSimulation.add(startItem);
startButton = new JButton(getImageIcon("start.png", prefs.getString("lang.start")));
startButton.addActionListener(this);
toolBar.add(startButton);
JMenuBar mainMenuBar = new JMenuBar();
mainMenuBar.add(menuFile);
mainMenuBar.add(menuEdit);
mainMenuBar.add(menuSimulation);
return mainMenuBar;
}
private Container createContentPane() {
Container pane = getContentPane();
tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM, JTabbedPane.SCROLL_TAB_LAYOUT);
tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent ce) {
JTabbedPane pane = (JTabbedPane) ce.getSource();
currentSimulation = (VSSimulation) pane.getSelectedComponent();
currentSimulation.getSimulationCanvas().paint();
updateEditMenu();
updateSimulationMenu();
}
});
pane.add(toolBar, BorderLayout.PAGE_START);
pane.add(tabbedPane, BorderLayout.CENTER);
return pane;
}
public void updateEditMenu() {
menuEdit.removeAll();
JMenuItem globalPrefsItem = new JMenuItem(prefs.getString("lang.prefs"));
globalPrefsItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
VSPrefs simulationPrefs = currentSimulation.getPrefs();
VSSimulationEditor.TAKEOVER_BUTTON = true;
VSSimulationEditor simulationEditor = new VSSimulationEditor(
simulationPrefs, VSSimulatorFrame.this, currentSimulation);
new VSEditorFrame(prefs, VSSimulatorFrame.this, simulationEditor);
}
});
menuEdit.add(globalPrefsItem);
if (currentSimulation == null)
return;
final String processString = prefs.getString("lang.process");
final ArrayList<VSProcess> arr = currentSimulation.getSimulationCanvas().getProcessesArray();
final int numProcesses = arr.size();
int processNum = 0;
for (VSProcess process : arr) {
int processID = process.getProcessID();
JMenuItem processItem = new JMenuItem(processString + " " + processID);
processItem.setAccelerator(KeyStroke.getKeyStroke(0x31+processID,
ActionEvent.ALT_MASK));
final int finalProcessNum = processNum++;
processItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
currentSimulation.getSimulationCanvas().editProcess(finalProcessNum);
}
});
menuEdit.add(processItem);
}
}
/* updateSimulationMenu can be called from concurrent threads */
public synchronized void updateSimulationMenu() {
VSSimulation.VSMenuItemStates menuItemState = currentSimulation.getMenuItemStates();
pauseItem.setEnabled(menuItemState.getPause());
replayItem.setEnabled(menuItemState.getReplay());
resetItem.setEnabled(menuItemState.getReset());
startItem.setEnabled(menuItemState.getStart());
pauseButton.setEnabled(menuItemState.getPause());
replayButton.setEnabled(menuItemState.getReplay());
resetButton.setEnabled(menuItemState.getReset());
startButton.setEnabled(menuItemState.getStart());
}
public void dispose() {
synchronized (simulations) {
for (VSSimulation simulation : simulations)
simulation.getSimulationCanvas().stopThread();
}
super.dispose();
}
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 VSSimulationEditor(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"))) {
VSSimulation.VSMenuItemStates menuItemState = currentSimulation.getMenuItemStates();
menuItemState.setStart(false);
menuItemState.setPause(true);
menuItemState.setReset(false);
menuItemState.setReplay(true);
currentSimulation.getSimulationCanvas().play();
updateSimulationMenu();
} else if (sourceText.equals(prefs.getString("lang.pause"))) {
VSSimulation.VSMenuItemStates menuItemState = currentSimulation.getMenuItemStates();
menuItemState.setStart(true);
menuItemState.setPause(false);
menuItemState.setReset(true);
menuItemState.setReplay(true);
currentSimulation.getSimulationCanvas().pause();
updateSimulationMenu();
} else if (sourceText.equals(prefs.getString("lang.reset"))) {
VSSimulation.VSMenuItemStates menuItemState = currentSimulation.getMenuItemStates();
menuItemState.setStart(true);
menuItemState.setPause(false);
menuItemState.setReset(false);
menuItemState.setReplay(false);
currentSimulation.getSimulationCanvas().reset();
updateSimulationMenu();
} else if (sourceText.equals(prefs.getString("lang.replay"))) {
VSSimulation.VSMenuItemStates menuItemState = currentSimulation.getMenuItemStates();
menuItemState.setStart(false);
menuItemState.setPause(true);
menuItemState.setReset(false);
menuItemState.setReplay(true);
currentSimulation.getSimulationCanvas().reset();
currentSimulation.getSimulationCanvas().play();
updateSimulationMenu();
}
}
public void addSimulation(VSSimulation simulation) {
simulation.setLayout(new GridLayout(1, 1, 3, 3));
simulation.setMinimumSize(new Dimension(0, 0));
simulation.setMaximumSize(new Dimension(0, 0));
simulations.add(simulation);
tabbedPane.addTab(prefs.getString("lang.simulation")
+ " " + simulation.getSimulationNum(), simulation);
tabbedPane.setSelectedComponent(simulation);
if (simulations.size() == 1) {
menuEdit.setEnabled(true);
menuFile.setEnabled(true);
menuSimulation.setEnabled(true);
}
}
public void removeSimulation(VSSimulation simulationToRemove) {
if (simulations.size() == 1) {
dispose();
} else {
simulations.remove(simulationToRemove);
tabbedPane.remove(simulationToRemove);
simulationToRemove.getSimulationCanvas().stopThread();
}
}
private void removeCurrentSimulation() {
removeSimulation(currentSimulation);
}
public VSSimulation getCurrentSimulation() {
return currentSimulation;
}
private ImageIcon getImageIcon(String name, String descr) {
java.net.URL imageURL = getClass().getResource("/icons/"+name);
if (imageURL == null)
return new ImageIcon("icons/"+name, descr);
return new ImageIcon(imageURL, descr);
}
}
|