-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
329 lines (268 loc) · 9.69 KB
/
Copy pathMain.java
File metadata and controls
329 lines (268 loc) · 9.69 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
package com.lyrasaurus.ciphereditor;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.text.*;
// Set up the Editor
class editor extends JFrame implements ActionListener {
// Text component
JTextArea t;
// Frame
JFrame f;
// GUI
editor()
{
// Create a frame
f = new JFrame("Lyra's Cipher Editor");
Cipher cipher = new Cipher(3);
// actually close the program when we hit the "x" button. Not sure why this didn't work for default
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
initEditor();
System.exit(0);
}
});
// Text component
t = new JTextArea();
t.setLineWrap(true);
t.setWrapStyleWord(true);
// Scrollbar
JScrollPane scrollPane = new JScrollPane(t);
// Panel for Buttons
JPanel panel = new JPanel();
// Cipher button and action for it
JButton cipherButton = new JButton("Cipher Selected");
cipherButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (t.getSelectedText() != null) { // See if they selected something
String s = t.getSelectedText();
String cipheredString = cipher.caesar(s);
int start = t.getSelectionStart();
int end = t.getSelectionEnd();
StringBuilder strBuilder = new StringBuilder(t.getText());
strBuilder.replace(start, end, cipheredString);
t.setText(strBuilder.toString());
} else {
JOptionPane.showMessageDialog(f, "No text is selected");
}
}
});
// Decipher button and action for it
JButton decipherButton = new JButton("Decipher Selected");
decipherButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (t.getSelectedText() != null) { // See if they selected something
String s = t.getSelectedText();
String decipheredString = cipher.undoCaesar(s);
int start = t.getSelectionStart();
int end = t.getSelectionEnd();
StringBuilder strBuilder = new StringBuilder(t.getText());
strBuilder.replace(start, end, decipheredString);
t.setText(strBuilder.toString());
} else {
JOptionPane.showMessageDialog(f, "No text is selected");
}
}
});
// Create a menu bar
JMenuBar mb = new JMenuBar();
// Create a menu for menu
JMenu m1 = new JMenu("File");
// Create menu items
JMenuItem mi1 = new JMenuItem("New");
JMenuItem mi2 = new JMenuItem("Open");
JMenuItem mi3 = new JMenuItem("Save");
// Add action listeners
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
// Create a menu for menu
JMenu m2 = new JMenu("Edit");
// Create menu items
JMenuItem mi4 = new JMenuItem("Cut");
JMenuItem mi5 = new JMenuItem("Copy");
JMenuItem mi6 = new JMenuItem("Paste");
// Add action listener
mi4.addActionListener(this);
mi5.addActionListener(this);
mi6.addActionListener(this);
m2.add(mi4);
m2.add(mi5);
m2.add(mi6);
mb.add(m1);
mb.add(m2);
// add buttons to panel
panel.add(cipherButton, BorderLayout.LINE_END);
panel.add(decipherButton, BorderLayout.LINE_END);
// add everything to the frame
f.add(panel, BorderLayout.SOUTH);
f.setJMenuBar(mb);
f.add(scrollPane);
f.setSize(500, 500);
f.setVisible(true);
}
// Class to encrypt and decrypt text
public static class Cipher
{
int offset;
// Constructor
public Cipher(int offset)
{
this.offset = offset;
}
public String caesar(String input) {
String tobeEncrypted = input;
int shift = this.offset;
String cipheredString = "";
// Encrypt the string using caesar cipher
for (int i = 0; i < tobeEncrypted.length(); i++) {
cipheredString += Character.toString(tobeEncrypted.charAt(i) + shift);
}
return cipheredString;
}
public String undoCaesar(String input) {
String tobeDecrypted = input;
int shift = this.offset;
String decipheredString = "";
// Decrypt the string using caesar cipher
for (int i = 0; i < tobeDecrypted.length(); i++) {
decipheredString += Character.toString(tobeDecrypted.charAt(i) - shift);
}
return decipheredString;
}
}
// Function to save a file
public void saveFile() {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
// Invoke the showsSaveDialog function to show the save dialog
int r = j.showSaveDialog(null);
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
File fi = new File(j.getSelectedFile().getAbsolutePath());
try {
// Create a file writer
FileWriter wr = new FileWriter(fi, false);
// Create buffered writer to write
BufferedWriter w = new BufferedWriter(wr);
// Write
w.write(t.getText());
w.flush();
w.close();
}
catch (Exception evt) {
JOptionPane.showMessageDialog(f, evt.getMessage());
}
}
// If the user cancelled the operation
else
JOptionPane.showMessageDialog(f, "Operation Cancelled");
}
// Function to open a file
public void openFile() {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
// Invoke the showsOpenDialog function to show the open dialog
int r = j.showOpenDialog(null);
// If the user selects a file
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
File fi = new File(j.getSelectedFile().getAbsolutePath());
try {
// String
String s1 = "", sl = "";
// File reader
FileReader fr = new FileReader(fi);
// Buffered reader
BufferedReader br = new BufferedReader(fr);
// Initialize sl
sl = br.readLine();
// Take the input from the file
while ((s1 = br.readLine()) != null) {
sl = sl + "\n" + s1;
}
// Set the text
t.setText(sl);
}
catch (Exception evt) {
JOptionPane.showMessageDialog(f, evt.getMessage());
}
}
// If the user cancelled the operation
else
JOptionPane.showMessageDialog(f, "Operation Canceled");
}
// Ask to save before the editor gets nuked
public void initEditor() {
int n = JOptionPane.showConfirmDialog(
f,
"You're about to initialize the editor or close. Save first?",
"Initialize",
JOptionPane.YES_NO_OPTION);
if (n == 0) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
// Invoke the showsSaveDialog function to show the save dialog
int r = j.showSaveDialog(null);
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
File fi = new File(j.getSelectedFile().getAbsolutePath());
try {
// Create a file writer
FileWriter wr = new FileWriter(fi, false);
// Create buffered writer to write
BufferedWriter w = new BufferedWriter(wr);
// Write
w.write(t.getText());
w.flush();
w.close();
t.setText("");
}
catch (Exception evt) {
JOptionPane.showMessageDialog(f, evt.getMessage());
}
}
// If the user cancelled the operation
else
JOptionPane.showMessageDialog(f, "the user cancelled the operation");
}
else {
t.setText("");
}
}
// If a button is pressed on the menu
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("Cut")) {
t.cut();
}
else if (s.equals("Copy")) {
t.copy();
}
else if (s.equals("Paste")) {
t.paste();
}
else if (s.equals("Save")) {
saveFile();
}
else if (s.equals("Open")) {
openFile();
}
else if (s.equals("New")) {
initEditor();
}
}
// Main class
public static void main(String args[])
{
editor e = new editor();
}
}