-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexttoolsGUI.java
More file actions
352 lines (280 loc) · 8.64 KB
/
TexttoolsGUI.java
File metadata and controls
352 lines (280 loc) · 8.64 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
/**
* TexttoolsGUI.java
* @author Herb Wolfe Jr, hwolfe71@gmail.com
*
* A graphical interface for the Texttools class
*
* TODO:
* Implement/fix Menubar
* Add commands to activate a particular panel in results
* Add logic to get selected JRadioButton and display results
* Separate GUI from actual tools
* Read text from a file
* Center panel when opening
* reset label - when invisible, still need to reserve it's space.
* change text instead of setvisible(false) ?
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
class TexttoolsGUI {
private JFrame frame;
private JTextArea textArea;
private JRadioButton palindromeBtn;
private JRadioButton wordsBtn;
private JRadioButton lettersBtn;
private JRadioButton reversalBtn;
private JButton reset;
private JButton display;
private JPanel results;
private JPanel blank;
private JPanel labels;
private JPanel letters;
private JLabel[] letterCnt;
private JLabel resultsLabel;
private JLabel resetLabel;
private static String BLANKPANEL = "BLANKPANEL";
private static String LABELSPANEL = "LABELSPANEL";
private static String LETTERSPANEL = "LETTERSPANEL";
private static String RESETLABELTXT = "Press the reset button to continue";
private static Texttools tools;
private ActionListener resetAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO: set the blank panel to be the active panel
resetTextArea();
}
} ;
private ActionListener displayAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO: display results based on selected JRadioButton
/*
call displayPalindrome(); // completed
or displayReversedString(); // completed
or displayWordCount(); // in progress
or displayLetterCount(); // in progress
*/
if (palindromeBtn.isSelected()) {
displayPalindrome();
} else if (wordsBtn.isSelected()) {
displayWordCount();
} else if (lettersBtn.isSelected()) {
displayLetterCount();
} else if (reversalBtn.isSelected()) {
displayReversedString();
}
}
} ;
public TexttoolsGUI (JFrame f) {
this.frame = f;
Container contents = this.frame.getContentPane();
contents.setLayout(new BorderLayout());
Component resetPane = this.createResetPane();
contents.add(resetPane, BorderLayout.NORTH);
Component text = this.createTextPane();
contents.add(text, BorderLayout.CENTER);
Component buttons = this.createButtons();
contents.add(buttons, BorderLayout.EAST);
Component results = this.createResultsPane();
contents.add(results, BorderLayout.SOUTH);
// create file menu with open and exit options
JMenuBar menu = this.createMenuBar();
//frame.addMenuBar(menu);
tools = new Texttools();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
} // Constructor
/**
* Create choice buttons to check if palindrome, reverse the string, count
* the words, count the letters
* @return - the JPanel containing the buttongroup
*/
private Component createButtons() {
JPanel choices = new JPanel();
ButtonGroup options = new ButtonGroup();
choices.setLayout(new BoxLayout(choices, BoxLayout.Y_AXIS));
this.palindromeBtn = new JRadioButton("Test if a palindrome");
this.reversalBtn = new JRadioButton("Reverse String");
this.wordsBtn = new JRadioButton("Count words");
this.lettersBtn = new JRadioButton("Count letters");
options.add(palindromeBtn);
options.add(reversalBtn);
options.add(wordsBtn);
options.add(lettersBtn);
// TODO: create button group, add buttons to group, add group to panel
choices.add(palindromeBtn);
choices.add(reversalBtn);
choices.add(wordsBtn);
choices.add(lettersBtn);
// TODO: Add padding to buttons, to center them in layout?
reset = new JButton("Reset");
reset.addActionListener(resetAction);
display = new JButton("Display Results");
display.addActionListener(displayAction);
choices.add(reset);
choices.add(display);
return choices;
}
/**
* Create Menubar
* @return - the JMenubar
*/
private JMenuBar createMenuBar() {
JMenuBar fileMenuBar = new JMenuBar();
// TODO: Add file menu with open and exit opens
return fileMenuBar;
}
/**
* Create panel to display reset instructions
* @return - the JPanel containing the instructions
*/
private Component createResetPane() {
JPanel resetPanel = new JPanel();
resetLabel = new JLabel(" ");
resetPanel.add(resetLabel);
return resetPanel;
}
/**
* Create panel to display results
* @return - the JPanel containing the results
*/
private Component createResultsPane() {
int i = 0;
letterCnt = new JLabel[26];
results = new JPanel(new CardLayout());
// Panel to display either word count or is/is not a palindrome
labels = new JPanel();
resultsLabel = new JLabel();
labels.add(resultsLabel);
letters = new JPanel();
letters.setLayout(new GridLayout(2,13));
for (char ch = 'A'; ch <= 'Z'; ch++) {
String str = ch + ": ";
letterCnt[i] = new JLabel(str);
letters.add(letterCnt[i]);
i++;
}
blank = new JPanel();
results.add(blank, BLANKPANEL);
results.add(letters, LETTERSPANEL);
results.add(labels, LABELSPANEL);
return results;
}
/**
* Create panel for text entry
* @return - the JPanel containing the text area
*/
private Component createTextPane() {
// rows, cols?
textArea = new JTextArea(20,50);
return textArea;
}
/**
* Display the number of letters in the string
*/
private void displayLetterCount() {
String str = textArea.getText();
String resultStr = "", tmp;
int [] ltrCount = tools.getLetterCount(str);
int i = 0, total = 0;
for (char ch = 'A'; ch <= 'Z'; ch++) {
tmp = ch + ": " + ltrCount[i];
letterCnt[i].setText(tmp);
total += ltrCount[i];
i++;
resultStr += (tmp + "\t");
if (i % 5 == 0) {
resultStr += "\n";
}
}
// TODO: set the active result panel in results to letters
setEditable(false);
// test to determine if function works
this.output(resultStr);
}
/**
* Adds the text to the textarea - for testing purposes
* @param String - the text to add.
*/
private void output(String resultStr) {
textArea.append("\n\n--\n\n");
textArea.append(resultStr);
}
/**
* Display in the result pane, whether or not the text is a palindrome
*/
private void displayPalindrome() {
String str = textArea.getText();
String resultStr;
if (tools.isPalindrome(str)) {
resultStr = "The text is a palindrome!\n";
} else {
resultStr = "The text is not a palindrome!\n";
}
// test to determine if function works
this.output(resultStr);
resultsLabel.setText(resultStr);
// activate the resultsLabel's panel
// results.setActive(results);
setEditable(false);
}
/**
* Reverses the contents of the text area and display it below the text
*/
private void displayReversedString() {
String str = textArea.getText();
String results = tools.reverseString(str);
// test to determine if function works
this.output("The reversed string is:\n" + results);
setEditable(false);
// results.setActive(results);
}
/**
* Display in the result pane, the number of words in the text area
*/
private void displayWordCount() {
int count = tools.getWordCount(textArea.getText());
String resultStr;
switch (count) {
case 0 :
resultStr = "There are no words in the text area!";
break;
case 1:
resultStr = "There is one word in the text area!";
break;
default :
resultStr = "There are " + count + " words in the text area!";
break;
}
// for testing purposes
this.output(resultStr);
resultsLabel.setText(resultStr);
setEditable(false);
}
/**
* Clears the text area and sets it editable
*/
private void resetTextArea() {
this.textArea.setText("");
this.setEditable(true);
}
/**
* Sets whether or not the user can type in the text area and the status
* of the display button and the reset label
* @param boolean - if the user can type in the text area.
*/
private void setEditable(boolean b) {
textArea.setEditable(b);
display.setEnabled(b);
if (b) {
resetLabel.setText(" ");
} else {
resetLabel.setText(RESETLABELTXT);
}
}
public static void main (String args[]) {
JFrame frame = new JFrame("Text utils");
TexttoolsGUI app = new TexttoolsGUI(frame);
}
} // end class TexttoolsGUI