|
|
|
@ -8,26 +8,28 @@
|
|
|
|
|
*/ |
|
|
|
|
package com.fr.design.gui.autocomplete; |
|
|
|
|
|
|
|
|
|
import java.awt.*; |
|
|
|
|
import java.awt.event.*; |
|
|
|
|
import java.beans.PropertyChangeEvent; |
|
|
|
|
import java.beans.PropertyChangeListener; |
|
|
|
|
import java.util.List; |
|
|
|
|
import javax.swing.*; |
|
|
|
|
import javax.swing.event.CaretEvent; |
|
|
|
|
import javax.swing.event.CaretListener; |
|
|
|
|
import javax.swing.event.DocumentEvent; |
|
|
|
|
import javax.swing.event.DocumentListener; |
|
|
|
|
import javax.swing.text.*; |
|
|
|
|
import java.awt.*; |
|
|
|
|
import java.awt.event.*; |
|
|
|
|
import java.beans.PropertyChangeEvent; |
|
|
|
|
import java.beans.PropertyChangeListener; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
import static com.fr.design.gui.syntax.ui.rtextarea.RTADefaultInputMap.DEFAULT_MODIFIER; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Adds auto-completion to a text component. Provides a popup window with a |
|
|
|
|
* list of auto-complete choices on a given keystroke, such as Crtrl+Space.<p> |
|
|
|
|
* |
|
|
|
|
* <p> |
|
|
|
|
* Depending on the {@link CompletionProvider} installed, the following |
|
|
|
|
* auto-completion features may be enabled: |
|
|
|
|
* |
|
|
|
|
* <p> |
|
|
|
|
* <ul> |
|
|
|
|
* <li>An auto-complete choices list made visible via e.g. Ctrl+Space</li> |
|
|
|
|
* <li>A "description" window displayed alongside the choices list that |
|
|
|
@ -212,7 +214,7 @@ public class AutoCompletion {
|
|
|
|
|
* Stores how to render auto-completion-specific highlights in text |
|
|
|
|
* components. |
|
|
|
|
*/ |
|
|
|
|
private static final AutoCompletionStyleContext styleContext = |
|
|
|
|
private static final AutoCompletionStyleContext STYLE_CONTEXT = |
|
|
|
|
new AutoCompletionStyleContext(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -275,7 +277,7 @@ public class AutoCompletion {
|
|
|
|
|
* @return Whether to auto-complete single choices. |
|
|
|
|
* @see #setAutoCompleteSingleChoices(boolean) |
|
|
|
|
*/ |
|
|
|
|
public boolean getAutoCompleteSingleChoices() { |
|
|
|
|
public boolean isAutoCompleteSingleChoices() { |
|
|
|
|
return autoCompleteSingleChoices; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -295,7 +297,7 @@ public class AutoCompletion {
|
|
|
|
|
* |
|
|
|
|
* @return Whether debug is enabled. |
|
|
|
|
*/ |
|
|
|
|
static boolean getDebug() { |
|
|
|
|
static boolean isDebug() { |
|
|
|
|
return DEBUG; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -308,8 +310,7 @@ public class AutoCompletion {
|
|
|
|
|
*/ |
|
|
|
|
public static KeyStroke getDefaultTriggerKey() { |
|
|
|
|
// Default to CTRL, even on Mac, since Ctrl+Space activates Spotlight
|
|
|
|
|
int mask = InputEvent.CTRL_MASK; |
|
|
|
|
return KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, mask); |
|
|
|
|
return KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, DEFAULT_MODIFIER); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -397,7 +398,7 @@ public class AutoCompletion {
|
|
|
|
|
* @return Whether the description window should be shown. |
|
|
|
|
* @see #setShowDescWindow(boolean) |
|
|
|
|
*/ |
|
|
|
|
public boolean getShowDescWindow() { |
|
|
|
|
public boolean isShowDescWindow() { |
|
|
|
|
return showDescWindow; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -409,7 +410,7 @@ public class AutoCompletion {
|
|
|
|
|
* @return The style context. |
|
|
|
|
*/ |
|
|
|
|
public static AutoCompletionStyleContext getStyleContext() { |
|
|
|
|
return styleContext; |
|
|
|
|
return STYLE_CONTEXT; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -680,60 +681,32 @@ public class AutoCompletion {
|
|
|
|
|
* @return The current line number of the caret. |
|
|
|
|
*/ |
|
|
|
|
protected int refreshPopupWindow() { |
|
|
|
|
|
|
|
|
|
// A return value of null => don't suggest completions
|
|
|
|
|
String text = provider.getAlreadyEnteredText(textComponent); |
|
|
|
|
if (text == null && !isPopupVisible()) { |
|
|
|
|
return getLineOfCaret(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// If the popup is currently visible, and they type a space (or any
|
|
|
|
|
// character that resets the completion list to "all completions"),
|
|
|
|
|
// the popup window should be hidden instead of being reset to show
|
|
|
|
|
// everything.
|
|
|
|
|
int textLen = text == null ? 0 : text.length(); |
|
|
|
|
if (textLen==0) { |
|
|
|
|
if (isPopupVisible()) { |
|
|
|
|
if (textLen == 0 && isPopupVisible()) { |
|
|
|
|
hidePopupWindow(); |
|
|
|
|
return getLineOfCaret(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
final List<Completion> completions = provider. |
|
|
|
|
getCompletions(textComponent); |
|
|
|
|
final List<Completion> completions = provider.getCompletions(textComponent); |
|
|
|
|
int count = completions.size(); |
|
|
|
|
|
|
|
|
|
if (count>1 || (count==1 && (isPopupVisible() || textLen==0)) || |
|
|
|
|
(count==1 && !getAutoCompleteSingleChoices())) { |
|
|
|
|
|
|
|
|
|
if (needSetPopupWindow(count, textLen)) { |
|
|
|
|
if (popupWindow == null) { |
|
|
|
|
popupWindow = new AutoCompletePopupWindow(parentWindow, this); |
|
|
|
|
// Completion is usually done for code, which is always done
|
|
|
|
|
// LTR, so make completion stuff RTL only if text component is
|
|
|
|
|
// also RTL.
|
|
|
|
|
popupWindow.applyComponentOrientation( |
|
|
|
|
getTextComponentOrientation()); |
|
|
|
|
if (renderer!=null) { |
|
|
|
|
popupWindow.setListCellRenderer(renderer); |
|
|
|
|
} |
|
|
|
|
if (preferredChoicesWindowSize!=null) { |
|
|
|
|
popupWindow.setSize(preferredChoicesWindowSize); |
|
|
|
|
} |
|
|
|
|
if (preferredDescWindowSize!=null) { |
|
|
|
|
popupWindow.setDescriptionWindowSize( |
|
|
|
|
preferredDescWindowSize); |
|
|
|
|
popupWindow = createAutoCompletePopupWindow(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
popupWindow.setCompletions(completions); |
|
|
|
|
|
|
|
|
|
if (!popupWindow.isVisible()) { |
|
|
|
|
Rectangle r = null; |
|
|
|
|
try { |
|
|
|
|
r = textComponent.modelToView(textComponent. |
|
|
|
|
getCaretPosition()); |
|
|
|
|
r = textComponent.modelToView(textComponent.getCaretPosition()); |
|
|
|
|
} catch (BadLocationException ble) { |
|
|
|
|
|
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
Point p = new Point(r.x, r.y); |
|
|
|
@ -743,25 +716,43 @@ public class AutoCompletion {
|
|
|
|
|
popupWindow.setLocationRelativeTo(r); |
|
|
|
|
popupWindow.setVisible(true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
else if (count==1) { // !isPopupVisible && autoCompleteSingleChoices
|
|
|
|
|
} else if (count == 1) { // !isPopupVisible && autoCompleteSingleChoices
|
|
|
|
|
SwingUtilities.invokeLater(new Runnable() { |
|
|
|
|
public void run() { |
|
|
|
|
insertCompletion(completions.get(0)); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
else { |
|
|
|
|
} else { |
|
|
|
|
hidePopupWindow(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return getLineOfCaret(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean needSetPopupWindow(int count, int textLen) { |
|
|
|
|
return (count == 1 && (isPopupVisible() || textLen == 0)) |
|
|
|
|
|| (count == 1 && !isAutoCompleteSingleChoices()) |
|
|
|
|
|| count > 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private AutoCompletePopupWindow createAutoCompletePopupWindow() { |
|
|
|
|
AutoCompletePopupWindow popupWindow = new AutoCompletePopupWindow(parentWindow, this); |
|
|
|
|
// Completion is usually done for code, which is always done
|
|
|
|
|
// LTR, so make completion stuff RTL only if text component is
|
|
|
|
|
// also RTL.
|
|
|
|
|
popupWindow.applyComponentOrientation( |
|
|
|
|
getTextComponentOrientation()); |
|
|
|
|
if (renderer != null) { |
|
|
|
|
popupWindow.setListCellRenderer(renderer); |
|
|
|
|
} |
|
|
|
|
if (preferredChoicesWindowSize != null) { |
|
|
|
|
popupWindow.setSize(preferredChoicesWindowSize); |
|
|
|
|
} |
|
|
|
|
if (preferredDescWindowSize != null) { |
|
|
|
|
popupWindow.setDescriptionWindowSize( |
|
|
|
|
preferredDescWindowSize); |
|
|
|
|
} |
|
|
|
|
return popupWindow; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the delay between when the user types a character and when the |
|
|
|
@ -791,8 +782,7 @@ public class AutoCompletion {
|
|
|
|
|
if (textComponent != null) { |
|
|
|
|
if (autoActivationEnabled) { |
|
|
|
|
autoActivationListener.addTo(textComponent); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
} else { |
|
|
|
|
autoActivationListener.removeFrom(textComponent); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -819,7 +809,7 @@ public class AutoCompletion {
|
|
|
|
|
* be automatically inserted, without displaying the popup menu. |
|
|
|
|
* |
|
|
|
|
* @param autoComplete Whether to auto-complete single choices. |
|
|
|
|
* @see #getAutoCompleteSingleChoices() |
|
|
|
|
* @see #isAutoCompleteSingleChoices() |
|
|
|
|
*/ |
|
|
|
|
public void setAutoCompleteSingleChoices(boolean autoComplete) { |
|
|
|
|
autoCompleteSingleChoices = autoComplete; |
|
|
|
@ -956,7 +946,7 @@ public class AutoCompletion {
|
|
|
|
|
* completion window. |
|
|
|
|
* |
|
|
|
|
* @param show Whether to show the description window. |
|
|
|
|
* @see #getShowDescWindow() |
|
|
|
|
* @see #isShowDescWindow() |
|
|
|
|
*/ |
|
|
|
|
public void setShowDescWindow(boolean show) { |
|
|
|
|
hidePopupWindow(); // Needed to force it to take effect
|
|
|
|
@ -1012,8 +1002,7 @@ public class AutoCompletion {
|
|
|
|
|
textComponent.replaceSelection(Character.toString(p.getParameterListStart())); |
|
|
|
|
TemplateCompletion tc = new TemplateCompletion(p, null, null, template); |
|
|
|
|
pc = tc; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
} else { |
|
|
|
|
text = p.getParameterListStart() + text; |
|
|
|
|
textComponent.replaceSelection(text); |
|
|
|
|
return; |
|
|
|
@ -1130,8 +1119,7 @@ public class AutoCompletion {
|
|
|
|
|
public void caretUpdate(CaretEvent e) { |
|
|
|
|
if (justInserted) { |
|
|
|
|
justInserted = false; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
} else { |
|
|
|
|
timer.stop(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -1153,12 +1141,10 @@ public class AutoCompletion {
|
|
|
|
|
if (provider.isAutoActivateOkay(textComponent)) { |
|
|
|
|
timer.restart(); |
|
|
|
|
justInserted = true; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
} else { |
|
|
|
|
timer.stop(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
} else { |
|
|
|
|
timer.stop(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -1187,8 +1173,7 @@ public class AutoCompletion {
|
|
|
|
|
public void actionPerformed(ActionEvent e) { |
|
|
|
|
if (isAutoCompleteEnabled()) { |
|
|
|
|
refreshPopupWindow(); |
|
|
|
|
} |
|
|
|
|
else if (oldTriggerAction!=null) { |
|
|
|
|
} else if (oldTriggerAction != null) { |
|
|
|
|
oldTriggerAction.actionPerformed(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|