001    /*
002     * This file is part of muCommander, http://www.mucommander.com
003     * Copyright (C) 2002-2008 Maxence Bernard
004     *
005     * muCommander is free software; you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation; either version 3 of the License, or
008     * (at your option) any later version.
009     *
010     * muCommander is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    
019    package com.mucommander.ui.combobox;
020    
021    import com.mucommander.runtime.JavaVersions;
022    import com.mucommander.ui.autocomplete.EditableComboboxCompletion;
023    import com.mucommander.ui.autocomplete.TypicalAutocompleterEditableCombobox;
024    import com.mucommander.ui.autocomplete.completers.Completer;
025    
026    import javax.swing.*;
027    import java.awt.event.KeyEvent;
028    import java.util.Vector;
029    
030    /**
031     * <code>AutocompletedEditableCombobox</code> is an editable combo-box that provides
032     * auto-completion capabilities based on the given <code>Completer</code>.
033     * 
034     * @author Arik Hadas
035     */
036    
037    public class AutocompletedEditableCombobox extends EditableComboBox {
038    
039            /**
040         * Creates a new editable combo box and a JTextField to be used as the editor.
041         * Has the same effect as calling {@link #EditableComboBox(javax.swing.JTextField)} with a null value.
042         */
043        public AutocompletedEditableCombobox(Completer completer) {
044            super();
045            enableAutoCompletion(completer);
046        }
047    
048        /**
049         * Creates a new editable combo box using the given text field as the editor.
050         *
051         * @param textField the text field to be used as the combo box's editor. If null, a new JTextField instance
052         * will be created and used.
053         */
054        public AutocompletedEditableCombobox(JTextField textField, Completer completer) {
055            super(textField);
056            enableAutoCompletion(completer);
057        }
058    
059        /**
060         * Creates a new editable combo box using the given text field as the editor and ComboBoxModel.
061         *
062         * @param textField the text field to be used as the combo box's editor. If null, a new JTextField instance
063         * will be created and used.
064         * @param comboBoxModel the ComboBoxModel to use for this combo box
065         */
066        public AutocompletedEditableCombobox(JTextField textField, ComboBoxModel comboBoxModel, Completer completer) {
067            super(textField, comboBoxModel);
068            enableAutoCompletion(completer);
069        }
070    
071        /**
072         * Creates a new editable combo box using the given text field as the editor and items to populate the initial items list.
073         *
074         * @param textField the text field to be used as the combo box's editor. If null, a new JTextField instance
075         * will be created and used.
076         * @param items items used to populate the initial items list.
077         */
078        public AutocompletedEditableCombobox(JTextField textField, Object[] items, Completer completer) {
079            super(textField, items);
080            enableAutoCompletion(completer);
081        }
082    
083        /**
084         * Creates a new editable combo box using the given text field as the editor and items to populate the initial items list.
085         *
086         * @param textField the text field to be used as the combo box's editor. If null, a new JTextField instance
087         * will be created and used.
088         * @param items items used to populate the initial items list.
089         */
090        public AutocompletedEditableCombobox(JTextField textField, Vector items, Completer completer) {
091            super(textField, items);
092            enableAutoCompletion(completer);
093        }
094        
095        private void enableAutoCompletion(Completer completer) {
096            new EditableComboboxCompletion(new TypicalAutocompleterEditableCombobox(this), completer);
097        }
098            
099        /**
100         * The desired behavior of this editable combo-box when enter key is pressed.
101         * 
102         * @param keyEvent - the KeyEvent that occurred. 
103         */
104            public void respondToEnterKeyPressing(KeyEvent keyEvent) {
105                    // Combo popup menu is visible
106                    if(isPopupVisible()) {
107                            // Under Java 1.5 or under, we need to explicitely hide the popup.
108                            if(JavaVersions.JAVA_1_5.isCurrentOrLower())
109                                    hidePopup();
110                            
111                            // Note that since the event is not consumed, JComboBox will catch it and fire
112                    }
113                    // Combo popup menu is not visible, these events really belong to the text field
114                    else {
115                            // Notify listeners that the text field has been validated
116                            fireComboFieldValidated();
117                
118                // /!\ Consume the event so to prevent JComboBox from firing an ActionEvent (default JComboBox behavior)
119                keyEvent.consume();
120                    }               
121            }
122    
123            /**
124         * The desired behavior of this editable combo-box when escape key is pressed.
125         * 
126         * @param keyEvent - the KeyEvent that occurred. 
127         */
128            public void respondToEscapeKeyPressing(KeyEvent keyEvent) {
129                    // Combo popup menu is visible
130                    if(isPopupVisible()) {
131                             // Explicitely hide popup menu, JComboBox does not seem do it automatically (at least under Mac OS X + Java 1.5 and Java 1.4)
132                hidePopup();
133                // Consume the event so that it is not propagated, since dialogs catch this event to close the window
134                keyEvent.consume();
135                    }
136                    // Combo popup menu is not visible, these events really belong to the text field
137            else {
138                    // Notify listeners that the text field has been cancelled
139                    fireComboFieldCancelled();
140            }
141            }
142    }