View Javadoc

1   /*
2    * $Id: ComboBoxNode.java,v 1.7 2006/09/07 14:03:04 edankert Exp $
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  
22  
23  package org.xmlhammer.gui.overview;
24  
25  import java.awt.event.ItemEvent;
26  import java.awt.event.ItemListener;
27  import java.util.ArrayList;
28  
29  import javax.swing.Icon;
30  import javax.swing.ImageIcon;
31  import javax.swing.JComboBox;
32  import javax.swing.JTextField;
33  import javax.swing.event.DocumentEvent;
34  import javax.swing.event.DocumentListener;
35  
36  import org.bounce.image.ImageLoader;
37  
38  
39  public class ComboBoxNode implements OverviewNode, DocumentListener, ItemListener {
40      private static final ImageIcon TEXT_FIELD_ICON = ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/elcl16/textfield_obj.gif");
41      private static final ImageIcon COMBO_ICON = ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/elcl16/view_menu.gif");
42  
43  	private ArrayList<OverviewNode> empty = new ArrayList<OverviewNode>();
44  	private JComboBox combo = null;
45  	private OverviewNode parent = null;
46      private String label = null;
47      private String error = null;
48  
49      public ComboBoxNode( OverviewNode parent, JComboBox combo, String label) {
50          this.parent = parent;
51          this.combo = combo;
52          this.label = label;
53          
54          if ( combo.isEditable()) {
55              ((JTextField)combo.getEditor().getEditorComponent()).getDocument().addDocumentListener( this);
56          } else {
57              combo.addItemListener( this);
58          }
59      }
60  
61      public String getNodeName() {
62          if ( combo.isEditable()) {
63              return (String)combo.getEditor().getItem()+ " ("+label+")";
64          }
65          
66          return (String)combo.getSelectedItem()+ " ("+label+")";
67      }
68  
69      public ArrayList<OverviewNode> getChildNodes() {
70          return empty;
71      }
72  
73      public OverviewNode getParentNode() {
74          return parent;
75      }
76  
77      public Icon getNodeIcon() {
78          if ( combo.isEditable()) {
79              return TEXT_FIELD_ICON;
80          }
81          
82          return COMBO_ICON;
83      }
84      
85      private void nodeChanged() {
86          OverviewNode node = parent;
87          
88          while ( node != null && !(node.getParentNode() instanceof OverviewTreeModel)) {
89              node = node.getParentNode();
90          }
91          
92          if ( node != null) {
93              ((OverviewTreeModel)node.getParentNode()).nodeChanged( this);
94          }
95      }
96  
97      public void itemStateChanged(ItemEvent arg0) {
98          nodeChanged();
99      }
100     
101     public void setError(String error) {
102         this.error = error;
103     }
104     
105     public String getError() {
106         return error;
107     }
108 
109     public void insertUpdate(DocumentEvent arg0) {
110         nodeChanged();
111     }
112 
113     public void removeUpdate(DocumentEvent arg0) {
114         nodeChanged();
115     }
116 
117     public void changedUpdate(DocumentEvent arg0) {}
118 }