Skip to content

fixed #13, fixed #9, new New events for the Autocomplete: #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
63 changes: 46 additions & 17 deletions src/main/java/org/fife/ui/autocomplete/AutoCompletion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -611,6 +618,28 @@ protected void insertCompletion(Completion c,
}

}

/**
* Method called when the {@link ParameterizedCompletionContext} is active and the assistance ends.<br/>
* 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.<br/>
* 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);
}


/**
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -318,5 +319,4 @@ public void setReturnValueDescription(String desc) {
this.returnValDesc = desc;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>null</code> for
* "no parameters," but might also be an empty list.
*/
public List<Completion> getParameterChoices(JTextComponent tc,
ParameterizedCompletion.Parameter param);
public List<Completion> getParameterChoices(JTextComponent tc, ParameterizedCompletion pc , ParameterizedCompletion.Parameter param);


}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public ParameterizedCompletionInsertionInfo getInsertionInfo(
*/
public boolean getShowParameterToolTip();


/**
* A parameter passed to a parameterized {@link Completion}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void initialize(ParameterizedCompletion pc) {

for (int i=0; i<paramCount; i++) {
ParameterizedCompletion.Parameter param = pc.getParam(i);
List<Completion> choices = pcp.getParameterChoices(tc, param);
List<Completion> choices = pcp.getParameterChoices(tc, pc, param);
choicesListList.add(choices);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,7 +60,7 @@
* @author Robert Futrell
* @version 1.0
*/
class ParameterizedCompletionContext {
public class ParameterizedCompletionContext {

/**
* The parent window.
Expand Down Expand Up @@ -98,6 +100,8 @@ class ParameterizedCompletionContext {
* The tags for the highlights around parameters.
*/
private List<Object> tags;

private List<String> parameterValues;

private List<ParamCopyInfo> paramCopyInfos;

Expand Down Expand Up @@ -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) {
Expand All @@ -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<Highlight> highlights = getParameterHighlights();

List<String> values = new LinkedList<String>();

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.
*
Expand Down Expand Up @@ -419,6 +452,14 @@ public List<Highlight> getParameterHighlights() {
}
return paramHighlights;
}

/**
* Get current parameter values.
* @return
*/
public List<String> getParameterValues() {
return parameterValues;
}


/**
Expand All @@ -437,8 +478,12 @@ boolean insertSelectedChoice() {
// "+1" is a workaround for Java Highlight issues.
tc.setSelectionStart(h.getStartOffset()+1);
tc.setSelectionEnd(h.getEndOffset());
tc.replaceSelection(choice);
moveToNextParam();
ac.onParameterizedCompletionSelect(this, getCurrentParameterIndex(), choice);
if(tags.size() > 1){
moveToNextParam();
}else{
deactivate();
}
}
else {
UIManager.getLookAndFeel().provideErrorFeedback(tc);
Expand All @@ -448,7 +493,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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
*/
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 param index.
*/
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 selected choice
*/
public String getChoice() {
return choice;
}

}