-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor.java
More file actions
781 lines (683 loc) · 15.7 KB
/
Copy pathEditor.java
File metadata and controls
781 lines (683 loc) · 15.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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Editor extends WindowAdapter implements ActionListener,TextListener
{
Frame f;
MenuBar mb;
Menu m1,m2;
MenuItem nw,opn,sve,svea,ext,fnd,fndrep;
TextArea txt;
SavePrompt sp;
Find findbox;
FindReplace findreplacebox;
NoMatch nomatchfound;
String file_path; // stores path of current file
String file_content; //stores content of textarea
String default_path; // stores path of untitled file
boolean isCancel = true; // In save prompt,if cancelled is clicked then true.
boolean firstrun = true;
Matcher m;
Pattern p;
public Editor()
{
f = new Frame();
f.setSize(800,700);
mb = new MenuBar();
m1 = new Menu("File");
m2 = new Menu("Edit");
nw = new MenuItem("New");
opn = new MenuItem("Open");
sve = new MenuItem("Save");
svea = new MenuItem("Save As");
ext = new MenuItem("Exit");
fnd = new MenuItem("Find");
fndrep = new MenuItem("Find and Replace");
nw.addActionListener(this);
opn.addActionListener(this);
sve.addActionListener(this);
ext.addActionListener(this);
svea.addActionListener(this);
fnd.addActionListener(this);
fndrep.addActionListener(this);
txt = new TextArea();
txt.addTextListener(this);
m1.add(nw);
m1.add(opn);
m1.add(sve);
m1.add(svea);
m1.add(ext);
m2.add(fnd);
m2.add(fndrep);
mb.add(m1);
mb.add(m2);
f.add(txt);
f.addWindowListener(this);
f.setMenuBar(mb);
try
{
File file = new File("temp");
String str;
if(!file.isDirectory())
file.mkdir();
str=file.getAbsolutePath();
str=str+"\\untitled.txt";
f.setTitle("untitled.txt");
FileOutputStream fos = new FileOutputStream(str); //File named "untitled" is created
file_path = str;
default_path = str;
file_content="";
}
catch(Exception e1)
{
System.out.print("Exception in creating default file \t"+e1.getMessage());
}
sp = new SavePrompt();
findbox = new Find();
findreplacebox = new FindReplace();
nomatchfound = new NoMatch();
fnd.setEnabled(false);
fndrep.setEnabled(false);
f.setVisible(true);
}
public void windowClosing(WindowEvent e)
{
fexit2();
}
public void actionPerformed(ActionEvent e)
{
Object ae=e.getSource();
if(ae==nw)
{
System.out.println("New is clicked");
fnew2();
}
if(ae==opn)
{
System.out.println("Open is clicked");
fopen2();
}
if(ae==sve)
{
System.out.println("Save is clicked");
fsave();
}
if(ae==svea)
{
System.out.println("Save As is clicked");
fsaveas();
}
if(ae==ext)
{
System.out.println("Exit is clicked");
fexit2();
}
if(ae==fnd)
{
System.out.println("Find is clicked");
findbox.show();
}
if(ae==fndrep)
{
System.out.println("Find and Replace is clicked");
findreplacebox.show();
}
}
public void fexit2()
{
if(isSaved())
fexit();
else
{
sp.openAskSaveDialog();
if(!isCancel)
fexit();
}
}
public void fexit()
{
f.setVisible(false);
f.dispose();
System.exit(1);
}
public void fopen2()
{
if(isSaved())
{
fopen();
}
else
{
sp.openAskSaveDialog();
if(!isCancel)
fopen();
}
}
public void fnew2()
{
if(isSaved())
{
fnew();
}
else
{
sp.openAskSaveDialog();
if(!isCancel)
fnew();
}
}
public void fopen()
{
//1. Check if file is saved or not
FileDialog fd = new FileDialog(f,"Open file",FileDialog.LOAD);
fd.setVisible(true);
String file_name = fd.getFile();
String dir_name = fd.getDirectory();
//System.out.println("File location:\n"+dir_name+"\\"+file_name);
File opened_file = new File(dir_name+"\\"+file_name);
System.out.println(opened_file.getAbsolutePath());
if(file_name!=null)
{
try
{
FileInputStream fis = new FileInputStream(opened_file);
int ch;
String stropen="";
while((ch=fis.read())!=-1)
{
stropen = stropen + (char)ch;
}
//System.out.println(stropen);
txt.setText(stropen);
f.setTitle(file_name);
file_path = opened_file.getAbsolutePath();
file_content = stropen;
}
catch(Exception e1)
{
System.out.print("Exception at fopen \t"+e1.getMessage());
}
}
else
{
System.out.println("Cancel was clicked");
}
}
public void fsaveas()
{
FileDialog fd = new FileDialog(f,"Save file",FileDialog.SAVE);
fd.setVisible(true);
String file_name = fd.getFile();
String dir_name = fd.getDirectory();
//System.out.println("File location:\n"+dir_name+"\\"+file_name);
File opened_file = new File(dir_name+"\\"+file_name);
System.out.println(opened_file.getAbsolutePath());
System.out.println("File_name = "+file_name);
if(file_name!=null)
{
try
{
FileOutputStream fos = new FileOutputStream(opened_file);
String write = txt.getText();
int ch;
for(int i=0;i<write.length();i++)
{
ch = (int)write.charAt(i);
fos.write(ch);
}
f.setTitle(file_name);
file_path = opened_file.getAbsolutePath();
file_content = write;
}
catch(Exception e1)
{
System.out.print("Exception at fsaveas \t"+e1.getMessage());
}
}
else
{
System.out.println("Cancel is clicked");
isCancel=true;
}
}
public void fsave()
{
if(file_path.equals(default_path))
{
System.out.println("run fsaveas");
fsaveas();
}
else
{
System.out.println("Run save");
//save code
try
{
FileOutputStream fos = new FileOutputStream(file_path);
String write = txt.getText();
int ch;
for(int i=0;i<write.length();i++)
{
ch = (int)write.charAt(i);
fos.write(ch);
}
file_content = write;
}
catch(Exception e1)
{
System.out.print("Exception at fsave \t"+e1.getMessage());
e1.printStackTrace();
}
}
}
public void fnew()
{
try
{
File file = new File("temp");
String str;
if(!file.isDirectory())
file.mkdir();
str=file.getAbsolutePath();
str=str+"\\untitled.txt";
f.setTitle("untitled.txt");
txt.setText("");
FileOutputStream fos = new FileOutputStream(str); //File named "untitled" is created
file_path = str;
default_path = str;
file_content="";
}
catch(Exception e1)
{
System.out.print("Exception in fnew \t"+e1.getMessage());
}
}
public boolean isSaved() // checks if the currently opened file is saved or not. - returns true if file is saved.
{
String currenttext = txt.getText();
//System.out.println("Current Text = \n"+currenttext + "\nfile_content=\n" + file_content);
if(currenttext.equals(file_content))
return true;
else
return false;
}
class SavePrompt extends WindowAdapter implements ActionListener
{
Dialog d;
Button b1,b2,b3;
Label l;
Panel p1;
public SavePrompt()
{
d = new Dialog(f,"Want to save?",Dialog.ModalityType.APPLICATION_MODAL);
d.setSize(200,100);
b1 = new Button("Yes");
b2 = new Button("No");
b3 = new Button("Cancel");
Label l = new Label("Do you want to save?");
Panel p1 = new Panel();
p1.add(b1);
p1.add(b2);
p1.add(b3);
d.add(l);
d.add(p1,"South");
d.addWindowListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
isCancel = true;
}
public void openAskSaveDialog()
{
d.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
Object o = ae.getSource();
if(o==b1)
{
System.out.println("Yes is clicked");
isCancel = false;
fsave();
d.setVisible(false);
}
if(o==b2)
{
System.out.println("No is clicked");
isCancel = false;
d.setVisible(false);
}
if(o==b3)
{
System.out.println("Cancel is clicked");
isCancel = true;
d.setVisible(false);
}
}
}
class Find extends WindowAdapter implements ActionListener,TextListener
{
Dialog find;
Label find_label;
TextField find_tf;
Button find_b1,find_b2;
Panel find_p1;
String currenttxt;
int currentposition;
String currentfind;
int nc;
boolean isFind=true;
public Find()
{
find = new Dialog(f,false);
find.setSize(200,120);
find_label = new Label("Find :");
find_tf = new TextField();
find_b1 = new Button("Find Next");
find_b2 = new Button("Close");
find_b1.setEnabled(false);
find.setTitle("Find :");
find_p1 = new Panel(new FlowLayout());
find_p1.add(find_b1);
find_p1.add(find_b2);
find.add(find_label,"North");
find.add(find_tf,"Center");
find.add(find_p1,"South");
//find.setAlwaysOnTop(true);
/*Adding actionlistener,windowlisterner to all the button in Find*/
find_b1.addActionListener(this);
find_b2.addActionListener(this);
find.addWindowListener(this);
find_tf.addTextListener(this);
}
public void windowClosing(WindowEvent e)
{
find.setVisible(false);
find_tf.setText("");
firstrun = true;
}
public void textValueChanged(TextEvent e)
{
if(find_tf.getText().length()==0)
{
find_b1.setEnabled(false);
}
else
find_b1.setEnabled(true);
}
public void show()
{
find.setVisible(true);
findreplacebox.hide();
firstrun = true;
}
public void actionPerformed(ActionEvent ae)
{
Object o = ae.getSource();
if(o==find_b1)
{
System.out.println("Find Next is clicked");
findit();
}
if(o==find_b2)
{
System.out.println("Close is clicked");
find.setVisible(false);
find_tf.setText("");
firstrun=true;
}
}
public void findit()
{
String newtxt = txt.getText();
String newfindtxt = find_tf.getText();
int newpos = txt.getCaretPosition();
if(firstrun || (newpos!=currentposition) || !(newtxt.equals(currenttxt)) || !(newfindtxt.equals(currentfind)))
{
System.out.println("Inside firstrun");
currenttxt = txt.getText();
currentposition = txt.getCaretPosition();
currentfind = find_tf.getText();
p = Pattern.compile(Pattern.quote(find_tf.getText()));
m = p.matcher(currenttxt);
if(txt.getSelectedText().length()>0)
currentposition = txt.getSelectionEnd();
if(m.find(currentposition))
{
m.find(currentposition);
count_new_line(m.start());
txt.select(m.start()-nc,m.end()-nc);
isFind = true;
currentposition = txt.getCaretPosition();
}
else
{
System.out.println("No match found");
nomatchfound.show();
isFind = false;
}
firstrun = false;
}
else
{
if(m.find())
{
count_new_line(m.start());
txt.select(m.start()-nc,m.end()-nc);
isFind = true;
}
else
{
System.out.print("No match found");
nomatchfound.show();
isFind = false;
}
}
f.toFront();
currentposition = txt.getCaretPosition();
}
public void count_new_line(int n)
{
nc=0;
String str = txt.getText();
int ch;
for(int i=0;i<=n;i++)
{
ch = str.charAt(i);
if(ch==10)
{
nc++;
//System.out.println(nc);
}
}
}
public void hide()
{
find.setVisible(false);
}
}
class FindReplace extends Find implements ActionListener,TextListener
{
Dialog find_replace;
Label fr_label1,fr_label2;
TextField fr_tf1,fr_tf2;
Panel fr_p1,fr_p2;
Button fr_b1,fr_b2,fr_b3,fr_b4;
public FindReplace()
{
find_replace = new Dialog(f,false);
find_replace.setSize(400,200);
fr_label1 = new Label("Find Next");
fr_label2 = new Label("Replace With");
fr_tf1 = new TextField();
fr_tf2 = new TextField();
fr_p1 = new Panel(new GridLayout(4,1));
fr_p2 = new Panel(new FlowLayout());
fr_b1 = new Button("Find Next");
fr_b2 = new Button("Replace");
fr_b3 = new Button("Replace All");
fr_b4 = new Button("Close");
fr_p1.add(fr_label1);
fr_p1.add(fr_tf1);
fr_p1.add(fr_label2);
fr_p1.add(fr_tf2);
fr_p2.add(fr_b1);
fr_p2.add(fr_b2);
fr_p2.add(fr_b3);
fr_p2.add(fr_b4);
find_replace.add(fr_p1,"Center");
find_replace.add(fr_p2,"South");
fr_b1.setEnabled(false);
fr_b2.setEnabled(false);
fr_b3.setEnabled(false);
/*Adding actionlistener to all the button in Find and Replace*/
fr_b1.addActionListener(this);
fr_b2.addActionListener(this);
fr_b3.addActionListener(this);
fr_b4.addActionListener(this);
find_replace.addWindowListener(this);
fr_tf1.addTextListener(this);
/*Find textfield and find dialog now refers to findreplace textfield and findreplace dialog so that findit() runs smoothly in this. */
find_tf = fr_tf1;
find = find_replace;
}
public void show()
{
find_replace.setVisible(true);
findbox.hide();
firstrun = true;
}
public void hide()
{
find_replace.setVisible(false);
}
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if(o==fr_b1)
{
System.out.println("Find Next is clicked");
findit();
}
if(o==fr_b2)
{
System.out.println("Replace is clicked");
fReplace();
}
if(o==fr_b3)
{
System.out.println("Replace All is clicked");
fReplaceAll();
}
if(o==fr_b4)
{
System.out.println("Close is clicked");
find_replace.setVisible(false);
fr_tf1.setText("");
firstrun = true;
}
}
public void fReplace()
{
if(!firstrun)
{
if(isFind)
{
String replacetext = fr_tf2.getText();
int x = txt.getSelectionStart()-nc;
int y = txt.getSelectionEnd()-nc;
txt.replaceRange(replacetext,x,y);
}
}
findit();
}
public void fReplaceAll()
{
findit();
if(isFind)
{
String nice = m.replaceAll(fr_tf2.getText());
txt.setText(nice);
}
}
public void textValueChanged(TextEvent e)
{
if(fr_tf1.getText().length()==0)
{
fr_b1.setEnabled(false);
fr_b2.setEnabled(false);
fr_b3.setEnabled(false);
}
else
{
fr_b1.setEnabled(true);
fr_b2.setEnabled(true);
fr_b3.setEnabled(true);
}
}
public void windowClosing(WindowEvent e)
{
find_replace.setVisible(false);
fr_tf1.setText("");
firstrun = true;
}
}
public void textValueChanged(TextEvent e)
{
if(txt.getText().length()>0)
{
fnd.setEnabled(true);
fndrep.setEnabled(true);
}
else
{
fnd.setEnabled(false);
fndrep.setEnabled(false);
}
}
public static void main(String args[])
{
Editor edit = new Editor();
}
class NoMatch extends WindowAdapter implements ActionListener
{
Dialog d;
Label l;
Button b;
public NoMatch()
{
d = new Dialog(f,true);
d.setSize(100,100);
l = new Label("No Match Found");
b = new Button("OK");
d.add(l);
d.add(b,"South");
b.addActionListener(this);
d.addWindowListener(this);
d.setLocation(new Point(200,200));
}
public void show()
{
d.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if(o==b)
{
d.setVisible(false);
}
}
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
}
}