forked from stg1908/Text_editor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEdi.java
More file actions
277 lines (258 loc) · 5.48 KB
/
Edi.java
File metadata and controls
277 lines (258 loc) · 5.48 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
import java.io.*;
import java.util.*;
import java.util.regex.*;
import java.awt.*;
import java.awt.event.*;
class Edi extends KeyAdapter implements ActionListener
{
Frame f; MenuBar mb; Menu m[] = new Menu[2];
Frame wrn;
MenuItem nw,opn, sve, ext, svea, fnd,fndr;
TextArea ta;
File fa;
Frame fds;
FileDialog fd;
FileInputStream fis;
FileOutputStream fos;
boolean savem,saveasm,om=false,nm=false;
public Edi()
{
savem = false;
saveasm = false;
f=new Frame();
f.setSize(800,800);
mb = new MenuBar();
wrn = new Frame();
wrn.setSize(400,100);
Label wl1 = new Label("File not found",Label.CENTER);
wl1.setFont(new Font("Arial",Font.BOLD,12));
Button wb1= new Button("OK");
Panel wp1 = new Panel(new FlowLayout(FlowLayout.CENTER));
wp1.add(wb1);
wrn.add("South",wp1);
wrn.add(wl1);
m[0]= new Menu("File");
m[1]= 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");
fndr = new MenuItem("Find & Replace");
m[0].add(nw); m[0].add(opn); m[0].add(sve);
m[0].add(svea); m[0].addSeparator();m[0].add(ext);
m[1].add(fnd); m[1].add(fndr);
mb.add(m[0]); mb.add(m[1]);
nw.addActionListener(this);
opn.addActionListener(this);
svea.addActionListener(this);
sve.addActionListener(this);
ext.addActionListener(this);
fds=new Frame();
fds.setSize(500,100);
Label l1=new Label("Save changes to document “Untitled Document 1” before closing?",Label.CENTER);
Label l2= new Label("If you don't save, changes will be permanently lost.",Label.CENTER);
Panel p1 = new Panel(new FlowLayout(FlowLayout.CENTER));
Panel p2 = new Panel(new GridLayout(2,1));
l1.setFont(new Font("Arial",Font.BOLD,12));
l2.setFont(new Font("Arial",Font.PLAIN,12));
Button b1= new Button("Don't Save");
Button b2= new Button("Cancel");
Button b3= new Button("Save");
b1.addActionListener(this);
wb1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p2.add(l1);
p2.add(l2);
fds.add(p2);
fds.add("South",p1);
//fds.setVisible(true);
ta= new TextArea();
ta.addKeyListener(new KeyListener()
{
public void keyTyped(KeyEvent ek)
{
savem=false;
System.out.print("yo");
}
public void keyPressed(KeyEvent ek){}
public void keyReleased(KeyEvent ek){}
});
f.add(ta);
Wi w = new Wi();
f.addWindowListener(w);
f.setMenuBar(mb); f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();
String tas;
int ch;
char cha,car[];
if(str.equals("New"))
{
if(savem||ta.getText().equals("\0"))
{
ta.setText("\0");
}
else
{
nm=true;
fds.setVisible(true);
}
}
if(str.equals("Exit"))
{
System.exit(1);
}
if(str.equals("Don't Save"))
{
if(nm)
{
ta.setText("\0");
nm=false;
fds.setVisible(false);
}
if(om)
{
//ta.setText("\0");
str="Open";
fds.setVisible(false);
}
}
if(str.equals("Save"))
{
if(!saveasm)
{
str = "Save As..." ;
}
else
{
if(!savem)
{
try
{
fos = new FileOutputStream(fa);
tas = ta.getText();
car = tas.toCharArray();
for(int ic=0;ic<car.length;ic++)
{
fos.write(car[ic]);
}
fos.close();
savem = true;
}
catch(IOException e1)
{
}
if(om)
{
//ta.setText("\0");
str="Open";
fds.setVisible(false);
}
if(nm)
{
ta.setText("\0");
nm=false;
fds.setVisible(false);
}
}
}
fds.setVisible(false);
}
if(str.equals("OK"))
{
wrn.setVisible(false);
str="Open";
}
if(str.equals("Open"))
{
if(savem||ta.getText().equals("")||om)
{
om=false;
fd = new FileDialog(f,"Open",FileDialog.LOAD);
fd.setVisible(true);
try
{
if(fd.getFile().equals(null))
{
//System.out.println("fhdfhehbdfn");
return;
}
fis = new FileInputStream(fd.getDirectory()+fd.getFile());
//System.out.println(fd.getDirectory()+fd.getFile());
ta.setText("\0");
while((ch=fis.read())!=-1)
{
cha =(char)ch;
ta.appendText(""+cha);
}
fa = new File(fd.getDirectory()+fd.getFile());
fis.close();
}
catch(IOException e1)
{
System.out.println(e1.getClass().getName());
//System.out.println(e1.getMessage());
wrn.setVisible(true);
}
saveasm=true;
savem=true;
}
else
{
om=true;
fds.setVisible(true);
}
}
if(str.equals("Save As..."))
{
try
{
fd = new FileDialog(f,"Save As",FileDialog.SAVE);
fd.setVisible(true);
}
catch(NullPointerException e12)
{
System.out.println("ygvrefgvo");
}
try
{
if(fd.getFile().equals(null))
{
System.out.print("ygvrefgvo");
return;
}
fa = new File(fd.getDirectory()+fd.getFile());
fos = new FileOutputStream(fa);
tas = ta.getText();
car = tas.toCharArray();
for(int ic=0;ic<car.length;ic++)
{
fos.write(car[ic]);
}
fos.close();
saveasm = true;
savem = true;
}
catch(IOException e1)
{
//System.out.print("ygvrefgvo");
}
}
if(str.equals("Cancel"))
{
fds.setVisible(false);
}
}
public static void main(String arg[])
{
Edi e = new Edi();
}
}