-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLMWorkspacePanel.java
More file actions
executable file
·615 lines (572 loc) · 18.8 KB
/
LMWorkspacePanel.java
File metadata and controls
executable file
·615 lines (572 loc) · 18.8 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.lang.Math;
/**
* Written by Daniel Barter, Alby Himelick, and Grace Whitmore.
* For CS204 - Software Design
* 4 June 2012
*
* Builds and runs the workspace where the NFA is drawn. Uses Java Graphics
* to draw the NFA. Implements MouseListener and MouseMotionListener to
* allow the drawing of the NFA with the mouse.
*
* "LM" objects: allow interaction between the workspace and those parts of
* the GUI
* State selectedState: the state most recently selected/clicked on by the user
* State initialState: for transitions, the first state selected for the
* transition
* NFA nfa: the NFA represented by the drawing
* int tool: the ID of the currently selected tool in the toolbar
* boolean modified: true if the NFA has been modified since the last save,
* otherwise false
* static final ints: public names for each of the tools to help readability
*/
public class LMWorkspacePanel
extends JPanel
implements MouseListener, MouseMotionListener
{
private LMOptionsPanel optionsPanel;
private LMToolbar toolbar;
private LMInputToolbar inputToolbar;
private LMMessagePanel messagePanel;
private State selectedState;
private State initialState;
private NFA nfa;
private int tool = 0;
private boolean modified;
public static final int
SELECT_TOOL = 0,
STATE_TOOL = 1,
ACCEPT_STATE_TOOL = 2,
TRANSITION_TOOL_INITIAL = 3,
TRANSITION_TOOL_FINAL = 4,
DELETE_TOOL = 5;
/**
* Constructor for the workspace. Calls JPanel's constructor. Sets the NFA
* to be a new, unmodified NFA, sets the mouse listeners, and repaints the
* workspace to display the new NFA.
*/
public LMWorkspacePanel()
{
super();
nfa = new NFA(new StartState(100, 250, false));
modified = false;
addMouseListener(this);
addMouseMotionListener(this);
repaint();
}
/**
* Returns the current NFA.
*/
public NFA getNFA()
{
return nfa;
}
/**
* Sets the current NFA and sets the input toolbar's NFA.
*/
public void setNFA(NFA nfa)
{
this.nfa = nfa;
inputToolbar.setNFA(nfa);
}
/**
* Gets the modified value.
*
* Returns modified, so true if the workspace has been modified since the
* last save and false otherwise.
*/
public boolean isModified()
{
return modified;
}
/**
* Sets the modified value.
*/
public void setModified(boolean b)
{
modified = b;
}
/**
* Sets the messagePanel. Allows the workspace to interact with this part
* of the GUI.
*
* LMMessagePanel messagePanel: the message panel of the frame
*/
public void setMessagePanel(LMMessagePanel messagePanel)
{
this.messagePanel = messagePanel;
}
/**
* Sets the optionsPanel. Allows the workspace to interact with this part
* of the GUI.
*
* LMOptionsPanel optionsPanel: the options panel of the frame
*/
public void setOptionsPanel(LMOptionsPanel optionsPanel)
{
this.optionsPanel = optionsPanel;
}
/**
* Sets the toolbar. Allows the workspace to interact with this part
* of the GUI.
*
* LMToolbar toolbar: the toolbar of the frame
*/
public void setToolbar(LMToolbar toolbar)
{
this.toolbar = toolbar;
}
/**
* Sets the inputToolbar. Allows the workspace to interact with this part
* of the GUI.
*
* LMInputToolbar inputToolbar: the input toolbar of the frame
*/
public void setInputToolbar(LMInputToolbar inputToolbar)
{
this.inputToolbar = inputToolbar;
}
/**
* Adds a state to the workspace and repaints.
*
* State s: the state to add
*/
public void addState(State s)
{
nfa.addState(s);
repaint();
}
/**
* Adds a transition between states and repaints.
*
* Transition t: the transition to add
* State sf: the initial state ('from state')
* State st: the final state ('to state')
*/
public void addTransition(Transition t, State sf, State st)
{
sf.addTransitionOut(t);
st.addTransitionIn(t);
repaint();
}
/**
* Sets the current tool being used in the workspace and highlights that
* tool in the toolbar.
*
* int x: the ID of the tool to use
*/
public void setTool(int x)
{
for (int i = 0; i <= 5; i++)
{
JPanel toolButtonPanel = toolbar.getToolButtonPanel(tool);
toolButtonPanel.setBorder(new LineBorder(new Color(237, 237, 237), 2));
}
if (x >= 0 && x <= 5)
{
tool = x;
JPanel toolButtonPanel = toolbar.getToolButtonPanel(tool);
toolButtonPanel.setBorder(new LineBorder(Color.GRAY, 2));
requestFocusInWindow();
}
else
{
messagePanel.setText("ERROR: Bad tool int, how?");
}
}
/**
* Paints the current NFA (states, transitions, and rules). Called by
* repaint().
*/
public void paint(Graphics g)
{
g.setColor(Color.DARK_GRAY);
g.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
g.setColor(Color.WHITE);
g.fillRoundRect(3, 3, getWidth() - 6, getHeight() - 6, 10, 10);
for (State s : nfa.getStates())
{
s.draw(g);
}
for (State s : nfa.getStates())
{
for (Transition t : s.getTransitionsOut())
{
t.draw(g);
}
}
for (State s : nfa.getStates())
{
for (Transition t : s.getTransitionsOut())
{
t.drawRule(g);
}
}
}
/**
* Performs an action when the mouse is pressed. The action is determined
* by the current tool. Part of the MouseListener implementation.
*
* MouseEvent e: the event generate by the mouse press
*/
public void mousePressed(MouseEvent e)
{
int x = Math.min(getWidth() - State.RADIUS - 5, Math.max(State.RADIUS + 5, e.getX()));
int y = Math.min(getHeight() - State.RADIUS - 5, Math.max(State.RADIUS + 5, e.getY()));
unselectAll();
inputToolbar.clearAcceptanceText();
messagePanel.clearText();
if (tool == SELECT_TOOL)
{
selectStateOrTransitionWithinRadius(x, y);
}
else if (tool == STATE_TOOL)
{
createStateOrConvertStateWithinRadius(x, y);
}
else if (tool == ACCEPT_STATE_TOOL)
{
createAcceptStateOrConvertStateWithinRadius(x, y);
}
else if (tool == TRANSITION_TOOL_INITIAL)
{
selectInitialStateForTransition(x, y);
}
else if (tool == TRANSITION_TOOL_FINAL)
{
selectFinalStateForTransitionOrEditExistingTransition(x, y);
}
else if (tool == DELETE_TOOL)
{
deleteStateOrTransition(x, y);
}
}
/**
* Performs an action when the mouse is dragged. Part of the MouseMotionListener
* implementation.
*
* MouseEvent e: the event generated by the mouse drag
*/
public void mouseDragged(MouseEvent e)
{
int x = Math.min(getWidth() - State.RADIUS - 5, Math.max(State.RADIUS + 5, e.getX()));
int y = Math.min(getHeight() - State.RADIUS - 5, Math.max(State.RADIUS + 5, e.getY()));
if (tool == SELECT_TOOL || tool == STATE_TOOL ||
tool == ACCEPT_STATE_TOOL)
{
dragState(x, y);
}
}
/**
* Uses the distance forumla to find a state within the given radius of the
* given point.
*
* int x: the x coordinate of the point
* int y: the y coordinate of the point
* int r: the radius to search
*
* Returns a state if one is found and null otherwise.
*/
private State findStateInRadius(int x, int y, int r)
{
for (State s : nfa.getStates())
{
if (Math.sqrt(Math.pow(x - s.getXPos(), 2) + Math.pow(y - s.getYPos(), 2)) < r)
{
return s;
}
}
return null;
}
/**
* Uses the distance formula to find a select circle of a transition within
* the given radius of the given point.
*
* int x: the x coordinate of the point
* int y: the y coordinate of the point
* int r: the radius to search
*
* Returns a transition if one is found and null otherwise.
*/
private Transition findTransitionInRadius(int x, int y, int r)
{
for (State s : nfa.getStates())
{
for (Transition t : s.getTransitionsOut())
{
if (Math.sqrt(Math.pow(x - t.getSelectOvalX(), 2) + Math.pow(y - t.getSelectOvalY(), 2)) < r)
{
return t;
}
}
}
return null;
}
/**
* Finds a transition between two given states.
*
* State from: the first state
* State to: the second state
*
* Returns a transition if one is found and null otherwise.
*/
public Transition findTransitionBetween(State from, State to)
{
for (Transition t : from.getTransitionsOut())
{
if (t.getToState() == to)
{
return t;
}
}
return null;
}
/**
* Unselects any selected object in the workspace. Called whenever the
* mouse is pressed in the workspace. Repaints the workspace.
*/
public void unselectAll()
{
for (State s : nfa.getStates())
{
s.setSelected(false);
s.setTransitionSelected(false);
for (Transition t : s.getTransitionsOut())
{
t.setSelected(false);
}
}
optionsPanel.changeOptions("blank");
repaint();
}
/**
* Selects an object within a certain radius of the given point. Only
* called in mousePressed().
*
* int x: the x coordinate of the point
* int y: the y coordinate of the point
*/
private void selectStateOrTransitionWithinRadius(int x, int y)
{
selectedState = findStateInRadius(x, y, State.RADIUS + 2);
Transition selectedTransition = findTransitionInRadius(x, y, Transition.SELECT_RADIUS + 2);
if (selectedTransition != null)
{
selectedTransition.setSelected(true);
optionsPanel.changeOptions("transition");
optionsPanel.getTransitionToolbar().setTransitionInfo(selectedTransition);
optionsPanel.getTransitionToolbar().getRuleTextField().requestFocusInWindow();
}
else if (selectedState != null)
{
selectedState.setSelected(true);
optionsPanel.changeOptions("state");
optionsPanel.getStateToolbar().setStateInfo(selectedState);
requestFocusInWindow();
}
}
/**
* Creates a new (non-accept) state or selects a state if you click on one.
* Converts accept states to non-accept states. Only called in mousePressed().
*
* int x: the x coordinate of the clicked point
* int y: the y coordinate of the clicked point
*/
private void createStateOrConvertStateWithinRadius(int x, int y)
{
if (findStateInRadius(x, y, 30) == null && findTransitionInRadius(x, y, 35) == null)
{
selectedState = new State(x, y, false);
addState(selectedState);
optionsPanel.changeOptions("state");
optionsPanel.getStateToolbar().setStateInfo(selectedState);
modified = true;
messagePanel.setFileModified();
}
else if (findStateInRadius(x, y, 30) != null)
{
selectedState = findStateInRadius(x, y, 30);
if (selectedState.isAcceptState())
{
selectedState.setAcceptState(false);
}
selectedState.setSelected(true);
optionsPanel.changeOptions("state");
optionsPanel.getStateToolbar().setStateInfo(selectedState);
modified = true;
messagePanel.setFileModified();
}
else
{
messagePanel.setText("ERROR: Don't overlap these objects.");
}
requestFocusInWindow();
}
/**
* Creates a new accept state or selects a state if you click on one.
* Converts non-accept states to accept states. Only called in mousePressed().
*
* int x: the x coordinate of the clicked point
* int y: the y coordinate of the clicked point
*/
private void createAcceptStateOrConvertStateWithinRadius(int x, int y)
{
if (findStateInRadius(x, y, 60) == null && findTransitionInRadius(x, y, 35) == null)
{
selectedState = new State(x, y, true);
addState(selectedState);
optionsPanel.changeOptions("state");
optionsPanel.getStateToolbar().setStateInfo(selectedState);
modified = true;
messagePanel.setFileModified();
}
else if (findStateInRadius(x, y, 30) != null)
{
selectedState = findStateInRadius(x, y, 30);
if (!selectedState.isAcceptState())
{
selectedState.setAcceptState(true);
}
selectedState.setSelected(true);
optionsPanel.changeOptions("state");
optionsPanel.getStateToolbar().setStateInfo(selectedState);
modified = true;
messagePanel.setFileModified();
}
else
{
messagePanel.setText("ERROR: Don't overlap objects.");
}
requestFocusInWindow();
}
/**
* Selects the first state when drawing a transition. Also allows selecting
* existing transitions. Only called in mousePressed().
*
* int x: the x coordinate of the clicked point
* int y: the y coordinate of the clicked point
*/
private void selectInitialStateForTransition(int x, int y)
{
initialState = findStateInRadius(x, y, 25);
Transition selectedTransition = findTransitionInRadius(x, y, Transition.SELECT_RADIUS + 2);
if (initialState != null)
{
tool = TRANSITION_TOOL_FINAL;
initialState.setTransitionSelected(true);
}
else if (selectedTransition != null)
{
selectedTransition.setSelected(true);
optionsPanel.changeOptions("transition");
optionsPanel.getTransitionToolbar().setTransitionInfo(selectedTransition);
optionsPanel.getTransitionToolbar().getRuleTextField().requestFocusInWindow();
}
}
/**
* Selects the final state when drawing a transition. Even if no state is
* clicked, sets the tool back to the initial transition select tool. Only
* called in mousePressed().
*
* int x: the x coordinate of the clicked point
* int y: the y coordinate of the clicked point
*/
private void selectFinalStateForTransitionOrEditExistingTransition(int x, int y)
{
selectedState = findStateInRadius(x, y, 25);
if (selectedState != null)
{
Transition transition = findTransitionBetween(initialState, selectedState);
if (transition != null)
{
transition.setSelected(true);
messagePanel.setText("You may modify this transition in the Transition Options panel.");
optionsPanel.changeOptions("transition");
optionsPanel.getTransitionToolbar().setTransitionInfo(transition);
optionsPanel.getTransitionToolbar().getRuleTextField().requestFocusInWindow();
}
else
{
transition = new Transition(initialState, selectedState);
Transition oppositeTransition = findTransitionBetween(selectedState, initialState);
if (oppositeTransition != null && selectedState != initialState)
{
transition.setTangent(true);
oppositeTransition.setTangent(true);
}
addTransition(transition, initialState, selectedState);
optionsPanel.changeOptions("transition");
optionsPanel.getTransitionToolbar().setTransitionInfo(transition);
optionsPanel.getTransitionToolbar().getRuleTextField().requestFocusInWindow();
modified = true;
messagePanel.setFileModified();
}
}
tool = TRANSITION_TOOL_INITIAL;
}
/**
* Deletes the clicked object. Only called in mousePressed().
*
* int x: the x coordinate of the clicked point
* int y: the y coordinate of the clicked point
*/
private void deleteStateOrTransition(int x, int y)
{
selectedState = findStateInRadius(x, y, State.RADIUS + 2);
Transition selectedTransition = findTransitionInRadius(x, y, Transition.SELECT_RADIUS + 2);
if (selectedTransition != null)
{
if (selectedTransition.isTangent())
{
Transition oppositeTransition = findTransitionBetween(selectedTransition.getToState(), selectedTransition.getFromState());
oppositeTransition.setTangent(false);
}
nfa.removeTransition(selectedTransition);
modified = true;
messagePanel.setFileModified();
}
else if (selectedState != null && selectedState != nfa.getStartState())
{
nfa.removeState(selectedState);
modified = true;
messagePanel.setFileModified();
}
requestFocusInWindow();
}
/**
* Drags the clicked state to a new point. Only called in mouseDragged().
*
* int x: the x coordinate of the new point
* int y: the y coordinate of the new point
*/
private void dragState(int x, int y)
{
if (selectedState != null)
{
selectedState.move(x, y);
for (Transition t : selectedState.getTransitionsOut())
{
t.moveStart();
}
for (Transition t : selectedState.getTransitionsIn())
{
t.moveEnd();
}
modified = true;
messagePanel.setFileModified();
repaint();
}
}
/**
* Unimplemented MouseListener and MouseMotionListener methods.
*/
public void mouseMoved(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}