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;
 
 
AutoCompletion class handles user input and suggests matching variants (see CAY-911)
 
 public class AutoCompletion implements FocusListenerKeyListenerRunnable {
    
Property to mark combobox as 'auto-completing'
 
     public static final String AUTOCOMPLETION_PROPERTY = "JComboBox.autoCompletion";
    
    
A list with matching items
 
     private final SuggestionList suggestionList

    
Combo with auto-completion
 
     private final JComboBox comboBox;
     
     private final JTextComponent textEditor;
     
     private final boolean allowsUserValues;
     
     protected AutoCompletion(final JComboBox comboBoxboolean strictboolean allowsUserValues) {
         this. = comboBox;
          = ((JTextComponent)comboBox.getEditor().getEditorComponent());
         
         this. = allowsUserValues
         
          = new SuggestionList(comboBoxstrict);
        
        
Marking combobox as auto-completing
 
     }

    
Enables auto-completion for specified combobox

Parameters:
comboBox Combo to be featured
strict Whether strict matching (check 'startWith' or 'contains') should be used
allowsUserValues Whether non-present items are allowed
 
     public static void enable(JComboBox comboBoxboolean strictboolean allowsUserValues) {
         comboBox.setEditable(true);
         
         comboBox.setEditor(new CustomTypeComboBoxEditor(comboBoxallowsUserValues));
         
         AutoCompletion ac = new AutoCompletion(comboBoxstrictallowsUserValues);
         comboBox.addFocusListener(ac);
         ac.textEditor.addKeyListener(ac);
         
         //original keys would not work properly
         SwingUtilities.replaceUIActionMap(comboBoxnull);
     }
    
    
Enables auto-completion for specified combobox
 
     public static void enable(JComboBox comboBox) {
         enable(comboBoxtruefalse);
     }
 
     public void focusGained(FocusEvent e) {}
 
     public void focusLost(FocusEvent e) {
         .hide();
    }
    public void keyPressed(KeyEvent e) {
        handleKeyPressed(e);
    }
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}
    
    public void run() {
        String text = .getText();
        
        //need to hide first because Swing incorrectly updates popups (getSize() returns
        //dimension not the same as seen on the screen)
        .hide();
        
        if (.isShowing()) {
            .filter(text);
            
            if (.getItemCount() > 0) {
                .show();
            }
        }
    }
    
    
Calculates next selection row, according to a pressed key and selects it. This might affect either suggestion list or original popup
    private void handleKeyPressed(JComboBox comboBoxKeyEvent e) {
        boolean suggest = .isVisible();
        
        int selnextmax;
        
        if (suggest) {
            sel = .getSelectedIndex();
            max = .getItemCount() - 1;
        }
        else {
            sel = comboBox.getSelectedIndex();
            max = comboBox.getItemCount() - 1;
        }
        
        switch (e.getKeyCode()) {
            case .:
            case .:
                next = sel - 1; 
                break;
            case .:
            case .:
                next = sel + 1;
                break;
            case .:
                next = sel - comboBox.getMaximumRowCount();
                break;
            case .:
                next = sel + comboBox.getMaximumRowCount();
                break;
            case .:
                next = 0;
                break;
            case .:
                next = max;
                break;
            case .:
                if (suggest) {
                    Object value = .getSelectedValue();
                    if (! && value == null && .getItemCount() > 0) {
                        value = .getItemAt(0);
                    }
                    
                    //reset the item (value == null) only if user values are not supported
                    if (value != null || !) {
                        comboBox.setSelectedItem(value);
                    }
                    .hide();
                }
                return;
                
            case .:
                if (suggest) {
                    .hide();
                }
                return;
                
            case .:
            case .:
            case .:
                return;
                
            default:
                //invoke in end of AWT thread so that information in textEditor would update
                SwingUtilities.invokeLater(this);
                return;
        }
        
        
Handle navigation keys
        e.consume();
        if (!suggest && !comboBox.isPopupVisible()) {
            comboBox.setPopupVisible(true);
            return;
        }
        if (comboBox.getItemCount() > 0) {
            if (next < 0) {
                next = 0;
            }
        
            if (next > max) {
                next = max;
            }
            
            if (next != sel) {
                if (suggest) {
                    .setSelectedIndex(next);
                }
                else {
                   comboBox.setPopupVisible(true);
                   comboBox.setSelectedIndex(next);
                }
            }
            
            .requestFocus();
        }
    }
New to GrepCode? Check out our FAQ X