Repository navigation
Expand file tree
/
Copy pathNotepad.java
More file actions
324 lines (291 loc) · 8.85 KB
/
Copy pathNotepad.java
File metadata and controls
324 lines (291 loc) · 8.85 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
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
public class Notepad extends Frame implements ActionListener{
TextArea ta;
FileOperations fileOps = new FileOperations();
HashMap<String, Boolean> extensions;
Notepad(){
super("Notepad");
setSize(500, 500);
setVisible(true);
ta = new TextArea();
add(ta);
ta.requestFocus();
MenuBar mbar = new MenuBar();
setMenuBar(mbar);
Menu fileMenu = new Menu("File");
Menu editMenu = new Menu("Edit");
Menu helpMenu = new Menu("Help");
mbar.add(fileMenu);
mbar.add(editMenu);
mbar.add(helpMenu);
// File Menu
MenuItem new1 = new MenuItem("New");
MenuItem open = new MenuItem("Open");
MenuItem save = new MenuItem("Save");
MenuItem exit = new MenuItem("Exit");
MenuItem dash = new MenuItem("-");
// EditMenu objects
MenuItem cut = new MenuItem("Cut");
MenuItem copy = new MenuItem("Copy");
MenuItem paste = new MenuItem("Paste");
MenuItem find = new MenuItem("Find");
MenuItem replace = new MenuItem("Replace");
// HelpMenu object
MenuItem about = new MenuItem("About");
fileMenu.add(new1);
fileMenu.add(open);
fileMenu.add(save);
fileMenu.add(dash);
fileMenu.add(exit);
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
editMenu.add(find);
editMenu.add(replace);
helpMenu.add(about);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
new1.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
find.addActionListener(this);
replace.addActionListener(this);
ta.addKeyListener(new KeyAdapter(){
// I think there is no reason to make custom checking
// of the copy and paste events.
// as they are handled by the text area.
@Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_N && e.isControlDown()){
newWindow();
}else if(e.getKeyCode() == KeyEvent.VK_O && e.isControlDown()){
open();
}else if(e.getKeyCode() == KeyEvent.VK_S && e.isControlDown()){
save();
}else if(e.getKeyCode() == KeyEvent.VK_F && e.isControlDown()){
invokeFindDialog();
}else if(e.getKeyCode() == KeyEvent.VK_H && e.isControlDown()){
invokeReplaceDialog();
}else if(e.getKeyCode() == KeyEvent.VK_W && e.isControlDown()){
exit();
}
}
});
extensions = new HashMap<String, Boolean>();
initExtensionMap();
}
private void initExtensionMap(){
extensions.put("txt", true);
extensions.put("mg", true);
extensions.put("java", true);
extensions.put("c", true);
extensions.put("cpp", true);
extensions.put("py", true);
extensions.put("php", true);
extensions.put("js", true);
extensions.put("html", true);
extensions.put("css", true);
extensions.put("xml", true);
extensions.put("log", true);
}
StringBuilder cutOrCopyText = new StringBuilder();
static final String path = "cd E:\\Java\\Advance Java\\Homeworks\\Notepad && java Notepad";
public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand().equals("New")){
newWindow();
}else if(ae.getActionCommand().equals("Open")){
open();
}else if(ae.getActionCommand().equals("Save")){
save();
}else if(ae.getActionCommand().equals("Cut")){
cut();
}else if(ae.getActionCommand().equals("Copy")){
copy();
}else if(ae.getActionCommand().equals("Paste")){
paste();
}else if(ae.getActionCommand().equals("Find")){
invokeFindDialog();
}else if(ae.getActionCommand().equals("Replace")){
invokeReplaceDialog();
}
}
private void newWindow(){
try{
ProcessBuilder builder = new ProcessBuilder("cmd", "/c", path);
builder.start();
}catch(Exception e){
e.printStackTrace();
}
}
private void open(){
FileDialog fd = new FileDialog(this, "Open a file", FileDialog.LOAD);
fd.setMultipleMode(false);
fd.setVisible(true);
try{
if(fd.getFile() != null){
String fileExtension = fd.getFile().substring(fd.getFile().lastIndexOf(".") + 1);
if(extensions.containsKey(fileExtension)){
fileOps.readAndappendToTextArea(fd.getDirectory() + fd.getFile(), ta);
setTitle(fd.getFile() + " - Notepad");
ta.setCaretPosition(0);
}else{
new AlertDialogBox(this, "Error", "Cannot open " + fd.getFile()).setVisible(true);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
private void save(){
if(fileOps.f != null){
try{
fileOps.saveFile(fileOps.f.getPath(), ta);
}catch(Exception e){
e.printStackTrace();
}
}else{
FileDialog fd = new FileDialog(this, "Save file", FileDialog.SAVE);
fd.setFile("Untitled.mg");
fd.setVisible(true);
if(fd.getFile() != null){
try{
fileOps.saveFile(fd.getDirectory() + fd.getFile(), ta);
setTitle(fd.getFile() + " - Notepad");
}catch(Exception e){
e.printStackTrace();
}
}
}
}
private void exit(){
System.exit(0);
}
private void cut(){
if(!ta.getSelectedText().equals("")){
cutOrCopyText.setLength(0);
cutOrCopyText.insert(0, ta.getSelectedText());
ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
}
}
private void copy(){
if(!ta.getSelectedText().equals("")){
cutOrCopyText.setLength(0);
cutOrCopyText.insert(0, ta.getSelectedText());
}
}
private void paste(){
if(cutOrCopyText.length() != 0){
if(!ta.getSelectedText().equals("")){
ta.replaceRange(cutOrCopyText.toString(), ta.getSelectionStart(), ta.getSelectionEnd());
}else{
ta.insert(cutOrCopyText.toString(), ta.getCaretPosition());
}
}
}
FindDialogBox findDialog = new FindDialogBox(this, "Find Next", "Find what: ");
ReplaceDialogBox replaceDialog = new ReplaceDialogBox(this, "Replace");
private void invokeFindDialog(){
if(replaceDialog.isVisible()){
replaceDialog.dispose();
}
if(!ta.getText().equals("")){ // making sure that the Dialog gets invoked only if the text is present.
if(!findDialog.isVisible()){
findDialog.setVisible(true);
}
}
}
private void invokeReplaceDialog(){
if(findDialog.isVisible()){
findDialog.dispose();
}
if(!ta.getText().equals("")){ // making sure that the Dialog gets invoked only if the text is present.
if(!replaceDialog.isVisible()){
replaceDialog.setVisible(true);
}
}
}
int prevIndex = -1;
protected int findText(String textToFind){
String text = ta.getText().replaceAll("\n", "");
if(!text.equals("")){ // ensuring if the text is present, as the Dialog is modeless, user can change the text.
int index = -1;
if(prevIndex != -1){
index = searchForText(text, textToFind, prevIndex + 1);
}
if(index == -1){
index = searchForText(text, textToFind);
}
if(index != -1){
ta.select(index, index + textToFind.length());
ta.requestFocus();
prevIndex = index;
return index;
}else{
new AlertDialogBox(this, "Error", "The given text was not found!").setVisible(true);
}
}
return -1;
}
// Purely done to reset the variable of the prevIndex. This method is called by the FindDialogBox class,
// once the Dialog box has been disposed off.
protected void resetVariable(){
prevIndex = -1;
}
// protected void replaceText(String textToFind, String replaceText){
// if(!ta.getText().equals("")){
// String text = ta.getText().replaceAll("\n", "");
// int index = searchForText(text, textToFind);
// if(prevIndex != -1){
// index = searchForText(text, textToFind, prevIndex + 1);
// }
// if(index == -1){
// index = searchForText(text, textToFind);
// }
// if(index != -1){
// ta.replaceRange(replaceText, index, index + textToFind.length());
// }else{
// new AlertDialogBox(this, "Error", "The given text was not found!").setVisible(true);
// }
// }
// }
protected void replaceText(String textToFind, String replaceText, int index){
if(!ta.getText().equals("")){
String text = ta.getText().replaceAll("\n", "");
if(index != -1){
ta.replaceRange(replaceText, index, index + ta.getSelectedText().length());
}
}
}
protected void replaceAllText(String findText, String replaceText){
if(!ta.getText().equals("")){
String text = ta.getText();
int index = searchForText(text, findText);
if(index != -1){
if(findText.equals(".") || findText.equals("*") || findText.equals("+")){
ta.setText(text.replaceAll("\\" + findText, replaceText));
}else{
ta.setText(text.replaceAll(findText, replaceText));
}
}else{
new AlertDialogBox(this, "Error", "The given text was not found!").setVisible(true);
}
}
}
private int searchForText(String haystack, String needle){
return haystack.indexOf(needle);
}
private int searchForText(String haystack, String needle, int index){
return haystack.indexOf(needle, index);
}
public static void main(String[] args) {
new Notepad();
}
}