public UrlComboBox() { setToolTipText("URL"); setEditable(true); final JTextField editorComponent = (JTextField) getEditor().getEditorComponent(); editorComponent.addFocusListener(new FocusAdapter() { @Override public void focusGained(FocusEvent e) { editorComponent.selectAll(); } }); AutoCompletion ac = new AutoCompletion(this); ac.setStrict(false); ac.setStrictCompletion(false); }
public static AutoCompletion installSearchable(final JComboBox combo) { boolean wasEditable = combo.isEditable(); combo.setEditable(false); ComboBoxSearchable searchable = SearchableUtils.installSearchable(combo); searchable.setCaseSensitive(false); searchable.setWildcardEnabled(true); searchable.setCountMatch(true); searchable.setShowPopupDuringSearching(true); searchable.setRefreshPopupDuringSearching(true); AutoCompletion autoCompletion = new AutoCompletion(combo, searchable) { @Override protected AutoCompletionDocument createDocument() { return super.createDocument(); } }; autoCompletion.setStrict(false); autoCompletion.setStrictCompletion(false); autoCompletion.installListeners(); combo.setEditable(wasEditable); return autoCompletion; }
public AutoCompletion(final JTextComponent textComponent, final Searchable searchable) { _searchable = searchable; _searchable.setWildcardEnabled(false); _textComponent = textComponent; registerSelectionListener(getSearchable()); installListeners(); }
/** * Creates the <code>AutoCompletion</code>. * * @return the <code>AutoCompletion</code>. */ protected AutoCompletion createAutoCompletion() { return new AutoCompletion(this, new ComboBoxSearchable(this) { @Override protected void setSelectedIndex(int index, boolean incremental) { Object property = AutoCompletionComboBox.this.getClientProperty("JComboBox.isTableCellEditor"); if (property instanceof Boolean && (Boolean) property) { _preventActionEvent = true; } try { super.setSelectedIndex(index, incremental); } finally { _preventActionEvent = false; } } }); }
if (_deletePressed) { _deletePressed = false; String text = getTextComponent().getText(); int index = getSearchable().findFirst(text); if (index != -1) { if (text.length() == 0) { Object item = getSearchable().getElementAt(index); setSelectedItem(item); getTextComponent().setText(getSearchable().convertElementToString(item)); // this is what auto complete is if (isStrict()) { getTextComponent().setText(_saveText); e.consume(); PortingUtils.notifyUser(_textComponent);
_document = createDocument(); configureEditor(getTextComponent()); getTextComponent().putClientProperty(CLIENT_PROPERTY_AUTO_COMPLETION, this);
public void valueChanged(ListSelectionEvent e) { int index = list.getSelectedIndex(); if (index != -1) { getTextComponent().setText(getSearchable().convertElementToString(list.getModel().getElementAt(index))); highlightCompletedText(0); } } });
/** * Sets the strict property. If true, it will not allow user to type in anything that is not in the known item list. * If false, user can type in whatever he/she wants. If the text can match with a item in the known item list, it * will still auto-complete. * * @param strict true or false. */ public void setStrict(boolean strict) { getAutoCompletion().setStrict(strict); }
/** * Sets the strict completion property. If true, in case insensitive searching, it will always use the exact item in * the Searchable to replace whatever user types. For example, when Searchable has an item "Arial" and user types in * "AR", if this flag is true, it will auto-completed as "Arial". If false, it will be auto-completed as "ARial". Of * course, this flag will only make a difference if Searchable is case insensitive. * * @param strictCompletion */ public void setStrictCompletion(boolean strictCompletion) { getAutoCompletion().setStrictCompletion(strictCompletion); }
private void setInitValue() { int index = getSearchable().getSelectedIndex(); if (index != -1) { Object selected = getSearchable().getElementAt(index); if (selected != null) _document.setText(getSearchable().convertElementToString(selected)); highlightCompletedText(0); } else { _document.setText(""); } }
if (isKeyTyped() || isStrict()) { int exactIndex = getSearchable().findFirstExactly(text); if (exactIndex != -1) { return; int index = getSearchable().findFromCursor(text); Object item; if (index != -1) { item = getSearchable().getElementAt(index); setSelectedItem(item); setText(getSearchable().convertElementToString(item)); // this is what auto complete is if (isStrict()) { if (offs == 0 && text.equals(str)) { getSearchable().textChanged(""); index = getSearchable().findFromCursor(text); if (index != -1) { item = getSearchable().getElementAt(index); setSelectedItem(item); setText(getSearchable().convertElementToString(item)); // this is what auto complete is if (isStrict()) { index = getSearchable().getSelectedIndex(); if (index == -1) { if (getSearchable().getElementCount() > 0) { index = 0; getSearchable().setSelectedIndex(0, false);
@Override public void keyPressed(KeyEvent e) { _hitBackspace = false; if (KeyEvent.VK_ESCAPE != e.getKeyCode()) { setKeyTyped(true); } switch (e.getKeyCode()) { // determine if the pressed key is backspace (needed by the remove method) case KeyEvent.VK_BACK_SPACE: if (isStrict()) { _hitBackspace = true; _hitBackspaceOnSelection = getTextComponent().getSelectionStart() != getTextComponent().getSelectionEnd(); } break; // ignore delete key case KeyEvent.VK_DELETE: if (isStrict()) { _deletePressed = true; _saveText = getTextComponent().getText(); } break; } }
private void highlightCompletedText(int start) { int length = getTextComponent().getDocument().getLength(); getTextComponent().setCaretPosition(length); if (start < 0) { start = 0; } if (start > length) { start = length; } getTextComponent().moveCaretPosition(start); }
private void installSearchable() { if (autoCompletion == null) { this.autocomplete = SearchableDemo.installSearchable(classField); this.searchable = (ComboBoxSearchable) this.autocomplete.getSearchable(); } }
@Override protected AutoCompletionDocument createDocument() { return super.createDocument(); } };
public AutoCompletion(final JComboBox comboBox, Searchable searchable) { _searchable = searchable; _propertyChangeListener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { if ("editor".equals(e.getPropertyName())) { if (e.getNewValue() != null) { _textComponent = (JTextComponent) ((ComboBoxEditor) e.getNewValue()).getEditorComponent(); configureEditor(getTextComponent()); } } } }; _comboBox = comboBox; _searchable.setWildcardEnabled(false); if (_searchable instanceof ComboBoxSearchable) { ((ComboBoxSearchable) _searchable).setShowPopupDuringSearching(false); } _textComponent = (JTextComponent) comboBox.getEditor().getEditorComponent(); installListeners(); }
final TreeSearchable searchable = new TreeSearchable(tree); searchable.setRecursive(true); new AutoCompletion(treeTextField, searchable); panel.add(new JLabel("AutoCompletion JTextField with JTree")); panel.add(Box.createVerticalStrut(3), JideBoxLayout.FIX);
private void registerSelectionListener(Searchable searchable) { if (searchable.getComponent() instanceof JList) { final JList list = (JList) getSearchable().getComponent(); list.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { DelegateAction.replaceAction(getTextComponent(), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, list, JComponent.WHEN_FOCUSED, KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)); DelegateAction.replaceAction(getTextComponent(), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, list, JComponent.WHEN_FOCUSED, KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)); DelegateAction.replaceAction(getTextComponent(), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, list, JComponent.WHEN_FOCUSED, KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0)); DelegateAction.replaceAction(getTextComponent(), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, list, JComponent.WHEN_FOCUSED, KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0)); final JTree tree = (JTree) getSearchable().getComponent(); tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { DelegateAction.replaceAction(getTextComponent(), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, tree, JComponent.WHEN_FOCUSED, KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)); DelegateAction.replaceAction(getTextComponent(), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, tree, JComponent.WHEN_FOCUSED, KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)); DelegateAction.replaceAction(getTextComponent(), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, tree, JComponent.WHEN_FOCUSED, KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0)); DelegateAction.replaceAction(getTextComponent(), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, tree, JComponent.WHEN_FOCUSED, KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0));
public void valueChanged(TreeSelectionEvent e) { TreePath treePath = tree.getSelectionPath(); if (treePath != null) { getTextComponent().setText("" + treePath.getLastPathComponent()); highlightCompletedText(0); } else { getTextComponent().setText(""); } } });
private void setSelectedItem(Object item) { _selecting = true; for (int i = 0, n = getSearchable().getElementCount(); i < n; i++) { Object currentItem = getSearchable().getElementAt(i); // current item starts with the pattern? if (JideSwingUtilities.equals(item, currentItem)) { getSearchable().setSelectedIndex(i, false); } } _selecting = false; }