-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocument.java
286 lines (264 loc) · 9.01 KB
/
Document.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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Document extends JFrame implements ActionListener
{
private JTextArea ta;
private int count;
private JMenuBar menuBar;
private JMenu fileM, editM, viewM;
private JScrollPane scpane;
private JMenuItem exitI,cutI,copyI,pasteI,selectI,saveI,loadI,newI,fullScreenI;
private String pad;
private JToolBar toolBar;
private String doc_name;
private static int doc_count = 0;
public Document(String document_name)
{
super(document_name);
doc_name = document_name;
setSize(600,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
count = 0;
pad = " ";
ta = new JTextArea(); //textarea
menuBar = new JMenuBar(); //menubar
fileM = new JMenu("File"); //file menu
editM = new JMenu("Edit"); //edit menu
viewM = new JMenu("View"); //view menu
scpane = new JScrollPane(ta); //adding textarea to scrollpane
exitI = new JMenuItem("Exit"); //exit option in file menu
cutI = new JMenuItem("Cut"); //cut option in edit menu
copyI = new JMenuItem("Copy"); //copy option in edit menu
pasteI = new JMenuItem("Paste"); //paste option in edit menu
selectI = new JMenuItem("Select All"); //selectall option in edit menu
saveI = new JMenuItem("Save"); //save option in file menu
loadI = new JMenuItem("Load"); //load option in file menu
newI = new JMenuItem("New"); //new option in file menu
fullScreenI = new JMenuItem("Toggle Full Screen"); //fullscreen option in view menu
toolBar = new JToolBar(); //toolBar
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
setJMenuBar(menuBar);
menuBar.add(fileM);
menuBar.add(editM);
menuBar.add(viewM);
fileM.add(newI);
fileM.add(saveI);
fileM.add(loadI);
fileM.add(exitI);
editM.add(cutI);
editM.add(copyI);
editM.add(pasteI);
editM.add(selectI);
viewM.add(fullScreenI);
newI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
saveI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
loadI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
cutI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
copyI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
pasteI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
selectI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
fullScreenI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.ALT_MASK));
exitI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
pane.add(scpane, BorderLayout.CENTER);
pane.add(toolBar,BorderLayout.SOUTH);
newI.addActionListener(this);
saveI.addActionListener(this);
loadI.addActionListener(this);
exitI.addActionListener(this);
cutI.addActionListener(this);
copyI.addActionListener(this);
pasteI.addActionListener(this);
selectI.addActionListener(this);
fullScreenI.addActionListener(this);
setVisible(true);
}
public Document()
{
super("Untitled"+(++doc_count)+".txt");
doc_name = "Untitled"+(doc_count)+".txt";
setSize(600,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
count = 0;
pad = " ";
ta = new JTextArea(); //textarea
menuBar = new JMenuBar(); //menubar
fileM = new JMenu("File"); //file menu
editM = new JMenu("Edit"); //edit menu
viewM = new JMenu("View"); //view menu
scpane = new JScrollPane(ta); //adding textarea to scrollpane
exitI = new JMenuItem("Exit"); //exit option in file menu
cutI = new JMenuItem("Cut"); //cut option in edit menu
copyI = new JMenuItem("Copy"); //copy option in edit menu
pasteI = new JMenuItem("Paste"); //paste option in edit menu
selectI = new JMenuItem("Select All"); //selectall option in edit menu
saveI = new JMenuItem("Save"); //save option in file menu
loadI = new JMenuItem("Load"); //load option in file menu
newI = new JMenuItem("New"); //new option in file menu
fullScreenI = new JMenuItem("Toggle Full Screen"); //fullscreen option in view menu
toolBar = new JToolBar(); //toolBar
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
setJMenuBar(menuBar);
menuBar.add(fileM);
menuBar.add(editM);
menuBar.add(viewM);
fileM.add(newI);
fileM.add(saveI);
fileM.add(loadI);
fileM.add(exitI);
editM.add(cutI);
editM.add(copyI);
editM.add(pasteI);
editM.add(selectI);
viewM.add(fullScreenI);
newI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
saveI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
loadI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
cutI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
copyI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
pasteI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
selectI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
fullScreenI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.ALT_MASK));
exitI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
pane.add(scpane, BorderLayout.CENTER);
pane.add(toolBar,BorderLayout.SOUTH);
newI.addActionListener(this);
saveI.addActionListener(this);
loadI.addActionListener(this);
exitI.addActionListener(this);
cutI.addActionListener(this);
copyI.addActionListener(this);
pasteI.addActionListener(this);
selectI.addActionListener(this);
fullScreenI.addActionListener(this);
setVisible(true);
}
public void loadFromFile(String file_name) throws IOException
{
BufferedReader br = new BufferedReader( new FileReader(new File(file_name)));
String line = br.readLine();
while( line != null)
{
this.ta.insert((line+"\n"), ta.getCaretPosition());
line = br.readLine();
}
doc_name = file_name;
}
public void saveToFile() throws IOException
{
PrintWriter writer = new PrintWriter(doc_name, "UTF-8");
writer.println(this.ta.getText());
writer.close();
}
public void actionPerformed(ActionEvent e)
{
Scanner in = new Scanner(System.in);
JMenuItem choice = (JMenuItem) e.getSource();
try
{
if(choice == saveI)
{
if (doc_name.equals("Untitled"+(doc_count)+".txt"))
{
System.out.print("Save as?");
doc_name = in.next();
saveToFile();
if ((getExtendedState() & Frame.MAXIMIZED_BOTH) != 0) // checks if already full screen
{
this.setTitle(doc_name);
setExtendedState(JFrame.MAXIMIZED_BOTH); // full screen
setUndecorated(true);
}
else
{
this.setTitle(doc_name);
}
}
else
{
saveToFile();
}
}
else if (choice == exitI)
{
dispose();
doc_count--;
}
else if (choice == cutI)
{
pad = ta.getSelectedText();
ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
}
else if (choice == copyI)
pad = ta.getSelectedText();
else if (choice == pasteI)
ta.insert(pad, ta.getCaretPosition());
else if (choice == selectI)
ta.selectAll();
else if (choice == fullScreenI)
{
if ((getExtendedState() & Frame.MAXIMIZED_BOTH) != 0) // checks if already full screen
{
setSize(600,600);
setLocationRelativeTo(null);
setUndecorated(false);
}
else
{
setExtendedState(JFrame.MAXIMIZED_BOTH); // full screen
setUndecorated(true);
}
}
else if (choice == newI)
{
new Document();
}
else if (choice == loadI)
{
System.out.print("Enter file name:");
String file_name = in.next();
if (doc_name.equals("Untitled"+(doc_count)+".txt"))
{
loadFromFile(file_name);
if ((getExtendedState() & Frame.MAXIMIZED_BOTH) != 0) // checks if already full screen
{
this.setTitle(doc_name);
setExtendedState(JFrame.MAXIMIZED_BOTH); // full screen
setUndecorated(true);
}
else
{
this.setTitle(doc_name);
}
}
else
{
Document new_doc = new Document(file_name);
new_doc.loadFromFile(file_name);
}
}
else
{
System.err.print("Wrong Option");
}
}
catch(Exception error)
{
System.err.print(error);
}
}
public static void main(String[] args)
{
new Document();
}
}