Skip to content

Commit 36a9632

Browse files
authored
Merge pull request #15 from ehermellin/master
Add Highlight and fix open/save issue
2 parents 5ea87f8 + 683ebe7 commit 36a9632

File tree

3 files changed

+104
-60
lines changed

3 files changed

+104
-60
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package simplejavatexteditor;
2+
3+
import javax.swing.text.*;
4+
import java.awt.*;
5+
6+
public class HighlightText extends DefaultHighlighter.DefaultHighlightPainter{
7+
8+
public HighlightText(Color color) {
9+
super(color);
10+
}
11+
12+
public void highLight(JTextComponent textComp, String[] pattern) {
13+
removeHighlights(textComp);
14+
15+
try {
16+
Highlighter hilite = textComp.getHighlighter();
17+
Document doc = textComp.getDocument();
18+
String text = doc.getText(0, doc.getLength());
19+
for (int i = 0; i < pattern.length; i++) {
20+
int pos = 0;
21+
22+
while ((pos = text.indexOf(pattern[i], pos)) >= 0) {
23+
hilite.addHighlight(pos, pos + pattern[i].length(), this);
24+
pos += pattern[i].length();
25+
}
26+
}
27+
} catch (BadLocationException e) {}
28+
29+
}
30+
31+
public void removeHighlights(JTextComponent textComp) {
32+
33+
Highlighter hilite = textComp.getHighlighter();
34+
Highlighter.Highlight[] hilites = hilite.getHighlights();
35+
36+
for (int i = 0; i < hilites.length; i++) {
37+
if (hilites[i].getPainter() instanceof HighlightText) {
38+
hilite.removeHighlight(hilites[i]);
39+
}
40+
}
41+
}
42+
}

src/simplejavatexteditor/SupportedKeywords.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
public class SupportedKeywords {
1414

15+
private String[] supportedLangage = {".cpp",".java"};
16+
1517
private String[] java = {"abstract", "assert", "boolean",
1618
"break", "byte", "case", "catch", "char", "class", "const",
1719
"continue", "default", "do", "double", "else", "extends", "false",
@@ -31,6 +33,10 @@ public class SupportedKeywords {
3133
"template", "typename", "class", "friend", "private", "this", "using", "const_cast",
3234
"inline", "public", "throw", "virtual", "delete", "mutable", "protected", "true", "wchar_t" };
3335

36+
public String[] getSupportedLangage() {
37+
return supportedLangage;
38+
}
39+
3440
private String[] brackets = { "{", "(" };
3541
private String[] bCompletions = { "}", ")" };
3642
public String[] getJavaKeywords() {

src/simplejavatexteditor/UI.java

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
import javax.swing.*;
2525
import javax.swing.border.Border;
2626
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.*;
3128
import java.io.BufferedWriter;
3229
import java.io.File;
3330
import 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

Comments
 (0)