-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.java
More file actions
352 lines (313 loc) · 10.7 KB
/
Dictionary.java
File metadata and controls
352 lines (313 loc) · 10.7 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
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
/** This class represents a Dictionary,
* You can search for an expressions.
* You can load expressions from a file, add expressions, delete, update and save to file
* **/
public class Dictionary extends JFrame implements ActionListener {
private final HashMap<String,String> dictionary; // expression's list as keys, the value is the meaning
private final JButton load = new JButton("Load file");
private final JButton save = new JButton("Save file");
private final JButton add = new JButton("Add Expression");
private final JButton delete = new JButton("Delete Expression");
private final JButton update = new JButton("Update Expression");
private final JButton search = new JButton("Search Expression");
private final JList <Object> allExp; // expressions list
private final DefaultListModel <Object> model; // model for the list
private final JTextArea text; // text area showing expressions
private final JPanel expressions; // holding the text area and the list
private final JScrollPane moreExp; // scroll for the list
public Dictionary()
{
super("Dictionary");
dictionary = new HashMap<>();
JPanel panel = new JPanel();
JPanel searchPanel = new JPanel();
JPanel buttons = new JPanel();
JPanel fileButtons = new JPanel();
JPanel updateButtons = new JPanel();
JPanel textArea = new JPanel();
expressions = new JPanel();
text = new JTextArea("Welcome to your Dictionary",50,20);
model = new DefaultListModel<>();
allExp = new JList<>(model);
moreExp = new JScrollPane(allExp);
setSize(700,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
searchPanel.setLayout(new BorderLayout());
buttons.setLayout(new BorderLayout());
panel.setLayout(new BorderLayout());
updateButtons.setLayout(new BorderLayout());
fileButtons.setLayout(new BorderLayout());
textArea.setLayout(new GridLayout(0,2));
// Adding panels, buttons and listeners
load.addActionListener(this);
save.addActionListener(this);
add.addActionListener(this);
delete.addActionListener(this);
update.addActionListener(this);
search.addActionListener(this);
expressions.add(moreExp);
moreExp.setPreferredSize(new Dimension(expressions.getWidth(),expressions.getHeight()));
allExp.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
String exp = (String)allExp.getSelectedValue();
displayExp(exp);
}
});
allExp.setVisible(true);
allExp.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
textArea.add(expressions);
textArea.add(text);
fileButtons.add(load,BorderLayout.NORTH);
fileButtons.add(save,BorderLayout.SOUTH);
updateButtons.add(add,BorderLayout.NORTH);
updateButtons.add(update,BorderLayout.CENTER);
updateButtons.add(delete,BorderLayout.SOUTH);
buttons.add(updateButtons,BorderLayout.EAST);
buttons.add(fileButtons,BorderLayout.WEST);
searchPanel.add(textArea,BorderLayout.CENTER);
searchPanel.add(search,BorderLayout.AFTER_LAST_LINE);
panel.add(searchPanel);
panel.add(buttons,BorderLayout.SOUTH);
add(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==load)
load();
if(e.getSource()==save) {
save();
}
if(e.getSource()==search)
search();
if(e.getSource()==add)
add();
if(e.getSource()==delete)
delete();
if(e.getSource()==update)
update();
}
// Loading the input from a file. fist line is expression, second line is meaning
private void load()
{
File input;
JFileChooser fileChooser = new JFileChooser();
int res;
int line = 0;
Scanner s;
String exp = "";
String mean = "";
res = fileChooser.showOpenDialog(this);
if(res == JFileChooser.CANCEL_OPTION)
return;
input = fileChooser.getSelectedFile();
try {
s = new Scanner(input);
} catch (FileNotFoundException e) {
System.err.println("File not found");
return;
}
while (s.hasNext()) // scanning all expressions
{
if(line == 0) // expression
{
exp = s.nextLine();
}
else if(line == 1) // meaning
{
mean = s.nextLine();
}
line ++;
if(line == 2) // end of expression and meaning
{
addExp(exp,mean);
line = 0;
}
}
s.close();
sort();
}
// saving the dictionary to file
public void save() {
JFileChooser fileChooser = new JFileChooser();
int res = fileChooser.showSaveDialog(this);
if(res == JFileChooser.CANCEL_OPTION)
return;
if (res == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
FileWriter fw = null;
try {
fw = new FileWriter(file);
} catch (IOException e) {
e.printStackTrace();
}
try {
assert fw != null;
fw.write(this.toString());
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Adding an expression to the dictionary from user's choice
public void add()
{
String exp;
String expMean;
exp = JOptionPane.showInputDialog("Enter Expression:");
if(exp == null || exp.equals(""))
return;
if(dictionary.containsKey(exp)) {
text.setText(exp + " already exists, you can update it");
}
else {
expMean = JOptionPane.showInputDialog("Enter Meaning:");
addExp(exp,expMean);
}
setVisible(true);
}
// Adding an expression from file
private void addExp(String exp, String mean)
{
String upperExp = upperCase(exp); // makes sure the first letter in the expression is capital
String upperMean = upperCase(mean);
if(dictionary.containsKey(upperExp)) {
text.setText(exp + " already exists, you can update it");
}
else {
dictionary.put(upperExp,upperMean);
text.setText("Expression " + upperExp + " added");
sort(); // sorting the list with the new expression
}
setVisible(true);
}
// Changes the first letter in a string to uppercase
private String upperCase(String s)
{
String upper = s.substring(0,1).toUpperCase();
upper += s.substring(1);
return upper;
}
// Deleting an expression from the dictionary
public void delete()
{
String exp;
exp = JOptionPane.showInputDialog("Enter Expression to delete:");
if(exp == null || exp.equals("")) {
text.setText("Expression not entered");
}
else{
exp = upperCase(exp);
if(!dictionary.containsKey(exp)) {
text.setText("Expression " + exp + " does not exist");
}
else {
dictionary.remove(exp);
text.setText(exp + " removed from dictionary");
setVisible(true);
sort();
}
}
}
// Updating an expression's meaning
public void update()
{
String exp;
String mean;
exp = JOptionPane.showInputDialog("Enter Expression to update:");
if(exp == null || exp.equals("")) {
text.setText("Expression not entered");
}
else {
exp = upperCase(exp);
if(!dictionary.containsKey(exp)) {
text.setText("Expression " + exp + " does not exist");
}
else {
mean = JOptionPane.showInputDialog("Enter Expression's new meaning:");
mean = upperCase(mean);
dictionary.put(exp,mean);
sort();
}
setVisible(true);
}
}
// Searching for an expression in the dictionary
public void search()
{
String exp;
exp = JOptionPane.showInputDialog("Enter Expression to search:");
if(exp == null || exp.equals("")) {
text.setText("Expression not entered");
}
else{
exp = upperCase(exp);
if(!dictionary.containsKey(exp)) {
text.setText("Expression " + exp + " does not exist");
}
else {
displayExp(exp);
}
setVisible(true);
}
}
// Sorting the expressions in the displayed list
private void sort()
{
Object [] allKeys;
SortedSet<String> sortedKeys = new TreeSet<>(dictionary.keySet());
allKeys = sortedKeys.toArray();
model.clear();
for(Object ob : allKeys)
{
model.addElement(ob);
}
moreExp.setPreferredSize(new Dimension(expressions.getWidth(),expressions.getHeight()));
setVisible(true);
}
// Displaying expressions and meaning
private void displayExp(String exp)
{
if(exp == null)
{
text.setText("Welcome to Dictionary");
}
else {
String mean = dictionary.get(exp);
text.setText("Expression:\n\n"+exp + "\n\nMeaning:\n\n" + mean);
}
}
// A String representation of the dictionary
public String toString()
{
StringBuilder dic = new StringBuilder();
Object [] allKeys;
String exp;
String mean;
SortedSet<String> sortedKeys = new TreeSet<>(dictionary.keySet());
allKeys = sortedKeys.toArray();
for(Object ob : allKeys)
{
exp = (String)ob;
mean = dictionary.get(ob);
dic.append(exp).append("\n").append(mean).append("\n");
}
return dic.toString();
}
}