Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 package org.apache.cayenne.modeler.util.combo;
SuggestionList is a combo-popup displaying all items matching for autocompletion.
 public class SuggestionList extends BasicComboPopup {
'Strict' matching, i.e. whether 'startWith' or 'contains' function should be used for checking match
     protected boolean strict;
Creates a strict suggestion-popup for a combobox
     public SuggestionList(JComboBox cb) {
Creates a suggestion-popup for a combobox
     public SuggestionList(JComboBox cbboolean strict) {
         this. = strict;
         .addMouseListener(new MouseHandler());
'Filters' the list, leaving only matching items

prefix user-typed string, used to filter
     public void filter(String prefix) {
         ComboBoxModel model = .getModel();
         DefaultListModel lm = new DefaultListModel();
         for (int i = 0; i < model.getSize(); i++) {
             String item = CellRenderers.asString(model.getElementAt(i));
             if (matches(itemprefix)) {
Checks if an item matches input pattern
     protected boolean matches(String itemString pattern) {
         if () {
             return item.toLowerCase().startsWith(pattern.toLowerCase());
         else {
             return item.toLowerCase().contains(pattern.toLowerCase());
Retrieves the height of the popup based on the current ListCellRenderer and the maximum row count. Overrriden to count for local list size
    protected int getPopupHeightForRowCount(int maxRowCount) {
        int h = super.getPopupHeightForRowCount(Math.min(maxRowCount.getModel().getSize()));
        return h;


selected index in popup
    public int getSelectedIndex() {
        return .getSelectedIndex();

selected item in popup
    public Object getSelectedValue() {
        return .getSelectedValue();
Selects an item in list
    public void setSelectedIndex(int i) {

current suggestions count
    public int getItemCount() {
        return .getModel().getSize();

an item from the list
    public Object getItemAt(int i) {
        return .getModel().getElementAt(i);
        return new MouseHandler();
We don't want items in the list be automatically selected at all
    protected ItemListener createItemListener() {
          new ItemListener() {
            public void itemStateChanged(ItemEvent e) {}

Whether match-check is 'strict'
    public boolean isStrict() {
        return ;
    protected class MouseHandler extends MouseInputAdapter {
        public void mouseReleased(MouseEvent e) {
            // Workaround for cancelling an edited item (JVM bug 4530953).
            if (.isEditable() && .getEditor() != null) {
                .configureEditor(.getEditor(), .getSelectedItem());
New to GrepCode? Check out our FAQ X