AbstractIntelliHints is an abstract implementation of {@link com.jidesoft.hints.IntelliHints}. It covers
* functions such as showing the hint popup at the correct position, delegating keystrokes, updating and selecting hint.
* The only thing that is left out to subclasses is the creation of the hint popup.
*
* @author Santhosh Kumar T
* @author JIDE Software, Inc.
*/
public abstract class AbstractIntelliHints implements IntelliHints {
private JidePopup _popup;
private JTextComponent _textComponent;
private JComponent _hintsComponent;
private boolean _followCaret = false;
// we use this flag to workaround the bug that setText() will trigger the hint popup.
private boolean _keyTyped = false;
// Specifies whether the hints popup should be displayed automatically.
// Default is true for backward compatibility.
private boolean _autoPopup = true;
private int _showHintsDelay = 200;
private ListJTextComponent supports single-line text or multi-line text.
*
* @return true if the component supports multiple text lines, false otherwise
*/
protected boolean isMultilineTextComponent() {
return getTextComponent() instanceof JTextArea ||
getTextComponent() instanceof JEditorPane;
}
/**
* This method will call {@link #showHints(boolean)} if and only if the text component is enabled and has focus.
*
* @param autoPopup
*/
protected void showHintsPopup(boolean autoPopup) {
if (!getTextComponent().isEnabled() || !getTextComponent().isEditable() || !getTextComponent().hasFocus()) {
return;
}
showHints(autoPopup);
}
@Override
public boolean updateHints(Object context) {
return false;
}
@Override
public boolean updateHints(Object context, boolean autoPopup) {
return updateHints(context);
}
/**
* Shows the hints popup which contains the hints. It will call {@link #updateHints(Object, boolean)}. Only if it returns
* true, the popup will be shown. You can call this method to fore the hints to be displayed.
*
* @param autoPopup
*/
public void showHints(boolean autoPopup) {
if (_popup == null) {
return;
}
if (_hintsComponent == null) {
_hintsComponent = createHintsComponent();
_popup.add(_hintsComponent);
getDelegateComponent().setRequestFocusEnabled(false);
getDelegateComponent().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
hideHintsPopup();
setHintsEnabled(false);
acceptHint(getSelectedHint());
setHintsEnabled(true);
}
});
}
if (updateHints(getContext(), autoPopup)) {
if (!isHintsPopupVisible()) {
DelegateAction.replaceAction(getTextComponent(), JComponent.WHEN_FOCUSED, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), hideAction);
DelegateAction.replaceAction(getTextComponent(), JComponent.WHEN_FOCUSED, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), acceptAction, true);
}
int x = 0;
int y = 0;
int height = 0;
try {
int pos = getCaretPositionForPopup();
Rectangle position = getCaretRectangleForPopup(pos);
y = position.y;
x = position.x;
height = position.height;
}
catch (BadLocationException e) {
// this should never happen!!!
}
_popup.setOwner(getTextComponent());
_popup.showPopup(new Insets(y, x, getTextComponent().getHeight() - height - y, 0));
}
else {
_popup.hidePopup();
}
}
/**
* Gets the caret rectangle where caret is displayed. The popup will be show around the area so that the returned
* rectangle area is always visible. This method will be called twice.
*
* @param caretPosition the caret position.
*
* @return the popup position relative to the text component. We provide a default context in * AbstractIntelliHints. If it's a JTextArea, the context will be the string at the caret line from line beginning * to the caret position. If it's a JTextField, the context will be whatever string in the text field. Subclass can * always override it to return the context that is appropriate. * * @return the context. */ protected Object getContext() { if (isMultilineTextComponent()) { int pos = getTextComponent().getCaretPosition(); if (pos == 0) { return ""; } else { String text = getTextComponent().getText(); int start = text.lastIndexOf('\n', pos - 1); return text.substring(start + 1, pos); } } else { return getTextComponent().getText(); } } /** * Hides the hints popup. */ protected void hideHintsPopup() { if (_popup != null) { _popup.hidePopup(); } setKeyTyped(false); } /** * Enables or disables the hints popup. * * @param enabled true to enable the hints popup. Otherwise false. */ public void setHintsEnabled(boolean enabled) { if (!enabled) { // disable show hint temporarily getTextComponent().getDocument().removeDocumentListener(documentListener); } else { // enable show hint again getTextComponent().getDocument().addDocumentListener(documentListener); } } /** * Checks if the hints popup is visible. * * @return true if it's visible. Otherwise, false. */ public boolean isHintsPopupVisible() { return _popup != null && _popup.isPopupVisible(); } /** * Should the hints popup follows the caret. * * @return true if the popup shows up right below the caret. False if the popup always shows at the bottom-left * corner (or top-left if there isn't enough on the bottom of the screen) of the JTextComponent. */ public boolean isFollowCaret() { return _followCaret; } /** * Sets the position of the hints popup. If followCaret is true, the popup shows up right below the caret. * Otherwise, it will stay at the bottom-left corner (or top-left if there isn't enough on the bottom of the screen) * of JTextComponent. * * @param followCaret true or false. */ public void setFollowCaret(boolean followCaret) { _followCaret = followCaret; } /** * Returns whether the hints popup is automatically displayed. Default is true * * @return true if the popup should be automatically displayed. False will never show it automatically and then need * the user to manually activate it via the getShowHintsKeyStroke() key binding. * * @see #setAutoPopup(boolean) */ public boolean isAutoPopup() { return _autoPopup; } /** * Sets whether the popup should be displayed automatically. If autoPopup is true then is the popup automatically * displayed whenever updateHints() return true. If autoPopup is false it's not automatically displayed and will * need the user to activate the key binding defined by getShowHintsKeyStroke(). * * @param autoPopup true or false */ public void setAutoPopup(boolean autoPopup) { this._autoPopup = autoPopup; } /** * Gets the delegate keystrokes. *
* When hint popup is visible, the keyboard focus never leaves the text component. However the hint popup usually * contains a component that user will try to use navigation key to select an item. For example, use UP and DOWN key * to navigate the list. Those keystrokes, if the popup is visible, will be delegated to the the component that * returns from {@link #getDelegateComponent()}. * * NOTE: Since this method would be invoked inside the constructor of AbstractIntelliHints, please do not try to * return a field because the field is not initiated yet at this time. * * @return an array of keystrokes that will be delegate to {@link #getDelegateComponent()} when hint popup is shown. */ abstract protected KeyStroke[] getDelegateKeyStrokes(); /** * Gets the delegate component in the hint popup. * * @return the component that will receive the keystrokes that are delegated to hint popup. */ abstract protected JComponent getDelegateComponent(); /** * Gets the keystroke that will trigger the hint popup. Usually the hints popup will be shown automatically when * user types. Only when the hint popup is hidden accidentally, this keystroke will show the popup again. * * By default, it's the DOWN key for JTextField and CTRL+SPACE for JTextArea. * * @return the keystroke that will trigger the hint popup. */ protected KeyStroke getShowHintsKeyStroke() { if (isMultilineTextComponent()) { return KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, KeyEvent.CTRL_MASK); } else { return KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0); } } private DelegateAction acceptAction = new DelegateAction() { private static final long serialVersionUID = -2516216121942080133L; @Override public boolean isDelegateEnabled() { return isHintsPopupVisible() && getSelectedHint() != null; } @Override public boolean delegateActionPerformed(ActionEvent e) { JComponent tf = (JComponent) e.getSource(); IntelliHints hints = getIntelliHints(tf); if (hints instanceof AbstractIntelliHints) { AbstractIntelliHints aih = (AbstractIntelliHints) hints; aih.hideHintsPopup(); if (aih.getSelectedHint() != null) { aih.setHintsEnabled(false); aih.acceptHint(hints.getSelectedHint()); aih.setHintsEnabled(true); return true; } } return false; } }; private DelegateAction hideAction = new DelegateAction() { private static final long serialVersionUID = 1921213578011852535L; @Override public boolean isDelegateEnabled() { return _textComponent.isEnabled() && isHintsPopupVisible(); } @Override public boolean delegateActionPerformed(ActionEvent e) { if (isEnabled()) { hideHintsPopup(); return true; } return false; } }; private DocumentListener documentListener = new DocumentListener() { private Timer timer = new Timer(getShowHintsDelay(), new ActionListener() { public void actionPerformed(ActionEvent e) { if (isKeyTyped()) { if (isHintsPopupVisible() || isAutoPopup()) { showHintsPopup(true); } setKeyTyped(false); } } }); public void insertUpdate(DocumentEvent e) { startTimer(); } public void removeUpdate(DocumentEvent e) { startTimer(); } public void changedUpdate(DocumentEvent e) { } void startTimer() { if (timer.isRunning()) { timer.setInitialDelay(getShowHintsDelay()); timer.setDelay(getShowHintsDelay()); timer.restart(); } else { timer.setRepeats(false); timer.setInitialDelay(getShowHintsDelay()); timer.setDelay(getShowHintsDelay()); timer.start(); } } }; private boolean isKeyTyped() { return _keyTyped; } private void setKeyTyped(boolean keyTyped) { _keyTyped = keyTyped; } /** * Gets the delay after the key is pressed to show hints. * * @return the delay time on milliseconds. * * @see #setShowHintsDelay(int) */ public int getShowHintsDelay() { return _showHintsDelay; } /** * Sets the delay after the key is pressed to show hints. * * By default, the delay time is 200ms. * * @param showHintsDelay the delay time */ public void setShowHintsDelay(int showHintsDelay) { _showHintsDelay = showHintsDelay; } /** * Adds a new key stroke to show hints popup. * * @param keyStroke the key stroke * * @see #removeShowHintsKeyStroke(javax.swing.KeyStroke) * @see #getAllShowHintsKeyStrokes() * @since 3.2.2 */ public void addShowHintsKeyStroke(KeyStroke keyStroke) { if (_showHintsKeyStrokes == null) { _showHintsKeyStrokes = new ArrayList