forked from Taylormyers01/TextEditor-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEditor.java
More file actions
187 lines (178 loc) · 6.55 KB
/
TextEditor.java
File metadata and controls
187 lines (178 loc) · 6.55 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
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
//TextEditor class starts here
class TextEditor extends Frame implements ActionListener {
TextArea ta = new TextArea();
int i, len1, len, pos1;
String str = "", s3 = "", s2 = "", s4 = "", s32 = "", s6 = "", s7 = "", s8 = "", s9 = "";
String months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September",
"October", "November", "December" };
CheckboxMenuItem chkb = new CheckboxMenuItem("Word Wrap");
public TextEditor() {
MenuBar mb = new MenuBar();
setLayout(new BorderLayout());
add("Center", ta);
setMenuBar(mb);
Menu m1 = new Menu("File");
Menu m2 = new Menu("Edit");
Menu m3 = new Menu("Tools");
Menu m4 = new Menu("Help");
mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
MenuItem mi1[] = {
new MenuItem("New"), new MenuItem("Open"), new MenuItem("Save"), new MenuItem("Save As"),
new MenuItem("Page Setup"), new MenuItem("Print"), new MenuItem("Exit")
};
MenuItem mi2[] = { new MenuItem("Delete"), new MenuItem("Cut"),
new MenuItem("Copy"), new MenuItem("Paste"), new MenuItem("Find"),
new MenuItem("Find Next"), new MenuItem("Replace"),
new MenuItem("Go To"), new MenuItem("Select All"),
new MenuItem("Time Stamp") };
MenuItem mi3[] = { new MenuItem("Choose Font"), new MenuItem("Compile"),
new MenuItem("Run") };
MenuItem mi4[] = { new MenuItem("Help Topics"),
new MenuItem("About TextEditor") };
for (int i = 0; i < mi1.length; i++) {
m1.add(mi1[i]);
mi1[i].addActionListener(this);
}
for (int i = 0; i < mi2.length; i++) {
m2.add(mi2[i]);
mi2[i].addActionListener(this);
}
m3.add(chkb);
chkb.addActionListener(this);
for (int i = 0; i < mi3.length; i++) {
m3.add(mi3[i]);
mi3[i].addActionListener(this);
}
for (int i = 0; i < mi4.length; i++) {
m4.add(mi4[i]);
mi4[i].addActionListener(this);
}
MyWindowsAdapter mw = new MyWindowsAdapter(this);
addWindowListener(mw);
setSize(500, 500);
setTitle("untitled notepad");
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String arg = (String) ae.getActionCommand();
if (arg.equals("New")) {
dispose();
TextEditor t11 = new TextEditor();
t11.setSize(500, 500);
t11.setVisible(true);
}
try {
if (arg.equals("Open")) {
FileDialog fd1 = new FileDialog(this, "Select File", FileDialog.LOAD);
fd1.setVisible(true);
String s4 = "";
s2 = fd1.getFile();
s3 = fd1.getDirectory();
s32 = s3 + s2;
File f = new File(s32);
FileInputStream fii = new FileInputStream(f);
len = (int) f.length();
for (int j = 0; j < len; j++) {
char s5 = (char) fii.read();
s4 = s4 + s5;
}
ta.setText(s4);
fii.close();
}
} catch (IOException e) {
}
try {
if (arg.equals("Save As")) {
FileDialog dialog1 = new FileDialog(this, "Save As", FileDialog.SAVE);
dialog1.setVisible(true);
s7 = dialog1.getDirectory();
s8 = dialog1.getFile();
s9 = s7 + s8 + ".txt";
s6 = ta.getText();
len1 = s6.length();
byte buf[] = s6.getBytes();
File f1 = new File(s9);
FileOutputStream fobj1 = new FileOutputStream(f1);
for (int k = 0; k < len1; k++) {
fobj1.write(buf[k]);
}
fobj1.close();
}
this.setTitle(s8 + " TextEditor File");
} catch (IOException e) {
}
if (arg.equals("Exit")) {
System.exit(0);
}
if (arg.equals("Cut")) {
str = ta.getSelectedText();
i = ta.getText().indexOf(str);
ta.replaceRange(" ", i, i + str.length());
}
if (arg.equals("Copy")) {
str = ta.getSelectedText();
}
if (arg.equals("Paste")) {
pos1 = ta.getCaretPosition();
ta.insert(str, pos1);
}
if (arg.equals("Delete")) {
String msg = ta.getSelectedText();
i = ta.getText().indexOf(msg);
ta.replaceRange(" ", i, i + msg.length());
msg = "";
}
if (arg.equals("Select All")) {
String strText = ta.getText();
int strLen = strText.length();
ta.select(0, strLen);
}
if (arg.equals("Time Stamp")) {
GregorianCalendar gcalendar = new GregorianCalendar();
String h = String.valueOf(gcalendar.get(Calendar.HOUR));
String m = String.valueOf(gcalendar.get(Calendar.MINUTE));
String s = String.valueOf(gcalendar.get(Calendar.SECOND));
String date = String.valueOf(gcalendar.get(Calendar.DATE));
String mon = months[gcalendar.get(Calendar.MONTH)];
String year = String.valueOf(gcalendar.get(Calendar.YEAR));
String hms = "Time" + " - " + h + ":" + m + ":" + s + " Date" + " - " + date + " " + mon + " " + year + " ";
int loc = ta.getCaretPosition();
ta.insert(hms, loc);
}
if (arg.equals("About TextEditor")) {
AboutDialog d1 = new AboutDialog(this, "About TextEditor");
d1.setVisible(true);
setSize(500, 500);
}
}
public static void main(String args[]) {
TextEditor to = new TextEditor();
}
}
class MyWindowsAdapter extends WindowAdapter {
TextEditor tt;
public MyWindowsAdapter(TextEditor ttt) {
tt = ttt;
}
public void windowClosing(WindowEvent we) {
tt.dispose();
}
}
class AboutDialog extends Dialog implements ActionListener {
AboutDialog(Frame parent, String title) {
super(parent, title, false);
this.setResizable(false);
setLayout(new FlowLayout(FlowLayout.LEFT));
setSize(500, 300);
}
public void actionPerformed(ActionEvent ae) {
dispose();
}
}