[autocomplete] 43/143: Refactoring of AutoCompletion, popup windows now auto-hide when text component loses focus. Improved API for tweaking features of Java, PHP and Shell code completion.
Benjamin Mesing
ben at alioth.debian.org
Sat Oct 19 12:53:14 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 86ac7ba6c6e62adf5d88effc3259adde4d846e98
Author: bobbylight <robert at fifesoft.com>
Date: Fri May 28 03:06:09 2010 +0000
Refactoring of AutoCompletion, popup windows now auto-hide when text component loses focus.
Improved API for tweaking features of Java, PHP and Shell code completion.
---
src/org/fife/ui/autocomplete/AutoCompletion.java | 148 ++++++++++++--------
.../ui/autocomplete/CompletionCellRenderer.java | 10 +-
src/org/fife/ui/autocomplete/FastListUI.java | 4 +-
3 files changed, 97 insertions(+), 65 deletions(-)
diff --git a/src/org/fife/ui/autocomplete/AutoCompletion.java b/src/org/fife/ui/autocomplete/AutoCompletion.java
index 3e077a5..1932545 100644
--- a/src/org/fife/ui/autocomplete/AutoCompletion.java
+++ b/src/org/fife/ui/autocomplete/AutoCompletion.java
@@ -59,7 +59,7 @@ import javax.swing.text.*;
* It also handles communication between the CompletionProvider and the actual
* popup Window.
*/
-public class AutoCompletion implements HierarchyListener {
+public class AutoCompletion {
/**
* The text component we're providing completion for.
@@ -167,9 +167,15 @@ public class AutoCompletion implements HierarchyListener {
/**
* Listens for events in the parent window that affect the visibility of
- * the popup window.
+ * the popup windows.
*/
- private Listener parentWindowListener;
+ private ParentWindowListener parentWindowListener;
+
+ /**
+ * Listens for events from the text component that affect the visibility
+ * of the popup windows.
+ */
+ private TextComponentListener textComponentListener;
/**
* The key used in the input map for the AutoComplete action.
@@ -204,7 +210,8 @@ public class AutoCompletion implements HierarchyListener {
setAutoCompleteEnabled(true);
setAutoCompleteSingleChoices(true);
setShowDescWindow(false);
- parentWindowListener = new Listener();
+ parentWindowListener = new ParentWindowListener();
+ textComponentListener = new TextComponentListener();
// Automatically update LAF of popup windows on LookAndFeel changes
UIManager.addPropertyChangeListener(new PropertyChangeListener() {
@@ -416,32 +423,6 @@ public class AutoCompletion implements HierarchyListener {
/**
- * Called when the component hierarchy for our text component changes.
- * When the text component is added to a new {@link Window}, this method
- * registers listeners on that <code>Window</code>.
- *
- * @param e The event.
- */
- public void hierarchyChanged(HierarchyEvent e) {
-
- // NOTE: e many be null as we call this method at other times.
- //System.out.println("Hierarchy changed! " + e);
-
- Window oldParentWindow = parentWindow;
- parentWindow = SwingUtilities.getWindowAncestor(textComponent);
- if (parentWindow!=oldParentWindow) {
- if (oldParentWindow!=null) {
- parentWindowListener.removeFrom(oldParentWindow);
- }
- if (parentWindow!=null) {
- parentWindowListener.addTo(parentWindow);
- }
- }
-
- }
-
-
- /**
* Hides any child windows being displayed by the auto-completion system.
*
* @return Whether any windows were visible.
@@ -581,8 +562,9 @@ try {
new ParameterizedCompletionStartAction(start));
}
- this.textComponent.addHierarchyListener(this);
- hierarchyChanged(null); // In case textComponent is already in a window
+ textComponentListener.addTo(this.textComponent);
+ // In case textComponent is already in a window...
+ textComponentListener.hierarchyChanged(null);
}
@@ -905,7 +887,7 @@ try {
am.put(PARAM_COMPLETE_KEY, oldParenAction);
}
- textComponent.removeHierarchyListener(this);
+ textComponentListener.removeFrom(textComponent);
if (parentWindow!=null) {
parentWindowListener.removeFrom(parentWindow);
}
@@ -969,13 +951,50 @@ try {
/**
+ * Action that starts a parameterized completion, e.g. after '(' is
+ * typed.
+ *
+ * @author Robert Futrell
+ * @version 1.0
+ */
+ private class ParameterizedCompletionStartAction extends AbstractAction {
+
+ private String start;
+
+ public ParameterizedCompletionStartAction(char ch) {
+ this.start = Character.toString(ch);
+ }
+
+ public void actionPerformed(ActionEvent e) {
+
+ // Prevents keystrokes from messing up
+ boolean wasVisible = hidePopupWindow();
+
+ // Only proceed if they were selecting a completion
+ if (!wasVisible || !isParameterAssistanceEnabled()) {
+ textComponent.replaceSelection(start);
+ return;
+ }
+
+ Completion c = popupWindow.getSelection();
+ if (c instanceof ParameterizedCompletion) { // Should always be true
+ // Fixes capitalization of the entered text.
+ insertCompletion(c);
+ }
+
+ }
+
+ }
+
+
+ /**
* Listens for events in the parent window of the text component with
* auto-completion enabled.
*
* @author Robert Futrell
* @version 1.0
*/
- private class Listener extends ComponentAdapter
+ private class ParentWindowListener extends ComponentAdapter
implements WindowFocusListener {
public void addTo(Window w) {
@@ -1011,37 +1030,52 @@ try {
/**
- * Action that starts a parameterized completion, e.g. after '(' is
- * typed.
- *
- * @author Robert Futrell
- * @version 1.0
+ * Listens for events from the text component we're installed on.
*/
- private class ParameterizedCompletionStartAction extends AbstractAction {
-
- private String start;
+ private class TextComponentListener extends FocusAdapter
+ implements HierarchyListener {
- public ParameterizedCompletionStartAction(char ch) {
- this.start = Character.toString(ch);
+ void addTo(JTextComponent tc) {
+ tc.addFocusListener(this);
+ tc.addHierarchyListener(this);
}
- public void actionPerformed(ActionEvent e) {
-
- // Prevents keystrokes from messing up
- boolean wasVisible = hidePopupWindow();
+ /**
+ * Hide the auto-completion windows when the text component loses
+ * focus.
+ */
+ public void focusLost(FocusEvent e) {
+ hideChildWindows();
+ }
- // Only proceed if they were selecting a completion
- if (!wasVisible || !isParameterAssistanceEnabled()) {
- textComponent.replaceSelection(start);
- return;
+ /**
+ * Called when the component hierarchy for our text component changes.
+ * When the text component is added to a new {@link Window}, this
+ * method registers listeners on that <code>Window</code>.
+ *
+ * @param e The event.
+ */
+ public void hierarchyChanged(HierarchyEvent e) {
+
+ // NOTE: e many be null as we call this method at other times.
+ //System.out.println("Hierarchy changed! " + e);
+
+ Window oldParentWindow = parentWindow;
+ parentWindow = SwingUtilities.getWindowAncestor(textComponent);
+ if (parentWindow!=oldParentWindow) {
+ if (oldParentWindow!=null) {
+ parentWindowListener.removeFrom(oldParentWindow);
+ }
+ if (parentWindow!=null) {
+ parentWindowListener.addTo(parentWindow);
+ }
}
- Completion c = popupWindow.getSelection();
- if (c instanceof ParameterizedCompletion) { // Should always be true
- // Fixes capitalization of the entered text.
- insertCompletion(c);
- }
+ }
+ public void removeFrom(JTextComponent tc) {
+ tc.removeFocusListener(this);
+ tc.removeHierarchyListener(this);
}
}
diff --git a/src/org/fife/ui/autocomplete/CompletionCellRenderer.java b/src/org/fife/ui/autocomplete/CompletionCellRenderer.java
index 61a4345..a702fd6 100644
--- a/src/org/fife/ui/autocomplete/CompletionCellRenderer.java
+++ b/src/org/fife/ui/autocomplete/CompletionCellRenderer.java
@@ -28,7 +28,6 @@ import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
-
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.plaf.basic.BasicHTML;
@@ -56,7 +55,7 @@ public class CompletionCellRenderer extends DefaultListCellRenderer {
* The alternating background color, or <code>null</code> if alternating
* row colors should not be used.
*/
- private Color altBG;
+ private static Color altBG;
/**
* The font to use when rendering items, or <code>null</code> if the
@@ -86,7 +85,6 @@ private Rectangle paintTextR;
*/
public CompletionCellRenderer() {
//setDisplayFont(new Font("Monospaced", Font.PLAIN, 12));
- setAlternateBackground(new Color(0xf4f4f4));
setShowTypes(true);
paintTextR = new Rectangle();
}
@@ -99,7 +97,7 @@ private Rectangle paintTextR;
* alternating colors are not used.
* @see #setAlternateBackground(Color)
*/
- public Color getAlternateBackground() {
+ public static Color getAlternateBackground() {
return altBG;
}
@@ -365,8 +363,8 @@ this.realBG = altBG!=null && (index&1)==0 ? altBG : list.getBackground();
* background colors.
* @see #getAlternateBackground()
*/
- public void setAlternateBackground(Color altBG) {
- this.altBG = altBG;
+ public static void setAlternateBackground(Color altBG) {
+ CompletionCellRenderer.altBG = altBG;
}
diff --git a/src/org/fife/ui/autocomplete/FastListUI.java b/src/org/fife/ui/autocomplete/FastListUI.java
index 14d6a80..6dafa08 100644
--- a/src/org/fife/ui/autocomplete/FastListUI.java
+++ b/src/org/fife/ui/autocomplete/FastListUI.java
@@ -42,7 +42,7 @@ import javax.swing.plaf.basic.BasicListUI;
* with no performance penalty. With standard BasicListUI subclasses, this can
* cause very poor performance <b>each time</b> the list is displayed, which
* is bad for lists that are repeatedly hidden and re-displayed, such as
- * completion choices. This is all becasue the calculation to get the
+ * completion choices. This is all because the calculation to get the
* preferred size of each list item, when it is displayed with HTML, is slow.
*
* @author Robert Futrell
@@ -176,7 +176,7 @@ class FastListUI extends BasicListUI {
if (list.getParent() instanceof JViewport) { // Always true for us
cellWidth = list.getParent().getWidth();
}
- System.out.println(cellWidth);
+ //System.out.println(cellWidth);
// We're getting a fixed cell height for all cells
cellHeights = null;
--
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