forked from pH-7/Simple-Java-Text-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.java
More file actions
295 lines (249 loc) · 11 KB
/
UI.java
File metadata and controls
295 lines (249 loc) · 11 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
/**
* @name Simple Java NotePad
* @package ph.notepad
* @file UI.java
* @author SORIA Pierre-Henry
* @email pierrehs@hotmail.com
* @link http://github.com/pH-7
* @copyright Copyright Pierre-Henry SORIA, All Rights Reserved.
* @license Apache (http://www.apache.org/licenses/LICENSE-2.0)
* @create 2012-05-04
* @update 2015-09-4
*
*
* @modifiedby Achintha Gunasekara
* @modweb http://www.achinthagunasekara.com
* @modemail contact@achinthagunasekara.com
*/
package simplejavatexteditor;
// GUI
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.*;
// Input Stream
import java.io.*;
// Various
import java.util.Scanner;
import javax.swing.border.Border;
public class UI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private final Container container;
private final JTextArea textArea;
private final JMenuBar menuBar;
private final JMenu menuFile, menuEdit, menuFind, menuAbout;
private final JMenuItem newFile, openFile, saveFile, close, clearFile, quickFind, aboutMe, aboutSoftware;
private final JToolBar mainToolbar;
JButton newButton, openButton, saveButton, clearButton, quickButton, aboutMeButton, aboutButton, closeButton, spaceButton1, spaceButton2;
//setup icons - File Menu
private final ImageIcon newIcon = new ImageIcon("icons/new.png");
private final ImageIcon openIcon = new ImageIcon("icons/open.png");
private final ImageIcon saveIcon = new ImageIcon("icons/save.png");
private final ImageIcon closeIcon = new ImageIcon("icons/close.png");
//setup icons - Search Menu
private final ImageIcon clearIcon = new ImageIcon("icons/clear.png");
//setup icons - Search Menu
private final ImageIcon searchIcon = new ImageIcon("icons/search.png");
//setup icons - Help Menu
private final ImageIcon aboutMeIcon = new ImageIcon("icons/about_me.png");
private final ImageIcon aboutIcon = new ImageIcon("icons/about.png");
public UI() {
container = getContentPane();
// Set the initial size of the window
setSize(700, 500);
// Set the title of the window
setTitle("Undefined | " + SimpleJavaTextEditor.NAME);
// Set the default close operation (exit when it gets closed)
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Set a default font for the TextArea
textArea = new JTextArea("", 0,0);
textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));
// This is why we didn't have to worry about the size of the TextArea!
getContentPane().setLayout(new BorderLayout()); // the BorderLayout bit makes it fill it automatically
getContentPane().add(textArea);
// Set the Menus
menuFile = new JMenu("File");
menuEdit = new JMenu("Edit");
menuFind = new JMenu("Search");
menuAbout = new JMenu("About");
// Set the Items Menu
newFile = new JMenuItem("New", newIcon);
openFile = new JMenuItem("Open", openIcon);
saveFile = new JMenuItem("Save", saveIcon);
close = new JMenuItem("Close", closeIcon);
clearFile = new JMenuItem("Clear", clearIcon);
quickFind = new JMenuItem("Quick", searchIcon);
aboutMe = new JMenuItem("About Me", aboutMeIcon);
aboutSoftware = new JMenuItem("About Software", aboutIcon);
// Set the Menu Bar into the our GUI
menuBar = new JMenuBar();
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuFind);
menuBar.add(menuAbout);
this.setJMenuBar(menuBar);
// New File
newFile.addActionListener(this); // Adding an action listener (so we know when it's been clicked).
newFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK)); // Set a keyboard shortcut
menuFile.add(newFile); // Adding the file menu
// Open File
openFile.addActionListener(this);
openFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
menuFile.add(openFile);
// Save File
saveFile.addActionListener(this);
saveFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
menuFile.add(saveFile);
// Close File
/*
* Along with our "CTRL+F4" shortcut to close the window, we also have
* the default closer, as stated at the beginning of this tutorial.
* this means that we actually have TWO shortcuts to close:
* 1) the default close operation (example, Alt+F4 on Windows)
* 2) CTRL+F4, which we are about to define now: (this one will appear in the label).
*/
close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.CTRL_MASK));
close.addActionListener(this);
menuFile.add(close);
// Clear File (Code)
clearFile.addActionListener(this);
clearFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, InputEvent.CTRL_MASK));
menuEdit.add(clearFile);
// Find Word
quickFind.addActionListener(this);
quickFind.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK));
menuFind.add(quickFind);
// About Me
aboutMe.addActionListener(this);
aboutMe.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
menuAbout.add(aboutMe);
// About Software
aboutSoftware.addActionListener(this);
aboutSoftware.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0));
menuAbout.add(aboutSoftware);
mainToolbar = new JToolBar();
this.add(mainToolbar, BorderLayout.NORTH);
//used to create space between button groups
Border emptyBorder = BorderFactory.createEmptyBorder(0, 0, 0, 50);
newButton = new JButton(newIcon);
newButton.setToolTipText("New");
newButton.addActionListener(this);
mainToolbar.add(newButton);
mainToolbar.addSeparator();
openButton = new JButton(openIcon);
openButton.setToolTipText("Open");
openButton.addActionListener(this);
mainToolbar.add(openButton);
mainToolbar.addSeparator();
saveButton = new JButton(saveIcon);
saveButton.setToolTipText("Save");
saveButton.addActionListener(this);
mainToolbar.add(saveButton);
mainToolbar.addSeparator();
clearButton = new JButton(clearIcon);
clearButton.setToolTipText("Clear All");
clearButton.addActionListener(this);
mainToolbar.add(clearButton);
mainToolbar.addSeparator();
quickButton = new JButton(searchIcon);
quickButton.setToolTipText("Quick Search");
quickButton.addActionListener(this);
mainToolbar.add(quickButton);
//create space between button groups
spaceButton1 = new JButton();
spaceButton1.setBorder(emptyBorder);
mainToolbar.add(spaceButton1);
aboutMeButton = new JButton(aboutMeIcon);
aboutMeButton.setToolTipText("About Me");
aboutMeButton.addActionListener(this);
mainToolbar.add(aboutMeButton);
mainToolbar.addSeparator();
aboutButton = new JButton(aboutIcon);
aboutButton.setToolTipText("About NotePad PH");
aboutButton.addActionListener(this);
mainToolbar.add(aboutButton);
//create space between button groups
spaceButton2 = new JButton();
spaceButton2.setBorder(emptyBorder);
mainToolbar.add(spaceButton2);
closeButton = new JButton(closeIcon);
closeButton.setToolTipText("Close");
closeButton.addActionListener(this);
mainToolbar.add(closeButton);
}
public void actionPerformed (ActionEvent e) {
// If the source of the event was our "close" option
if(e.getSource() == close || e.getSource() == closeButton)
this.dispose(); // dispose all resources and close the application
// If the source was the "new" file option
else if(e.getSource() == newFile || e.getSource() == newButton) {
FEdit.clear(textArea);
}
// If the source was the "open" option
else if(e.getSource() == openFile || e.getSource() == openButton) {
JFileChooser open = new JFileChooser(); // open up a file chooser (a dialog for the user to browse files to open)
int option = open.showOpenDialog(this); // get the option that the user selected (approve or cancel)
/*
* NOTE: because we are OPENing a file, we call showOpenDialog~
* if the user clicked OK, we have "APPROVE_OPTION"
* so we want to open the file
*/
if(option == JFileChooser.APPROVE_OPTION) {
FEdit.clear(textArea); // clear the TextArea before applying the file contents
try {
// create a scanner to read the file (getSelectedFile().getPath() will get the path to the file)
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
while (scan.hasNext()) // while there's still something to read
textArea.append(scan.nextLine() + "\n"); // append the line to the TextArea
} catch (Exception ex) { // catch any exceptions, and...
// ...write to the debug console
System.out.println(ex.getMessage());
}
}
}
// If the source of the event was the "save" option
else if(e.getSource() == saveFile || e.getSource() == saveButton) {
// Open a file chooser
JFileChooser fileChoose = new JFileChooser();
// Open the file, only this time we call
int option = fileChoose.showSaveDialog(this);
/*
* ShowSaveDialog instead of showOpenDialog
* if the user clicked OK (and not cancel)
*/
if(option == JFileChooser.APPROVE_OPTION) {
try {
File file = fileChoose.getSelectedFile();
// Set the new title of the window
setTitle(file.getName() + " | " + SimpleJavaTextEditor.NAME);
// Create a buffered writer to write to a file
BufferedWriter out = new BufferedWriter(new FileWriter(file.getPath()));
// Write the contents of the TextArea to the file
out.write(textArea.getText());
// Close the file stream
out.close();
} catch (Exception ex) { // again, catch any exceptions and...
// ...write to the debug console
System.out.println(ex.getMessage());
}
}
}
// Clear File (Code)
if(e.getSource() == clearFile || e.getSource() == clearButton) {
FEdit.clear(textArea);
}
// Find
if(e.getSource() == quickFind || e.getSource() == quickButton) {
new Find(textArea);
}
// About Me
else if(e.getSource() == aboutMe || e.getSource() == aboutMeButton) {
new About().me();
}
// About Software
else if(e.getSource() == aboutSoftware || e.getSource() == aboutButton) {
new About().software();
}
}
}