-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
425 lines (372 loc) · 10.8 KB
/
Copy pathMain.java
File metadata and controls
425 lines (372 loc) · 10.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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
public class Main extends JFrame{
static JTextArea mainArea;
JMenuBar mbar;
JMenu mnuFile,mnuEdit,mnuFormat,mnuHelp;
JMenuItem itmNew,itmOpen,itmSave,itmSaveAs,itmExit;
JMenuItem itmCut,itmCopy,itmPaste;
JCheckBoxMenuItem wordWrap;
JMenuItem itmFontColor,itmFind,itmReplace;
JFileChooser fileChooser;
String fileName,fileContent;
// Undo & Redo
UndoManager undoManager;
UndoAction undoAction;
RedoAction redoAction;
Main(){
initComponent();
itmSave.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
save();
}
});
itmSaveAs.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
saveAs();
}
});
itmOpen.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
open();
}
});
itmNew.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
open_new();
}
});
itmExit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
});
itmCut.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
mainArea.cut();
}
});
itmCopy.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
mainArea.copy();
}
});
itmPaste.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
mainArea.paste();
}
});
// Word Wrap Style
wordWrap.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(wordWrap.isSelected()){
mainArea.setWrapStyleWord(true);
mainArea.setLineWrap(true);
}
else{
mainArea.setLineWrap(false);
mainArea.setWrapStyleWord(false);
}
}
});
itmFontColor.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Color color = JColorChooser.showDialog(rootPane, "Choose Font Color", Color.BLACK);
mainArea.setForeground(color);
}
});
// Undo & Redo
/* getDocument():
Fetches the model associated with the editor. This is primarily for the UI to get at the minimal
amount of state required to be a text editor. Subclasses will return the actual type of the model
which will typically be something that extends Document.
*/
mainArea.getDocument().addUndoableEditListener(new UndoableEditListener(){
@Override
public void undoableEditHappened(UndoableEditEvent e) {
// TODO Auto-generated method stub
undoManager.addEdit(e.getEdit());
undoAction.update();
redoAction.update();
}
});
}
private void initComponent(){
mainArea = new JTextArea();
fileChooser = new JFileChooser(".");
// Text Area added to content panel of JFrame
getContentPane().add(mainArea);
getContentPane().add(new JScrollPane(mainArea),BorderLayout.CENTER);
setTitle("Untitled Document");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800,600);
setVisible(true);
// Undo
undoManager = new UndoManager();
undoAction = new UndoAction();
redoAction = new RedoAction();
// Menu bar & menu
mbar = new JMenuBar();
mnuFile = new JMenu("File");
mnuEdit = new JMenu("Edit");
mnuFormat = new JMenu("Format");
mnuHelp = new JMenu("Help");
// Add icons to Menu Items
// ImageIcon newIcon = new ImageIcon(getClass().getResource("/img/new.gif"));
// Adding icons to menu item not working in linux+openjdk1.7
// ep02/05/1895: itmNew = new JMenuItem("New",newIcon);
// Menu Items
itmNew = new JMenuItem("New");
itmOpen = new JMenuItem("Open");
itmSave = new JMenuItem("Save");
itmSaveAs = new JMenuItem("Save As");
itmExit = new JMenuItem("Exit");
itmCut = new JMenuItem("Cut");
itmCopy = new JMenuItem("Copy");
itmPaste = new JMenuItem("Paste");
wordWrap = new JCheckBoxMenuItem("Word Wrap");
itmFontColor = new JMenuItem("Font Color");
itmFind = new JMenuItem("Find");
itmReplace = new JMenuItem("Replace");
// Adding Shortcut to menu items
itmNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));
itmOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));
itmSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
itmExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,ActionEvent.CTRL_MASK));
itmCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));
itmCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));
itmPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));
// Menu Items are added to corresponding menu
mnuFile.add(itmNew);
mnuFile.add(itmOpen);
mnuFile.addSeparator();
mnuFile.add(itmSave);
mnuFile.add(itmSaveAs);
mnuFile.addSeparator();
mnuFile.add(itmExit);
mnuEdit.add(itmCut);
mnuEdit.add(itmCopy);
mnuEdit.add(itmPaste);
// Undo & Redo
mnuEdit.addSeparator();
mnuEdit.add(undoAction);
mnuEdit.add(redoAction);
mnuFormat.add(wordWrap);
mnuFormat.addSeparator();
mnuFormat.add(itmFontColor);
mnuFormat.addSeparator();
mnuFormat.add(itmFind);
mnuFormat.add(itmReplace);
// Menu added to corresponding Menu Bar
mbar.add(mnuFile);
mbar.add(mnuEdit);
mbar.add(mnuFormat);
mbar.add(mnuHelp);
// Menu Bar is added to J-Frame Menu-bar
setJMenuBar(mbar);
}
private void save(){
PrintWriter pOut = null;
int retVal = -1;
try {
if(fileName==null){
saveAs();
}
else{
pOut = new PrintWriter(new FileWriter(fileName));
String str = mainArea.getText().toString();
StringTokenizer st = new StringTokenizer(str,System.getProperty("line.separator"));
while(st.hasMoreElements()){
pOut.println(st.nextToken());
}
// Show as no other way to ensure occured
JOptionPane.showMessageDialog(rootPane, "File Saved.");
fileContent = mainArea.getText().toString();
}
} catch (IOException e) {
e.printStackTrace();
} finally{
pOut.close();
}
}
private void saveAs(){
PrintWriter pOut = null;
int retVal = -1;
try{
retVal = fileChooser.showSaveDialog(this);
if(retVal == JFileChooser.APPROVE_OPTION)
pOut = new PrintWriter(new FileWriter(fileChooser.getSelectedFile()));
String str = mainArea.getText().toString();
StringTokenizer st = new StringTokenizer(str,System.getProperty("line.separator"));
while(st.hasMoreElements()){
pOut.println(st.nextToken());
}
// Show as no other way to ensure occured
JOptionPane.showMessageDialog(rootPane, "File Saved.");
fileName = fileChooser.getSelectedFile().getName();
setTitle(fileName); // Change
fileContent = mainArea.getText().toString();
} catch(IOException e){
e.printStackTrace();
} finally{
pOut.close();
}
}
private void open(){
int retVal = -1;
FileReader pIn = null;
try{
retVal = fileChooser.showSaveDialog(this);
if(retVal == JFileChooser.APPROVE_OPTION){
mainArea.setText(null);
Reader in = new FileReader(fileChooser.getSelectedFile());
char[] buffer = new char[1000000];
int nchar;
while((nchar = in.read(buffer, 0, buffer.length)) != -1){
mainArea.append(new String(buffer,0,nchar));
}
fileName = fileChooser.getSelectedFile().getName();
setTitle(fileName);
}
} catch(Exception e){
e.printStackTrace();
} finally{
}
}
private void open_new(){
if(!mainArea.getText().equals("") && !mainArea.getText().equals(fileContent)){
if(fileName == null){
int option = JOptionPane.showConfirmDialog(rootPane, "Do you want to save the file?");
if(option == 0)
saveAs();
clear();
}
else{
int option = JOptionPane.showConfirmDialog(rootPane, "Do you want to save?");
if(option==0){ // YES
save();
clear();
}
else if(option==2){ // CANCEL
// No operation
}
else{ // NO
clear();
}
}
}
else
clear();
}
private void clear(){
mainArea.setText(null);
setTitle("Untitled File");
fileName = null;
fileContent = null;
}
public static JTextArea getArea(){
return mainArea;
}
public static void main(String args[]){
Main notepad = new Main();
}
// Undo Class
public class UndoAction extends AbstractAction{
UndoAction(){
super("Undo");
setEnabled(false);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try{
undoManager.undo();
} catch(CannotUndoException e){
e.printStackTrace();
}
update();
redoAction.update();
}
void update(){
if(undoManager.canUndo()){
setEnabled(true);
putValue(Action.NAME,"Undo");
}
else{
setEnabled(false);
putValue(Action.NAME,"Undo");
}
}
}
// Redo Class
public class RedoAction extends AbstractAction{
RedoAction(){
super("Redo");
setEnabled(false);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try{
undoManager.redo();
} catch(CannotRedoException e){
e.printStackTrace();
}
update();
undoAction.update();
}
void update(){
if(undoManager.canRedo()){
setEnabled(true);
putValue(Action.NAME,"Redo");
}
else{
setEnabled(false);
putValue(Action.NAME,"Redo");
}
}
}
/*
An Action can be used to separate functionality and state from a component.
For example, if you have two or more components that perform the same function, consider using an
Action object to implement the function. An Action object is an action listener that provides not
only action-event handling, but also centralized handling of the state of action-event-firing
components such as tool bar buttons, menu items, common buttons, and text fields. The state that
an action can handle includes text, icon, mnemonic, enabled, and selected status.
*/
}