-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLMStateOptionsToolbar.java
More file actions
executable file
·209 lines (188 loc) · 6.82 KB
/
LMStateOptionsToolbar.java
File metadata and controls
executable file
·209 lines (188 loc) · 6.82 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Written by Daniel Barter, Alby Himelick, and Grace Whitmore.
* For CS204 - Software Design
* 6 June 2012
*
* Creates and implements a toolbar for the state options.
*
* Toolbar contains:
* - a title
* - buttons to switch between an accept state and a non-accept state
* - a delete button
*/
public class LMStateOptionsToolbar
extends JToolBar
implements ActionListener
{
private LMOptionsPanel optionsPanel;
private JButton acceptButton;
private JButton nonAcceptButton;
private JButton deleteButton;
private State state;
/**
* The constructor for LMStateOptionsToolbar.
*
* LMOptionsPanel optionsPanel: the options panel that controls the
* visible options toolbar in the panel
*/
public LMStateOptionsToolbar(LMOptionsPanel optionsPanel)
{
super(JToolBar.VERTICAL);
setFloatable(false);
setLayout(new GridBagLayout());
this.optionsPanel = optionsPanel;
buildUI();
}
/**
* Sets the buttons that apply to the selected state. Sets buttons to
* be enabled or not depending on whether the state is an accept state
* or not.
*
* State s: the state that is selected in order to
* bring up the state options panel
*/
public void setStateInfo(State s)
{
deleteButton.setEnabled(true);
state = s;
if (state.isAcceptState())
{
acceptButton.setEnabled(false);
nonAcceptButton.setEnabled(true);
}
else
{
acceptButton.setEnabled(true);
nonAcceptButton.setEnabled(false);
}
if (s == optionsPanel.getWorkspacePanel().getNFA().getStartState())
{
deleteButton.setEnabled(false);
}
}
/**
* Performs all the actions that are possible in the toolbar. Clears the
* acceptance text and message panel text, and sets setModified to true.
*
* ActionEvent e: an action event that allows us to handle actions
* performed in the toolbar
*/
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if (command.equals("accept state"))
{
changeStateToAcceptState();
}
else if (command.equals("non-accept state"))
{
changeStateToNonAcceptState();
}
else if (command.equals("delete"))
{
deleteSelectedState();
}
optionsPanel.getInputToolbar().clearAcceptanceText();
optionsPanel.getMessagePanel().clearText();
optionsPanel.getWorkspacePanel().setModified(true);
}
/**
* Changes the selected state from a non-accept state to an accept state,
* then repaints the workspace panel and sets the tool back to the select tool.
*/
private void changeStateToAcceptState()
{
state.setAcceptState(true);
acceptButton.setEnabled(false);
nonAcceptButton.setEnabled(true);
optionsPanel.getWorkspacePanel().repaint();
optionsPanel.getWorkspacePanel().setTool(LMWorkspacePanel.SELECT_TOOL);
}
/**
* Changes the selected state from a non-accept state to an accept state,
* then repaints the workspace panel and sets the tool back to the select tool.
*/
private void changeStateToNonAcceptState()
{
state.setAcceptState(false);
acceptButton.setEnabled(true);
nonAcceptButton.setEnabled(false);
optionsPanel.getWorkspacePanel().repaint();
optionsPanel.getWorkspacePanel().setTool(LMWorkspacePanel.SELECT_TOOL);
}
/**
* Deletes the selected state when you click the delete button. Sets the
* options panel to blank, and sets the tool back to the select tool.
*/
private void deleteSelectedState()
{
optionsPanel.getWorkspacePanel().getNFA().removeState(state);
optionsPanel.getWorkspacePanel().repaint();
optionsPanel.getWorkspacePanel().setTool(LMWorkspacePanel.SELECT_TOOL);
optionsPanel.changeOptions("blank");
}
/**
* Builds the state options panel.
*/
private void buildUI()
{
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 0;
// Adds the title to the panel, changes the font, and centers it
Font displayFont = new Font("Serif", Font.BOLD, 18);
JLabel cardTitle = new JLabel("State Options");
cardTitle.setFont(displayFont);
cardTitle.setHorizontalAlignment(JLabel.CENTER);
add(cardTitle, c);
// Adds a separator between the title and the accept/non-accept buttons
c.gridy++;
JSeparator separator = new JSeparator();
separator.setPreferredSize(new Dimension(0, 17));
add(separator, c);
// Sets the insets to 3 on every side
c.insets = new Insets(3, 3, 3, 3);
// Adds a make accepting button
c.gridy++;
acceptButton = new JButton("Make Accepting", new ImageIcon("images/acceptstate32.png"));
acceptButton.setDisabledIcon(new ImageIcon("images/state32d.png"));
acceptButton.setVerticalTextPosition(AbstractButton.BOTTOM);
acceptButton.setHorizontalTextPosition(AbstractButton.CENTER);
acceptButton.setActionCommand("accept state");
acceptButton.addActionListener(this);
add(acceptButton, c);
// Adds a make non-accepting button
c.gridy++;
nonAcceptButton = new JButton("Make Non-Accepting", new ImageIcon("images/state32.png"));
nonAcceptButton.setDisabledIcon(new ImageIcon("images/state32d.png"));
nonAcceptButton.setVerticalTextPosition(AbstractButton.BOTTOM);
nonAcceptButton.setHorizontalTextPosition(AbstractButton.CENTER);
nonAcceptButton.setActionCommand("non-accept state");
nonAcceptButton.addActionListener(this);
add(nonAcceptButton, c);
// Sets the insets back to 0 on every side
c.insets = new Insets(0, 0, 0, 0);
// Adds a separator between the accept/non-accept buttons and the delete button
c.gridy++;
separator = new JSeparator();
separator.setPreferredSize(new Dimension(0, 12));
add(separator, c);
// Adds a delete button
c.gridy++;
deleteButton = new JButton("Delete State");
deleteButton.setVerticalTextPosition(AbstractButton.BOTTOM);
deleteButton.setHorizontalTextPosition(AbstractButton.CENTER);
deleteButton.setActionCommand("delete");
deleteButton.addActionListener(this);
add(deleteButton, c);
// I hate Alby for making this work.
// Pushes the panel to the top
c.gridy++;
c.weighty = 1;
JLabel blank = new JLabel();
add(blank, c);
}
}