View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  package org.xmlhammer.gui.history;
22  
23  import javax.swing.InputVerifier;
24  import javax.swing.JComponent;
25  
26  public class HistoryInputVerifier extends InputVerifier {
27      private HistoryComboBox combo = null;
28  
29      public HistoryInputVerifier(HistoryComboBox combo) {
30          this.combo = combo;
31      }
32      
33      @Override
34      public boolean verify(JComponent component) {
35          setValue();
36          
37          return true;
38      }
39      
40      public HistoryComboBox getCombo() {
41          return combo;        
42      }
43      
44      protected void setValue() {
45  
46          if (!combo.isEmpty()) {
47              Object previous = combo.getSelectedItem();
48              if ( previous != null) {
49                  previous = previous.toString().trim();
50              }
51  
52              HistoryComboBoxModel model = (HistoryComboBoxModel)combo.getModel();
53              String value = combo.getEditor().getItem().toString().trim();
54      
55              if ( value != previous && !value.equals(previous)) {
56  //            if ( value != null && value.length() > 0) {
57                  ((HistoryComboBoxEditor)combo.getEditor()).setChangable(false);
58                  
59                  model.add(value);
60                  combo.setSelectedItem(value);
61                  
62                  ((HistoryComboBoxEditor)combo.getEditor()).setChangable(true);
63      
64  //                if ( value != previous && !value.equals(previous)) {
65                      combo.getEditor().setItem( value);
66  //                }
67              }
68          } else {
69              ((HistoryComboBoxEditor)combo.getEditor()).setChangable(false);
70              combo.setSelectedItem(null);
71              ((HistoryComboBoxEditor)combo.getEditor()).setChangable(true);
72          }
73      }
74  }