-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLMInputToolbar.java
More file actions
executable file
·196 lines (175 loc) · 5.14 KB
/
LMInputToolbar.java
File metadata and controls
executable file
·196 lines (175 loc) · 5.14 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Written by Daniel Barter, Alby Himelick, and Grace Whitmore.
* For CS204 - Software Design
* 4 June 2012
*
* LMInputToolbar is the class that displays executes appropriate behaviors
* for the part of the GUI where the user inputs strings to test on thier
* NFA or DFA.
*
* LMInputToolbar contains:
* - LM Objects workspacePanel, messagePanel: allows the Input toolbar to
* interact with these other GUI components.
* - JTextField inputText: here, the user inputs strings to test with his/her
* NFA
* - JButton enterButton: allows the user to submit text in the above field
* for processing by the NFA
* - JLabel acceptanceLabel: this tells the user whether or not their string
* is accepted by the NFA
* - NFA nfa: the NFA created by the user that is displayed in the workspace.
*/
public class LMInputToolbar
extends JToolBar
implements ActionListener
{
private LMWorkspacePanel workspacePanel;
private LMMessagePanel messagePanel;
private JTextField inputText;
private JButton enterButton;
private JLabel acceptanceLabel;
private NFA nfa;
/**
* Constructor for LMInputToolbar.
*/
public LMInputToolbar()
{
super();
setFloatable(false);
setLayout(new GridBagLayout());
buildUI();
}
/**
* Returns the acceptanceLabel.
*/
public JLabel getAcceptanceLabel()
{
return acceptanceLabel;
}
/**
* Returns the inputTextField.
*/
public JTextField getInputTextField()
{
return inputText;
}
/**
* Sets the workspace and nfa.
*
* LMWorkspacePanel workspacePanel: the workspace being drawn in
*/
public void setWorkspacePanelAndNFA(LMWorkspacePanel workspacePanel)
{
this.workspacePanel = workspacePanel;
nfa = workspacePanel.getNFA();
}
/**
* Sets the message pannel to the one displayed in the frame.
*
* LMMessagePanel messagePanel: the message panel in the frame
*/
public void setMessagePanel(LMMessagePanel messagePanel)
{
this.messagePanel = messagePanel;
}
/**
* Sets the nfa to the one displayed.
*
* NFA nfa: the NFA currently being worked on by the user
*/
public void setNFA(NFA nfa)
{
this.nfa = nfa;
}
/**
* Stops displaying the result of the user's last test.
*/
public void clearAcceptanceText()
{
acceptanceLabel.setText("");
}
/**
* Performs actions based on user input, here the primary action is
* testing strings against NFAs and displaying the result.
*
* ActionEvent e: the action the user performed in the input toolbar
*/
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
acceptanceLabel.setText("");
if (command.equals("enter"))
{
submitInputTextToNFAAndDisplayOutput();
}
}
/**
* Runs the user's NFA on the user's input and displays the result
*/
private void submitInputTextToNFAAndDisplayOutput()
{
for (State s : nfa.getStates())
{
for (Transition t : s.getTransitionsOut())
{
if (t.getRules().size() == 0)
{
messagePanel.setText("WARNING: You have transition(s) without any rules.");
break;
}
}
}
String inputString = inputText.getText();
boolean accepts = nfa.run(inputString);
if (accepts)
{
acceptanceLabel.setForeground(Color.BLUE);
acceptanceLabel.setText("Accepts");
}
else
{
acceptanceLabel.setForeground(Color.RED);
acceptanceLabel.setText("Rejects");
}
inputText.requestFocusInWindow();
inputText.selectAll();
}
/**
* Places GUI objects within the input toolbar.
*/
private void buildUI()
{
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.insets = new Insets(3, 3, 3, 3);
JLabel title = new JLabel("Input your test string:");
//title.setHorizontalAlignment(JLabel.CENTER);
add(title, c);
c.gridx++;
c.ipadx = 497;
inputText = new JTextField();
inputText.setActionCommand("enter");
inputText.addActionListener(this);
inputText.setPreferredSize(new Dimension(0, 100));
add(inputText, c);
c.gridx++;
c.ipadx = 0;
enterButton = new JButton("Enter");
enterButton.setActionCommand("enter");
enterButton.addActionListener(this);
add(enterButton, c);
c.gridx++;
Font displayFont = new Font("Serif", Font.BOLD, 18);
acceptanceLabel = new JLabel();
acceptanceLabel.setFont(displayFont);
add(acceptanceLabel, c);
// I hate Alby for making this work.
c.gridx++;
c.weightx = 1;
JLabel blank = new JLabel();
add(blank, c);
}
}