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 http://www.apache.org/licenses/LICENSE-2.0 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) {
        this(cbfalse); 
     }
    
    
Creates a suggestion-popup for a combobox
 
     public SuggestionList(JComboBox cbboolean strict) {
         super(cb);
         
         this. = strict;
         .addMouseListener(new MouseHandler());
     }
    
    
'Filters' the list, leaving only matching items

Parameters:
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)) {
                 lm.addElement(model.getElementAt(i));
             }
         }
         
         .setModel(lm);
     }
    
    
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
 
    @Override
    protected int getPopupHeightForRowCount(int maxRowCount) {
        int h = super.getPopupHeightForRowCount(Math.min(maxRowCount.getModel().getSize()));
        return h;
    }

    

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

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

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

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

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