/** * Sets the combo box which this object is auto completing and updates the * drop down list with the auto completion for the currently selected item. * * @param comboBox The combo box to enable browsing for auto completions. * May be {@code null}. */ public void setComboBox(final @CheckForNull JComboBox<E> comboBox) { changeComboBox(getComboBox(), comboBox, true); }
private void documentUpdated() { if (lock()) return; try { final JComboBox<E> cb = getComboBox(); final ComboBoxEditor cbe = cb.getEditor(); final JTextComponent tc = (JTextComponent) cbe.getEditorComponent(); assert cb.isShowing() || !tc.isFocusOwner(); if (!tc.isFocusOwner() /* || !cb.isShowing() */) return; //cb.setPopupVisible(update(tc.getText())); // doesn't work: adjusts popup size! cb.setPopupVisible(false); if (update(tc.getText())) cb.setPopupVisible(true); } finally { unlock(); } }
private void updateEditor(final ComboBoxEditor cbe, final @CheckForNull Object item) { if (lock()) return; try { cbe.setItem(item); if (!(item instanceof String)) return; final JComboBox<E> cb = getComboBox(); final JTextComponent tc = (JTextComponent) cbe.getEditorComponent(); assert cb.isShowing() || !tc.isFocusOwner(); if (!tc.isFocusOwner() /* || !cb.isShowing() */) return; // Compensate for an issue with some look and feels // which select the entire tc if an item is changed. // This is inconvenient for auto completion because the // next typed character would replace the entire tc... final Caret caret = tc.getCaret(); caret.setDot(((String) item).length()); } finally { unlock(); } }
/** * Creates a new combo box auto completion browser. Note that this * constructor does <em>not</em> call {@link #update} and hence the drop * down list of the combo box is left unchanged. * * @param comboBox The combo box to enable browsing for auto completions. * May be {@code null}. */ protected AbstractComboBoxBrowser(final @CheckForNull JComboBox<E> comboBox) { changeComboBox(null, comboBox, false); }
private void changeDocument( final @CheckForNull Document oldDoc, final @CheckForNull Document newDoc, final boolean update) { if (newDoc == oldDoc) return; if (null != oldDoc) oldDoc.removeDocumentListener(listener); if (null != newDoc) { if (update) { String txt; try { txt = newDoc.getText(0, newDoc.getLength()); } catch (BadLocationException e) { txt = null; } update(txt); } newDoc.addDocumentListener(listener); } }
private void changeText( final @CheckForNull JTextComponent oldTC, final @CheckForNull JTextComponent newTC, final boolean update) { if (newTC == oldTC) return; Document oldDocument = null; if (null != oldTC) { oldTC.removePropertyChangeListener("document", listener); oldDocument = oldTC.getDocument(); } Document newDocument = null; if (null != newTC) { newDocument = newTC.getDocument(); newTC.addPropertyChangeListener("document", listener); } changeDocument(oldDocument, newDocument, update); }
private void changeEditor( final @CheckForNull ComboBoxEditor oldCBE, final @CheckForNull ComboBoxEditor newCBE, final boolean update) { if (newCBE == oldCBE) return; JTextComponent oldText = null; if (null != oldCBE) { final Component component = oldCBE.getEditorComponent(); if (component instanceof JTextComponent) oldText = (JTextComponent) component; } JTextComponent newText = null; if (null != newCBE) { final Component component = newCBE.getEditorComponent(); if (component instanceof JTextComponent) newText = (JTextComponent) component; } changeText(oldText, newText, update); }
private void changeComboBox( final @CheckForNull JComboBox<E> oldCB, final @CheckForNull JComboBox<E> newCB, final boolean update) { if (newCB == oldCB) return; ComboBoxEditor oldCBE = null; if (null != oldCB) { oldCB.removePropertyChangeListener("editor", listener); oldCBE = oldCB.getEditor(); oldCB.setEditor(((AbstractComboBoxBrowser<?>.DecoratingComboBoxEditor) oldCBE).getEditor()); } this.comboBox = newCB; ComboBoxEditor newCBE = null; if (null != newCB) { newCB.updateUI(); // ensure comboBoxEditor is initialized newCBE = new DecoratingComboBoxEditor(newCB.getEditor()); newCB.setEditor(newCBE); newCB.addPropertyChangeListener("editor", listener); } changeEditor(oldCBE, newCBE, update); }