-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaryApplication.java
More file actions
468 lines (413 loc) · 18.8 KB
/
Copy pathDictionaryApplication.java
File metadata and controls
468 lines (413 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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class DictionaryApplication extends JFrame implements DocumentListener {
private static final long serialVersionUID = 1L;
private DefaultListModel<String> vocabularyList;
private JList<String> lexiconList;
private JTextField textBox;
private JTextArea defDisplay;
private JPanel defPanel, lexPanel, taskBarPanel;
private JButton soundButton, addButton, deleteButton, editButton, restartButton, googleTranslateButton;
public DictionaryCommandline dictCom = new DictionaryCommandline();
public DictionaryApplication() {
super("ViEn Dictionary");
setLayout(null);
setFontForWords();
textBox.getDocument().addDocumentListener(this);
setLexiconPanel();
setDefinitionPanel();
setTaskBarPanel();
setSoundButton();
setSizeOfComponent();
add(textBox);
add(lexPanel);
add(defPanel);
add(taskBarPanel);
add(soundButton);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setMinimumSize(new Dimension(600, 650));
setResizable(false);
pack();
setVisible(true);
}
public void runApplication() {
new DictionaryApplication();
}
/**
* Create panel with scroll bar to show vocabulary.
*/
private void setLexiconPanel() {
lexPanel = new JPanel();
lexPanel.setLayout(new BorderLayout());
lexPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
// Create list holding data from file
vocabularyList = new DefaultListModel<>();
addWordsTo(vocabularyList);
lexiconList = new JList<>(vocabularyList);
lexiconList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
lexiconList.setVisibleRowCount(26);
lexiconList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
showDefinition();
}
});
JScrollPane lexiconScroll = new JScrollPane(lexiconList);
lexiconScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
lexiconScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
lexPanel.add(lexiconScroll, BorderLayout.CENTER);
}
/**
* Create panel with scroll bar to show definition.
*/
private void setDefinitionPanel() {
defPanel = new JPanel();
defPanel.setLayout(new BorderLayout());
defPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
defDisplay.setBackground(Color.LIGHT_GRAY);
defDisplay.setEditable(false);
// Set scroll bar for panel
JScrollPane defScroll = new JScrollPane(defDisplay);
defScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
defScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
defPanel.add(defScroll, BorderLayout.CENTER);
}
private void setSoundButton() {
Icon soundIcon = new ImageIcon("Images/sound.png");
soundButton = new JButton(soundIcon);
soundButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TextToSpeech audio = new TextToSpeech();
// Setting the voice
audio.setVoice("cmu-slt-hsmm");
// TTS say something
audio.speak(lexiconList.getSelectedValue(), 2.0f, false, false);
}
});
}
private void setTaskBarPanel() {
taskBarPanel = new JPanel();
taskBarPanel.setLayout(new GridBagLayout());
// taskBarPanel.setBackground(Color.BLACK);
GridBagConstraints gbc = new GridBagConstraints();
// Delete function
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
deleteButton = new JButton("Delete");
taskBarPanel.add(deleteButton, gbc);
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String inputString = JOptionPane.showInputDialog(deleteButton, "Type in your word");
if (inputString != null && !inputString.equals("")) {
if (!dictCom.dictManage.wordPair.containsKey(inputString)) {
JOptionPane.showMessageDialog(null, inputString + " not found");
} else {
dictCom.dictManage.deleteWord(inputString);
dictCom.dictManage.wordPair.remove(inputString);
dictCom.dictManage.tree.delete(inputString);
FileManager fileManager = new FileManager();
try {
fileManager.deleteManager(inputString);
JOptionPane.showMessageDialog(null, "Please restart to save your changes");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
});
// Add function
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridy = 0;
addButton = new JButton("Add");
taskBarPanel.add(addButton, gbc);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createAddWordFrame();
}
});
// Edit function
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 2;
gbc.gridy = 0;
editButton = new JButton("Edit");
taskBarPanel.add(editButton, gbc);
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createEditWordFrame();
}
});
// call Google Translate function
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 3;
gbc.gridy = 0;
googleTranslateButton = new JButton("Google Translate");
taskBarPanel.add(googleTranslateButton, gbc);
googleTranslateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createGoogleTranslateFrame();
}
});
// Restart function
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 4;
gbc.gridy = 0;
restartButton = new JButton("Restart");
// taskBarPanel.add(restartButton, gbc);
restartButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
new DictionaryApplication();
}
});
}
private void setSizeOfComponent() {
lexPanel.setBounds(10, 90, 150, 480);
textBox.setBounds(lexPanel.getX(), 50, lexPanel.getWidth(), 25);
defPanel.setBounds(lexPanel.getWidth() + 20, 50, 400, lexPanel.getHeight() + textBox.getY());
soundButton.setBounds(lexPanel.getWidth() + defPanel.getWidth() - 20, 30, 20, 20);
taskBarPanel.setBounds(0, 0, 600, 20);
}
private void setFontForWords() {
// Set font for lexicon
Font lexiconFont = new Font("Fonts/PlayfairDisplay-VariableFont_wght.ttf", Font.PLAIN, 16);
textBox = new JTextField();
textBox.setFont(lexiconFont);
// Set font for definition
Font defFont = new Font("Fonts/Judson-Regular", Font.PLAIN, 17);
defDisplay = new JTextArea();
defDisplay.setFont(defFont);
}
private void addWordsTo(DefaultListModel<String> listModel) {
dictCom.dictManage.insertFromFile();
for (int i = 0; i < dictCom.dictManage.dict.wordsList.size(); i++) {
listModel.addElement(dictCom.dictManage.dict.vocabList.get(i));
}
}
private void showDefinition() {
String lexiconSelected = lexiconList.getSelectedValue();
String definitionValue = dictCom.dictManage.wordPair.get(lexiconSelected);
defDisplay.setText(definitionValue);
}
private void createGoogleTranslateFrame() {
JTextArea lexBox = new JTextArea();
lexBox.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
JScrollPane lexiconScroll = new JScrollPane(lexBox);
lexiconScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
lexiconScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JTextArea defBox = new JTextArea();
defBox.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
defBox.setEditable(false);
JScrollPane defScroll = new JScrollPane(defBox);
defScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
defScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JLabel langFrom = new JLabel();
JLabel langTo = new JLabel();
DefaultComboBoxModel<String> list = new DefaultComboBoxModel<>();
list.addElement("Choose one option");
list.addElement("English -> Vietnamese");
list.addElement("Vietnamese -> English");
JComboBox<String> choiceBox = new JComboBox<>(list);
choiceBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String option = choiceBox.getSelectedItem().toString();
if (option.equals("English -> Vietnamese")) {
langFrom.setText("English:");
langTo.setText("Vietnamese:");
} else if (option.equals("Vietnamese -> English")) {
langFrom.setText("Vietnamese:");
langTo.setText("English:");
}
}
});
JButton translateButton = new JButton("Translate");
translateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Translator trans = new Translator();
try {
String definition;
String option = choiceBox.getSelectedItem().toString();
if(option.equals("English -> Vietnamese")) {
definition = trans.callUrlAndParseResult("en", "vi", lexBox.getText());
defBox.setText(definition);
}
else if(option.equals("Vietnamese - > English")) {
definition = trans.callUrlAndParseResult("vi", "en", lexBox.getText());
defBox.setText(definition);
}
} catch (Exception e1) {
System.out.println("Cannot access Google Translate API");
e1.printStackTrace();
}
}
});
JPanel myPanel = new JPanel(new GridLayout(3, 2));
myPanel.add(langFrom);
myPanel.add(lexiconScroll);
myPanel.add(langTo);
myPanel.add(defScroll);
myPanel.add(translateButton);
myPanel.add(choiceBox);
myPanel.setPreferredSize(new Dimension(400, 275));
JOptionPane.showConfirmDialog(null, myPanel, "Google Translator", JOptionPane.OK_CANCEL_OPTION);
}
private void createAddWordFrame() {
JTextArea lexBox = new JTextArea();
lexBox.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
JScrollPane lexiconScroll = new JScrollPane(lexBox);
lexiconScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
lexiconScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JTextArea defBox = new JTextArea();
defBox.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
JScrollPane defScroll = new JScrollPane(defBox);
defScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
defScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel myPanel = new JPanel(new GridLayout(2, 2));
myPanel.add(new JLabel("Lexicon: "));
myPanel.add(lexiconScroll);
myPanel.add(new JLabel("Definition: "));
myPanel.add(defScroll);
myPanel.setPreferredSize(new Dimension(300, 200));
int result = JOptionPane.showConfirmDialog(null, myPanel, "Add word", JOptionPane.OK_CANCEL_OPTION);
if(result == JOptionPane.OK_OPTION) {
String newLexicon = lexBox.getText().toLowerCase();
String newDefinition = defBox.getText();
if(newLexicon == null || newDefinition == null || newLexicon.equals("") || newDefinition.equals("")) {
JOptionPane.showMessageDialog(null, "Please finish your word", "Error", JOptionPane.INFORMATION_MESSAGE);
}
else if(dictCom.dictManage.wordPair.containsKey(newLexicon)) {
JOptionPane.showMessageDialog(null, newLexicon + " is already existed", "Invalid", JOptionPane.INFORMATION_MESSAGE);
}
else {
Word word = new Word(newLexicon, newDefinition);
dictCom.dictManage.dict.addWord(word);
dictCom.dictManage.wordPair.put(newLexicon, newDefinition);
dictCom.dictManage.tree.insert(newLexicon);
FileManager fileManager = new FileManager();
try {
fileManager.addManager(newLexicon, newDefinition, dictCom.dictManage.dict);
JOptionPane.showMessageDialog(null, "Please restart to save your changes");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void createEditWordFrame() {
JTextArea lexBox = new JTextArea();
lexBox.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
JScrollPane lexiconScroll = new JScrollPane(lexBox);
lexiconScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
lexiconScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JTextArea defBox = new JTextArea();
defBox.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
JScrollPane defScroll = new JScrollPane(defBox);
defScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
defScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
DefaultComboBoxModel<String> list = new DefaultComboBoxModel<>();
list.addElement("Choose one option");
list.addElement("Edit lexicon only");
list.addElement("Edit definition only");
JComboBox<String> choiceBox = new JComboBox<>(list);
choiceBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String option = choiceBox.getSelectedItem().toString();
if(option.equals("Edit lexicon only")) {
lexBox.setEditable(true);
defBox.setEditable(false);
}
else if (option.equals("Edit definition only")) {
defBox.setEditable(true);
lexBox.setEditable(false);
}
}
});
// Store lexicon value that need to change
JLabel saveItem = new JLabel();
JButton saveButton = new JButton("Get definition");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defBox.setText(dictCom.dictManage.wordPair.get(lexBox.getText()));
saveItem.setText(lexBox.getText());
}
});
JPanel myPanel = new JPanel(new GridLayout(3, 2));
myPanel.add(new JLabel("Lexicon: "));
myPanel.add(lexiconScroll);
myPanel.add(new JLabel("Definition: "));
myPanel.add(defScroll);
myPanel.add(saveButton);
myPanel.add(choiceBox);
myPanel.setPreferredSize(new Dimension(400, 275));
int result = JOptionPane.showConfirmDialog(null, myPanel, "Edit word", JOptionPane.OK_CANCEL_OPTION);
if(result == JOptionPane.OK_OPTION) {
String optionChosen = choiceBox.getSelectedItem().toString();
FileManager fileManager = new FileManager();
if(optionChosen.equals("Edit lexicon only")) {
String oldLexicon = saveItem.getText();
String newLexicon = lexBox.getText();
String oldDefinition = defBox.getText();
try {
fileManager.editLexiconManager(oldLexicon, newLexicon, oldDefinition, dictCom.dictManage.dict);
} catch (IOException e1) {
e1.printStackTrace();
}
}
else if (optionChosen.equals("Edit definition only")) {
String oldLexicon = lexBox.getText();
String newDefinition = defBox.getText();
try {
fileManager.editDefinitionManager(oldLexicon, newDefinition);
} catch (IOException e1) {
e1.printStackTrace();
}
}
JOptionPane.showMessageDialog(null, "Please restart to save your changes");
}
}
private int linearSearch(JList<String> list, String pattern, int size) {
for(int i = 0; i < size; i++) {
int length = list.getModel().getElementAt(i).length();
if(length >= pattern.length()) {
String sub = list.getModel().getElementAt(i).substring(0, pattern.length());
if(sub.equals(pattern)) {
return i;
}
}
}
return -1;
}
@Override
public void insertUpdate(DocumentEvent e) {
// I will optimize this algorithm later (using ternary search tree)
String pattern = textBox.getText();
// Get size of list
int size = lexiconList.getModel().getSize();
// Apply linear search algorithm
int pos = linearSearch(lexiconList, pattern, size);
if (pos != -1) {
lexiconList.setSelectedIndex(pos);
lexiconList.ensureIndexIsVisible(pos);
}
}
@Override
public void removeUpdate(DocumentEvent e) {
insertUpdate(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
insertUpdate(e);
}
}