-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotepad.java
More file actions
531 lines (500 loc) · 11.6 KB
/
notepad.java
File metadata and controls
531 lines (500 loc) · 11.6 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
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class closeDialog implements WindowListener
{
public void windowOpened(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
}
}
class Notepad_2 extends closeDialog implements ActionListener,ItemListener,WindowListener,TextListener //,TextListener
{
private Frame f;
private MenuBar mb;
private Font ft;
private Menu file,edit,option,others;
private MenuItem neu,open,save,saveAs,quit,cut,copy,paste,find,replace,one,two;
private CheckboxMenuItem toolbar,statusBar;
private TextArea ta;
private File fl=new File("");
private String nameFile="";
private String nameDir="";
private String pasteWord="";
private boolean saveStatus=true;
private Dialog findTxt;
private Button findBut=new Button("Find >>");
private Dialog replaceTxt;
private Button replaceBut=new Button("Replace >>");
private TextField findStr,replaceWhat,replaceWith;
private CheckboxGroup findCbg,replaceCbg;
private Checkbox findUp,findDown,replaceUp,replaceDown;
private int findCount=0,replaceCount=0;
private Button yes,no,cancel;
private Dialog saveExit=new Dialog(f,"File is not Saved",true);
private int findState=2,replaceState=2;
Notepad_2()
{
f=new Frame();
f.setLocation(550,100);
f.setSize(800,600);
mb=new MenuBar();
file=new Menu("File");
edit=new Menu("Edit");
option=new Menu("Options");
others=new Menu("Others");
neu=new MenuItem("New");
open=new MenuItem("Open");
save=new MenuItem("Save");
saveAs=new MenuItem("Save As");
quit=new MenuItem("Quit");
cut=new MenuItem("Cut");
copy=new MenuItem("Copy");
paste=new MenuItem("Paste");
one=new MenuItem("One");
two=new MenuItem("Two");
find=new MenuItem("Find");
replace=new MenuItem("Replace");
toolbar=new CheckboxMenuItem("Toolbar");
statusBar=new CheckboxMenuItem("Statusbar");
ta=new TextArea();
ta.setText("");
ft=new Font("Arial",Font.PLAIN,15);
ta.setFont(ft);
ta.addTextListener(this);
neu.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
saveAs.addActionListener(this);
quit.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
find.addActionListener(this);
replace.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
toolbar.addItemListener(this);
statusBar.addItemListener(this);
findBut.addActionListener(this);
replaceBut.addActionListener(this);
file.add(neu);
file.add(open);
file.addSeparator();
file.add(save);
file.add(saveAs);
file.add(quit);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.addSeparator();
edit.add(find);
edit.add(find);
edit.add(replace);
option.add(toolbar);
option.add(statusBar);
others.add(one);
others.add(two);
option.add(others);
mb.add(file);
mb.add(edit);
mb.add(option);
findTxt=new Dialog(f,"Find");
findTxt.setLayout(new BorderLayout());
findTxt.setLocationRelativeTo(null);
findTxt.setSize(300,100);
Panel pFind=new Panel();
pFind.setLayout(new FlowLayout());
pFind.add(new Label("Find What:"));
findStr=new TextField(20);
pFind.add(findStr);
pFind.add(findBut);
findCbg=new CheckboxGroup();
findUp=new Checkbox("UP",false,findCbg);
findDown=new Checkbox("DOWN",true,findCbg);
findUp.addItemListener(this);
findDown.addItemListener(this);
pFind.add(findUp);
pFind.add(findDown);
findTxt.add(pFind);
GridBagConstraints gbc1=new GridBagConstraints();
gbc1.insets=new Insets(5,3,5,3);
gbc1.anchor=GridBagConstraints.WEST;
replaceTxt=new Dialog(f,"Replace");
replaceTxt.setLayout(new GridBagLayout());
replaceTxt.setLocationRelativeTo(null);
replaceTxt.setSize(450,100);
gbc1.gridx=0;
gbc1.gridy=0;
replaceTxt.add(new Label("Find What:"),gbc1);
replaceWhat=new TextField(20);
replaceWith=new TextField(20);
gbc1.gridx=1;
replaceTxt.add(replaceWhat,gbc1);
gbc1.gridx=2;
gbc1.fill=GridBagConstraints.HORIZONTAL;
replaceTxt.add(replaceBut,gbc1);
gbc1.fill=GridBagConstraints.NONE;
gbc1.gridx=0;
gbc1.gridy=1;
gbc1.gridwidth=2;
replaceTxt.add(new Label("Replace With:"),gbc1);
gbc1.gridwidth=1;
gbc1.gridx=1;
gbc1.gridy=1;
replaceTxt.add(replaceWith,gbc1);
replaceCbg=new CheckboxGroup();
replaceUp=new Checkbox("UP",false,replaceCbg);
replaceDown=new Checkbox("DOWN",true,replaceCbg);
replaceUp.addItemListener(this);
replaceDown.addItemListener(this);
gbc1.anchor=GridBagConstraints.CENTER;
gbc1.gridx=2;
replaceTxt.add(replaceUp,gbc1);
gbc1.gridx=3;
replaceTxt.add(replaceDown,gbc1);
f.setMenuBar(mb);
f.add(ta);
f.setVisible(true);
f.addWindowListener(this);
findTxt.addWindowListener(this);
replaceTxt.addWindowListener(this);
}
public void actionPerformed(ActionEvent e)
{
System.out.println("HI");
Object o=e.getSource();
if(o==neu)
{
ta.setText("");
}
if(o==open)
{
if(saveStatus==false)
{
ta.append("\n\n"+"open save:false");
saveFun();
}
saveStatus=true;
ta.setText("");
FileDialog fd=new FileDialog(f,"Open File",FileDialog.LOAD);
fd.setVisible(true);
nameFile=fd.getFile();
nameDir=fd.getDirectory();
f.setTitle(nameFile);
if(nameFile!=null&&nameFile.length()!=0)
{
ta.setText("");
fl=new File(nameDir,nameFile);
FileInputStream fis;
try
{
fis=new FileInputStream(fl);
int x=0;
while((x=fis.read())!=-1)
{
ta.append(""+(char)x);
}
}
catch(IOException io)
{
ta.setText(io.getMessage());
}
}
}
if(o==save)
{
saveFun();
}
if(o==saveAs)
{
FileDialog fd=new FileDialog(f,"Save As",FileDialog.SAVE);
fd.setVisible(true);
nameFile=fd.getFile();
nameDir=fd.getDirectory();
f.setTitle(nameFile);
fl=new File(nameDir,nameFile);
FileOutputStream fos;
try
{
fos=new FileOutputStream(fl);
char ch[]=ta.getText().toCharArray();
for(char x:ch)
{
fos.write(x);
}
}
catch(IOException io)
{
ta.append("\n\n"+io.getMessage());
}
saveStatus=true;
}
if(o==cut)
{
pasteWord=ta.getSelectedText();
ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
}
if(o==copy)
{
pasteWord=ta.getSelectedText();
}
if(o==paste)
{
if((ta.getSelectionEnd()-ta.getSelectionStart())!=0)
{
ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
}
ta.insert(pasteWord,ta.getSelectionStart());
}
if(o==find)
{
findTxt.setVisible(true);
}
if(o==replace)
{
System.out.println("replace menu");
replaceTxt.setVisible(true);
}
if(o==findBut)
{
if(findState==2)
{
// System.out.println("findBut");
String findx=findStr.getText();
if(findx!=null&&findx.length()!=0)
{
if(findCount==-1)
{
findTxt.add(new Label("End OF File"),"South");
findTxt.validate();
}
String temp=ta.getText();
findCount=temp.indexOf(findx,findCount);
System.out.println(findCount);
ta.select(findCount,findCount+findx.length());
ta.requestFocus();
findCount++;
}
else
{
findTxt.add(new Label("No Argument Is Supplied To Find"),"South");
findTxt.validate();
}
}
if(findState==1)
{
String findx=findStr.getText();
if(findx!=null&&findx.length()!=0)
{
if(findCount==-1)
{
findTxt.add(new Label("Start OF File reached"),"South");
findTxt.validate();
}
String temp=ta.getText();
findCount=temp.lastIndexOf(findx,findCount);
System.out.println(findCount);
ta.select(findCount,findCount+findx.length());
ta.requestFocus();
findCount--;
}
else
{
findTxt.add(new Label("No Argument Is Supplied To Find"),"South");
findTxt.validate();
}
}
}
if(o==replaceBut)
{
if(replaceState==2)
{
String replacex=replaceWhat.getText();
// System.out.println(replacex);
if(replacex!=null&&replacex.length()!=0)
{
String temp=ta.getText();
replaceCount=temp.indexOf(replacex,replaceCount);
if(replaceCount==-1)
{
replaceTxt.add(new Label("End OF File"));
replaceTxt.validate();
}
else
{
ta.replaceRange("",replaceCount,replaceCount+replacex.length());
ta.insert(replaceWith.getText(),replaceCount);
replaceCount++;
}
}
else
{
replaceTxt.add(new Label("No Argument Is Supplied To Replace"));
replaceTxt.validate();
}
}
if(replaceState==1)
{
System.out.println("replaceState1");
String replacex=replaceWhat.getText();
// System.out.println(replacex);
if(replacex!=null&&replacex.length()!=0)
{
String temp=ta.getText();
replaceCount=temp.lastIndexOf(replacex,replaceCount);
if(replaceCount==-1)
{
replaceTxt.add(new Label("Start OF File reached"));
replaceTxt.validate();
}
else
{
ta.replaceRange("",replaceCount,replaceCount+replacex.length());
ta.insert(replaceWith.getText(),replaceCount);
replaceCount++;
}
}
else
{
replaceTxt.add(new Label("No Argument Is Supplied To Replace"));
replaceTxt.validate();
}
}
}
if(o==yes || o==no)
{
System.out.println("Hello");
saveExit.setVisible(false);
saveExit.dispose();
saveFun();
f.setVisible(false);
f.dispose();
System.exit(1);
}
}
public void itemStateChanged(ItemEvent ie)
{
Object o=ie.getSource();
if(o==findUp)
{
findState=1;
findCount=ta.getText().length();
}
if(o==findDown)
findState=2;
if(o==replaceUp)
{
replaceState=1;
if(replaceCount==0)
{
replaceCount=ta.getText().length();
}
}
if(o==replaceDown)
{
System.out.println("replaveDown");
replaceState=2;
}
}
public void textValueChanged(TextEvent e)
{
if(e.getSource()==ta)
{
saveStatus=false;
}
}
public static void main(String args[])
{
new Notepad_2();
}
public void windowClosing(WindowEvent e)
{
Object o=e.getSource();
if(o==f)
{
Window w=e.getWindow();
if(saveStatus==false)
{
saveExit.add(new Label("Do you want to Save it"));
saveExit.setLocation(300,300);
saveExit.setSize(200,100);
saveExit.setLayout(new FlowLayout());
yes=new Button("Save");
no=new Button("Don't Save");
cancel=new Button("Cancel");
saveExit.add(yes);
saveExit.add(no);
saveExit.add(cancel);
yes.addActionListener(this);
no.addActionListener(this);
cancel.addActionListener(this);
saveExit.setVisible(true);
saveExit.validate();
}
saveExit.setVisible(false);
saveExit.dispose();
f.setVisible(false);
f.dispose();
System.exit(1);
}
else
{
Window w=e.getWindow();
w.setVisible(false);
findCount=0;
}
}
private void saveFun()
{
// ta.append("\n\nSave:\t"+fl);
if(fl.exists()==false)
{
ta.append("\n\n"+"fl.exists()==false");
FileDialog fd=new FileDialog(f,"Save File",FileDialog.SAVE);
fd.setVisible(true);
nameFile=fd.getFile();
nameDir=fd.getDirectory();
System.out.println(nameDir+"\n"+nameFile);
if(nameFile==null||nameFile.length()==0&&nameDir==null||nameDir.length()==0)
{
return;
}
f.setTitle(nameFile);
}
FileOutputStream fos;
try
{
// ta.append("\n\n"+nameFile+"\t"+nameDir);
fl=new File(nameDir,nameFile);
fos=new FileOutputStream(fl);
char ch[]=ta.getText().toCharArray();
for(char x:ch)
{
fos.write(x);
}
}
catch(IOException io)
{
ta.append("\n\n"+io.getMessage());
}
saveStatus=true;
}
}