From e23bea0ee6156213050ac361b846d06bd5958f7d Mon Sep 17 00:00:00 2001 From: ricardojlrufino Date: Wed, 17 Dec 2014 00:41:51 -0300 Subject: [PATCH 1/3] fixed #13, fixed #9, new New events for the Autocomplete: COMPLETION_FINISH, PARAMETER_COMPLETION_SELECT - Allow the user to get the values of the parameters of one ParameterizedCompletion --- .../ui/autocomplete/AbstractCompletion.java | 2 +- .../fife/ui/autocomplete/AutoCompletion.java | 63 +++++++++++++----- .../ui/autocomplete/AutoCompletionEvent.java | 8 ++- .../ui/autocomplete/FunctionCompletion.java | 2 +- .../ParameterChoicesProvider.java | 4 +- .../autocomplete/ParameterizedCompletion.java | 1 - .../ParameterizedCompletionChoicesWindow.java | 2 +- .../ParameterizedCompletionContext.java | 66 +++++++++++++++++-- .../ParameterizedCompletionEvent.java | 60 +++++++++++++++++ 9 files changed, 177 insertions(+), 31 deletions(-) create mode 100755 src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionEvent.java diff --git a/src/main/java/org/fife/ui/autocomplete/AbstractCompletion.java b/src/main/java/org/fife/ui/autocomplete/AbstractCompletion.java index 8c6d2dc..351fc29 100644 --- a/src/main/java/org/fife/ui/autocomplete/AbstractCompletion.java +++ b/src/main/java/org/fife/ui/autocomplete/AbstractCompletion.java @@ -79,7 +79,7 @@ public int compareTo(Completion c2) { return 0; } else if (c2!=null) { - return toString().compareToIgnoreCase(c2.toString()); + return getInputText().compareToIgnoreCase(c2.getInputText()); } return -1; } diff --git a/src/main/java/org/fife/ui/autocomplete/AutoCompletion.java b/src/main/java/org/fife/ui/autocomplete/AutoCompletion.java index 8a68e78..ac8bcd1 100644 --- a/src/main/java/org/fife/ui/autocomplete/AutoCompletion.java +++ b/src/main/java/org/fife/ui/autocomplete/AutoCompletion.java @@ -10,10 +10,11 @@ import java.awt.*; import java.awt.event.*; -import java.beans.*; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.util.List; + import javax.swing.*; -import javax.swing.Timer; import javax.swing.event.*; import javax.swing.text.*; @@ -295,24 +296,30 @@ public void doCompletion() { */ protected void fireAutoCompletionEvent(AutoCompletionEvent.Type type) { - // Guaranteed to return a non-null array - Object[] listeners = this.listeners.getListenerList(); - AutoCompletionEvent e = null; - - // Process the listeners last to first, notifying those that are - // interested in this event - for (int i=listeners.length-2; i>=0; i-=2) { - if (listeners[i] == AutoCompletionListener.class) { - if (e==null) { - e = new AutoCompletionEvent(this, type); - } - ((AutoCompletionListener)listeners[i+1]).autoCompleteUpdate(e); - } - } + fireAutoCompletionEvent(new AutoCompletionEvent(this, type)); } + /** + * Fires an {@link AutoCompletionEvent}. + * @param event + */ + protected void fireAutoCompletionEvent(AutoCompletionEvent event) { + + // Guaranteed to return a non-null array + Object[] listeners = this.listeners.getListenerList(); + + // Process the listeners last to first, notifying those that are + // interested in this event + for (int i=listeners.length-2; i>=0; i-=2) { + if (listeners[i] == AutoCompletionListener.class) { + ((AutoCompletionListener)listeners[i+1]).autoCompleteUpdate(event); + } + } + + } + /** * Returns the delay between when the user types a character and when the * code completion popup should automatically appear (if applicable). @@ -611,6 +618,28 @@ protected void insertCompletion(Completion c, } } + + /** + * Method called when the {@link ParameterizedCompletionContext} is active and the assistance ends.
+ * You can use {@link ParameterizedCompletionContext#getParameterValues()}, to get the values of the parameters entered by the user. + * @param context + */ + protected void onParameterizedCompletionFinish( ParameterizedCompletionContext context ){ + fireAutoCompletionEvent(new ParameterizedCompletionEvent(context, AutoCompletionEvent.Type.PARAMETER_COMPLETION_FINISH)); + } + + /** + * Method called when the {@link ParameterChoicesProvider} is active and the user selects the parameter in the list of available values.
+ * The default implementation simply replaces the text of the current selection with the 'choice'. + * @param context + */ + protected void onParameterizedCompletionSelect( ParameterizedCompletionContext context , int paramIndex , String choice ){ + textComponent.replaceSelection(choice); + ParameterizedCompletionEvent event = new ParameterizedCompletionEvent(context, AutoCompletionEvent.Type.PARAMETER_COMPLETION_SELECT); + event.setParamIndex(paramIndex); + event.setChoice(choice); + fireAutoCompletionEvent(event); + } /** @@ -1141,7 +1170,7 @@ public void setTriggerKey(KeyStroke ks) { * @param typedParamListStartChar Whether the parameterized completion list * starting character was typed. */ - private void startParameterizedCompletionAssistance( + public void startParameterizedCompletionAssistance( ParameterizedCompletion pc, boolean typedParamListStartChar) { // Get rid of the previous tool tip window, if there is one. diff --git a/src/main/java/org/fife/ui/autocomplete/AutoCompletionEvent.java b/src/main/java/org/fife/ui/autocomplete/AutoCompletionEvent.java index 9174a4c..0abc9b0 100644 --- a/src/main/java/org/fife/ui/autocomplete/AutoCompletionEvent.java +++ b/src/main/java/org/fife/ui/autocomplete/AutoCompletionEvent.java @@ -64,7 +64,13 @@ public Type getEventType() { */ public static enum Type { POPUP_SHOWN, - POPUP_HIDDEN + POPUP_HIDDEN, + /** Type of {@link ParameterizedCompletionEvent} + * @see {@link AutoCompletion#onParameterizedCompletionFinish(ParameterizedCompletionContext)}*/ + PARAMETER_COMPLETION_FINISH, + /** Type of {@link ParameterizedCompletionEvent} + * @see {@link AutoCompletion#onParameterizedCompletionSelect(ParameterizedCompletionContext, int, String)*/ + PARAMETER_COMPLETION_SELECT } diff --git a/src/main/java/org/fife/ui/autocomplete/FunctionCompletion.java b/src/main/java/org/fife/ui/autocomplete/FunctionCompletion.java index 94d9622..54d7e75 100644 --- a/src/main/java/org/fife/ui/autocomplete/FunctionCompletion.java +++ b/src/main/java/org/fife/ui/autocomplete/FunctionCompletion.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.List; + import javax.swing.text.BadLocationException; import javax.swing.text.JTextComponent; import javax.swing.text.Position; @@ -318,5 +319,4 @@ public void setReturnValueDescription(String desc) { this.returnValDesc = desc; } - } \ No newline at end of file diff --git a/src/main/java/org/fife/ui/autocomplete/ParameterChoicesProvider.java b/src/main/java/org/fife/ui/autocomplete/ParameterChoicesProvider.java index dc80325..f01bdc5 100644 --- a/src/main/java/org/fife/ui/autocomplete/ParameterChoicesProvider.java +++ b/src/main/java/org/fife/ui/autocomplete/ParameterChoicesProvider.java @@ -29,12 +29,12 @@ public interface ParameterChoicesProvider { * Returns a list of choices for a specific parameter. * * @param tc The text component. + * @param pc The currently ParameterizedCompletion * @param param The currently focused parameter. * @return The list of parameters. This may be null for * "no parameters," but might also be an empty list. */ - public List getParameterChoices(JTextComponent tc, - ParameterizedCompletion.Parameter param); + public List getParameterChoices(JTextComponent tc, ParameterizedCompletion pc , ParameterizedCompletion.Parameter param); } \ No newline at end of file diff --git a/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletion.java b/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletion.java index e13ada0..601d139 100644 --- a/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletion.java +++ b/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletion.java @@ -61,7 +61,6 @@ public ParameterizedCompletionInsertionInfo getInsertionInfo( */ public boolean getShowParameterToolTip(); - /** * A parameter passed to a parameterized {@link Completion}. */ diff --git a/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionChoicesWindow.java b/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionChoicesWindow.java index 6cc5a1e..266b5ed 100644 --- a/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionChoicesWindow.java +++ b/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionChoicesWindow.java @@ -171,7 +171,7 @@ public void initialize(ParameterizedCompletion pc) { for (int i=0; i choices = pcp.getParameterChoices(tc, param); + List choices = pcp.getParameterChoices(tc, pc, param); choicesListList.add(choices); } diff --git a/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionContext.java b/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionContext.java index 7eda9d5..5c1d6bf 100644 --- a/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionContext.java +++ b/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionContext.java @@ -18,7 +18,9 @@ import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; + import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.ActionMap; @@ -58,7 +60,7 @@ * @author Robert Futrell * @version 1.0 */ -class ParameterizedCompletionContext { +public class ParameterizedCompletionContext { /** * The parent window. @@ -98,6 +100,8 @@ class ParameterizedCompletionContext { * The tags for the highlights around parameters. */ private List tags; + + private List parameterValues; private List paramCopyInfos; @@ -246,15 +250,15 @@ private ParameterizedCompletionChoicesWindow createParamChoicesWindow() { /** - * Hides any popup windows and terminates parameterized completion - * assistance. - * + * Hides any popup windows and terminates parameterized completion assistance. * @see #activate() */ public void deactivate() { if (!active) { return; } + updateParamValues(); + active = false; listener.uninstall(); if (tip!=null) { @@ -263,10 +267,39 @@ public void deactivate() { if (paramChoicesWindow!=null) { paramChoicesWindow.setVisible(false); } + + ac.onParameterizedCompletionFinish(this); + } - /** + private void updateParamValues() { + + if(active){ + ParameterizedCompletion completion = getParameterizedCompletion(); + + JTextComponent texarea = ac.getTextComponent(); + + List highlights = getParameterHighlights(); + + List values = new LinkedList(); + + for (int i = 0; i < completion.getParamCount(); i++) { + Highlight h = highlights.get(i); + int start = h.getStartOffset()+1; // "+1" is a workaround for Java Highlight issues. + try { + values.add(texarea.getText(start, h.getEndOffset() - start)); + } catch (BadLocationException e) { + values.add(""); + } + } + + parameterValues = values; + } + + } + + /** * Returns the text inserted for the parameter containing the specified * offset. * @@ -419,6 +452,14 @@ public List getParameterHighlights() { } return paramHighlights; } + + /** + * Get current parameter values. + * @return + */ + public List getParameterValues() { + return parameterValues; + } /** @@ -437,7 +478,7 @@ boolean insertSelectedChoice() { // "+1" is a workaround for Java Highlight issues. tc.setSelectionStart(h.getStartOffset()+1); tc.setSelectionEnd(h.getEndOffset()); - tc.replaceSelection(choice); + ac.onParameterizedCompletionSelect(this, getCurrentParameterIndex(), choice); moveToNextParam(); } else { @@ -448,7 +489,18 @@ boolean insertSelectedChoice() { } return false; } - + + public JTextComponent getTextComponent(){ + return ac.getTextComponent(); + } + + public ParameterizedCompletion getParameterizedCompletion() { + return pc; + } + + public AutoCompletion getAutoCompletion() { + return ac; + } /** * Installs key bindings on the text component that facilitate the user diff --git a/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionEvent.java b/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionEvent.java new file mode 100755 index 0000000..cf6996b --- /dev/null +++ b/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionEvent.java @@ -0,0 +1,60 @@ +package org.fife.ui.autocomplete; + + +/** + * An event fired by an instance of {@link AutoCompletion}. + * This can be used by applications that wish to be notified of the auto-complete of {@link ParameterizedCompletion} + * @author Ricardo JL Rufino (ricardo@criativasoft.com.br) + */ +public class ParameterizedCompletionEvent extends AutoCompletionEvent{ + + private ParameterizedCompletionContext context; + + private int paramIndex; + private String choice; + + public ParameterizedCompletionEvent(ParameterizedCompletionContext context, Type type) { + super(context.getAutoCompletion(), type); + this.context = context; + } + + public void setParamIndex( int paramIndex ) { + this.paramIndex = paramIndex; + } + + public void setChoice( String choice ) { + this.choice = choice; + } + + /** + * Get the execution context of {@link ParameterizedCompletion} + * You can use {@link ParameterizedCompletionContext#getParameterValues()}, to get the values of the parameters entered by the user. + * @return The ParameterizedCompletionContext + */ + public ParameterizedCompletionContext getContext() { + return context; + } + + public ParameterizedCompletion getCompletion() { + return context.getParameterizedCompletion(); + } + + /** + * Get the index of the parameter being edited.
+ * This only works for the event: {@link AutoCompletionEvent.Type#PARAMETER_COMPLETION_SELECT} + * @return + */ + public int getParamIndex() { + return paramIndex; + } + + /** + * Gets the element that was selected in the parameter autocomplete list.
+ * This only works for the event: {@link AutoCompletionEvent.Type#PARAMETER_COMPLETION_SELECT} + * @return + */ + public String getChoice() { + return choice; + } + +} From b7a80955bd0c973ff06f2d8c8abc123598b52693 Mon Sep 17 00:00:00 2001 From: ricardojlrufino Date: Sun, 8 Mar 2015 19:34:04 -0300 Subject: [PATCH 2/3] fix autocomplete ParamChoicesWindow with one parameter --- .../ui/autocomplete/ParameterizedCompletionContext.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionContext.java b/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionContext.java index 5c1d6bf..8f06fec 100644 --- a/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionContext.java +++ b/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionContext.java @@ -479,7 +479,11 @@ boolean insertSelectedChoice() { tc.setSelectionStart(h.getStartOffset()+1); tc.setSelectionEnd(h.getEndOffset()); ac.onParameterizedCompletionSelect(this, getCurrentParameterIndex(), choice); - moveToNextParam(); + if(tags.size() > 1){ + moveToNextParam(); + }else{ + deactivate(); + } } else { UIManager.getLookAndFeel().provideErrorFeedback(tc); From 140c9956a40190ccbc0e258beb1dc317c8a50e09 Mon Sep 17 00:00:00 2001 From: ricardojlrufino Date: Sun, 8 Mar 2015 23:10:13 -0300 Subject: [PATCH 3/3] javadocs --- .../ui/autocomplete/ParameterizedCompletionEvent.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionEvent.java b/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionEvent.java index cf6996b..88f7162 100755 --- a/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionEvent.java +++ b/src/main/java/org/fife/ui/autocomplete/ParameterizedCompletionEvent.java @@ -40,18 +40,18 @@ public ParameterizedCompletion getCompletion() { } /** - * Get the index of the parameter being edited.
+ * Get the index of the parameter being edited. * This only works for the event: {@link AutoCompletionEvent.Type#PARAMETER_COMPLETION_SELECT} - * @return + * @return param index. */ public int getParamIndex() { return paramIndex; } /** - * Gets the element that was selected in the parameter autocomplete list.
+ * Gets the element that was selected in the parameter autocomplete list. * This only works for the event: {@link AutoCompletionEvent.Type#PARAMETER_COMPLETION_SELECT} - * @return + * @return selected choice */ public String getChoice() { return choice;