[autocomplete] 07/143: Enhanced the demo, the CompletionCellRenderer, and added the beginnings of a JarCompletionProvider for Java.
Benjamin Mesing
ben at alioth.debian.org
Sat Oct 19 12:53:08 UTC 2013
This is an automated email from the git hooks/post-receive script.
ben pushed a commit to branch master
in repository autocomplete.
commit ec8217df07b8c30144b8c9f53ded5a3b6f60ffb8
Author: bobbylight <robert at fifesoft.com>
Date: Thu Jan 8 04:10:37 2009 +0000
Enhanced the demo, the CompletionCellRenderer, and added the beginnings of a JarCompletionProvider for Java.
---
.classpath | 1 -
img/localVar.png | Bin 1026 -> 0 bytes
img/method.png | Bin 1024 -> 0 bytes
.../ui/autocomplete/AutoCompletePopupWindow.java | 7 +-
src/org/fife/ui/autocomplete/AutoCompletion.java | 11 +
.../ui/autocomplete/CompletionCellRenderer.java | 45 ++--
.../ui/autocomplete/JarCompletionProvider.java | 274 ++++++++++++++++++++
7 files changed, 313 insertions(+), 25 deletions(-)
diff --git a/.classpath b/.classpath
index 3e0eb1d..1208b06 100644
--- a/.classpath
+++ b/.classpath
@@ -2,7 +2,6 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="i18n"/>
- <classpathentry kind="src" path="img"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry combineaccessrules="false" kind="src" path="/RSyntaxTextArea"/>
<classpathentry kind="output" path="bin"/>
diff --git a/img/localVar.png b/img/localVar.png
deleted file mode 100644
index a8e1cf1..0000000
Binary files a/img/localVar.png and /dev/null differ
diff --git a/img/method.png b/img/method.png
deleted file mode 100644
index 3308b72..0000000
Binary files a/img/method.png and /dev/null differ
diff --git a/src/org/fife/ui/autocomplete/AutoCompletePopupWindow.java b/src/org/fife/ui/autocomplete/AutoCompletePopupWindow.java
index c232153..6b04b18 100644
--- a/src/org/fife/ui/autocomplete/AutoCompletePopupWindow.java
+++ b/src/org/fife/ui/autocomplete/AutoCompletePopupWindow.java
@@ -459,8 +459,11 @@ lastLine = -1;
unregisterActions();
}
super.setVisible(visible);
- if (descWindow!=null && ac.getShowDescWindow()) {
- descWindow.setVisible(visible);
+ // Must set descWindow's visibility one way or the other each time,
+ // because of the way child JWindows' visibility is handled - in
+ // some ways it's dependent on the parent, in other ways it's not.
+ if (descWindow!=null) {
+ descWindow.setVisible(visible && ac.getShowDescWindow());
}
}
}
diff --git a/src/org/fife/ui/autocomplete/AutoCompletion.java b/src/org/fife/ui/autocomplete/AutoCompletion.java
index ffa6d37..e2f3677 100644
--- a/src/org/fife/ui/autocomplete/AutoCompletion.java
+++ b/src/org/fife/ui/autocomplete/AutoCompletion.java
@@ -161,6 +161,16 @@ public class AutoCompletion implements HierarchyListener {
/**
+ * Returns the completion provider.
+ *
+ * @return The completion provider.
+ */
+ public CompletionProvider getCompletionProvider() {
+ return provider;
+ }
+
+
+ /**
* Returns the default autocomplete "trigger key" for this OS. For
* Windows, for example, it is Ctrl+Space.
*
@@ -540,6 +550,7 @@ try {
* @see #getShowDescWindow()
*/
public void setShowDescWindow(boolean show) {
+ hidePopupWindow(); // Needed to force it to take effect
showDescWindow = show;
}
diff --git a/src/org/fife/ui/autocomplete/CompletionCellRenderer.java b/src/org/fife/ui/autocomplete/CompletionCellRenderer.java
index 527f546..92401bf 100644
--- a/src/org/fife/ui/autocomplete/CompletionCellRenderer.java
+++ b/src/org/fife/ui/autocomplete/CompletionCellRenderer.java
@@ -120,7 +120,7 @@ public class CompletionCellRenderer extends DefaultListCellRenderer {
}
else {
Completion c = (Completion)value;
- prepareForCompletion(list, c, index, selected, hasFocus);
+ prepareForOtherCompletion(list, c, index, selected, hasFocus);
}
if (!selected && (index&1)==0 && altBG!=null) {
@@ -133,27 +133,6 @@ public class CompletionCellRenderer extends DefaultListCellRenderer {
/**
- * Prepares this renderer to display a shorthand completion.
- *
- * @param list The list of choices being rendered.
- * @param c The completion to render.
- * @param index The index into <code>list</code> being rendered.
- * @param selected Whether the item is selected.
- * @param hasFocus Whether the item has focus.
- */
- protected void prepareForCompletion(JList list,
- Completion c, int index, boolean selected, boolean hasFocus) {
-
- StringBuffer sb = new StringBuffer("<html><b><em>");
- sb.append(c.getInputText());
- sb.append("</em></b>");
-
- setText(sb.toString());
-
- }
-
-
- /**
* Prepares this renderer to display a function completion.
*
* @param list The list of choices being rendered.
@@ -230,6 +209,28 @@ public class CompletionCellRenderer extends DefaultListCellRenderer {
/**
+ * Prepares this renderer to display a completion not specifically handled
+ * elsewhere.
+ *
+ * @param list The list of choices being rendered.
+ * @param c The completion to render.
+ * @param index The index into <code>list</code> being rendered.
+ * @param selected Whether the item is selected.
+ * @param hasFocus Whether the item has focus.
+ */
+ protected void prepareForOtherCompletion(JList list,
+ Completion c, int index, boolean selected, boolean hasFocus) {
+
+ StringBuffer sb = new StringBuffer("<html><b><em>");
+ sb.append(c.getInputText());
+ sb.append("</em></b>");
+
+ setText(sb.toString());
+
+ }
+
+
+ /**
* Prepares this renderer to display a variable completion.
*
* @param list The list of choices being rendered.
diff --git a/src/org/fife/ui/autocomplete/JarCompletionProvider.java b/src/org/fife/ui/autocomplete/JarCompletionProvider.java
new file mode 100644
index 0000000..92a6dfd
--- /dev/null
+++ b/src/org/fife/ui/autocomplete/JarCompletionProvider.java
@@ -0,0 +1,274 @@
+package org.fife.ui.autocomplete;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.jar.JarFile;
+import java.util.zip.ZipEntry;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.Element;
+import javax.swing.text.JTextComponent;
+import javax.swing.text.Segment;
+
+
+/**
+ * The beginnings of a provider that uses reflection to offer completions for
+ * all classes in a jar.<p>
+ *
+ * This class is unfinished and should not be used.
+ *
+ * @author Robert Futrell
+ * @version 1.0
+ */
+public class JarCompletionProvider extends AbstractCompletionProvider {
+
+ private Segment seg;
+ private boolean loaded;
+ private TreeMap packageSet;
+
+
+ public JarCompletionProvider() {
+ seg = new Segment();
+ completions = new ArrayList(0);
+ packageSet = new TreeMap();
+ }
+
+
+ public String getAlreadyEnteredText(JTextComponent comp) {
+
+ Document doc = comp.getDocument();
+
+ int dot = comp.getCaretPosition();
+ Element root = doc.getDefaultRootElement();
+ int index = root.getElementIndex(dot);
+ Element elem = root.getElement(index);
+ int start = elem.getStartOffset();
+ int len = dot-start;
+ try {
+ doc.getText(start, len, seg);
+ } catch (BadLocationException ble) {
+ ble.printStackTrace();
+ return EMPTY_STRING;
+ }
+
+ int segEnd = seg.offset + len;
+ start = segEnd - 1;
+ while (start>=seg.offset && isValidChar(seg.array[start])) {
+ start--;
+ }
+ start++;
+
+ len = segEnd - start;
+ return len==0 ? EMPTY_STRING : new String(seg.array, start, len);
+
+ }
+
+
+ private String getAlreadyEnteredText2(JTextComponent comp) {
+
+ Document doc = comp.getDocument();
+
+ int dot = comp.getCaretPosition();
+ Element root = doc.getDefaultRootElement();
+ int index = root.getElementIndex(dot);
+ Element elem = root.getElement(index);
+ int start = elem.getStartOffset();
+ int len = dot-start;
+ try {
+ doc.getText(start, len, seg);
+ } catch (BadLocationException ble) {
+ ble.printStackTrace();
+ return EMPTY_STRING;
+ }
+
+ int segEnd = seg.offset + len;
+ start = segEnd - 1;
+ while (start>=seg.offset && isValidChar2(seg.array[start])) {
+ start--;
+ }
+ start++;
+
+ len = segEnd - start;
+ return len==0 ? EMPTY_STRING : new String(seg.array, start, len);
+
+ }
+
+
+ /**
+ * Does the dirty work of creating a list of completions.
+ *
+ * @param comp The text component to look in.
+ * @return The list of possible completions, or an empty list if there
+ * are none.
+ */
+ protected List getCompletionsImpl(JTextComponent comp) {
+
+ if (!loaded) {
+ loadCompletions();
+ }
+
+ List retVal = new ArrayList();
+
+ String text2 = getAlreadyEnteredText2(comp);
+ String[] pkgNames = splitOnChar(text2, '.');
+
+ TreeMap map = packageSet;
+ for (int i=0; i<pkgNames.length-1; i++) {
+
+ Object obj = map.get(pkgNames[i]);
+
+ if (obj==null) {
+ return retVal; // empty
+ }
+
+ if (obj instanceof TreeMap) {
+ map = (TreeMap)obj;
+ }
+ else {
+ return retVal; // Will be empty
+ }
+
+ }
+
+ String fromKey = pkgNames[pkgNames.length-1];
+ String toKey = fromKey + '{'; // Ascii char > largest valid class char
+ SortedMap sm = map.subMap(fromKey, toKey);
+
+ for (Iterator i=sm.keySet().iterator(); i.hasNext(); ) {
+ Object obj = i.next();
+ retVal.add(new BasicCompletion(this, obj.toString()));
+ }
+
+ return retVal;
+
+ }
+
+
+ /**
+ * Returns whether the specified character is valid in an auto-completion.
+ *
+ * @param ch The character.
+ * @return Whether the character is valid.
+ */
+ protected boolean isValidChar(char ch) {
+ return Character.isLetterOrDigit(ch) || ch=='_';
+ }
+
+
+ /**
+ * Returns whether the specified character is valid in an auto-completion.
+ *
+ * @param ch The character.
+ * @return Whether the character is valid.
+ */
+ protected boolean isValidChar2(char ch) {
+ return Character.isLetterOrDigit(ch) || ch=='.' || ch=='_';
+ }
+
+
+ protected void loadCompletions() {
+
+ loaded = true;
+
+ String javaHome = System.getProperty("java.home");
+ //System.out.println(javaHome);
+ JarFile jar = null;
+ try {
+ jar = new JarFile(new File(javaHome, "lib/rt.jar"));
+ } catch (Exception e) {
+ e.printStackTrace();
+ return;
+ }
+
+ java.util.Enumeration e = jar.entries();
+ while (e.hasMoreElements()) {
+ ZipEntry entry = (ZipEntry)e.nextElement();
+ String entryName = entry.getName();
+ if (entryName.endsWith(".class")) {
+ entryName = entryName.substring(0, entryName.length()-6);
+ String[] items = splitOnChar(entryName, '/');
+ TreeMap m = packageSet;
+ for (int i=0; i<items.length-1; i++) {
+ TreeMap submap = (TreeMap)m.get(items[i]);
+ if (submap==null) {
+ submap = new TreeMap();
+ m.put(items[i], submap);
+ }
+ m = submap;
+ }
+ String className = items[items.length-1];
+ m.put(className, null);
+ }
+ }
+
+ }
+
+
+ /**
+ * A faster way to split on a single char than String#split(), since
+ * we'll be doing this in a tight loop possibly thousands of times (rt.jar).
+ *
+ * @param str The string to split.
+ * @return The string, split on '<tt>/</tt>'.
+ */
+ private String[] splitOnChar(String str, int ch) {
+ List list = new ArrayList(3);
+ int pos = 0;
+ int old = 0;
+ while ((pos=str.indexOf(ch, old))>-1) {
+ list.add(str.substring(old, pos));
+ old = pos+1;
+ }
+ // If str ends in ch, this adds an empty item to the end of the list.
+ // This is what we want.
+ list.add(str.substring(old));
+ String[] array = new String[list.size()];
+ return (String[])list.toArray(array);
+ }
+
+/*
+ private class ClassOrInterfaceCompletion extends AbstractCompletion {
+
+ private String replacementText;
+
+ public ClassOrInterfaceCompletion(String name) {
+ super(JarCompletionProvider.this);
+ this.replacementText = name;
+ }
+
+ public String getReplacementText() {
+ return replacementText;
+ }
+
+ public String getSummary() {
+ return null;
+ }
+
+ }
+
+
+ private class PackageCompletion extends AbstractCompletion {
+
+ private String replacementText;
+
+ public PackageCompletion(String name) {
+ super(JarCompletionProvider.this);
+ this.replacementText = name;
+ }
+
+ public String getReplacementText() {
+ return replacementText;
+ }
+
+ public String getSummary() {
+ return null;
+ }
+
+ }
+*/
+
+}
\ No newline at end of file
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/autocomplete.git
More information about the pkg-java-commits
mailing list