2424import javax .swing .*;
2525import javax .swing .border .Border ;
2626import 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 .*;
3128import java .io .BufferedWriter ;
3229import java .io .File ;
3330import java .io .FileReader ;
@@ -49,8 +46,6 @@ public class UI extends JFrame implements ActionListener {
4946 JButton newButton , openButton , saveButton , clearButton , quickButton , aboutMeButton , aboutButton , closeButton ;
5047 private final Action selectAllAction ;
5148
52-
53-
5449 // setup icons - File Menu
5550 private final ImageIcon newIcon = new ImageIcon ("icons/new.png" );
5651 private final ImageIcon openIcon = new ImageIcon ("icons/open.png" );
@@ -72,6 +67,8 @@ public class UI extends JFrame implements ActionListener {
7267 private final ImageIcon aboutMeIcon = new ImageIcon ("icons/about_me.png" );
7368 private final ImageIcon aboutIcon = new ImageIcon ("icons/about.png" );
7469
70+ private SupportedKeywords kw = new SupportedKeywords ();
71+ private HighlightText languageHighlighter = new HighlightText (Color .GRAY );
7572 AutoComplete autocomplete ;
7673 private boolean hasListener = false ;
7774
@@ -99,6 +96,14 @@ public UI()
9996 /* SETTING BY DEFAULT WORD WRAP ENABLED OR TRUE */
10097 textArea .setLineWrap (true );
10198
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+
102107 // This is why we didn't have to worry about the size of the TextArea!
103108 getContentPane ().setLayout (new BorderLayout ()); // the BorderLayout bit makes it fill it automatically
104109 getContentPane ().add (textArea );
@@ -128,8 +133,6 @@ public UI()
128133
129134 menuBar .add (menuAbout );
130135
131-
132-
133136 this .setJMenuBar (menuBar );
134137
135138 // Set Actions:
@@ -364,13 +367,43 @@ public void actionPerformed(ActionEvent ev)
364367 //FONT SIZE SETTINGS SECTION END
365368 }
366369
367-
368-
369370 // Make the TextArea available to the autocomplete handler
370371 protected JTextArea getEditor () {
371372 return textArea ;
372373 }
373374
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+
374407 public void actionPerformed (ActionEvent e ) {
375408 // If the source of the event was our "close" option
376409 if (e .getSource () == close || e .getSource () == closeButton ) {
@@ -394,11 +427,13 @@ else if (e.getSource() == openFile || e.getSource() == openButton) {
394427 if (option == JFileChooser .APPROVE_OPTION ) {
395428 FEdit .clear (textArea ); // clear the TextArea before applying the file contents
396429 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 );
402437 } catch (Exception ex ) { // catch any exceptions, and...
403438 // ...write to the debug console
404439 System .out .println (ex .getMessage ());
@@ -418,54 +453,15 @@ else if (e.getSource() == saveFile || e.getSource() == saveButton) {
418453 */
419454 if (option == JFileChooser .APPROVE_OPTION ) {
420455 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 ()));
427460 out .write (textArea .getText ());
428- // Close the file stream
429461 out .close ();
430462
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+
469465 } catch (Exception ex ) { // again, catch any exceptions and...
470466 // ...write to the debug console
471467 System .out .println (ex .getMessage ());
0 commit comments