-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathActions.java
More file actions
497 lines (454 loc) · 15.5 KB
/
Copy pathActions.java
File metadata and controls
497 lines (454 loc) · 15.5 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
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*; public class Actions {
//declaration of the private variables used in the program
private int returnVal;
//for JFileChooser
private int option;
//for using it into JOptionPane
private String fileContent = null;
//to get the text from the text area
private String fileName = null;
//for using the name of the file
private JFileChooser jfc = new JFileChooser(".");
//for using a open & save dialog
private ExampleFileFilter filter = new ExampleFileFilter();
//for filtering the file
Notepad n;
//for using the object in the Notepad.java
public Fonts font = new Fonts();
public Actions(Notepad n){
this.n = n;
}
/**
*If we want to write a new text, first we want to know if the text area is empty or not,
*second we want to know if the text was saved or not.
*If the text area isn't empty & the text wasn't saved before this time, then the program display ->
*for the user an option for saving the text in a new file or in the same file
*/
public void neW(){
/**
*if the text area isn't empty & if the text area hasn't
*a text not saved before (fileContent != null)
*/
if(!n.getTextArea().getText().equals("") && !n.getTextArea().getText().equals(fileContent)){
/**
*here, we have two things, first if the text wasn't be opened or saved before
*second if the text was be opened or saved
*/
//if there was no file opened or saved
if(fileName == null){
/**
*this method has 3 options (1 = YES, 2 = NO, 3 = Cancel)
*this is an option pop up to the user, for saving the old text or not
*/
option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??");
//if the user click on YES button,
//then the program will save the text & make a new text area
if(option == 0){
//to save the text into a new file
saveAs();
//to create new getTextArea() after saving the old getTextArea()
n.getTextArea().setText("");
}
//if the user click on NO button
if(option == 1){
//to create new getTextArea() without saving the old getTextArea()
n.getTextArea().setText("");
}
}
//if there was a text which was be opend or saved
else{
/**
*this method has 3 options (1 = YES, 2 = NO, 3 = Cancel)
*this is an option pop up to the user, for saving the old text or not
*/
option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??");
/**
*if the user click on YES button,
*then the program will save the text into the old file &
*make a new text area,,,
*/
if(option == 0){
save();
//to create new getTextArea() after saving the old getTextArea()
n.getTextArea().setText("");
}
//if the user click on NO button
if(option == 1){
//to create new getTextArea() without saving the old getTextArea()
n.getTextArea().setText("");
}
}
}
/**
*if the text was be opened or saved before but wasn't be changed,
*and we want to write a new text,,, this option will be actived
*/
else{
n.getTextArea().setText("");
}
n.setTitle("Untitled - JAVA??? Notepad");
}
/**
*If we want to open a new text, first we want to know if the text area
* is empty or not, second we want to kwnow if the text was saved or not.
*If the text area isn't empty & the text wasn't saved befor this time,
*then the program display for the user an option for saving the text
*in a new file or in the same file
*/
public void opeN(){
/**
*if the text area isn't empty & if the text area hasn't a text which
*not saved befor (fileContent != null)
*/
if(!n.getTextArea().getText().equals("") && !n.getTextArea().getText().equals(fileContent)){
/**
*here, we have 2 thing, first if the text wasn't be opened or saved befor
*second if the text was be opened or saved
*/
//if there was no file opened or saved
if(fileName == null){
/**
*this method has 3 options (1 = YES, 2 = NO, 3 = Cancel)
*this is an option pop up to the user, for saving the old text or not
*/
option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??");
//if the user click on YES button,
//then the program will be saved the text & opened a new documents
if(option == 0){
//for saving the text in a new file
saveAs();
//for opening the new documents
open();
}
/**
*if the user click on NO button,
*the program will be opened the new documents
*/
if(option == 1){
//for opening the new documents
open();
}
}
//if there was a text which was be opend or saved
else{
/**
*this method has 3 options (1 = YES, 2 = NO, 3 = Cancel)
*this is an option pop up to the user, for saving the old text or not
*/
option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??");
/**
*if the user click on YES button,
*then the program will save the text into the same file &
*open the new documents
*/
if(option == 0){
//for saving the text into the same file
save();
//for opening the new documents
open();
}
/**
*if the user click on NO button,
*the program will be opened the new documents
*/
if(option == 1){
//for opening the new documents
open();
}
}
}
/**
*if the text was be opened or saved before but wasn't be changed,
*and we want to open a new document,,, this option will be actived
*/
else{
//for opening the new documents
open();
}
}
/**
*THIS IS FOR SAVE ACTION, SaveAs ACTION has saveAs() method
*If we want to save a new text, then we want to know
*if the text was saved befor or not.
*If the text wasn't be saved befor, then we will use saveAs()
*If the text was be save befor, then we will use save()
*/
public void savE(){
/**
*if String fileName is null, then using saveAs().
*Because there was no file was be saved
*/
if(fileName == null){
saveAs();
}
/**
*if String fileName has a String (file name), then using save().
*Because we want to save the new text in the same file.
*if the user want to save the all text (old & new text) in a new file, ->
*he can presses SaveAs button (@saveAs())
*/
else{
save();
}
}
/**
*THIS FROM SUN??? WEBSITE (@Print.java)
*if we want to print the text, we can do this by print method
*/
public void prinT(){
//import printer class
Print.printComponent(n.getTextArea());
}
/**
*If we want to exit from the program,
*first we want to know if the text area is empty or not,
*second we want to kwnow if the text was saved or not.
*If the text area isn't empty & the text wasn't saved befor this time,
*then the program display ->
*for the user an option for saving the text in a new file or in the same file
*/
public void exiT(){
/**
*if the text area isn't empty & if the text area hasn't a text which
*not saved befor (fileContent != null)
*/
if(!n.getTextArea().getText().equals("") && !n.getTextArea().getText().equals(fileContent)){
//if there was no file opened or saved
if(fileName == null){
/**
*this method has 3 options (1 = YES, 2 = NO, 3 = Cancel)
*this is an option pop up to the user, for saving the old text or not
*/
option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??");
/**
*if the user click on YES button,
*then the program will be saved the text & close the program
*/
if(option == 0){
//for saving the text into new file
saveAs();
//for closing the program
System.exit(0);
}
/**
*if the user click on NO button,
*then the program will be closed without save the text
*/
if(option == 1){
//for closing the program
System.exit(0);
}
}
//if there was a text which was be opend or saved
else{
/**
*this method has 3 options (1 = YES, 2 = NO, 3 = Cancel)
*this is an option pop up to the user, for saving the old text or not
*/
option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??");
/**
*if the user click on YES button,
*then the program will be saved the text & close the program
*/
if(option == 0){
//for saving the text into the same file
save();
//for closing the program
System.exit(0);
}
/**
*if the user click on NO button,
*then the program will be closed without save the text
*/
if(option == 1){
//for closing the program
System.exit(0);
}
}
}
/**
*if the text was be opened or saved before but wasn't be changed,
*and we want to close the program. This option will be actived
*/
else{
//for closing the program
System.exit(0);
}
}
//to select all the text
public void selectALL(){
n.getTextArea().selectAll();
}
//for wraping the line & wraping the style word
public void lineWraP(){
if(n.getLineWrap().isSelected()){
/**
*make the line wrap & wrap style word is true
*when the line wrap is selected
*/
n.getTextArea().setLineWrap(true);
n.getTextArea().setWrapStyleWord(true);
}
else{
/**
*make the line wrap & wrap style word is false
*when the line wrap isn't selected
*/
n.getTextArea().setLineWrap(false);
n.getTextArea().setWrapStyleWord(false);
}
}
/**
*@see FONTS.JAVA
*this is a font class which is for changing the font, style & size
*/
public void fonT(){
font.setVisible(true); //setting the visible is true
font.pack(); //pack the panel
//making an action for ok button, so we can change the font
font.getOkjb().addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
n.getTextArea().setFont(font.font());
//after we chose the font, then the JDialog will be closed
font.setVisible(false);
}
});
//making an action for cancel button, so we can return to the old font.
font.getCajb().addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
//after we cancel the, then the JDialog will be closed
font.setVisible(false);
}
});
}
/**
*@see ABOUT.JAVA
*it's a Message Dialog to show the information about this program
*/
public void abouT(){
JOptionPane.showMessageDialog(null, new About(),"About Notepad",JOptionPane.PLAIN_MESSAGE);
}
/**
*THIS IS THE WAY FOR OPENING THE TEXT FILE
*/
public void open(){
//filter the kind of files, we want only TXT file
filter.addExtension("txt");
//to set a description for the file (TXT)
filter.setDescription("TXT Documents");
//setting the FileFilter to JFileChooser
jfc.setFileFilter(filter);
returnVal = jfc.showOpenDialog(n); //to show JFileChooser
if(returnVal == JFileChooser.APPROVE_OPTION){
//to erase any text in the text area before adding new text
n.getTextArea().setText(null);
try{
//to get the name of the selected file
fileName = jfc.getSelectedFile().getPath();
//to read the selected file
Reader in = new FileReader(jfc.getSelectedFile());
//100000 is the max. char can be written in the text area
char[] buff = new char[100000];
int nch;
while((nch = in.read(buff, 0, buff.length)) != -1)
n.getTextArea().append(new String(buff, 0, nch));
//to get more text from the file if the array wasn't full
fileContent = n.getTextArea().getText();
}
catch(FileNotFoundException x){}
catch(IOException ioe){
System.err.println("I/O Error on Open");
}
}
n.setTitle(jfc.getSelectedFile().getName() + " - JAVA??? Notepad");
}
/**
*THIS IS THE WAY FOR SAVING THE TEXT IN THE SAME FILE
*/
public void save(){
//initializing 'fout' to write all text in the selected file
try{
PrintWriter fout = new PrintWriter(new FileWriter(jfc.getSelectedFile()));
//for getting the text from the text area
fileContent = n.getTextArea().getText();
//using StringTokenizer for the 'fileContent' String
StringTokenizer st=new StringTokenizer(fileContent,System.getProperty("line.separator"));
while(st.hasMoreTokens()){
//write the string (text) in the selected file
fout.println(st.nextToken());
}
//closing fout
fout.close();
}
catch(IOException ioe){
System.err.println("I/O Error on Save");
}
n.setTitle(jfc.getSelectedFile().getName() + " - JAVA??? Notepad");
}
/**
*THIS IS THE WAY FOR SAVING THE TEXT IN A NEW FILE
*/
public void saveAs(){
//filter the kind of files, we want only TXT file
filter.addExtension("txt");
//to set a description for the file (TXT)
filter.setDescription("TXT Documents");
//setting the FileFilter to JFileChooser
jfc.setFileFilter(filter);
returnVal = jfc.showSaveDialog(n);
if(returnVal == JFileChooser.APPROVE_OPTION){
//initializing the PrintWriter, to save the text in a new file
PrintWriter fout = null;
try{
fout = new PrintWriter(new FileWriter(jfc.getSelectedFile() + ".txt"));
//getting the text from the text area
fileContent = n.getTextArea().getText();
//getting the name of the selected file
fileName = jfc.getSelectedFile().getPath();
//using StringTokenizer for the 'fileContent' String
StringTokenizer st=new StringTokenizer(fileContent,System.getProperty("line.separator"));
while(st.hasMoreTokens()){
//write the string (text) in the selected file
fout.println(st.nextToken());
}
//closing 'fout'
fout.close();
}
catch(IOException ioe){
System.err.println ("I/O Error on Save");
}
}
n.setTitle(jfc.getSelectedFile().getName() + " - JAVA??? Notepad");
}
private String findword;
//for searching & finding the word
//this is a method for searching the input text from the text area
public void finD(){
try{
//this is an input dialog which return a string (findword)
findword = JOptionPane.showInputDialog("Type the word to find");
//if the JTextField in the input dialog is empty (null), then return a message dialog
while(n.getTextArea().getText().indexOf(findword) == -1){
/**
*this is a message dialog which is warning the user,
*because he didn't or forgot to enter the word
*/
JOptionPane.showMessageDialog(null,"Word not found!","No match",JOptionPane.WARNING_MESSAGE);
findword = JOptionPane.showInputDialog("Type the word to find");
}
//for selecting the word which the user search for it
n.getTextArea().select(n.getTextArea().getText().indexOf(findword),
n.getTextArea().getText().indexOf(findword) + findword.length());
}
catch(Exception ex){
JOptionPane.showMessageDialog(null,"Search canceled","Abourted",JOptionPane.WARNING_MESSAGE);
}
}
public void findNexT(){
n.getTextArea().select(n.getTextArea().getText().indexOf(findword,(int)n.getTextArea().getText().indexOf(findword)+1),
n.getTextArea().getText().indexOf(findword,(int)n.getTextArea().getText().indexOf(findword)+1));
}
}