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
587 lines (512 loc) · 23 KB
/
UI.java
File metadata and controls
587 lines (512 loc) · 23 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/**
* @name Simple Java NotePad
* @package ph.notepad
* @file UI.java
*
* @author Pierre-Henry Soria
* @email [email protected]
* @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-04-05
* @update 2017-02-18
*
* @modifiedby Achintha Gunasekara
* @modemail [email protected]
*
* @modifiedby Marcus Redgrave-Close
* @modemail [email protected]
*
* @Modifiedby SidaDan
* @modemail [email protected]
* Added Tooltip Combobox Font type and Font size
* Overwrite method processWindowEvent to detect window closing event.
* Added safety query to save the file before exit
* or the user select "newfile"
* Only available if the user has pressed a key
* Added safety query if user pressed the clearButton
*
* @Modifiedby SidaDan
* @modemail [email protected]
* Removed unuse objects like container, Border emptyBorder
* Removed unsused imports
*
* @Modifiedby Giorgos Pasentsis
* @modemail [email protected]
*/
package simplejavatexteditor;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.text.DefaultEditorKit;
public class UI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private final JTextArea textArea;
private final JMenuBar menuBar;
private final JComboBox fontSize, fontType;
private final JMenu menuFile, menuEdit, menuFind, menuAbout;
private final JMenuItem newFile, openFile, saveFile, close, cut, copy, paste, clearFile, selectAll, quickFind,
aboutMe, aboutSoftware, wordWrap;
private final JToolBar mainToolbar;
JButton newButton, openButton, saveButton, clearButton, quickButton, aboutMeButton, aboutButton, closeButton, boldButton, italicButton;
private final Action selectAllAction;
//setup icons - Bold and Italic
private final ImageIcon boldIcon = new ImageIcon("icons/bold.png");
private final ImageIcon italicIcon = new ImageIcon("icons/italic.png");
// 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 - Edit Menu
private final ImageIcon clearIcon = new ImageIcon("icons/clear.png");
private final ImageIcon cutIcon = new ImageIcon("icons/cut.png");
private final ImageIcon copyIcon = new ImageIcon("icons/copy.png");
private final ImageIcon pasteIcon = new ImageIcon("icons/paste.png");
private final ImageIcon selectAllIcon = new ImageIcon("icons/selectall.png");
private final ImageIcon wordwrapIcon = new ImageIcon("icons/wordwrap.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");
private SupportedKeywords kw = new SupportedKeywords();
private HighlightText languageHighlighter = new HighlightText(Color.GRAY);
AutoComplete autocomplete;
private boolean hasListener = false;
private boolean edit = false;
public UI() {
// Set the initial size of the window
setSize(800, 500);
// Set the title of the window
setTitle("Untitled | " + SimpleJavaTextEditor.NAME);
// Set the default close operation (exit when it gets closed)
setDefaultCloseOperation(EXIT_ON_CLOSE);
// center the frame on the monitor
setLocationRelativeTo(null);
// Set a default font for the TextArea
textArea = new JTextArea("", 0, 0);
textArea.setFont(new Font("Century Gothic", Font.PLAIN, 12));
textArea.setTabSize(2);
textArea.setFont(new Font("Century Gothic", Font.PLAIN, 12));
textArea.setTabSize(2);
/* SETTING BY DEFAULT WORD WRAP ENABLED OR TRUE */
textArea.setLineWrap(true);
// Set an higlighter to the JTextArea
textArea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
edit = true;
languageHighlighter.highLight(textArea, kw.getCppKeywords());
languageHighlighter.highLight(textArea, kw.getJavaKeywords());
}
});
// 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");
//Font Settings menu
// Set the Items Menu
newFile = new JMenuItem("New", newIcon);
openFile = new JMenuItem("Open", openIcon);
saveFile = new JMenuItem("Save", saveIcon);
close = new JMenuItem("Quit", closeIcon);
clearFile = new JMenuItem("Clear", clearIcon);
quickFind = new JMenuItem("Quick", searchIcon);
aboutMe = new JMenuItem("About Me", aboutMeIcon);
aboutSoftware = new JMenuItem("About Software", aboutIcon);
menuBar = new JMenuBar();
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuFind);
menuBar.add(menuAbout);
this.setJMenuBar(menuBar);
// Set Actions:
selectAllAction = new SelectAllAction("Select All", clearIcon, "Select all text", new Integer(KeyEvent.VK_A),
textArea);
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_Q, InputEvent.CTRL_MASK));
close.addActionListener(this);
menuFile.add(close);
// Select All Text
selectAll = new JMenuItem(selectAllAction);
selectAll.setText("Select All");
selectAll.setIcon(selectAllIcon);
selectAll.setToolTipText("Select All");
selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
menuEdit.add(selectAll);
// Clear File (Code)
clearFile.addActionListener(this);
clearFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, InputEvent.CTRL_MASK));
menuEdit.add(clearFile);
// Cut Text
cut = new JMenuItem(new DefaultEditorKit.CutAction());
cut.setText("Cut");
cut.setIcon(cutIcon);
cut.setToolTipText("Cut");
cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
menuEdit.add(cut);
// WordWrap
wordWrap = new JMenuItem();
wordWrap.setText("Word Wrap");
wordWrap.setIcon(wordwrapIcon);
wordWrap.setToolTipText("Word Wrap");
//Short cut key or key stroke
wordWrap.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK));
menuEdit.add(wordWrap);
/* CODE FOR WORD WRAP OPERATION
* BY DEFAULT WORD WRAPPING IS ENABLED.
*/
wordWrap.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
// If wrapping is false then after clicking on menuitem the word wrapping will be enabled
if (textArea.getLineWrap() == false) {
/* Setting word wrapping to true */
textArea.setLineWrap(true);
} else {
// else if wrapping is true then after clicking on menuitem the word wrapping will be disabled
/* Setting word wrapping to false */
textArea.setLineWrap(false);
}
}
});
// Copy Text
copy = new JMenuItem(new DefaultEditorKit.CopyAction());
copy.setText("Copy");
copy.setIcon(copyIcon);
copy.setToolTipText("Copy");
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
menuEdit.add(copy);
// Paste Text
paste = new JMenuItem(new DefaultEditorKit.PasteAction());
paste.setText("Paste");
paste.setIcon(pasteIcon);
paste.setToolTipText("Paste");
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
menuEdit.add(paste);
// 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
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);
mainToolbar.addSeparator();
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);
mainToolbar.addSeparator();
closeButton = new JButton(closeIcon);
closeButton.setToolTipText("Quit");
closeButton.addActionListener(this);
mainToolbar.add(closeButton);
mainToolbar.addSeparator();
boldButton = new JButton(boldIcon);
boldButton.setToolTipText("Bold");
boldButton.addActionListener(this);
mainToolbar.add(boldButton);
mainToolbar.addSeparator();
italicButton = new JButton(italicIcon);
italicButton.setToolTipText("Italic");
italicButton.addActionListener(this);
mainToolbar.add(italicButton);
mainToolbar.addSeparator();
/**
* **************** FONT SETTINGS SECTION **********************
*/
//FONT FAMILY SETTINGS SECTION START
fontType = new JComboBox();
//GETTING ALL AVAILABLE FONT FOMILY NAMES
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (int i = 0; i < fonts.length; i++) {
//Adding font family names to font[] array
fontType.addItem(fonts[i]);
}
//Setting maximize size of the fontType ComboBox
fontType.setMaximumSize(new Dimension(170, 30));
fontType.setToolTipText("Font Type");
mainToolbar.add(fontType);
mainToolbar.addSeparator();
//Adding Action Listener on fontType JComboBox
fontType.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
//Getting the selected fontType value from ComboBox
String p = fontType.getSelectedItem().toString();
//Getting size of the current font or text
int s = textArea.getFont().getSize();
textArea.setFont(new Font(p, Font.PLAIN, s));
}
});
//FONT FAMILY SETTINGS SECTION END
//FONT SIZE SETTINGS START
fontSize = new JComboBox();
for (int i = 5; i <= 100; i++) {
fontSize.addItem(i);
}
fontSize.setMaximumSize(new Dimension(70, 30));
fontSize.setToolTipText("Font Size");
mainToolbar.add(fontSize);
fontSize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
String sizeValue = fontSize.getSelectedItem().toString();
int sizeOfFont = Integer.parseInt(sizeValue);
String fontFamily = textArea.getFont().getFamily();
Font font1 = new Font(fontFamily, Font.PLAIN, sizeOfFont);
textArea.setFont(font1);
}
});
//FONT SIZE SETTINGS SECTION END
}
@Override
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
if (edit) {
Object[] options = {"Save and exit", "No Save and exit", "Return"};
int n = JOptionPane.showOptionDialog(this, "Do you want to save the file ?", "Question",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
if (n == 0) {// save and exit
saveFile();
this.dispose();// dispose all resources and close the application
} else if (n == 1) {// no save and exit
this.dispose();// dispose all resources and close the application
}
} else {
System.exit(99);
}
}
}
// Make the TextArea available to the autocomplete handler
protected JTextArea getEditor() {
return textArea;
}
// Enable autocomplete option
public void enableAutoComplete(File file) {
if (hasListener) {
textArea.getDocument().removeDocumentListener(autocomplete);
hasListener = false;
}
ArrayList<String> arrayList;
String[] list = kw.getSupportedLanguages();
for (int i = 0; i < list.length; i++) {
if (file.getName().endsWith(list[i])) {
switch (i) {
case 0:
String[] jk = kw.getJavaKeywords();
arrayList = kw.setKeywords(jk);
autocomplete = new AutoComplete(this, arrayList);
textArea.getDocument().addDocumentListener(autocomplete);
hasListener = true;
break;
case 1:
String[] ck = kw.getCppKeywords();
arrayList = kw.setKeywords(ck);
autocomplete = new AutoComplete(this, arrayList);
textArea.getDocument().addDocumentListener(autocomplete);
hasListener = true;
break;
}
}
}
}
public void actionPerformed(ActionEvent e) {
// If the source of the event was our "close" option
if (e.getSource() == close || e.getSource() == closeButton) {
if (edit) {
Object[] options = {"Save and exit", "No Save and exit", "Return"};
int n = JOptionPane.showOptionDialog(this, "Do you want to save the file ?", "Question",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
if (n == 0) {// save and exit
saveFile();
this.dispose();// dispose all resources and close the application
} else if (n == 1) {// no save and exit
this.dispose();// dispose all resources and close the application
}
} else {
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) {
if (edit) {
Object[] options = {"Save", "No Save", "Return"};
int n = JOptionPane.showOptionDialog(this, "Do you want to save the file at first ?", "Question",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
if (n == 0) {// save
saveFile();
edit = false;
} else if (n == 1) {
edit = false;
FEdit.clear(textArea);
}
} else {
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 {
File openFile = open.getSelectedFile();
setTitle(openFile.getName() + " | " + SimpleJavaTextEditor.NAME);
Scanner scan = new Scanner(new FileReader(openFile.getPath()));
while (scan.hasNext()) {
textArea.append(scan.nextLine() + "\n");
}
enableAutoComplete(openFile);
} catch (Exception ex) { // catch any exceptions, and...
// ...write to the debug console
System.err.println(ex.getMessage());
}
}
} // If the source of the event was the "save" option
else if (e.getSource() == saveFile || e.getSource() == saveButton) {
saveFile();
}// If the source of the event was the "Bold" button
else if (e.getSource() == boldButton) {
if (textArea.getFont().getStyle() == Font.BOLD){
textArea.setFont(textArea.getFont().deriveFont(Font.PLAIN));
} else {
textArea.setFont(textArea.getFont().deriveFont(Font.BOLD));
}
}// If the source of the event was the "Italic" button
else if (e.getSource() == italicButton) {
if (textArea.getFont().getStyle() == Font.ITALIC){
textArea.setFont(textArea.getFont().deriveFont(Font.PLAIN));
} else {
textArea.setFont(textArea.getFont().deriveFont(Font.ITALIC));
}
}
// Clear File (Code)
if (e.getSource() == clearFile || e.getSource() == clearButton) {
Object[] options = {"Yes", "No"};
int n = JOptionPane.showOptionDialog(this, "Are you sure to clear the text Area ?", "Question",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
if (n == 0) {// clear
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(this).me();
} // About Software
else if (e.getSource() == aboutSoftware || e.getSource() == aboutButton) {
new About(this).software();
}
}
class SelectAllAction extends AbstractAction {
/**
* Used for Select All function
*/
private static final long serialVersionUID = 1L;
public SelectAllAction(String text, ImageIcon icon, String desc, Integer mnemonic, final JTextArea textArea) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnemonic);
}
public void actionPerformed(ActionEvent e) {
textArea.selectAll();
}
}
private void 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 openFile = fileChoose.getSelectedFile();
setTitle(openFile.getName() + " | " + SimpleJavaTextEditor.NAME);
BufferedWriter out = new BufferedWriter(new FileWriter(openFile.getPath()));
out.write(textArea.getText());
out.close();
enableAutoComplete(openFile);
edit = false;
} catch (Exception ex) { // again, catch any exceptions and...
// ...write to the debug console
System.err.println(ex.getMessage());
}
}
}
}