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
212 lines (180 loc) · 6.79 KB
/
UI.java
File metadata and controls
212 lines (180 loc) · 6.79 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
/**
* @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 2012-05-06
*/
package ph.notepad;
// 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;
public class UI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private Container container;
private JTextArea textArea;
private JMenuBar menuBar;
private JMenu menuFile, menuEdit, menuFind, menuAbout;
private JMenuItem newFile, openFile, saveFile, close, clearFile, quickFind, aboutMe, aboutSoftware;
public UI() {
container = getContentPane();
// Set the initial size of the window
setSize(500, 300);
// Set the title of the window
setTitle("Undefined | " + Main.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");
openFile = new JMenuItem("Open");
saveFile = new JMenuItem("Save");
close = new JMenuItem("Close");
clearFile = new JMenuItem("Clear");
quickFind = new JMenuItem("Quick");
aboutMe = new JMenuItem("About Me");
aboutSoftware = new JMenuItem("About Software");
// Set the Menu Bar into the our GUI
menuBar = new JMenuBar();
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuFind);
menuBar.add(menuAbout);
container.add(menuBar, BorderLayout.NORTH);
// 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);
}
public void actionPerformed (ActionEvent e) {
// If the source of the event was our "close" option
if(e.getSource() == close)
this.dispose(); // dispose all resources and close the application
// If the source was the "new" file option
else if(e.getSource() == newFile) {
FEdit.clear(textArea);
}
// If the source was the "open" option
else if(e.getSource() == openFile) {
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) {
// 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() + " | " + Main.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) {
FEdit.clear(textArea);
}
// Find
if(e.getSource() == quickFind) {
new Find(textArea);
}
// About Me
else if(e.getSource() == aboutMe) {
new About().me();
}
// About Software
else if(e.getSource() == aboutSoftware) {
new About().software();
}
}
}