Skip to content

Commit 9222947

Browse files
Final Commit
This is the completed enhancement. I have found this useful so I might expand on it in the future, but for now I think I am finished with it.
1 parent 6a1ed78 commit 9222947

File tree

3 files changed

+55
-52
lines changed

3 files changed

+55
-52
lines changed

src/simplejavatexteditor/JavaAutoComplete.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
import javax.swing.text.BadLocationException;
1717

1818
/**
19-
* <h1>Auto complete functionality for java keywords, brackets and
19+
* <h1>Auto complete functionality multiple programming languages, including brackets and
2020
* parentheses</h1>
2121
*
2222
* <p>
23-
* An ArrayList is created for the keywords and the brackets. If the word
24-
* currently being typed matches a word in the list, a Runnable inner class is
23+
* An ArrayList is created for the keywords and the brackets.
24+
* Logic for setting the content of the ArrayList is
25+
* found in UI.java. If the word currently being typed
26+
* matches a word in the list, a Runnable inner class is
2527
* implemented to handle the word completion.
2628
*
2729
* Two other inner classes are also used. The second one handles when the enter
@@ -42,6 +44,7 @@ public class JavaAutoComplete
4244
private ArrayList<String> words = new ArrayList<>();
4345

4446
SupportedKeywords kw;
47+
4548
//Keep track of when code completion
4649
//has been activated
4750
private enum Mode {
@@ -57,12 +60,8 @@ private enum Mode {
5760
private int pos;
5861
private String content;
5962

60-
public JavaAutoComplete(UI ui) {
61-
this.ui = ui;
62-
textArea = ui.getEditor();
63-
}
64-
6563
public JavaAutoComplete(UI ui, ArrayList<String> al) {
64+
//Set the keywords
6665
words = al;
6766
kw = new SupportedKeywords();
6867
brackets = kw.getbrackets();
@@ -281,8 +280,4 @@ public void removeUpdate(DocumentEvent e) {
281280
@Override
282281
public void changedUpdate(DocumentEvent e) {
283282
}
284-
285-
public void removeListener() {
286-
textArea.getDocument().removeDocumentListener(this);
287-
}
288283
}

src/simplejavatexteditor/SupportedKeywords.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
import java.util.ArrayList;
44

5+
/**
6+
* <h1>A class to store the programming language keywords and
7+
* provide access to them.</h1>
8+
*
9+
* <p>Makes multiple language support possible and makes adding new language
10+
* support convenient. To add more keywords, add a string array and getters
11+
* to this class. Then, update the switch statement in UI.java.</p>
12+
*/
513
public class SupportedKeywords {
614
private String[] java = {"abstract", "assert", "boolean",
715
"break", "byte", "case", "catch", "char", "class", "const",
@@ -19,8 +27,8 @@ public class SupportedKeywords {
1927
"typedef", "volatile", "char", "do", "extern", "if", "return", "static",
2028
"union", "while", "asm", "dynamic_cast", "namespace", "reinterpret_cast", "try",
2129
"bool", "explicit", "new", "static_cast", "typeid", "catch", "false", "operator",
22-
"template", "typename", "class", "friend", "private", "this", "using", "const_cast", "inline",
23-
"public", "throw", "virtual", "delete", "mutable", "protected", "true", "wchar_t", };
30+
"template", "typename", "class", "friend", "private", "this", "using", "const_cast",
31+
"inline", "public", "throw", "virtual", "delete", "mutable", "protected", "true", "wchar_t" };
2432

2533
private String[] brackets = { "{", "(" };
2634
private String[] bCompletions = { "}", ")" };
@@ -31,18 +39,18 @@ public String[] getCppKeywords() {
3139
return cpp;
3240
}
3341
public ArrayList<String> getbracketCompletions() {
34-
ArrayList<String> alist = new ArrayList<>();
42+
ArrayList<String> al = new ArrayList<>();
3543
for(String completion : bCompletions) {
36-
alist.add(completion);
44+
al.add(completion);
3745
}
38-
return alist;
46+
return al;
3947
}
4048
public ArrayList<String> getbrackets() {
41-
ArrayList<String> alist = new ArrayList<>();
49+
ArrayList<String> al = new ArrayList<>();
4250
for(String completion : brackets) {
43-
alist.add(completion);
51+
al.add(completion);
4452
}
45-
return alist;
53+
return al;
4654
}
4755
public ArrayList<String> setKeywords(String[] arr) {
4856
ArrayList<String> al = new ArrayList<>();

src/simplejavatexteditor/UI.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@
1919
package simplejavatexteditor;
2020

2121
// GUI
22+
2223
import javax.swing.*;
23-
import java.awt.BorderLayout;
24-
import java.awt.Container;
25-
import java.awt.Font;
26-
import java.awt.event.*;
27-
// Input Stream
28-
import java.io.*;
29-
// Various
24+
import javax.swing.border.Border;
25+
import java.awt.*;
26+
import java.awt.event.ActionEvent;
27+
import java.awt.event.ActionListener;
28+
import java.awt.event.InputEvent;
29+
import java.awt.event.KeyEvent;
30+
import java.io.BufferedWriter;
31+
import java.io.File;
32+
import java.io.FileReader;
33+
import java.io.FileWriter;
3034
import java.util.ArrayList;
31-
import java.util.EventListener;
3235
import java.util.Scanner;
33-
import javax.swing.border.Border;
34-
import javax.swing.event.DocumentListener;
35-
import javax.xml.bind.Marshaller;
36+
37+
// Input Stream
38+
// Various
3639

3740
public class UI extends JFrame implements ActionListener {
3841

@@ -43,7 +46,11 @@ public class UI extends JFrame implements ActionListener {
4346
private final JMenu menuFile, menuEdit, menuFind, menuAbout;
4447
private final JMenuItem newFile, openFile, saveFile, close, clearFile, quickFind, aboutMe, aboutSoftware;
4548
private final JToolBar mainToolbar;
46-
JButton newButton, openButton, saveButton, clearButton, quickButton, aboutMeButton, aboutButton, closeButton, spaceButton1, spaceButton2;
49+
JButton newButton, openButton,
50+
saveButton, clearButton,
51+
quickButton, aboutMeButton,
52+
aboutButton, closeButton,
53+
spaceButton1, spaceButton2;
4754

4855
//setup icons - File Menu
4956
private final ImageIcon newIcon = new ImageIcon("icons/new.png");
@@ -61,8 +68,8 @@ public class UI extends JFrame implements ActionListener {
6168
private final ImageIcon aboutMeIcon = new ImageIcon("icons/about_me.png");
6269
private final ImageIcon aboutIcon = new ImageIcon("icons/about.png");
6370

64-
private boolean hasListener = false;
65-
JavaAutoComplete autocomplete;
71+
JavaAutoComplete autocomplete;
72+
private boolean hasListener = false;
6673

6774
public UI() {
6875
container = getContentPane();
@@ -78,14 +85,10 @@ public UI() {
7885

7986
// Set a default font for the TextArea
8087
textArea = new JTextArea("", 0,0);
81-
<<<<<<< Updated upstream
82-
textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));
83-
textArea.setTabSize(2);
84-
=======
8588
textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));
8689
textArea.setTabSize(2);
87-
88-
>>>>>>> Stashed changes
90+
textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));
91+
textArea.setTabSize(2);
8992

9093
// This is why we didn't have to worry about the size of the TextArea!
9194
getContentPane().setLayout(new BorderLayout()); // the BorderLayout bit makes it fill it automatically
@@ -285,15 +288,24 @@ else if(e.getSource() == saveFile || e.getSource() == saveButton) {
285288
// Close the file stream
286289
out.close();
287290

291+
//If the user saves files with supported
292+
//file types more than once, we need to remove
293+
//previous listeners to avoid bugs.
288294
if(hasListener) {
289295
textArea.getDocument().removeDocumentListener(autocomplete);
290296
hasListener = false;
291297
}
292298

299+
//With the keywords located in a separate class,
300+
//we can support multiple languages and not have to do
301+
//much to add new ones.
293302
SupportedKeywords kw = new SupportedKeywords();
294303
ArrayList<String> arrayList;
295304
String[] list = { ".java", ".cpp" };
296305

306+
//Iterate through the list, find the supported
307+
//file extension, apply the appropriate getter method from
308+
//the keyword class
297309
for(int i = 0; i < list.length; i++) {
298310
if(file.getName().endsWith(list[i])) {
299311
switch(i) {
@@ -340,16 +352,4 @@ else if(e.getSource() == aboutSoftware || e.getSource() == aboutButton) {
340352
}
341353

342354
}
343-
344-
/*
345-
bool hasListener = false
346-
347-
for..
348-
if..
349-
if(hasListener) {
350-
removeListener
351-
}
352-
switch..
353-
//It has a
354-
*/
355355
}

0 commit comments

Comments
 (0)