-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLMMenubar.java
More file actions
executable file
·393 lines (346 loc) · 12.6 KB
/
LMMenubar.java
File metadata and controls
executable file
·393 lines (346 loc) · 12.6 KB
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
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
/**
* Written by Daniel Barter, Alby Himelick, and Grace Whitmore.
* For CS204 - Software Design
* 4 June 2012
*
* Builds a menu for the frame. Includes a file menu with options to start a
* new NFA, open an NFA, save the current NFA, and close the window, and a
* help menu with a small 'Getting Started' help dialog. Implements
* ActionListener for the menu options and WindowListener to handle the
* closing of the frame.
*
* "LM" objects: allow interaction between the menubar and those parts of
* the GUI
* JFileChooser fileChooser: used for save and open dialogs
* File curFile: the currently opened file, or null if file is unsaved
* File curDir: the directory of the currently opened file, or null if the
* file is unsaved
*/
public class LMMenubar
extends JMenuBar
implements ActionListener, WindowListener
{
private LMWorkspacePanel workspacePanel;
private LMOptionsPanel optionsPanel;
private LMInputToolbar inputToolbar;
private LMMessagePanel messagePanel;
private JFileChooser fileChooser = new JFileChooser();
private File curFile;
private File curDir;
/**
* Constructor for the Menubar. Calls JMenuBar's constructor and buildUI()
* to construct the menu.
*/
public LMMenubar()
{
super();
buildUI();
}
/**
* Sets the workspacePanel. Allows the menu to interact with the
* workspace.
*
* LMWorkspacePanel workspacePanel: the current (and only) workspace
*/
public void setWorkspacePanel(LMWorkspacePanel workspacePanel)
{
this.workspacePanel = workspacePanel;
}
/**
* Sets the OptionsPanel. Allows the menu to interact with the
* options panel.
*
* LMOptionsPanel optionsPanel: the current (and only) options panel
*/
public void setOptionsPanel(LMOptionsPanel optionsPanel)
{
this.optionsPanel = optionsPanel;
}
/**
* Sets the InputToolbar. Allows the menu to interact with the
* input toolbar.
*
* LMInputToolbar inputToolbar: the current (and only) input toolbar
*/
public void setInputToolbar(LMInputToolbar inputToolbar)
{
this.inputToolbar = inputToolbar;
}
/**
* Sets the MessagePanel. Allows the menu to interact with the
* message panel.
*
* LMMessagePanel messagePanel: the current (and only) message panel
*/
public void setMessagePanel(LMMessagePanel messagePanel)
{
this.messagePanel = messagePanel;
}
/**
* Displays a "save changes?" dialog when the user does something that
* would lose the current NFA if the current NFA has been modified.
*
* Returns true if the user cancels the action (so they stay on the current
* NFA) or false if they don't (either choosing to save or not).
*/
private boolean showSaveChangesDialog()
{
if (!workspacePanel.isModified())// && curFile != null && curFile.exists())
{
return false;
}
int returnVal = JOptionPane.showConfirmDialog(workspacePanel,
"Do you want to save changes to the current NFA?",
"Save current file?",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (returnVal == JOptionPane.YES_OPTION)
{
saveNFA();
return false;
}
else if (returnVal == JOptionPane.NO_OPTION)
{
workspacePanel.setModified(false);
return false;
}
else //if (returnVal == JOptionPane.CANCEL_OPTION)
{
return true;
}
// return false;
}
/**
* Called when a menu item is selected. Chooses an action based on the
* item selected. Part of ActionListener implementation.
*
* ActionEvent e: the ActionEvent generated by clicking on a menu item
*/
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if (command.equals("new nfa"))
{
startNewNFA();
}
else if (command.equals("open"))
{
openNFA();
}
else if (command.equals("save"))
{
saveNFA();
}
else if (command.equals("save as"))
{
saveAsNFA();
}
else if (command.equals("close"))
{
close();
}
else if (command.equals("started"))
{
JOptionPane.showMessageDialog(this,
"Language Machine is a tool that lets you draw NFAs (or DFAs) and test them with input strings.\n\nTo draw a machine, use the state tools to add non-accepting and accepting states and connect\nthem with the transition tool. Assign rules to transitions in the Transition Options panel on the\nright. Use the select tool to move states and the delete tool to remove states or transitions.\nLanguage Machine uses the convention that any undrawn transitions go to a non-accepting\nstate which loops on all input.\n\n You can save and load NFAs (with any desired file extention).",
"Getting Started",
JOptionPane.PLAIN_MESSAGE);
}
}
/**
* Starts a new NFA. Asks the user if they want to save changes to the
* current NFA. Only called in actionPerformed().
*/
private void startNewNFA()
{
boolean canceled = showSaveChangesDialog();
if (!canceled)
{
workspacePanel.setNFA(new NFA(new StartState(100, 250, false)));
curFile = null;
curDir = null;
messagePanel.setFilename("Untitled NFA");
optionsPanel.changeOptions("blank");
inputToolbar.clearAcceptanceText();
inputToolbar.getInputTextField().setText("");
workspacePanel.setTool(LMWorkspacePanel.SELECT_TOOL);
workspacePanel.repaint();
}
}
/**
* Attempts to open a file as an NFA. If the load succeeds, loads and draws
* the new NFA. Otherwise, an error message is displayed and the current
* NFA is still open. Only called in actionPerformed().
*/
private void openNFA()
{
boolean canceled = showSaveChangesDialog();
if (!canceled)
{
int choice = fileChooser.showOpenDialog(workspacePanel);
if (choice == JFileChooser.APPROVE_OPTION)
{
curFile = fileChooser.getSelectedFile();
curDir = fileChooser.getCurrentDirectory();
if (curFile.exists())
{
String filename = curFile.getName();
NFA nfa = new NFA();
if (nfa.load(curDir + "/" + filename))
{
workspacePanel.setNFA(nfa);
workspacePanel.setTool(LMWorkspacePanel.SELECT_TOOL);
workspacePanel.repaint();
messagePanel.setFilename(filename);
}
}
else
{
JOptionPane.showMessageDialog(workspacePanel,
"Invalid file name. Failed to open file.",
"Invalid file name",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
/**
* Saves the current NFA. If it hasn't already been saved, opens a 'save as'
* dialog. Otherwise, saves the NFA using the current filename (curFile).
* Only called in actionPerformed().
*/
public void saveNFA()
{
// curFile = fileChooser.getSelectedFile();
// curDir = fileChooser.getCurrentDirectory();
if (curFile != null && curFile.exists())
{
workspacePanel.getNFA().save(curDir + "/" + curFile.getName());
workspacePanel.setModified(false);
messagePanel.setFilename(curFile.getName());
}
else
{
saveAsNFA();
}
}
/**
* Does a 'save as' of the current NFA, allowing the user to select a name
* and location to save the file. Called by saveNFA() when the file hasn't
* yet been saved. Shows a warning message if a file with the given name
* already exists.
*/
public void saveAsNFA()
{
int returnVal = fileChooser.showSaveDialog(workspacePanel);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
curFile = fileChooser.getSelectedFile();
curDir = fileChooser.getCurrentDirectory();
if (!curFile.exists())
{
workspacePanel.getNFA().save(curDir + "/" + curFile.getName());
workspacePanel.setModified(false);
messagePanel.setFilename(curFile.getName());
}
else
{
int choice = JOptionPane.showConfirmDialog(workspacePanel,
"This file already exists. Are you sure you want to overwirte it?",
"File already exists",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (choice == JOptionPane.YES_OPTION)
{
workspacePanel.getNFA().save(curDir + "/" + curFile.getName());
workspacePanel.setModified(false);
messagePanel.setFilename(curFile.getName());
}
}
// workspacePanel.setModified(false);
}
}
/**
* Closes the window. Equivalent to clicking the 'x' in the corner. Shows
* the save dialog if the current NFA is unsaved.
*/
public void close()
{
boolean canceled = showSaveChangesDialog();
if (!canceled)
{
System.exit(0);
}
}
/**
* Defines the action to perform upon clicking the close 'x' in the corner.
* Uses the same closer() method as selecting close from the menu. Part of
* the WindowListener implementation.
*/
public void windowClosing(WindowEvent e)
{
close();
}
/**
* Adds items to the menu to build the menu's UI.
*/
private void buildUI()
{
JMenu fileMenu = new JMenu("File");
// fileMenu.setMnemonic(KeyEvent.VK_F);
fileMenu.setBorder(new EmptyBorder(5, 5, 5, 5));
JMenuItem newNFAItem = new JMenuItem("New NFA");
// newNFAItem.setBorder(new EmptyBorder(5, 5, 5, 5));
newNFAItem.setPreferredSize(new Dimension(150, 25));
newNFAItem.setActionCommand("new nfa");
newNFAItem.addActionListener(this);
fileMenu.add(newNFAItem);
JMenuItem openItem = new JMenuItem("Open...");
openItem.setPreferredSize(new Dimension(150, 25));
openItem.setActionCommand("open");
openItem.addActionListener(this);
fileMenu.add(openItem);
JMenuItem saveItem = new JMenuItem("Save");
saveItem.setPreferredSize(new Dimension(150, 25));
saveItem.setActionCommand("save");
saveItem.addActionListener(this);
fileMenu.add(saveItem);
JMenuItem saveAsItem = new JMenuItem("Save As...");
saveAsItem.setPreferredSize(new Dimension(150, 25));
saveAsItem.setActionCommand("save as");
saveAsItem.addActionListener(this);
fileMenu.add(saveAsItem);
JSeparator separator = new JSeparator();
separator.setPreferredSize(new Dimension(0, 10));
fileMenu.add(separator);
JMenuItem closeItem = new JMenuItem("Close");
// closeItem.setBorder(new EmptyBorder(5, 5, 5, 5));
closeItem.setPreferredSize(new Dimension(150, 25));
closeItem.setActionCommand("close");
closeItem.addActionListener(this);
fileMenu.add(closeItem);
add(fileMenu);
JMenu helpMenu = new JMenu("Help");
helpMenu.setBorder(new EmptyBorder(5, 5, 5, 5));
JMenuItem startedItem = new JMenuItem("Getting Started");
startedItem.setPreferredSize(new Dimension(150, 25));
startedItem.setActionCommand("started");
startedItem.addActionListener(this);
helpMenu.add(startedItem);
add(helpMenu);
}
/**
* Unimplemented WindowListener methods for the WindowListener implementation.
*/
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}