Can Combo Dropdown do Mode.CONTAINS results?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Combo Dropdown do Mode.CONTAINS results?

Jed Reynolds
I have been able to build a dropdown that shows me Mode.STARTSWITH
results.  When the user hits enter in the combo, I get the Mode.CONTAINS
results. I would like to see the dropdown show Mode.CONTAINS. Is there
something else I need to do to?

TIA

Jed

---8<---
final JComboBox         search_combo   = new JComboBox();
final StrList           item_names     = new StrList();
final EventList<String> event_list     = new BasicEventList<String>();
final TextFilterator    filterator     = new TextFilterator() {
    @Override
    public void getFilterStrings(List baseList, Object element) {
       if (search_combo.getSelectedItem() == null)
          return;
       String search_term =
lotrim(search_combo.getSelectedItem().toString());
       if(element instanceof StrList){
          StrList list = (StrList)element;
          for(String i : list.toArray()) {
             if (haslo(i, search_term)) {
                baseList.add(i);
             }
          }
       }
       else {
          System.out.println("element is not StrList, but:
"+element.getClass().getName());
       }
    } // ~getFilterStrings()
}; // ~class TextFilterator
final BasicComboBoxEditor  bcbe        = new BasicComboBoxEditor();
search_combo.setEditor(bcbe);
final TextComponentMatcherEditor tcme        =
    new
TextComponentMatcherEditor((JTextComponent)bcbe.getEditorComponent(),
filterator);
tcme.setMode(TextMatcherEditor.CONTAINS);
final FilterList        filter_list    = new FilterList(event_list, tcme);


search_combo.setPreferredSize(new Dimension(100,28));
search_combo.setEnabled(true);
search_combo.setEditable(true);

search_combo.getEditor().getEditorComponent().addFocusListener(new
FocusAdapter() {
    @Override
    public void focusGained(FocusEvent e) {
       super.focusGained(e);
       if (item_names.size() < 1) {
          fillLabelDigest(item_names);
          DefaultEventComboBoxModel<String> model = new
DefaultEventComboBoxModel<String>(filter_list);
          search_combo.setModel(model);
          System.out.println("Delayed init: " + Stdlib.join(", ",
item_names.toArray()));
          AutoCompleteSupport.install(search_combo,
             GlazedLists.eventListOf(item_names.toArray())
          );
       }
    }
});
search_combo.addActionListener(new ItemHighlightListener( filter_list,
all_fields[0]));

class FieldLabelMatcher implements ca.odell.glazedlists.matchers.Matcher {
    final JComboBox _search_box;
    public FieldLabelMatcher(final JComboBox search_box) {
       _search_box = search_box;
    }

    @Override
    public boolean matches(Object o) {
       String equiv = o.toString().trim().toLowerCase();
       String search_term =
_search_box.getSelectedItem().toString().trim().toLowerCase();
       return equiv.contains(search_term);
    }
} // ~FieldLableMatcher

class ItemHighlightListener implements ActionListener {
    private final FieldPartsList  _list;
    private final FilterList      _filter_list;
    private Color                 _background     = null;
    private Color                 _highlight      = Color.decode("#FFCC44");

    ItemHighlightListener(final FilterList filter_list, final
FieldPartsList field_parts_list) {
       _list          = field_parts_list;
       _filter_list   = filter_list;
       for (FieldParts j : _list) {
          _background = j.getLabel().getBackground();
          break;
       }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
       JComboBox source_combo = (JComboBox)e.getSource();
       System.out.println("Item Highlight Listener: "+e.paramString());

       if(source_combo.isPopupVisible())
          return;

       for (FieldParts j : _list) {
          j.getLabel().setBackground(_background);
          j.getLabel().setOpaque(false);
       }
       if (  source_combo.getSelectedItem() == null ) {
          //System.out.println("no selected item");
          return;
       }

       String search_txt =
lotrim(source_combo.getSelectedItem().toString());
       System.out.println("source_combo_action: " + search_txt);
       // highlight the labels

       if (_background == null) {
          FieldParts j = _list.get(0);
          _background = j.getLabel().getBackground();
       }
       int highest_prio = 0;
       for (FieldParts j : _list) {
          if (  j.getLabel().isEnabled() &&
haslo(j.getLabel().getText(), search_txt)) {
             System.out.println("HI "+j.getLabel().getText());
             j.getLabel().setBackground(_highlight);
             j.getLabel().setOpaque(true);
             for(PortItem p:PortItem.values()) {
                if(p.getLabelText().equals(j.getLabel().getText())) {
                   if(p.getPriority() > highest_prio) {
                      highest_prio = p.getPriority();
                   }
                }
             }
          }
       } // for each label
       if(highest_prio > 0)
          all_panels.getThumbwheel().focusTo(highest_prio);
       source_combo.requestFocus();
    } //actionPerformed
} // ItemHighlightListener

---8<---
Loading...