-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyEditer.java
381 lines (269 loc) · 8.41 KB
/
MyEditer.java
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.plaf.FileChooserUI;
public class MyEditer implements ActionListener
{
JFrame jf, fontFrame;
JMenuBar menuBar;
JMenu menu1,menu2,menu3;
JMenuItem neww,open,save,saveAs,exit,cut ,copy,paste, selectAll, delete , font, color,background;
JTextArea textArea=new JTextArea();
JComboBox size, style, fonts;
JButton okButton;
MyEditer(){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");//This line gives Windows Theme
}
catch (Exception e)
{
e.printStackTrace();
}
jf = new JFrame("/* MY Notepad */");
jf.setBounds(200, 20, 500, 500);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
// jf.setLayout(null);
//creating menuBar
JMenuBar menuBar =new JMenuBar();
//creating menu
menu1 = new JMenu("FILE");
menu2= new JMenu("EDIT");
menu3=new JMenu("FORMAT");
// creating menu items
neww=new JMenuItem("New");
neww.addActionListener(this);
open=new JMenuItem("Open");
open.addActionListener(this);
save =new JMenuItem("Save file");
save.addActionListener(this);
saveAs=new JMenuItem("Save as");
saveAs.addActionListener(this);
exit=new JMenuItem("Exit");
exit.addActionListener(this);
cut=new JMenuItem("Cut");
cut.addActionListener(this);
copy=new JMenuItem("Copy");
copy.addActionListener(this);
paste=new JMenuItem("Paste");
paste.addActionListener(this);
selectAll=new JMenuItem("Select All");
selectAll.addActionListener(this);
delete=new JMenuItem("Delete");
delete.addActionListener(this);
font=new JMenuItem("Font...");
font.addActionListener(this);
color=new JMenuItem("Color");
color.addActionListener(this);
background=new JMenuItem("BackgroundColor");
background.addActionListener(this);
//adding menuItems
menu1.add(neww);
menu1.add(open);
menu1.add(save);
menu1.add(saveAs);
menu1.add(exit);
menu2.add(cut);
menu2.add(copy);
menu2.add(paste);
menu2.add(selectAll);
menu2.add(delete);
menu3.add(font);
menu3.add(color);
menu3.add(background);
// adding menu in menuBar
menuBar.add(menu1);
menuBar.add(menu2);
menuBar.add(menu3);
// set textArea
textArea=new JTextArea();
//scrollBar in textArea
JScrollPane scroll=new JScrollPane(textArea);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//
//JScrollBar scroll =new JScrollBar();
//adding Elements in JFrame
jf.setJMenuBar(menuBar);
jf.add(scroll);
jf.setVisible(true);
}
public static void main(String[] args) {
new MyEditer();
}
JFileChooser fileChooser =new JFileChooser();
public void actionPerformed(ActionEvent event) {
//String cmnd = event.getActionCommand();
//openFile action setting
if(event.getSource()==open) {
textArea.setText(null);
fileChooser.showOpenDialog(jf);
File file=fileChooser.getSelectedFile();
// setting selected file name in titlebar
String name = file.getName();
jf.setTitle("SELECTED File: "+name);
try (FileInputStream fis=new FileInputStream(file))
{
int i;
while(( i= fis.read())!= -1) {
textArea.append((String.valueOf((char)i)));
}
}
catch(Exception e) {
e.printStackTrace();
}
}
if (event.getSource()==neww) { //action on new button
textArea.setText(null);
jf.setTitle("/* Untitled notepad */");
}
if(event.getSource()== exit) { // actions on exit button
System.exit(0);
}
if(event.getSource()==save) { // Actions on save button
if(jf.getTitle().equals("/* MY Notepad */")) {
saveAsText();
}
else
saveText();
}
if(event.getSource()==saveAs) { // Actions on saveAs button
saveAsText();
}
//actions on menu2 Items
if(event.getSource()==cut)
textArea.cut();
if(event.getSource()==copy)
textArea.copy();
if(event.getSource()==paste)
textArea.paste();
if(event.getSource()==selectAll)
textArea.selectAll();
if(event.getSource()==delete)
textArea.setText(null);
//actions on menu3
if(event.getSource()==font) {
setFont();
}
if(event.getSource()==okButton) {
okFun();
fontFrame.setVisible(false);
}
if (event.getSource()==color) {
Color c=JColorChooser.showDialog(jf, "Choose Font Color", Color.black);
textArea.setForeground(c);
}
if (event.getSource()==background) {
Color c=JColorChooser.showDialog(jf, "Choose Font Color", Color.white);
textArea.setBackground(c);
}
}
//save as function
public void saveAsText()
{
fileChooser.showSaveDialog(jf);
String msg= textArea.getText();
byte[] buffer = msg.getBytes();
File file=fileChooser.getSelectedFile();
try {
FileOutputStream fos= new FileOutputStream(file);
fos.write(buffer);
fos.close();
String name = file.getName();
jf.setTitle("SELECTED File: "+name);
JOptionPane.showMessageDialog(jf, "Your file is successflly saved.");
}
catch(Exception e) {
e.printStackTrace();
}
}
//save function
public void saveText()
{
String msg= textArea.getText();
byte[] buffer = msg.getBytes();
File file=fileChooser.getSelectedFile();
try {
FileOutputStream fos= new FileOutputStream(file);
fos.write(buffer);
fos.close();
String name = file.getName();
jf.setTitle("SELECTED File: "+name);
}
catch(Exception e) {
e.printStackTrace();
}
}
// set Fonts function
public void setFont() {
fontFrame= new JFrame("/* FONTCHOOSER */");
fontFrame.setSize(400, 400);
fontFrame.setLocationRelativeTo(jf);
fontFrame.setLayout(null);
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fontFamilies= e.getAllFonts();
String [] arr= {"12", "14","16","18","22","26","30","35","40","50"};
String[] fontStyle= {"Regular","Italic","Bold","Bold Italic"};
String[] fontName =new String[fontFamilies.length];
int i=0;
for(i=0;i<fontFamilies.length;i++) {
fontName[i]=fontFamilies[i].getFontName();
}
size= new JComboBox(arr);
size.setBounds(30,50,50, 30);
fontFrame.add(size);
style=new JComboBox(fontStyle);
style.setBounds(120,50,80, 30);
fontFrame.add(style);
fonts=new JComboBox(fontName);
fonts.setBounds(220,50,100, 30);
fontFrame.add(fonts);
okButton=new JButton("OK");
okButton.addActionListener(this);
okButton.setBounds(170, 270, 60, 50);
fontFrame.add(okButton);
fontFrame.setVisible(true);
}
public void okFun() {
String setSize=(String)size.getSelectedItem();
String setStyle=(String)style.getSelectedItem();
String setFontFamily=(String)fonts.getSelectedItem();
int style;
if(setStyle=="Regular")
style=0;
else if(setStyle=="Italic")
style=1;
else if(setStyle=="Bold")
style=2;
else if (setStyle=="Bold Italic")
style=3;
else
style=4;
Font font=new Font(setFontFamily, style , Integer.parseInt(setSize));
textArea.setFont(font);
}
}