-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyEditor.java
More file actions
572 lines (554 loc) · 20 KB
/
MyEditor.java
File metadata and controls
572 lines (554 loc) · 20 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.Pattern;
import java.util.Vector;
import java.util.regex.Matcher;
public class MyEditor implements ActionListener{
//all window exits
private void AllExits(){
if(find!=null && find.isVisible()==true){
find.setVisible(false);
find.dispose();
}
if(findnreplace!=null && findnreplace.isVisible()==true){
findnreplace.setVisible(false);
findnreplace.dispose();
}
}
//Window Closing
class MainWindowCloser extends WindowAdapter{
public void windowClosing(WindowEvent e){
File file = new File(path+FileName);
if(t.getText().equals("")){
Window w = e.getWindow();
w.setVisible(false);
w.dispose();
AllExits();
}
//file is already in the system then
else if(file.exists())
{
try
{
//writing content of textarea into file which i have change in it
String str = t.getText();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(str);
bw.close();
}
catch(IOException exp)
{
System.out.println(exp.getMessage());
}
Window w = e.getWindow();
w.setVisible(false);
w.dispose();
AllExits();
} else {
sve = new Frame();
sve.setSize(500, 200);
Label l = new Label("Do you want to Save this file?");
sve.add(l,"North");
Panel p = new Panel();
Button yes = new Button("Yes");
p.add(yes,"West");
yes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1){
sve.setVisible(false);
saving(Save);
Window w = e.getWindow();
w.setVisible(false);
w.dispose();
AllExits();
}
});
Button no = new Button("No");
p.add(no,"East");
no.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1){
sve.setVisible(false);
sve.dispose();
Window w = e.getWindow();
w.setVisible(false);
w.dispose();
AllExits();
}
});
sve.add(p,"South");
sve.addWindowListener(new WindowCloser());
sve.setVisible(true);
//sve.dispose();
}
}
}
class WindowCloser extends WindowAdapter{
public void windowClosing(WindowEvent e){
Window w = e.getWindow();
w.setVisible(false);
w.dispose();
}
}
//Menu Button Actions
public void actionPerformed(ActionEvent e){
//Exit funtion
if(e.getSource()==Exit)
{
ActionEvent saveEvent = new ActionEvent(Save, ActionEvent.ACTION_PERFORMED, "Save");
actionPerformed(saveEvent);
f.dispose();
AllExits();
}
//New Text Editor function
else if(e.getSource()==New)
{
//new MyEditor();
File file = new File(path+FileName);
if(t.getText().equals("")){
AllExits();
}
//file is already in the system then
else if(file.exists())
{
try
{
//writing content of textarea into file which i have change in it
String str = t.getText();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(str);
bw.close();
}
catch(IOException exp)
{
System.out.println(exp.getMessage());
}
AllExits();
FileName = "Untitle*";
f.setTitle(FileName);
t.setText("");
} else {
sve = new Frame();
sve.setSize(500, 200);
Label l = new Label("Do you want to Save this file?");
sve.add(l,"North");
Panel p = new Panel();
yes = new Button("Yes");
p.add(yes,"West");
yes.addActionListener(this);
no = new Button("No");
p.add(no,"East");
no.addActionListener(this);
sve.add(p,"South");
sve.addWindowListener(new WindowCloser());
sve.setVisible(true);
}
}
//opening function
else if(e.getSource()==Open)
{
File file = new File(path+FileName);
if(t.getText().equals("")){
AllExits();
//now opening new file
open();
}
//file is already in the system then
else if(file.exists())
{
try
{
//writing content of textarea into file which i have change in it
String str = t.getText();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(str);
bw.close();
}
catch(IOException exp)
{
System.out.println(exp.getMessage());
}
AllExits();
FileName = "Untitle*";
f.setTitle(FileName);
t.setText("");
//now opening new file
open();
} else {
sve = new Frame();
sve.setSize(500, 200);
Label l = new Label("Do you want to Save this file?");
sve.add(l,"North");
Panel p = new Panel();
Button yes = new Button("Yes");
p.add(yes,"West");
yes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1){
sve.setVisible(false);
sve.dispose();
saving(Save);
FileName = "Untitle*";
f.setTitle(FileName);
t.setText("");
//now opening new file
open();
}
});
Button no = new Button("No");
p.add(no,"East");
no.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1){
sve.setVisible(false);
sve.dispose();
AllExits();
t.setText("");
//now opening new file
open();
}
});
sve.add(p,"South");
sve.addWindowListener(new WindowCloser());
sve.setVisible(true);
//sve.dispose();
}
}
//saving function
else if(e.getSource()==Save)
{
File file = new File(path+FileName);
//file is already in the system then
if(file.exists())
{
try
{
//writing content of textarea into file which i have change in it
String str = t.getText();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(str);
bw.close();
}
catch(IOException exp)
{
System.out.println(exp.getMessage());
}
}
else
{
saving(Save);
}
}
//SaveAs function
else if(e.getSource()==SaveAs)
{
saving(SaveAs);
}
//find funtion
else if(e.getSource()==Find)
{
find = new Frame();
find.setSize(500, 200);
Panel p1 = new Panel();
Label l1 = new Label("Find");
p1.add(l1);
TextField t1 = new TextField(20);
p1.add(t1);
find.add(p1,"North");
Panel p2 = new Panel();
Button FindNext = new Button("Find Next");
Vector<Integer> s = new Vector<>();
Vector<Integer> ed = new Vector<>();
Vector<Integer> selectstart = new Vector<>();
FindNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1){
findingnext(s, ed, t1 , selectstart);
}
});
p2.add(FindNext);
Button close = new Button("Close");
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1){
t.setSelectionStart(-1);
t.setSelectionEnd(-1);
s.clear();
ed.clear();
System.out.println(s);
System.out.println(ed);
find.setVisible(false);
find.dispose();
}
});
p2.add(close);
find.add(p2,"South");
find.addWindowListener(new WindowCloser());
find.setVisible(true);
}
//find and replace funtion
else if(e.getSource()==FindandReplace)
{
findnreplace = new Frame();
findnreplace.setSize(500, 200);
Panel p1 = new Panel();
Label l1 = new Label("Find");
p1.add(l1);
TextField t1 = new TextField(20);
p1.add(t1);
findnreplace.add(p1,"North");
Panel p3 = new Panel();
Panel p4 = new Panel();
Label l2 = new Label("Replace");
p4.add(l2);
TextField t3 = new TextField(20);
p4.add(t3);
p3.add(p4,"North");
findnreplace.add(p3);
Panel p2 = new Panel();
Button FindNext = new Button("Find Next");
Vector<Integer> s = new Vector<>();
Vector<Integer> ed = new Vector<>();
Vector<Integer> selectstart = new Vector<>();
FindNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1){
findingnext(s, ed, t1, selectstart);
}
});
p2.add(FindNext);
Button replace = new Button("Replace");
replace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
if(!t1.getText().equals("") && !t3.getText().equals("")){
if(selectstart.size()>0){
String str = t.getText();
str = str.substring(0, selectstart.get(0))+t3.getText()+str.substring(selectstart.get(0)+t1.getText().length());
t.setText(str);
s.clear();ed.clear();
findingnext(s, ed, t1, selectstart);
} else if(selectstart.size()==0){
findingnext(s, ed, t1, selectstart);
String str = t.getText();
str = str.substring(0, selectstart.get(0))+t3.getText()+str.substring(selectstart.get(0)+t1.getText().length());
t.setText(str);
s.clear();ed.clear();
findingnext(s, ed, t1, selectstart);
}
}
}
});
p2.add(replace);
//finding and replacing all
Button replaceall = new Button("ReplaceAll");
replaceall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
if(!t1.getText().equals("") && !t3.getText().equals("")){
t.setSelectionStart(-1);
t.setSelectionEnd(-1);
String temp = t.getText();
Pattern p = Pattern.compile(t1.getText());
Matcher m = p.matcher(temp);
while(m.find()){
//System.out.println(temp);
temp = temp.substring(0, m.start()) + t3.getText() + temp.substring(m.end());
t.setText(temp);
m = p.matcher(temp).region(m.start()+t3.getText().length(), temp.length());
}
} else{
}
}
});
p2.add(replaceall);
//closing find and replace frame
Button close = new Button("Close");
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1){
t.setSelectionStart(-1);
t.setSelectionEnd(-1);
s.clear();
ed.clear();
findnreplace.setVisible(false);
findnreplace.dispose();
}
});
p2.add(close);
findnreplace.add(p2,"South");
findnreplace.addWindowListener(new WindowCloser());
findnreplace.setVisible(true);
}
else if(e.getSource()==no){
sve.setVisible(false);
sve.dispose();
AllExits();
t.setText("");
}
else if(e.getSource()==yes){
sve.setVisible(false);
sve.dispose();
saving(Save);
FileName = "Untitle*";
f.setTitle(FileName);
t.setText("");
}
}
Frame f,sve,find,findnreplace;
TextArea t;
MenuBar mb;
Menu m1,m2;
Button no,yes;
MenuItem New,Open,Save,SaveAs,Exit,Find,FindandReplace;
String FileName = "Untitle*";
String path = "";
public MyEditor(){
f = new Frame(FileName);
f.setSize(900, 500);
//TextArea
t = new TextArea();
t.setFont(new Font("arial", Font.PLAIN, 24));
f.add(t);
//MenuBar
mb = new MenuBar();
f.setMenuBar(mb);
//Menu
m1 = new Menu("File");
mb.add(m1);
m2 = new Menu("Edit");
mb.add(m2);
//MenuItem
New = new MenuItem("New");
New.addActionListener(this);
m1.add(New);
Open = new MenuItem("Open");
Open.addActionListener(this);
m1.add(Open);
Save = new MenuItem("Save");
Save.addActionListener(this);
m1.add(Save);
SaveAs = new MenuItem("SaveAs");
SaveAs.addActionListener(this);
m1.add(SaveAs);
m1.addSeparator();
Exit = new MenuItem("Exit");
Exit.addActionListener(this);
m1.add(Exit);
Find = new MenuItem("Find");
Find.addActionListener(this);
m2.add(Find);
FindandReplace = new MenuItem("Find & Replace");
FindandReplace.addActionListener(this);
m2.add(FindandReplace);
f.addWindowListener(new MainWindowCloser());
f.setVisible(true);
}
public static void main(String[] args) {
new MyEditor();
}
//saving main funtion
public void saving(MenuItem item){
FileDialog fd = new FileDialog(f,"Save",FileDialog.SAVE);
fd.setVisible(true);
File file = new File(path+FileName);
String str = t.getText();
if(fd.getDirectory()==null && fd.getFile()==null){
} else{
if(file.exists()){
//file.delete();
path = fd.getDirectory();
FileName = fd.getFile();
file = new File(path+FileName);
try{
file.createNewFile();
BufferedWriter wc = new BufferedWriter(new FileWriter(file));
wc.write(str);
wc.close();
} catch(IOException exc){
System.out.println(exc.getMessage());
}
} else {
if(item == Save){
path = fd.getDirectory();
FileName = fd.getFile();
file = new File(path+FileName);
}
try{
file.createNewFile();
BufferedWriter wc = new BufferedWriter(new FileWriter(file));
wc.write(str);
wc.close();
} catch(IOException exc){
System.out.println(exc.getMessage());
}
}
f.setTitle(FileName);
}
}
//findnext main funtion
public void findingnext(Vector<Integer> s,Vector<Integer> ed,TextField t1,Vector<Integer> selectstart){
if(!t1.getText().equals("")){
Pattern p = Pattern.compile(t1.getText());
Matcher m = p.matcher(t.getText());
// if(s.size()>0 && (t1.getText().length()<(ed.elementAt(0)-s.elementAt(0)) ||
// t1.getText().length()>(ed.elementAt(0)-s.elementAt(0))) ){
// s.clear();
// ed.clear();
// }
if(s.size()>0 && (!t1.getText().equals(t.getText().substring(s.elementAt(0), ed.elementAt(0))))){
s.clear();
ed.clear();
}
if(s.size()==0 || !t1.getText().equals(t.getText().substring(s.elementAt(0), ed.elementAt(0)))){
while(m.find()){
if(s.size()>0){
if(!t1.getText().equals(t.getText().substring(s.elementAt(0), ed.elementAt(0)))){
s.clear();
ed.clear();
}
}
s.add(m.start());
ed.add(m.end());
}
}
if(s.size()>0){
t.setSelectionStart(s.get(0));
selectstart.add(0, s.get(0));
t.setSelectionEnd(ed.get(0));
s.remove(0);
ed.remove(0);
} else {
t.setSelectionStart(-1);
t.setSelectionEnd(-1);
}
} else {
t.setSelectionStart(-1);
t.setSelectionEnd(-1);
}
}
//file opening function
private void open(){
//Selecting file
FileDialog fd = new FileDialog(f, "Open", 0);
fd.setTitle("Select file to open");
fd.setVisible(true);
if(fd.getDirectory()==null && fd.getFile()==null){
}else {
path = fd.getDirectory();
FileName = fd.getFile();
f.setTitle(FileName);
File file = new File(path+FileName);
// System.out.println(path);
// System.out.println(FileName);
try
{
//content of file writing in textarea
String str = "";
BufferedReader bis = new BufferedReader(new FileReader(file));
String oneline = bis.readLine();
while(oneline != null){
str += oneline+"\n";
oneline = bis.readLine();
}
t.setText(str);
bis.close();
}
catch(Exception exc)
{
System.out.println(exc.getMessage());
}
}
}
}