summaryrefslogtreecommitdiff
path: root/sources/simulator/VSSimulatorFrame.java
blob: 33d307443034d3b0f895f7a4f2e4fc611bdf9b93 (plain)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * VS is (c) 2008 by Paul C. Buetow
 * vs@dev.buetow.org
 */
package simulator;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

import core.*;
import prefs.*;
import prefs.editors.*;
import utils.*;

// TODO: Auto-generated Javadoc
/**
 * The Class VSSimulatorFrame.
 */
public class VSSimulatorFrame extends VSFrame implements ActionListener {

    /** The pause item. */
    private JMenuItem pauseItem;

    /** The replay item. */
    private JMenuItem replayItem;

    /** The reset item. */
    private JMenuItem resetItem;

    /** The start item. */
    private JMenuItem startItem;

    /** The pause button. */
    private JButton pauseButton;

    /** The replay button. */
    private JButton replayButton;

    /** The reset button. */
    private JButton resetButton;

    /** The start button. */
    private JButton startButton;

    /** The menu edit. */
    private JMenu menuEdit;

    /** The menu file. */
    private JMenu menuFile;

    /** The menu simulation. */
    private JMenu menuSimulation;

    /** The tool bar. */
    private JToolBar toolBar;

    /** The prefs. */
    private VSPrefs prefs;

    /** The simulations. */
    private Vector<VSSimulator> simulations;

    /** The current simulation. */
    private VSSimulator currentSimulation;

    /** The tabbed pane. */
    private JTabbedPane tabbedPane;
    //private JSlider speedSlider;

    /**
     * Instantiates a new lang.process.removesimulator frame.
     *
     * @param prefs the prefs
     * @param relativeTo the relative to
     */
    public VSSimulatorFrame(VSPrefs prefs, Component relativeTo) {
        super(prefs.getString("lang.name"), relativeTo);
        this.prefs = prefs;
        this.simulations = new Vector<VSSimulator>();

        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);
    }

    /**
     * Creates the menu bar.
     *
     * @return the j menu bar
     */
    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;
    }

    /**
     * Creates the content pane.
     *
     * @return the container
     */
    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 = (VSSimulator) pane.getSelectedComponent();
                currentSimulation.getSimulationCanvas().paint();
                updateEditMenu();
                updateSimulationMenu();
            }
        });

        pane.add(toolBar, BorderLayout.PAGE_START);
        pane.add(tabbedPane, BorderLayout.CENTER);

        return pane;
    }

    /**
     * Update edit menu.
     */
    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();
                VSSimulatorEditor.TAKEOVER_BUTTON = true;
                VSSimulatorEditor simulationEditor = new VSSimulatorEditor(
                    simulationPrefs, VSSimulatorFrame.this, currentSimulation);
                new VSEditorFrame(prefs, VSSimulatorFrame.this, simulationEditor);
            }
        });
        menuEdit.add(globalPrefsItem);
        menuEdit.addSeparator();

        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);
            if (processNum < 10)
                processItem.setAccelerator(KeyStroke.getKeyStroke(0x31+processNum,
                                           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 */
    /**
     * Update simulation menu.
     */
    public synchronized void updateSimulationMenu() {
        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());
    }

    /* (non-Javadoc)
     * @see java.awt.Window#dispose()
     */
    public void dispose() {
        synchronized (simulations) {
            for (VSSimulator simulation : simulations)
                simulation.getSimulationCanvas().stopThread();
        }
        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.getSimulationCanvas().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.getSimulationCanvas().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.getSimulationCanvas().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.getSimulationCanvas().reset();
            currentSimulation.getSimulationCanvas().play();
            updateSimulationMenu();
        }
    }

    /**
     * Adds the simulation.
     *
     * @param simulation the simulation
     */
    public void addSimulation(VSSimulator 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);
        }
    }

    /**
     * Removes the simulation.
     *
     * @param simulationToRemove the simulation to remove
     */
    public void removeSimulation(VSSimulator simulationToRemove) {
        if (simulations.size() == 1) {
            dispose();

        } else {
            simulations.remove(simulationToRemove);
            tabbedPane.remove(simulationToRemove);
            simulationToRemove.getSimulationCanvas().stopThread();
        }
    }

    /**
     * Removes the current simulation.
     */
    private void removeCurrentSimulation() {
        removeSimulation(currentSimulation);
    }

    /**
     * Gets the current simulation.
     *
     * @return the current simulation
     */
    public VSSimulator getCurrentSimulation() {
        return currentSimulation;
    }

    /**
     * Gets the image icon.
     *
     * @param name the name
     * @param descr the descr
     *
     * @return the image icon
     */
    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);
    }

}