-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinal2.java
More file actions
273 lines (240 loc) · 9.54 KB
/
Final2.java
File metadata and controls
273 lines (240 loc) · 9.54 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.lang.RuntimeException.*;
public class Final2 {
private static HashMap<String,String> engToFrn = new HashMap<String,String>();
private static HashMap<String,String> frnToEng = new HashMap<String,String>();
private static JRadioButton frButton = new JRadioButton("French");
private static JRadioButton esButton = new JRadioButton("Spanish");
public static void main(String[] args) {
JFrame window = new JFrame("Final Project");
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
JPanel top = new JPanel();
top.setLayout(new GridLayout(0,3));
JPanel middle = new JPanel();
middle.setLayout(new GridLayout(2,0));
JPanel bottom = new JPanel();
bottom.setLayout(new GridLayout(0,3));
JTextField searchField = new JTextField();
top.add(searchField);
JButton searchButton = new JButton("Search");
top.add(searchButton);
JLabel translationLabel = new JLabel("");
top.add(translationLabel);
content.add(top,BorderLayout.PAGE_START);
JLabel englishLabel = new JLabel("English:");
JLabel foreignLabel = new JLabel("French:");
JTextField englishTF = new JTextField(); //set preferred size n stuff
middle.add(englishLabel);
middle.add(englishTF);
JTextField foreignTF = new JTextField();
middle.add(foreignLabel);
middle.add(foreignTF);
//add foreign script (e.g. cyrillic, hanzi) option
content.add(middle,BorderLayout.CENTER);
JButton saveButton = new JButton("Save");
bottom.add(saveButton);
JButton displayButton = new JButton("Display");
bottom.add(displayButton);
JButton flashcardButton = new JButton("Flashcards");
bottom.add(flashcardButton);
JButton langSelectionButton = new JButton("Language Selection");
bottom.add(langSelectionButton);
JButton returnButton = new JButton("Return");
bottom.add(returnButton);
content.add(bottom,BorderLayout.PAGE_END);
readDoc();
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String value = engToFrn.get(searchField.getText().trim());
if(value != null) {
translationLabel.setText(value);
} else if(frnToEng.get(searchField.getText().trim()) != null) {
String translation = frnToEng.get(searchField.getText().trim());
translationLabel.setText(translation);
} else {
JFrame errorFrame = new JFrame("Error");
JPanel errorContent = new JPanel();
JOptionPane.showMessageDialog(errorFrame,"Word not found.","Inane error",JOptionPane.ERROR_MESSAGE);
errorFrame.setContentPane(errorContent);
errorFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
errorFrame.setSize(500,300);
errorFrame.setVisible(false);
}
}
});
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String eng = englishTF.getText();
String forn = foreignTF.getText();
//write to file
try {
FileWriter writer = new FileWriter("frenchWords.txt", true);
if (esButton.isSelected()) {
writer = new FileWriter("spanishWords.txt", true);
}
writer.write("\r\n"); // write new line
writer.write(eng + ":" + forn);
writer.close();
} catch (IOException i) {
i.printStackTrace();
}
translationLabel.setText("Your word is saved");
}
});
displayButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel displayContent = new JPanel(new GridLayout(0,1));
JPanel displayPanel = new JPanel(new GridBagLayout());
setDisplayPanelContent(displayPanel);
displayContent.add(displayPanel);
splitPane.setRightComponent(displayContent);
splitPane.setDividerLocation(0.6);
}
});
flashcardButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel flashcardContent = new JPanel(new GridLayout(0,1));
JPanel flashcardPanel = new JPanel();
setFlashcardPanelContent(flashcardPanel);
flashcardContent.add(flashcardPanel);
splitPane.setRightComponent(flashcardContent);
splitPane.setDividerLocation(0.6);
}
});
langSelectionButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel langSelectionContent = new JPanel();
langSelectionContent.setLayout(new GridLayout(0,1));
JPanel langSelectionPanel = new JPanel();
setLangSelectionPanelContent(langSelectionPanel);
langSelectionContent.add(langSelectionPanel);
splitPane.setRightComponent(langSelectionContent);
splitPane.setDividerLocation(0.6);
}
});
returnButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
splitPane.setDividerLocation(0.99999); //sets the divider to be at the right edge of the window; 1 does not work
}
});
esButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
engToFrn = new HashMap<String,String>();
frnToEng = new HashMap<String,String>();
readDoc();
foreignLabel.setText("Spanish");
}
});
frButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
engToFrn = new HashMap<String,String>();
frnToEng = new HashMap<String,String>();
readDoc();
foreignLabel.setText("French");
}
});
splitPane.setLeftComponent(content);
window.setContentPane(splitPane);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(100,100);
window.setSize(800,300);
window.setVisible(true);
}
/** Reads the document provided.
* By default, this document is foreignWords.txt.
* It contains English words and their French translations.
*/
static void readDoc(){
String line;
try{
BufferedReader reader = new BufferedReader(new FileReader("frenchWords.txt"));
if (esButton.isSelected()) {
reader = new BufferedReader(new FileReader("spanishWords.txt"));
}
while ((line = reader.readLine()) != null)
{
String[] parts = line.split(":", 2);
if (parts.length >= 2)
{
String key = parts[0];
String value = parts[1];
engToFrn.put(key,value);
frnToEng.put(value,key);
} else {
System.out.println("ignoring line: " + line);
}
}
} catch (Exception f){
System.out.println("File not found");
}
}
/** Sets the content of the display panel.
* This panel then becomes the right component of the JSplitPane.
* @param displayPanel the display panel
*/
static void setDisplayPanelContent(JPanel displayPanel) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
c.weightx = 0;
JLabel label1 = new JLabel("English Words : French Words");
displayPanel.add(label1, c);
c.anchor = GridBagConstraints.PAGE_END;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.gridy = 1;
c.ipady = 8;
c.weighty = .8;
JScrollPane scoller = getScroller(engToFrn);
displayPanel.add(getScroller(engToFrn), c);
}
/** Sets the content of the flashcard panel.
* This panel then becomes the right component of the JSplitPane.
* @param flashcardPanel the flashcard panel
*/
static void setFlashcardPanelContent(JPanel flashcardPanel) {
flashcardPanel.setLayout(new GridLayout(0,1));
flashcardPanel.add(new JLabel("<html><h1>Coming soon!</h1></html>"));
}
/** Sets the content of the language selection panel.
* This panel then becomes the right component of the JSplitPane.
* @param langSelectionPanel the language selection panel
*/
static void setLangSelectionPanelContent(JPanel langSelectionPanel) {
frButton.setMnemonic(KeyEvent.VK_F);
frButton.setActionCommand("French");
frButton.setSelected(true);
esButton.setMnemonic(KeyEvent.VK_S);
esButton.setActionCommand("Spanish");
ButtonGroup group = new ButtonGroup();
group.add(frButton);
group.add(esButton);
langSelectionPanel.add(frButton);
langSelectionPanel.add(esButton);
/*If we expand upon this program, frenchButton will have an ActionListener setting the language to French, and other buttons for other languages.*/
langSelectionPanel.add(new JLabel("<html>Users will soon be able to<br> store vocabulary for more languages.</html>"));
}
/** Creates a scroller for the displayContent panel from the contents of the hashmap.
* @param map the HashMap of the words to be displayed in the scroller
* @return engScroller the scroller to be displayed
*/
static JScrollPane getScroller(HashMap<String,String> map){
String[] engWords = new String[engToFrn.size()];
int y = 0;
for(HashMap.Entry<String, String> entry : map.entrySet()) {
engWords[y] = entry.getKey().toString() + " : " + entry.getValue().toString();
y++;
}
JList<String> engList = new JList<>(engWords);
engList.setLayoutOrientation(JList.VERTICAL);
JScrollPane engScroller = new JScrollPane(engList);
return engScroller;
}
}