[autocomplete] 88/143: Refactoring parameterized completion code in preparation for template completion fixes.
Benjamin Mesing
ben at alioth.debian.org
Sat Oct 19 12:53:23 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 886d3bd265339ce83095357d2dbc4ffbbc87b83d
Author: bobbylight <robert at fifesoft.com>
Date: Mon Jun 18 02:45:40 2012 +0000
Refactoring parameterized completion code in preparation for template completion fixes.
---
src/org/fife/ui/autocomplete/AutoCompletion.java | 121 +--
.../ParameterizedCompletionChoicesWindow.java | 6 +-
...ip.java => ParameterizedCompletionContext.java} | 328 ++----
.../ParameterizedCompletionDescriptionToolTip.java | 1086 +-------------------
src/org/fife/ui/autocomplete/TemplatePiece.java | 8 +
5 files changed, 176 insertions(+), 1373 deletions(-)
diff --git a/src/org/fife/ui/autocomplete/AutoCompletion.java b/src/org/fife/ui/autocomplete/AutoCompletion.java
index d044226..9c46c9e 100644
--- a/src/org/fife/ui/autocomplete/AutoCompletion.java
+++ b/src/org/fife/ui/autocomplete/AutoCompletion.java
@@ -82,9 +82,9 @@ public class AutoCompletion {
private Dimension preferredDescWindowSize;
/**
- * A "tool tip" describing a function just entered.
+ * Manages any parameterized completions that are inserted.
*/
- private ParameterizedCompletionDescriptionToolTip descToolTip;
+ private ParameterizedCompletionContext pcc;
/**
* Provides the completion options relevant to the current caret position.
@@ -235,51 +235,6 @@ public class AutoCompletion {
/**
- * Displays a "tooltip" detailing the inputs to the function just entered.
- *
- * @param pc The completion.
- * @param addParamListStart Whether or not
- * {@link CompletionProvider#getParameterListStart()} should be
- * added to the text component.
- */
- private void displayDescriptionToolTip(ParameterizedCompletion pc,
- boolean addParamListStart) {
-
- // Get rid of the previous tooltip window, if there is one.
- hideToolTipWindow();
-
- // Don't bother with a tooltip if there are no parameters.
- if (pc.getParamCount()==0) {
- CompletionProvider p = pc.getProvider();
- char end = p.getParameterListEnd(); // Might be '\0'
- String text = end=='\0' ? "" : Character.toString(end);
- if (addParamListStart) {
- text = p.getParameterListStart() + text;
- }
- textComponent.replaceSelection(text);
- return;
- }
-
- descToolTip = new ParameterizedCompletionDescriptionToolTip(
- parentWindow, this, pc);
- try {
- int dot = textComponent.getCaretPosition();
- Rectangle r = textComponent.modelToView(dot);
- Point p = new Point(r.x, r.y);
- SwingUtilities.convertPointToScreen(p, textComponent);
- r.x = p.x;
- r.y = p.y;
- descToolTip.setLocationRelativeTo(r);
- descToolTip.setVisible(true, addParamListStart);
- } catch (BadLocationException ble) { // Should never happen
- UIManager.getLookAndFeel().provideErrorFeedback(textComponent);
- ble.printStackTrace();
- }
-
- }
-
-
- /**
* Displays the popup window. Hosting applications can call this method
* to programmatically begin an auto-completion operation.
*/
@@ -465,39 +420,37 @@ public class AutoCompletion {
public boolean hideChildWindows() {
//return hidePopupWindow() || hideToolTipWindow();
boolean res = hidePopupWindow();
- res |= hideToolTipWindow();
+ res |= hideParameterCompletionPopups();
return res;
}
/**
- * Hides the popup window, if it is visible.
+ * Hides and disposes of any parameter completion-related popups.
*
- * @return Whether the popup window was visible.
+ * @return Whether any such windows were visible (and thus hidden).
*/
- private boolean hidePopupWindow() {
- if (popupWindow!=null) {
- if (popupWindow.isVisible()) {
- popupWindow.setVisible(false);
- return true;
- }
+ private boolean hideParameterCompletionPopups() {
+ if (pcc!=null) {
+ pcc.setVisible(false, false);
+ pcc = null;
+ return true;
}
return false;
}
/**
- * Hides the parameter tool tip and/or the parameter choices window, if
- * either one is visible.
+ * Hides the popup window, if it is visible.
*
- * @return Whether either of the two windows were visible (and thus
- * hidden).
+ * @return Whether the popup window was visible.
*/
- private boolean hideToolTipWindow() {
- if (descToolTip!=null) {
- descToolTip.setVisible(false, false);
- descToolTip = null;
- return true;
+ private boolean hidePopupWindow() {
+ if (popupWindow!=null) {
+ if (popupWindow.isVisible()) {
+ popupWindow.setVisible(false);
+ return true;
+ }
}
return false;
}
@@ -547,7 +500,7 @@ public class AutoCompletion {
if (isParameterAssistanceEnabled() &&
(c instanceof ParameterizedCompletion)) {
ParameterizedCompletion pc = (ParameterizedCompletion)c;
- displayDescriptionToolTip(pc, true);
+ startParameterizedCompletionAssistance(pc, true);
}
}
@@ -970,6 +923,38 @@ public class AutoCompletion {
/**
+ * Displays a "tooltip" detailing the inputs to the function just entered.
+ *
+ * @param pc The completion.
+ * @param addParamListStart Whether or not
+ * {@link CompletionProvider#getParameterListStart()} should be
+ * added to the text component.
+ */
+ private void startParameterizedCompletionAssistance(ParameterizedCompletion pc,
+ boolean addParamListStart) {
+
+ // Get rid of the previous tooltip window, if there is one.
+ hideParameterCompletionPopups();
+
+ // Don't bother with a tooltip if there are no parameters.
+ if (pc.getParamCount()==0) {
+ CompletionProvider p = pc.getProvider();
+ char end = p.getParameterListEnd(); // Might be '\0'
+ String text = end=='\0' ? "" : Character.toString(end);
+ if (addParamListStart) {
+ text = p.getParameterListStart() + text;
+ }
+ textComponent.replaceSelection(text);
+ return;
+ }
+
+ pcc = new ParameterizedCompletionContext(parentWindow, this, pc);
+ pcc.setVisible(true, addParamListStart);
+
+ }
+
+
+ /**
* Uninstalls this auto-completion from its text component. If it is not
* installed on any text component, nothing happens.
*
@@ -1035,8 +1020,8 @@ public class AutoCompletion {
if (popupWindow!=null) {
popupWindow.updateUI();
}
- if (descToolTip!=null) {
- descToolTip.updateUI();
+ if (pcc!=null) {
+ pcc.updateUI();
}
// Will practically always be a JComponent (a JLabel)
if (paramChoicesRenderer instanceof JComponent) {
diff --git a/src/org/fife/ui/autocomplete/ParameterizedCompletionChoicesWindow.java b/src/org/fife/ui/autocomplete/ParameterizedCompletionChoicesWindow.java
index 31f21e8..76f0f39 100644
--- a/src/org/fife/ui/autocomplete/ParameterizedCompletionChoicesWindow.java
+++ b/src/org/fife/ui/autocomplete/ParameterizedCompletionChoicesWindow.java
@@ -80,11 +80,11 @@ public class ParameterizedCompletionChoicesWindow extends JWindow {
*
* @param parent The parent window (hosting the text component).
* @param ac The auto-completion instance.
- * @param tip The parent parameter description tool tip.
+ * @param context The completion context.
*/
public ParameterizedCompletionChoicesWindow(Window parent,
AutoCompletion ac,
- final ParameterizedCompletionDescriptionToolTip tip) {
+ final ParameterizedCompletionContext context) {
super(parent);
this.ac = ac;
@@ -98,7 +98,7 @@ public class ParameterizedCompletionChoicesWindow extends JWindow {
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount()==2) {
- tip.insertSelectedChoice();
+ context.insertSelectedChoice();
}
}
});
diff --git a/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java b/src/org/fife/ui/autocomplete/ParameterizedCompletionContext.java
similarity index 77%
copy from src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java
copy to src/org/fife/ui/autocomplete/ParameterizedCompletionContext.java
index 47a1e48..37c9013 100644
--- a/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java
+++ b/src/org/fife/ui/autocomplete/ParameterizedCompletionContext.java
@@ -1,15 +1,14 @@
/*
- * 12/21/2008
+ * 06/17/2012
*
- * ParameterizedCompletionDescriptionToolTip.java - A "tool tip" displaying
- * information on the function or method currently being entered.
+ * ParameritizedCompletionContext.java - Manages the state of parameterized
+ * completion-related UI components during code completion.
*
* This library is distributed under a modified BSD license. See the included
* RSyntaxTextArea.License.txt file for details.
*/
package org.fife.ui.autocomplete;
-import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
@@ -25,11 +24,7 @@ import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
-import javax.swing.BorderFactory;
import javax.swing.InputMap;
-import javax.swing.JLabel;
-import javax.swing.JPanel;
-import javax.swing.JWindow;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
@@ -48,56 +43,58 @@ import javax.swing.text.Highlighter.Highlight;
import org.fife.ui.autocomplete.ParameterizedCompletion.Parameter;
import org.fife.ui.autocomplete.ParameterizedCompletionInsertionInfo.ReplacementCopy;
import org.fife.ui.rsyntaxtextarea.DocumentRange;
-import org.fife.ui.rsyntaxtextarea.PopupWindowDecorator;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
-import org.fife.ui.rsyntaxtextarea.RSyntaxUtilities;
import org.fife.ui.rtextarea.ChangeableHighlightPainter;
/**
- * A "tool tip" that displays information on the function or method currently
- * being entered.
+ * Manages UI and state specific to parameterized completions - the parameter
+ * description tool tip, the parameter completion choices list, the actual
+ * highlights in the editor, etc. This component installs new key bindings
+ * when appropriate to allow the user to cycle through the parameters of the
+ * completion, and optionally cycle through completion choices for those
+ * parameters.
*
* @author Robert Futrell
* @version 1.0
*/
-class ParameterizedCompletionDescriptionToolTip {
+class ParameterizedCompletionContext {
/**
- * The actual tool tip.
+ * The parent window.
*/
- private JWindow tooltip;
+ private Window parentWindow;
/**
- * The painter to paint borders around the variables.
+ * The parent AutoCompletion instance.
*/
- Highlighter.HighlightPainter p;
-
- Highlighter.HighlightPainter paramCopyP;
+ private AutoCompletion ac;
/**
- * The tags for the highlights around parameters.
+ * The completion being described.
*/
- List tags;
-
- private List paramCopyInfos;
-
- private transient boolean ignoringDocumentEvents;
+ private ParameterizedCompletion pc;
/**
- * The parent AutoCompletion instance.
+ * A tool tip displaying the currently edited parameter name and type.
*/
- private AutoCompletion ac;
+ private ParameterizedCompletionDescriptionToolTip tip;
/**
- * The label that holds the description.
+ * The painter to paint borders around the variables.
*/
- private JLabel descLabel;
+ private Highlighter.HighlightPainter p;
+
+ private Highlighter.HighlightPainter paramCopyP;
/**
- * The completion being described.
+ * The tags for the highlights around parameters.
*/
- private ParameterizedCompletion pc;
+ private List tags;
+
+ private List paramCopyInfos;
+
+ private transient boolean ignoringDocumentEvents;
/**
* Listens for events in the text component while this window is visible.
@@ -130,11 +127,6 @@ class ParameterizedCompletionDescriptionToolTip {
*/
private String paramPrefix;
- /**
- * The currently "selected" parameter in the displayed text.
- */
- private int lastSelectedParam;
-
private Object oldTabKey;
private Action oldTabAction;
private Object oldShiftTabKey;
@@ -150,56 +142,24 @@ class ParameterizedCompletionDescriptionToolTip {
private Object oldClosingKey;
private Action oldClosingAction;
- private static final String IM_KEY_TAB = "ParamCompDescTip.Tab";
- private static final String IM_KEY_SHIFT_TAB = "ParamCompDescTip.ShiftTab";
- private static final String IM_KEY_UP = "ParamCompDescTip.Up";
- private static final String IM_KEY_DOWN = "ParamCompDescTip.Down";
- private static final String IM_KEY_ESCAPE = "ParamCompDescTip.Escape";
- private static final String IM_KEY_ENTER = "ParamCompDescTip.Enter";
- private static final String IM_KEY_CLOSING = "ParamCompDescTip.Closing";
+ private static final String IM_KEY_TAB = "ParamCompKey.Tab";
+ private static final String IM_KEY_SHIFT_TAB = "ParamCompKey.ShiftTab";
+ private static final String IM_KEY_UP = "ParamCompKey.Up";
+ private static final String IM_KEY_DOWN = "ParamCompKey.Down";
+ private static final String IM_KEY_ESCAPE = "ParamCompKey.Escape";
+ private static final String IM_KEY_ENTER = "ParamCompKey.Enter";
+ private static final String IM_KEY_CLOSING = "ParamCompKey.Closing";
/**
* Constructor.
- *
- * @param owner The parent window.
- * @param ac The parent auto-completion.
- * @param pc The completion being described.
*/
- public ParameterizedCompletionDescriptionToolTip(Window owner,
- AutoCompletion ac, ParameterizedCompletion pc) {
-
- tooltip = new JWindow(owner);
+ public ParameterizedCompletionContext(Window owner,
+ AutoCompletion ac, ParameterizedCompletion pc) {
+ this.parentWindow = owner;
this.ac = ac;
this.pc = pc;
-
- descLabel = new JLabel();
- descLabel.setBorder(BorderFactory.createCompoundBorder(
- TipUtil.getToolTipBorder(),
- BorderFactory.createEmptyBorder(2, 5, 2, 5)));
- descLabel.setOpaque(true);
- descLabel.setBackground(TipUtil.getToolTipBackground());
- // It appears that if a JLabel is set as a content pane directly, when
- // using the JDK's opacity API's, it won't paint its background, even
- // if label.setOpaque(true) is called. You have to have a container
- // underneath it for it to paint its background. Thus, we embed our
- // label in a parent JPanel to handle this case.
- //tooltip.setContentPane(descLabel);
- JPanel panel = new JPanel(new BorderLayout());
- panel.add(descLabel);
- tooltip.setContentPane(panel);
-
- // Give apps a chance to decorate us with drop shadows, etc.
- PopupWindowDecorator decorator = PopupWindowDecorator.get();
- if (decorator!=null) {
- decorator.decorate(tooltip);
- }
-
- lastSelectedParam = -1;
- updateText(0);
-
- tooltip.setFocusableWindowState(false);
listener = new Listener();
p = new OutlineHighlightPainter(Color.GRAY);
@@ -207,6 +167,9 @@ class ParameterizedCompletionDescriptionToolTip {
tags = new ArrayList(1); // Usually small
paramCopyInfos = new ArrayList(1);
+ tip = new ParameterizedCompletionDescriptionToolTip(parentWindow,
+ this, ac, pc);
+
paramChoicesWindow = createParamChoicesWindow();
}
@@ -219,13 +182,21 @@ class ParameterizedCompletionDescriptionToolTip {
*/
private ParameterizedCompletionChoicesWindow createParamChoicesWindow() {
ParameterizedCompletionChoicesWindow pcw =
- new ParameterizedCompletionChoicesWindow(tooltip.getOwner(),
+ new ParameterizedCompletionChoicesWindow(parentWindow,
ac, this);
pcw.initialize(pc);
return pcw;
}
+ /**
+ * Returns the text inserted for the parameter containing the specified
+ * offset.
+ *
+ * @param offs The offset into the document.
+ * @return The text of the parameter containing the offset, or
+ * <code>null</code> if the offset is not in a parameter.
+ */
public String getArgumentText(int offs) {
List paramHighlights = getParameterHighlights();
if (paramHighlights==null || paramHighlights.size()==0) {
@@ -361,7 +332,7 @@ class ParameterizedCompletionDescriptionToolTip {
}
- private List getParameterHighlights() {
+ public List getParameterHighlights() {
List paramHighlights = new ArrayList(1);
JTextComponent tc = ac.getTextComponent();
Highlight[] highlights = tc.getHighlighter().getHighlights();
@@ -412,7 +383,7 @@ class ParameterizedCompletionDescriptionToolTip {
private void installKeyBindings() {
if (AutoCompletion.getDebug()) {
- System.out.println("ToolTip: Installing keybindings");
+ System.out.println("CompletionContext: Installing keybindings");
}
JTextComponent tc = ac.getTextComponent();
@@ -506,7 +477,7 @@ class ParameterizedCompletionDescriptionToolTip {
// "+1" is a workaround for Java Highlight issues.
tc.setSelectionStart(currentNext.getStartOffset()+1);
tc.setSelectionEnd(currentNext.getEndOffset());
- updateText(pos);
+ tip.updateText(pos);
}
@@ -531,6 +502,7 @@ class ParameterizedCompletionDescriptionToolTip {
Highlight currentPrev = null;
int pos = 0;
List highlights = getParameterHighlights();
+ int lastSelectedParam = tip.getLastSelectedParam();
for (int i=0; i<highlights.size(); i++) {
Highlight h = (Highlight)highlights.get(i);
@@ -552,13 +524,13 @@ class ParameterizedCompletionDescriptionToolTip {
// "+1" is a workaround for Java Highlight issues.
tc.setSelectionStart(currentPrev.getStartOffset()+1);
tc.setSelectionEnd(currentPrev.getEndOffset());
- updateText(pos);
+ tip.updateText(pos);
}
else if (currentPrev!=null && dot>currentPrev.getStartOffset()) {
// "+1" is a workaround for Java Highlight issues.
tc.setSelectionStart(currentPrev.getStartOffset()+1);
tc.setSelectionEnd(currentPrev.getEndOffset());
- updateText(pos);
+ tip.updateText(pos);
}
else {
tc.setCaretPosition(maxPos.getOffset());
@@ -639,6 +611,7 @@ class ParameterizedCompletionDescriptionToolTip {
}
// Toggles visibility, if necessary.
+ int lastSelectedParam = tip.getLastSelectedParam();
paramChoicesWindow.setParameter(lastSelectedParam, paramPrefix);
}
@@ -706,42 +679,6 @@ class ParameterizedCompletionDescriptionToolTip {
/**
- * Sets the location of this tool tip relative to the given rectangle.
- *
- * @param r The visual position of the caret (in screen coordinates).
- */
- public void setLocationRelativeTo(Rectangle r) {
-
- // Multi-monitor support - make sure the completion window (and
- // description window, if applicable) both fit in the same window in
- // a multi-monitor environment. To do this, we decide which monitor
- // the rectangle "r" is in, and use that one (just pick top-left corner
- // as the defining point).
- Rectangle screenBounds = Util.getScreenBoundsForPoint(r.x, r.y);
- //Dimension screenSize = tooltip.getToolkit().getScreenSize();
-
- // Try putting our stuff "above" the caret first.
- int y = r.y - 5 - tooltip.getHeight();
- if (y<0) {
- y = r.y + r.height + 5;
- }
-
- // Get x-coordinate of completions. Try to align left edge with the
- // caret first.
- int x = r.x;
- if (x<screenBounds.x) {
- x = screenBounds.x;
- }
- else if (x+tooltip.getWidth()>screenBounds.x+screenBounds.width) { // completions don't fit
- x = screenBounds.x + screenBounds.width - tooltip.getWidth();
- }
-
- tooltip.setLocation(x, y);
-
- }
-
-
- /**
* Toggles the visibility of this tool tip.
*
* @param visible Whether the tool tip should be visible.
@@ -752,10 +689,24 @@ class ParameterizedCompletionDescriptionToolTip {
*/
public void setVisible(boolean visible, boolean addParamListStart) {
- if (visible!=tooltip.isVisible()) {
+ if (visible!=tip.isVisible()) {
JTextComponent tc = ac.getTextComponent();
+ try {
+ int dot = tc.getCaretPosition();
+ Rectangle r = tc.modelToView(dot);
+ Point p = new Point(r.x, r.y);
+ SwingUtilities.convertPointToScreen(p, tc);
+ r.x = p.x;
+ r.y = p.y;
+ tip.setLocationRelativeTo(r);
+ tip.setVisible(true);
+ } catch (BadLocationException ble) { // Should never happen
+ UIManager.getLookAndFeel().provideErrorFeedback(tc);
+ ble.printStackTrace();
+ }
+
if (visible) {
listener.install(tc, addParamListStart);
// First time through, we'll need to create this window.
@@ -768,7 +719,7 @@ class ParameterizedCompletionDescriptionToolTip {
listener.uninstall();
}
- tooltip.setVisible(visible);
+ tip.setVisible(visible);
if (paramChoicesWindow!=null) {
// Only really needed to hide the window (i.e. visible==false)
paramChoicesWindow.setVisible(visible);
@@ -787,7 +738,7 @@ class ParameterizedCompletionDescriptionToolTip {
private void uninstallKeyBindings() {
if (AutoCompletion.getDebug()) {
- System.out.println("ToolTip: Uninstalling keybindings");
+ System.out.println("CompletionContext Uninstalling keybindings");
}
@@ -828,112 +779,11 @@ class ParameterizedCompletionDescriptionToolTip {
/**
- * Updates the text in the tool tip to have the current parameter
- * displayed in bold. The "current parameter" is determined from the
- * current caret position.
- *
- * @return Whether the text needed to be updated.
- */
- private boolean updateText() {
-
- JTextComponent tc = ac.getTextComponent();
- int dot = tc.getSelectionStart();
- int mark = tc.getSelectionEnd();
- int index = -1;
- paramPrefix = null;
-
- List paramHighlights = getParameterHighlights();
- for (int i=0; i<paramHighlights.size(); i++) {
- Highlight h = (Highlight)paramHighlights.get(i);
- // "+1" because of param hack - see OutlineHighlightPainter
- int start = h.getStartOffset()+1;
- if (dot>=start && dot<=h.getEndOffset()) {
- try {
- // All text selected => offer all suggestions
- if (dot==start && mark==h.getEndOffset()) {
- paramPrefix = null;
- }
- // Not everything selected => use prefix before selection
- else {
- paramPrefix = tc.getText(start, dot-start);
- }
- } catch (BadLocationException ble) {
- ble.printStackTrace();
- paramPrefix = null;
- }
- index = i;
- break;
- }
- }
-
- return updateText(index);
-
- }
-
-
- /**
- * Updates the text in the tool tip to have the current parameter
- * displayed in bold.
- *
- * @param selectedParam The index of the selected parameter.
- * @return Whether the text needed to be updated.
- */
- private boolean updateText(int selectedParam) {
-
- // Don't redo everything if they're just using the arrow keys to move
- // through each char of a single parameter, for example.
- if (selectedParam==lastSelectedParam) {
- return false;
- }
- lastSelectedParam = selectedParam;
-
- StringBuffer sb = new StringBuffer("<html>");
- int paramCount = pc.getParamCount();
- for (int i=0; i<paramCount; i++) {
-
- if (i==selectedParam) {
- sb.append("<b>");
- }
-
- // Some parameter types may have chars in them unfriendly to HTML
- // (such as type parameters in Java). We need to take care to
- // escape these.
- String temp = pc.getParam(i).toString();
- sb.append(RSyntaxUtilities.escapeForHtml(temp, "<br>", false));
-
- if (i==selectedParam) {
- sb.append("</b>");
- }
- if (i<paramCount-1) {
- sb.append(pc.getProvider().getParameterListSeparator());
- }
-
- }
-
- if (selectedParam>=0 && selectedParam<paramCount) {
- ParameterizedCompletion.Parameter param =
- pc.getParam(selectedParam);
- String desc = param.getDescription();
- if (desc!=null) {
- sb.append("<br>");
- sb.append(desc);
- }
- }
-
- descLabel.setText(sb.toString());
- tooltip.pack();
-
- return true;
-
- }
-
-
- /**
- * Updates the <tt>LookAndFeel</tt> of this window and the description
- * window.
+ * Updates the <code>LookAndFeel</code> of all popup windows this context
+ * manages.
*/
public void updateUI() {
- SwingUtilities.updateComponentTreeUI(tooltip);
+ tip.updateUI();
if (paramChoicesWindow!=null) {
paramChoicesWindow.updateUI();
}
@@ -1086,8 +936,8 @@ class ParameterizedCompletionDescriptionToolTip {
setVisible(false, false);
return;
}
- /*boolean updated = */updateText();
- if (tooltip.isVisible()) {
+ paramPrefix = tip.updateText();
+ if (tip.isVisible()) {
prepareParamChoicesWindow();
}
}
@@ -1146,11 +996,6 @@ class ParameterizedCompletionDescriptionToolTip {
*/
public void install(JTextComponent tc, boolean addParamStartList) {
- // Add listeners to the text component.
- tc.addCaretListener(this);
- tc.addFocusListener(this);
- installKeyBindings();
-
boolean replaceTabs = false;
if (tc instanceof RSyntaxTextArea) {
RSyntaxTextArea textArea = (RSyntaxTextArea)tc;
@@ -1203,6 +1048,11 @@ class ParameterizedCompletionDescriptionToolTip {
ble.printStackTrace(); // Never happens
}
+ // Add listeners to the text component, AFTER text insertion.
+ tc.addCaretListener(this);
+ tc.addFocusListener(this);
+ installKeyBindings();
+
}
@@ -1213,7 +1063,6 @@ class ParameterizedCompletionDescriptionToolTip {
/**
* Uninstalls this listener from the current text component.
- *
*/
public void uninstall() {
@@ -1241,9 +1090,6 @@ class ParameterizedCompletionDescriptionToolTip {
/**
* Action performed when the user presses the up or down arrow keys and
* the parameter completion choices popup is visible.
- *
- * @author Robert Futrell
- * @version 1.0
*/
private class NextChoiceAction extends AbstractAction {
@@ -1272,9 +1118,6 @@ class ParameterizedCompletionDescriptionToolTip {
/**
* Action performed when the user hits the tab key.
- *
- * @author Robert Futrell
- * @version 1.0
*/
private class NextParamAction extends AbstractAction {
@@ -1300,9 +1143,6 @@ class ParameterizedCompletionDescriptionToolTip {
/**
* Action performed when the user hits shift+tab.
- *
- * @author Robert Futrell
- * @version 1.0
*/
private class PrevParamAction extends AbstractAction {
diff --git a/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java b/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java
index 47a1e48..8d09c17 100644
--- a/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java
+++ b/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java
@@ -10,48 +10,20 @@
package org.fife.ui.autocomplete;
import java.awt.BorderLayout;
-import java.awt.Color;
-import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;
-import java.awt.event.ActionEvent;
-import java.awt.event.FocusEvent;
-import java.awt.event.FocusListener;
-import java.awt.event.InputEvent;
-import java.awt.event.KeyEvent;
-import java.util.ArrayList;
-import java.util.Iterator;
import java.util.List;
-import javax.swing.AbstractAction;
-import javax.swing.Action;
-import javax.swing.ActionMap;
import javax.swing.BorderFactory;
-import javax.swing.InputMap;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
-import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
-import javax.swing.UIManager;
-import javax.swing.event.CaretEvent;
-import javax.swing.event.CaretListener;
-import javax.swing.event.DocumentEvent;
-import javax.swing.event.DocumentListener;
-import javax.swing.text.AbstractDocument;
import javax.swing.text.BadLocationException;
-import javax.swing.text.Document;
-import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;
-import javax.swing.text.Position;
import javax.swing.text.Highlighter.Highlight;
-import org.fife.ui.autocomplete.ParameterizedCompletion.Parameter;
-import org.fife.ui.autocomplete.ParameterizedCompletionInsertionInfo.ReplacementCopy;
-import org.fife.ui.rsyntaxtextarea.DocumentRange;
import org.fife.ui.rsyntaxtextarea.PopupWindowDecorator;
-import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.RSyntaxUtilities;
-import org.fife.ui.rtextarea.ChangeableHighlightPainter;
/**
@@ -64,25 +36,14 @@ import org.fife.ui.rtextarea.ChangeableHighlightPainter;
class ParameterizedCompletionDescriptionToolTip {
/**
- * The actual tool tip.
+ * The parent context.
*/
- private JWindow tooltip;
+ private ParameterizedCompletionContext context;
/**
- * The painter to paint borders around the variables.
- */
- Highlighter.HighlightPainter p;
-
- Highlighter.HighlightPainter paramCopyP;
-
- /**
- * The tags for the highlights around parameters.
+ * The actual tool tip.
*/
- List tags;
-
- private List paramCopyInfos;
-
- private transient boolean ignoringDocumentEvents;
+ private JWindow tooltip;
/**
* The parent AutoCompletion instance.
@@ -100,64 +61,10 @@ class ParameterizedCompletionDescriptionToolTip {
private ParameterizedCompletion pc;
/**
- * Listens for events in the text component while this window is visible.
- */
- private Listener listener;
-
- /**
- * The minimum offset into the document that the caret can move to
- * before this tool tip disappears.
- */
- private int minPos;
-
- /**
- * The maximum offset into the document that the caret can move to
- * before this tool tip disappears.
- */
- private Position maxPos; // Moves with text inserted.
-
- private Position defaultEndOffs;
-
- /**
- * A small popup window giving likely choices for parameterized completions.
- */
- private ParameterizedCompletionChoicesWindow paramChoicesWindow;
-
- /**
- * The text before the caret for the current parameter. If
- * {@link #paramChoicesWindow} is non-<code>null</code>, this is used to
- * determine what parameter choices to actually show.
- */
- private String paramPrefix;
-
- /**
* The currently "selected" parameter in the displayed text.
*/
private int lastSelectedParam;
- private Object oldTabKey;
- private Action oldTabAction;
- private Object oldShiftTabKey;
- private Action oldShiftTabAction;
- private Object oldUpKey;
- private Action oldUpAction;
- private Object oldDownKey;
- private Action oldDownAction;
- private Object oldEnterKey;
- private Action oldEnterAction;
- private Object oldEscapeKey;
- private Action oldEscapeAction;
- private Object oldClosingKey;
- private Action oldClosingAction;
-
- private static final String IM_KEY_TAB = "ParamCompDescTip.Tab";
- private static final String IM_KEY_SHIFT_TAB = "ParamCompDescTip.ShiftTab";
- private static final String IM_KEY_UP = "ParamCompDescTip.Up";
- private static final String IM_KEY_DOWN = "ParamCompDescTip.Down";
- private static final String IM_KEY_ESCAPE = "ParamCompDescTip.Escape";
- private static final String IM_KEY_ENTER = "ParamCompDescTip.Enter";
- private static final String IM_KEY_CLOSING = "ParamCompDescTip.Closing";
-
/**
* Constructor.
@@ -167,10 +74,12 @@ class ParameterizedCompletionDescriptionToolTip {
* @param pc The completion being described.
*/
public ParameterizedCompletionDescriptionToolTip(Window owner,
- AutoCompletion ac, ParameterizedCompletion pc) {
+ ParameterizedCompletionContext context,
+ AutoCompletion ac, ParameterizedCompletion pc) {
tooltip = new JWindow(owner);
+ this.context = context;
this.ac = ac;
this.pc = pc;
@@ -200,508 +109,23 @@ class ParameterizedCompletionDescriptionToolTip {
updateText(0);
tooltip.setFocusableWindowState(false);
- listener = new Listener();
-
- p = new OutlineHighlightPainter(Color.GRAY);
- paramCopyP = new ChangeableHighlightPainter(new Color(255, 224, 224));
- tags = new ArrayList(1); // Usually small
- paramCopyInfos = new ArrayList(1);
-
- paramChoicesWindow = createParamChoicesWindow();
-
- }
-
-
- /**
- * Creates the completion window offering suggestions for parameters.
- *
- * @return The window.
- */
- private ParameterizedCompletionChoicesWindow createParamChoicesWindow() {
- ParameterizedCompletionChoicesWindow pcw =
- new ParameterizedCompletionChoicesWindow(tooltip.getOwner(),
- ac, this);
- pcw.initialize(pc);
- return pcw;
- }
-
-
- public String getArgumentText(int offs) {
- List paramHighlights = getParameterHighlights();
- if (paramHighlights==null || paramHighlights.size()==0) {
- return null;
- }
- for (int i=0; i<paramHighlights.size(); i++) {
- Highlight h = (Highlight)paramHighlights.get(i);
- if (offs>=h.getStartOffset() && offs<=h.getEndOffset()) {
- int start = h.getStartOffset() + 1;
- int len = h.getEndOffset() - start;
- JTextComponent tc = ac.getTextComponent();
- Document doc = tc.getDocument();
- try {
- return doc.getText(start, len);
- } catch (BadLocationException ble) {
- UIManager.getLookAndFeel().provideErrorFeedback(tc);
- ble.printStackTrace();
- return null;
- }
- }
- }
- return null;
- }
-
-
- /**
- * Returns the highlight of the current parameter.
- *
- * @return The current parameter's highlight, or <code>null</code> if
- * the caret is not in a parameter's bounds.
- * @see #getCurrentParameterStartOffset()
- */
- private Highlight getCurrentParameterHighlight() {
-
- JTextComponent tc = ac.getTextComponent();
- int dot = tc.getCaretPosition();
- if (dot>0) {
- dot--; // Workaround for Java Highlight issues
- }
-
- List paramHighlights = getParameterHighlights();
- for (int i=0; i<paramHighlights.size(); i++) {
- Highlight h = (Highlight)paramHighlights.get(i);
- if (dot>=h.getStartOffset() && dot<h.getEndOffset()) {
- return h;
- }
- }
-
- return null;
-
- }
-
-
- private int getCurrentParameterIndex() {
-
- JTextComponent tc = ac.getTextComponent();
- int dot = tc.getCaretPosition();
- if (dot>0) {
- dot--; // Workaround for Java Highlight issues
- }
-
- List paramHighlights = getParameterHighlights();
- for (int i=0; i<paramHighlights.size(); i++) {
- Highlight h = (Highlight)paramHighlights.get(i);
- if (dot>=h.getStartOffset() && dot<h.getEndOffset()) {
- return i;
- }
- }
-
- return -1;
}
- /**
- * Returns the starting offset of the current parameter.
- *
- * @return The current parameter's starting offset, or <code>-1</code> if
- * the caret is not in a parameter's bounds.
- * @see #getCurrentParameterHighlight()
- */
- private int getCurrentParameterStartOffset() {
- Highlight h = getCurrentParameterHighlight();
- return h!=null ? h.getStartOffset()+1 : -1;
- }
-
-
- /**
- * Returns the highlight from a list that comes "first" in a list. Even
- * though most parameter highlights are ordered, sometimes they aren't
- * (e.g. the "cursor" parameter in a template completion is always last,
- * even though it can be anywhere in the template).
- *
- * @param highlights The list of highlights. Assumed to be non-empty.
- * @return The highlight that comes first in the document.
- * @see #getLastHighlight(List)
- */
- private static final int getFirstHighlight(List highlights) {
- int first = -1;
- Highlight firstH = null;
- for (int i=0; i<highlights.size(); i++) {
- Highlight h = (Highlight)highlights.get(i);
- if (firstH==null || h.getStartOffset()<firstH.getStartOffset()) {
- firstH = h;
- first = i;
- }
- }
- return first;
- }
-
-
- /**
- * Returns the highlight from a list that comes "last" in that list. Even
- * though most parameter highlights are ordered, sometimes they aren't
- * (e.g. the "cursor" parameter in a template completion is always last,
- * even though it can be anywhere in the template.
- *
- * @param highlights The list of highlights. Assumed to be non-empty.
- * @return The highlight that comes last in the document.
- * @see #getFirstHighlight(List)
- */
- private static final int getLastHighlight(List highlights) {
- int last = -1;
- Highlight lastH = null;
- for (int i=highlights.size()-1; i>=0; i--) {
- Highlight h = (Highlight)highlights.get(i);
- if (lastH==null || h.getStartOffset()>lastH.getStartOffset()) {
- lastH = h;
- last = i;
- }
- }
- return last;
- }
-
-
- private List getParameterHighlights() {
- List paramHighlights = new ArrayList(1);
- JTextComponent tc = ac.getTextComponent();
- Highlight[] highlights = tc.getHighlighter().getHighlights();
- for (int i=0; i<highlights.length; i++) {
- if (highlights[i].getPainter()==p) {
- paramHighlights.add(highlights[i]);
- }
- }
- return paramHighlights;
- }
-
-
- /**
- * Inserts the choice selected in the parameter choices window.
- *
- * @return Whether the choice was inserted. This will be <code>false</code>
- * if the window is not visible, or no choice is selected.
- */
- boolean insertSelectedChoice() {
- if (paramChoicesWindow!=null && paramChoicesWindow.isVisible()) {
- String choice = paramChoicesWindow.getSelectedChoice();
- if (choice!=null) {
- JTextComponent tc = ac.getTextComponent();
- Highlight h = getCurrentParameterHighlight();
- if (h!=null) {
- // "+1" is a workaround for Java Highlight issues.
- tc.setSelectionStart(h.getStartOffset()+1);
- tc.setSelectionEnd(h.getEndOffset());
- tc.replaceSelection(choice);
- moveToNextParam();
- }
- else {
- UIManager.getLookAndFeel().provideErrorFeedback(tc);
- }
- return true;
- }
- }
- return false;
- }
-
-
- /**
- * Installs key bindings on the text component that facilitate the user
- * editing this completion's parameters.
- *
- * @see #uninstallKeyBindings()
- */
- private void installKeyBindings() {
-
- if (AutoCompletion.getDebug()) {
- System.out.println("ToolTip: Installing keybindings");
- }
-
- JTextComponent tc = ac.getTextComponent();
- InputMap im = tc.getInputMap();
- ActionMap am = tc.getActionMap();
-
- KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
- oldTabKey = im.get(ks);
- im.put(ks, IM_KEY_TAB);
- oldTabAction = am.get(IM_KEY_TAB);
- am.put(IM_KEY_TAB, new NextParamAction());
-
- ks = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK);
- oldShiftTabKey = im.get(ks);
- im.put(ks, IM_KEY_SHIFT_TAB);
- oldShiftTabAction = am.get(IM_KEY_SHIFT_TAB);
- am.put(IM_KEY_SHIFT_TAB, new PrevParamAction());
-
- ks = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
- oldUpKey = im.get(ks);
- im.put(ks, IM_KEY_UP);
- oldUpAction = am.get(IM_KEY_UP);
- am.put(IM_KEY_UP, new NextChoiceAction(-1, oldUpAction));
-
- ks = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
- oldDownKey = im.get(ks);
- im.put(ks, IM_KEY_DOWN);
- oldDownAction = am.get(IM_KEY_DOWN);
- am.put(IM_KEY_DOWN, new NextChoiceAction(1, oldDownAction));
-
- ks = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
- oldEnterKey = im.get(ks);
- im.put(ks, IM_KEY_ENTER);
- oldEnterAction = am.get(IM_KEY_ENTER);
- am.put(IM_KEY_ENTER, new GotoEndAction());
-
- ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
- oldEscapeKey = im.get(ks);
- im.put(ks, IM_KEY_ESCAPE);
- oldEscapeAction = am.get(IM_KEY_ESCAPE);
- am.put(IM_KEY_ESCAPE, new HideAction());
-
- char end = pc.getProvider().getParameterListEnd();
- ks = KeyStroke.getKeyStroke(end);
- oldClosingKey = im.get(ks);
- im.put(ks, IM_KEY_CLOSING);
- oldClosingAction = am.get(IM_KEY_CLOSING);
- am.put(IM_KEY_CLOSING, new ClosingAction());
-
- }
-
-
- /**
- * Moves to and selects the next parameter.
- *
- * @see #moveToPreviousParam()
- */
- private void moveToNextParam() {
-
- JTextComponent tc = ac.getTextComponent();
- int dot = tc.getCaretPosition();
- int tagCount = tags.size();
- if (tagCount==0) {
- tc.setCaretPosition(maxPos.getOffset());
- setVisible(false, false);
- }
-
- Highlight currentNext = null;
- int pos = -1;
- List highlights = getParameterHighlights();
- for (int i=0; i<highlights.size(); i++) {
- Highlight hl = (Highlight)highlights.get(i);
- // Check "< dot", not "<= dot" as OutlineHighlightPainter paints
- // starting at one char AFTER the highlight starts, to work around
- // Java issue. Thanks to Matthew Adereth!
- if (currentNext==null || currentNext.getStartOffset()</*=*/dot ||
- (hl.getStartOffset()>dot &&
- hl.getStartOffset()<=currentNext.getStartOffset())) {
- currentNext = hl;
- pos = i;
- }
- }
-
- // No params after caret - go to first one
- if (currentNext.getStartOffset()+1<=dot) {
- int nextIndex = getFirstHighlight(highlights);
- currentNext = (Highlight)highlights.get(nextIndex);
- pos = 0;
- }
-
- // "+1" is a workaround for Java Highlight issues.
- tc.setSelectionStart(currentNext.getStartOffset()+1);
- tc.setSelectionEnd(currentNext.getEndOffset());
- updateText(pos);
-
- }
-
-
- /**
- * Moves to and selects the previous parameter.
- *
- * @see #moveToNextParam()
- */
- private void moveToPreviousParam() {
-
- JTextComponent tc = ac.getTextComponent();
-
- int tagCount = tags.size();
- if (tagCount==0) { // Should never happen
- tc.setCaretPosition(maxPos.getOffset());
- setVisible(false, false);
- }
-
- int dot = tc.getCaretPosition();
- int selStart = tc.getSelectionStart()-1; // Workaround for Java Highlight issues.
- Highlight currentPrev = null;
- int pos = 0;
- List highlights = getParameterHighlights();
-
- for (int i=0; i<highlights.size(); i++) {
- Highlight h = (Highlight)highlights.get(i);
- if (currentPrev==null || currentPrev.getStartOffset()>=dot ||
- (h.getStartOffset()<selStart &&
- (h.getStartOffset()>currentPrev.getStartOffset() ||
- pos==lastSelectedParam))) {
- currentPrev = h;
- pos = i;
- }
- }
-
- // Loop back from param 0 to last param.
- int firstIndex = getFirstHighlight(highlights);
- //if (pos==0 && lastSelectedParam==0 && highlights.size()>1) {
- if (pos==firstIndex && lastSelectedParam==firstIndex && highlights.size()>1) {
- pos = getLastHighlight(highlights);
- currentPrev = (Highlight)highlights.get(pos);
- // "+1" is a workaround for Java Highlight issues.
- tc.setSelectionStart(currentPrev.getStartOffset()+1);
- tc.setSelectionEnd(currentPrev.getEndOffset());
- updateText(pos);
- }
- else if (currentPrev!=null && dot>currentPrev.getStartOffset()) {
- // "+1" is a workaround for Java Highlight issues.
- tc.setSelectionStart(currentPrev.getStartOffset()+1);
- tc.setSelectionEnd(currentPrev.getEndOffset());
- updateText(pos);
- }
- else {
- tc.setCaretPosition(maxPos.getOffset());
- setVisible(false, false);
- }
-
- }
-
-
- private void possiblyUpdateParamCopies(Document doc) {
-
- int index = getCurrentParameterIndex();
- // FunctionCompletions add an extra param at end of inserted text
- if (index>-1 && index<pc.getParamCount()) {
-
- // Typing in an "end parameter" => stop parameter assistance.
- Parameter param = pc.getParam(index);
- if (param.isEndParam()) {
- setVisible(false, false);
- return;
- }
-
- // Get the current value of the current parameter.
- List paramHighlights = getParameterHighlights();
- Highlight h = (Highlight)paramHighlights.get(index);
- int start = h.getStartOffset() + 1; // param offsets are offset (!) by 1
- int len = h.getEndOffset() - start;
- String replacement = null;
- try {
- replacement = doc.getText(start, len);
- } catch (BadLocationException ble) {
- ble.printStackTrace(); // Never happens
- }
-
- // Replace any param copies tracking this parameter with the
- // value of this parameter.
- for (Iterator i=paramCopyInfos.iterator(); i.hasNext(); ) {
- ParamCopyInfo pci = (ParamCopyInfo)i.next();
- if (pci.paramName.equals(param.getName())) {
- pci.h = replaceHighlightedText(doc, pci.h, replacement);
- }
- }
-
- }
-
- else { // Probably the "end parameter" for FunctionCompletions.
- setVisible(false, false);
- }
-
- }
-
-
- /**
- * Updates the optional window listing likely completion choices,
- */
- private void prepareParamChoicesWindow() {
-
- // If this window was set to null, the user pressed Escape to hide it
- if (paramChoicesWindow!=null) {
-
- int offs = getCurrentParameterStartOffset();
- if (offs==-1) {
- paramChoicesWindow.setVisible(false);
- return;
- }
-
- JTextComponent tc = ac.getTextComponent();
- try {
- Rectangle r = tc.modelToView(offs);
- Point p = new Point(r.x, r.y);
- SwingUtilities.convertPointToScreen(p, tc);
- r.x = p.x;
- r.y = p.y;
- paramChoicesWindow.setLocationRelativeTo(r);
- } catch (BadLocationException ble) { // Should never happen
- UIManager.getLookAndFeel().provideErrorFeedback(tc);
- ble.printStackTrace();
- }
-
- // Toggles visibility, if necessary.
- paramChoicesWindow.setParameter(lastSelectedParam, paramPrefix);
-
- }
-
- }
-
-
- /**
- * Removes the bounding boxes around parameters.
- */
- private void removeParameterHighlights() {
- JTextComponent tc = ac.getTextComponent();
- Highlighter h = tc.getHighlighter();
- for (int i=0; i<tags.size(); i++) {
- h.removeHighlight(tags.get(i));
- }
- tags.clear();
- for (int i=0; i<paramCopyInfos.size(); i++) {
- ParamCopyInfo pci = (ParamCopyInfo)paramCopyInfos.get(i);
- h.removeHighlight(pci.h);
- }
- paramCopyInfos.clear();
+ public int getLastSelectedParam() {
+ return lastSelectedParam;
}
/**
- * Replaces highlighted text with new text. Takes special care so that
- * the highlight stays just around the newly-highlighted text, since
- * Swing's <code>Highlight</code> classes are funny about insertions at
- * their start offsets.
+ * Returns whether this tool tip is visible.
*
- * @param doc The document.
- * @param h The highlight whose text to change.
- * @param replacement The new text to be in the highlight.
- * @return The replacement highlight for <code>h</code>.
+ * @return Whether this tool tip is visible.
+ * @see #setVisible(boolean)
*/
- private Highlight replaceHighlightedText(Document doc, Highlight h,
- String replacement) {
- try {
-
- int start = h.getStartOffset();
- int len = h.getEndOffset() - start;
- Highlighter highlighter = ac.getTextComponent().getHighlighter();
- highlighter.removeHighlight(h);
-
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).replace(start, len, replacement, null);
- }
- else {
- doc.remove(start, len);
- doc.insertString(start, replacement, null);
- }
-
- int newEnd = start + replacement.length();
- h = (Highlight)highlighter.addHighlight(start, newEnd, paramCopyP);
- return h;
-
- } catch (BadLocationException ble) {
- ble.printStackTrace(); // Never happens
- }
-
- return null;
-
+ public boolean isVisible() {
+ return tooltip.isVisible();
}
@@ -744,86 +168,11 @@ class ParameterizedCompletionDescriptionToolTip {
/**
* Toggles the visibility of this tool tip.
*
- * @param visible Whether the tool tip should be visible.
- * @param addParamListStart Whether or not
- * {@link CompletionProvider#getParameterListStart()} should be
- * added to the text component. If <code>visible</code> is
- * <code>false</code>, this parameter is ignored.
- */
- public void setVisible(boolean visible, boolean addParamListStart) {
-
- if (visible!=tooltip.isVisible()) {
-
- JTextComponent tc = ac.getTextComponent();
-
- if (visible) {
- listener.install(tc, addParamListStart);
- // First time through, we'll need to create this window.
- if (paramChoicesWindow==null) {
- paramChoicesWindow = createParamChoicesWindow();
- }
- prepareParamChoicesWindow();
- }
- else {
- listener.uninstall();
- }
-
- tooltip.setVisible(visible);
- if (paramChoicesWindow!=null) {
- // Only really needed to hide the window (i.e. visible==false)
- paramChoicesWindow.setVisible(visible);
- }
-
- }
-
- }
-
-
- /**
- * Removes the key bindings we installed.
- *
- * @see #installKeyBindings()
+ * @param visible Whether this tool tip should be visible.
+ * @see #isVisible()
*/
- private void uninstallKeyBindings() {
-
- if (AutoCompletion.getDebug()) {
- System.out.println("ToolTip: Uninstalling keybindings");
- }
-
-
- JTextComponent tc = ac.getTextComponent();
- InputMap im = tc.getInputMap();
- ActionMap am = tc.getActionMap();
-
- KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
- im.put(ks, oldTabKey);
- am.put(IM_KEY_TAB, oldTabAction);
-
- ks = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK);
- im.put(ks, oldShiftTabKey);
- am.put(IM_KEY_SHIFT_TAB, oldShiftTabAction);
-
- ks = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
- im.put(ks, oldUpKey);
- am.put(IM_KEY_UP, oldUpAction);
-
- ks = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
- im.put(ks, oldDownKey);
- am.put(IM_KEY_DOWN, oldDownAction);
-
- ks = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
- im.put(ks, oldEnterKey);
- am.put(IM_KEY_ENTER, oldEnterAction);
-
- ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
- im.put(ks, oldEscapeKey);
- am.put(IM_KEY_ESCAPE, oldEscapeAction);
-
- char end = pc.getProvider().getParameterListEnd();
- ks = KeyStroke.getKeyStroke(end);
- im.put(ks, oldClosingKey);
- am.put(IM_KEY_CLOSING, oldClosingAction);
-
+ public void setVisible(boolean visible) {
+ tooltip.setVisible(visible);
}
@@ -832,41 +181,38 @@ class ParameterizedCompletionDescriptionToolTip {
* displayed in bold. The "current parameter" is determined from the
* current caret position.
*
- * @return Whether the text needed to be updated.
+ * @return The "prefix" of text in the caret's parameter before the caret.
*/
- private boolean updateText() {
+ public String updateText() {
JTextComponent tc = ac.getTextComponent();
int dot = tc.getSelectionStart();
int mark = tc.getSelectionEnd();
int index = -1;
- paramPrefix = null;
+ String paramPrefix = null;
- List paramHighlights = getParameterHighlights();
+ List paramHighlights = context.getParameterHighlights();
for (int i=0; i<paramHighlights.size(); i++) {
Highlight h = (Highlight)paramHighlights.get(i);
// "+1" because of param hack - see OutlineHighlightPainter
int start = h.getStartOffset()+1;
if (dot>=start && dot<=h.getEndOffset()) {
try {
- // All text selected => offer all suggestions
- if (dot==start && mark==h.getEndOffset()) {
- paramPrefix = null;
- }
- // Not everything selected => use prefix before selection
- else {
+ // All text selected => offer all suggestions, otherwise
+ // use prefix before selection
+ if (dot!=start || mark!=h.getEndOffset()) {
paramPrefix = tc.getText(start, dot-start);
}
} catch (BadLocationException ble) {
ble.printStackTrace();
- paramPrefix = null;
}
index = i;
break;
}
}
- return updateText(index);
+ updateText(index);
+ return paramPrefix;
}
@@ -878,7 +224,7 @@ class ParameterizedCompletionDescriptionToolTip {
* @param selectedParam The index of the selected parameter.
* @return Whether the text needed to be updated.
*/
- private boolean updateText(int selectedParam) {
+ public boolean updateText(int selectedParam) {
// Don't redo everything if they're just using the arrow keys to move
// through each char of a single parameter, for example.
@@ -934,382 +280,6 @@ class ParameterizedCompletionDescriptionToolTip {
*/
public void updateUI() {
SwingUtilities.updateComponentTreeUI(tooltip);
- if (paramChoicesWindow!=null) {
- paramChoicesWindow.updateUI();
- }
- }
-
-
- /**
- * Called when the user presses Enter while entering parameters.
- *
- * @author Robert Futrell
- * @version 1.0
- */
- private class GotoEndAction extends AbstractAction {
-
- public void actionPerformed(ActionEvent e) {
-
- // If the param choices window is visible and something is chosen,
- // replace the parameter with it and move to the next one.
- if (paramChoicesWindow!=null && paramChoicesWindow.isVisible()) {
- if (insertSelectedChoice()) {
- return;
- }
- }
-
- // Otherwise, just move to the end.
- JTextComponent tc = ac.getTextComponent();
- tc.setCaretPosition(defaultEndOffs.getOffset());
- setVisible(false, false);
-
- }
-
- }
-
-
- /**
- * Called when the user types the character marking the closing of the
- * parameter list, such as '<code>)</code>'.
- *
- * @author Robert Futrell
- * @version 1.0
- */
- private class ClosingAction extends AbstractAction {
-
- public void actionPerformed(ActionEvent e) {
-
- JTextComponent tc = ac.getTextComponent();
- int dot = tc.getCaretPosition();
- char end = pc.getProvider().getParameterListEnd();
-
- // Are they at or past the end of the parameters?
- if (dot>=maxPos.getOffset()-2) { // ">=" for overwrite mode
-
- if (dot==maxPos.getOffset()-1) { // Happens in overwrite mode
- tc.replaceSelection(Character.toString(end));
- }
-
- else { // Typical case.
- // Try to decide if we're closing a paren that is a part
- // of the (last) arg being typed.
- String text = getArgumentText(dot);
- if (text!=null) {
- char start = pc.getProvider().getParameterListStart();
- int startCount = getCount(text, start);
- int endCount = getCount(text, end);
- if (startCount>endCount) { // Just closing a paren
- tc.replaceSelection(Character.toString(end));
- return;
- }
- }
- //tc.setCaretPosition(maxPos.getOffset());
- tc.setCaretPosition(tc.getCaretPosition()+1);
- }
-
- setVisible(false, false);
-
- }
-
- // If not (in the middle of parameters), just insert the paren.
- else {
- tc.replaceSelection(Character.toString(end));
- }
-
- }
-
- public int getCount(String text, char ch) {
- int count = 0;
- int old = 0;
- int pos = 0;
- while ((pos=text.indexOf(ch, old))>-1) {
- count++;
- old = pos + 1;
- }
-
- return count;
- }
-
- }
-
-
- /**
- * Action performed when the user hits the escape key.
- *
- * @author Robert Futrell
- * @version 1.0
- */
- private class HideAction extends AbstractAction {
-
- public void actionPerformed(ActionEvent e) {
- // On first escape press, if the param choices window is visible,
- // just remove it, but keep ability to tab through params. If
- // param choices window isn't visible, or second escape press,
- // exit tabbing through params entirely.
- if (paramChoicesWindow!=null && paramChoicesWindow.isVisible()) {
- paramChoicesWindow.setVisible(false);
- paramChoicesWindow = null;
- }
- else {
- setVisible(false, false);
- }
- }
-
- }
-
-
- /**
- * Listens for various events in the text component while this tool tip
- * is visible.
- *
- * @author Robert Futrell
- * @version 1.0
- */
- private class Listener implements FocusListener, CaretListener,
- DocumentListener {
-
- private boolean markOccurrencesEnabled;
-
- /**
- * Called when the text component's caret moves.
- *
- * @param e The event.
- */
- public void caretUpdate(CaretEvent e) {
- if (maxPos==null) { // Sanity check
- setVisible(false, false);
- return;
- }
- int dot = e.getDot();
- if (dot<minPos || dot>=maxPos.getOffset()) {
- System.err.println(">>>>> dot==" + dot + ", " + minPos + ", " + maxPos);
- setVisible(false, false);
- return;
- }
- /*boolean updated = */updateText();
- if (tooltip.isVisible()) {
- prepareParamChoicesWindow();
- }
- }
-
-
- public void changedUpdate(DocumentEvent e) {
- }
-
-
- /**
- * Called when the text component gains focus.
- *
- * @param e The event.
- */
- public void focusGained(FocusEvent e) {
- // Do nothing
- }
-
-
- /**
- * Called when the text component loses focus.
- *
- * @param e The event.
- */
- public void focusLost(FocusEvent e) {
- setVisible(false, false);
- }
-
-
- private void handleDocumentEvent(final DocumentEvent e) {
- if (!ignoringDocumentEvents) {
- ignoringDocumentEvents = true;
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- possiblyUpdateParamCopies(e.getDocument());
- ignoringDocumentEvents = false;
- }
- });
- }
- }
-
-
- public void insertUpdate(DocumentEvent e) {
- handleDocumentEvent(e);
- }
-
-
- /**
- * Installs this listener onto a text component.
- *
- * @param tc The text component to install onto.
- * @param addParamStartList Whether or not
- * {@link CompletionProvider#getParameterListStart()} should be
- * added to the text component.
- * @see #uninstall()
- */
- public void install(JTextComponent tc, boolean addParamStartList) {
-
- // Add listeners to the text component.
- tc.addCaretListener(this);
- tc.addFocusListener(this);
- installKeyBindings();
-
- boolean replaceTabs = false;
- if (tc instanceof RSyntaxTextArea) {
- RSyntaxTextArea textArea = (RSyntaxTextArea)tc;
- markOccurrencesEnabled = textArea.getMarkOccurrences();
- textArea.setMarkOccurrences(false);
- replaceTabs = textArea.getTabsEmulated();
- }
-
- Highlighter h = tc.getHighlighter();
-
- try {
-
- // Insert the parameter text
- ParameterizedCompletionInsertionInfo info =
- pc.getInsertionInfo(tc, addParamStartList, replaceTabs);
- tc.replaceSelection(info.getTextToInsert());
-
- // Add highlights around the parameters.
- for (int i=0; i<info.getReplacementCount(); i++) {
- DocumentRange dr = info.getReplacementLocation(i);
- // "-1" is a workaround for Java Highlight issues.
- tags.add(h.addHighlight(dr.getStartOffset()-1, dr.getEndOffset(), p));
- }
- for (int i=0; i<info.getReplacementCopyCount(); i++) {
- ReplacementCopy rc = info.getReplacementCopy(i);
- paramCopyInfos.add(new ParamCopyInfo(rc.getId(),
- (Highlight)h.addHighlight(rc.getStart(), rc.getEnd(),
- paramCopyP)));
- }
-
- // Go back and start at the first parameter.
- tc.setCaretPosition(info.getSelectionStart());
- if (info.hasSelection()) {
- tc.moveCaretPosition(info.getSelectionEnd());
- }
-
- minPos = info.getMinOffset();
- maxPos = info.getMaxOffset();
- try {
- defaultEndOffs = tc.getDocument().createPosition(
- info.getDefaultEndOffs());
- } catch (BadLocationException ble) {
- ble.printStackTrace(); // Never happens
- }
-
- // Listen for document events AFTER we insert
- tc.getDocument().addDocumentListener(this);
-
- } catch (BadLocationException ble) {
- ble.printStackTrace(); // Never happens
- }
-
- }
-
-
- public void removeUpdate(DocumentEvent e) {
- handleDocumentEvent(e);
- }
-
-
- /**
- * Uninstalls this listener from the current text component.
- *
- */
- public void uninstall() {
-
- JTextComponent tc = ac.getTextComponent();
- tc.removeCaretListener(this);
- tc.removeFocusListener(this);
- tc.getDocument().removeDocumentListener(this);
- uninstallKeyBindings();
-
- if (markOccurrencesEnabled) {
- ((RSyntaxTextArea)tc).setMarkOccurrences(markOccurrencesEnabled);
- }
-
- // Remove WeakReferences in javax.swing.text.
- maxPos = null;
- minPos = -1;
- removeParameterHighlights();
-
- }
-
-
- }
-
-
- /**
- * Action performed when the user presses the up or down arrow keys and
- * the parameter completion choices popup is visible.
- *
- * @author Robert Futrell
- * @version 1.0
- */
- private class NextChoiceAction extends AbstractAction {
-
- private Action oldAction;
- private int amount;
-
- public NextChoiceAction(int amount, Action oldAction) {
- this.amount = amount;
- this.oldAction = oldAction;
- }
-
- public void actionPerformed(ActionEvent e) {
- if (paramChoicesWindow!=null && paramChoicesWindow.isVisible()) {
- paramChoicesWindow.incSelection(amount);
- }
- else if (oldAction!=null) {
- oldAction.actionPerformed(e);
- }
- else {
- setVisible(false, false);
- }
- }
-
- }
-
-
- /**
- * Action performed when the user hits the tab key.
- *
- * @author Robert Futrell
- * @version 1.0
- */
- private class NextParamAction extends AbstractAction {
-
- public void actionPerformed(ActionEvent e) {
- moveToNextParam();
- }
-
- }
-
-
- private static class ParamCopyInfo {
-
- private String paramName;
- private Highlight h;
-
- public ParamCopyInfo(String paramName, Highlight h) {
- this.paramName = paramName;
- this.h = h;
- }
-
- }
-
-
- /**
- * Action performed when the user hits shift+tab.
- *
- * @author Robert Futrell
- * @version 1.0
- */
- private class PrevParamAction extends AbstractAction {
-
- public void actionPerformed(ActionEvent e) {
- moveToPreviousParam();
- }
-
}
diff --git a/src/org/fife/ui/autocomplete/TemplatePiece.java b/src/org/fife/ui/autocomplete/TemplatePiece.java
index df69343..095478f 100644
--- a/src/org/fife/ui/autocomplete/TemplatePiece.java
+++ b/src/org/fife/ui/autocomplete/TemplatePiece.java
@@ -1,3 +1,11 @@
+/*
+ * 06/17/2012
+ *
+ * TemplatePiece.java - A logical piece of a template completion.
+ *
+ * This library is distributed under a modified BSD license. See the included
+ * RSyntaxTextArea.License.txt file for details.
+ */
package org.fife.ui.autocomplete;
--
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