24
24
import javax .swing .*;
25
25
import javax .swing .border .Border ;
26
26
import java .awt .*;
27
- import java .awt .event .ActionEvent ;
28
- import java .awt .event .ActionListener ;
29
- import java .awt .event .InputEvent ;
30
- import java .awt .event .KeyEvent ;
27
+ import java .awt .event .*;
31
28
import java .io .BufferedWriter ;
32
29
import java .io .File ;
33
30
import java .io .FileReader ;
@@ -49,8 +46,6 @@ public class UI extends JFrame implements ActionListener {
49
46
JButton newButton , openButton , saveButton , clearButton , quickButton , aboutMeButton , aboutButton , closeButton ;
50
47
private final Action selectAllAction ;
51
48
52
-
53
-
54
49
// setup icons - File Menu
55
50
private final ImageIcon newIcon = new ImageIcon ("icons/new.png" );
56
51
private final ImageIcon openIcon = new ImageIcon ("icons/open.png" );
@@ -72,6 +67,8 @@ public class UI extends JFrame implements ActionListener {
72
67
private final ImageIcon aboutMeIcon = new ImageIcon ("icons/about_me.png" );
73
68
private final ImageIcon aboutIcon = new ImageIcon ("icons/about.png" );
74
69
70
+ private SupportedKeywords kw = new SupportedKeywords ();
71
+ private HighlightText languageHighlighter = new HighlightText (Color .GRAY );
75
72
AutoComplete autocomplete ;
76
73
private boolean hasListener = false ;
77
74
@@ -99,6 +96,14 @@ public UI()
99
96
/* SETTING BY DEFAULT WORD WRAP ENABLED OR TRUE */
100
97
textArea .setLineWrap (true );
101
98
99
+ // Set an higlighter to the JTextArea
100
+ textArea .addKeyListener (new KeyAdapter () {
101
+ public void keyPressed (KeyEvent ke ) {
102
+ languageHighlighter .highLight (textArea , kw .getCppKeywords ());
103
+ languageHighlighter .highLight (textArea , kw .getJavaKeywords ());
104
+ }
105
+ });
106
+
102
107
// This is why we didn't have to worry about the size of the TextArea!
103
108
getContentPane ().setLayout (new BorderLayout ()); // the BorderLayout bit makes it fill it automatically
104
109
getContentPane ().add (textArea );
@@ -128,8 +133,6 @@ public UI()
128
133
129
134
menuBar .add (menuAbout );
130
135
131
-
132
-
133
136
this .setJMenuBar (menuBar );
134
137
135
138
// Set Actions:
@@ -364,13 +367,43 @@ public void actionPerformed(ActionEvent ev)
364
367
//FONT SIZE SETTINGS SECTION END
365
368
}
366
369
367
-
368
-
369
370
// Make the TextArea available to the autocomplete handler
370
371
protected JTextArea getEditor () {
371
372
return textArea ;
372
373
}
373
374
375
+ // Enable autocomplete option
376
+ public void enableAutoComplete (File file ) {
377
+ if (hasListener ) {
378
+ textArea .getDocument ().removeDocumentListener (autocomplete );
379
+ hasListener = false ;
380
+ }
381
+
382
+ ArrayList <String > arrayList ;
383
+ String [] list = kw .getSupportedLangage ();
384
+
385
+ for (int i = 0 ; i < list .length ; i ++) {
386
+ if (file .getName ().endsWith (list [i ])) {
387
+ switch (i ) {
388
+ case 0 :
389
+ String [] jk = kw .getJavaKeywords ();
390
+ arrayList = kw .setKeywords (jk );
391
+ autocomplete = new AutoComplete (this , arrayList );
392
+ textArea .getDocument ().addDocumentListener (autocomplete );
393
+ hasListener = true ;
394
+ break ;
395
+ case 1 :
396
+ String [] ck = kw .getCppKeywords ();
397
+ arrayList = kw .setKeywords (ck );
398
+ autocomplete = new AutoComplete (this , arrayList );
399
+ textArea .getDocument ().addDocumentListener (autocomplete );
400
+ hasListener = true ;
401
+ break ;
402
+ }
403
+ }
404
+ }
405
+ }
406
+
374
407
public void actionPerformed (ActionEvent e ) {
375
408
// If the source of the event was our "close" option
376
409
if (e .getSource () == close || e .getSource () == closeButton ) {
@@ -394,11 +427,13 @@ else if (e.getSource() == openFile || e.getSource() == openButton) {
394
427
if (option == JFileChooser .APPROVE_OPTION ) {
395
428
FEdit .clear (textArea ); // clear the TextArea before applying the file contents
396
429
try {
397
- // create a scanner to read the file (getSelectedFile().getPath() will get the path to the file)
398
- Scanner scan = new Scanner (new FileReader (open .getSelectedFile ().getPath ()));
399
- while (scan .hasNext ()) // while there's still something to
400
- // read
401
- textArea .append (scan .nextLine () + "\n " ); // append the line to the TextArea
430
+ File openFile = open .getSelectedFile ();
431
+ setTitle (openFile .getName () + " | " + SimpleJavaTextEditor .NAME );
432
+ Scanner scan = new Scanner (new FileReader (openFile .getPath ()));
433
+ while (scan .hasNext ())
434
+ textArea .append (scan .nextLine () + "\n " );
435
+
436
+ enableAutoComplete (openFile );
402
437
} catch (Exception ex ) { // catch any exceptions, and...
403
438
// ...write to the debug console
404
439
System .out .println (ex .getMessage ());
@@ -418,54 +453,15 @@ else if (e.getSource() == saveFile || e.getSource() == saveButton) {
418
453
*/
419
454
if (option == JFileChooser .APPROVE_OPTION ) {
420
455
try {
421
- File file = fileChoose .getSelectedFile ();
422
- // Set the new title of the window
423
- setTitle (file .getName () + " | " + SimpleJavaTextEditor .NAME );
424
- // Create a buffered writer to write to a file
425
- BufferedWriter out = new BufferedWriter (new FileWriter (file .getPath ()));
426
- // Write the contents of the TextArea to the file
456
+ File openFile = fileChoose .getSelectedFile ();
457
+ setTitle (openFile .getName () + " | " + SimpleJavaTextEditor .NAME );
458
+
459
+ BufferedWriter out = new BufferedWriter (new FileWriter (openFile .getPath ()));
427
460
out .write (textArea .getText ());
428
- // Close the file stream
429
461
out .close ();
430
462
431
- //If the user saves files with supported
432
- //file types more than once, we need to remove
433
- //previous listeners to avoid bugs.
434
- if (hasListener ) {
435
- textArea .getDocument ().removeDocumentListener (autocomplete );
436
- hasListener = false ;
437
- }
438
-
439
- //With the keywords located in a separate class,
440
- //we can support multiple languages and not have to do
441
- //much to add new ones.
442
- SupportedKeywords kw = new SupportedKeywords ();
443
- ArrayList <String > arrayList ;
444
- String [] list = { ".java" , ".cpp" };
445
-
446
- //Iterate through the list, find the supported
447
- //file extension, apply the appropriate getter method from
448
- //the keyword class
449
- for (int i = 0 ; i < list .length ; i ++) {
450
- if (file .getName ().endsWith (list [i ])) {
451
- switch (i ) {
452
- case 0 :
453
- String [] jk = kw .getJavaKeywords ();
454
- arrayList = kw .setKeywords (jk );
455
- autocomplete = new AutoComplete (this , arrayList );
456
- textArea .getDocument ().addDocumentListener (autocomplete );
457
- hasListener = true ;
458
- break ;
459
- case 1 :
460
- String [] ck = kw .getCppKeywords ();
461
- arrayList = kw .setKeywords (ck );
462
- autocomplete = new AutoComplete (this , arrayList );
463
- textArea .getDocument ().addDocumentListener (autocomplete );
464
- hasListener = true ;
465
- break ;
466
- }
467
- }
468
- }
463
+ enableAutoComplete (openFile );
464
+
469
465
} catch (Exception ex ) { // again, catch any exceptions and...
470
466
// ...write to the debug console
471
467
System .out .println (ex .getMessage ());
0 commit comments