Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/virtuosotechnologies/lib/basiccommand/builder/ComboBoxBuilderNode.java


1   /*
2   ================================================================================
3   
4     FILE:  ComboBoxBuilderNode.java
5     
6     PROJECT:
7     
8       Virtuoso Utilities
9     
10    CONTENTS:
11    
12      A builder that creates a JComboBox
13    
14    PROGRAMMERS:
15    
16      Daniel Azuma (DA)  <dazuma@kagi.com>
17    
18    COPYRIGHT:
19    
20      Copyright (C) 2003  Daniel Azuma  (dazuma@kagi.com)
21      
22      This program is free software; you can redistribute it and/or
23      modify it under the terms of the GNU General Public License as
24      published by the Free Software Foundation; either version 2
25      of the License, or (at your option) any later version.
26      
27      This program is distributed in the hope that it will be useful,
28      but WITHOUT ANY WARRANTY; without even the implied warranty of
29      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30      GNU General Public License for more details.
31      
32      You should have received a copy of the GNU General Public
33      License along with this program; if not, write to
34        Free Software Foundation, Inc.
35        59 Temple Place, Suite 330
36        Boston, MA 02111-1307 USA
37  
38  ================================================================================
39  */
40  
41  
42  package com.virtuosotechnologies.lib.basiccommand.builder;
43  
44  
45  import java.awt.Component;
46  import java.awt.event.ActionListener;
47  import java.awt.event.ActionEvent;
48  import javax.swing.JComboBox;
49  import javax.swing.JList;
50  import javax.swing.plaf.basic.BasicComboBoxRenderer;
51  
52  import com.virtuosotechnologies.lib.command.AbstractCommandNode;
53  import com.virtuosotechnologies.lib.command.CommandNode;
54  import com.virtuosotechnologies.lib.command.CommandNodeFlavor;
55  import com.virtuosotechnologies.lib.basiccommand.BasicCommandNode;
56  import com.virtuosotechnologies.lib.propertyset.PropertySetEvent;
57  
58  
59  /**
60   * A builder that creates a JComboBox
61   */
62  public class ComboBoxBuilderNode
63  extends AbstractContainerElementBuilderNode
64  {
65    /**
66     * Public constructor used to make standalone JComboBoxes
67     */
68    public ComboBoxBuilderNode(
69      CommandNode commandNode)
70    {
71      this(commandNode, null, END_POSITION, null);
72    }
73    
74    
75    /**
76     * Public constructor used to make standalone JComboBoxes
77     */
78    public ComboBoxBuilderNode(
79      CommandNode commandNode,
80      JComboBox comboBox)
81    {
82      this(commandNode, null, END_POSITION, comboBox);
83    }
84    
85    
86    /**
87     * Internal constructor used to make JComboBoxes
88     */
89    protected ComboBoxBuilderNode(
90      CommandNode commandNode,
91      AbstractBranchBuilderNode parent,
92      int index)
93    {
94      this(commandNode, parent, index, null);
95    }
96    
97    
98    /**
99     * Internal constructor used to make JComboBoxes. This is the master constructor.
100    */
101   protected ComboBoxBuilderNode(
102     final CommandNode commandNode,
103     AbstractBranchBuilderNode parent,
104     int index,
105     JComboBox comboBox)
106   {
107     super(commandNode, parent, index, comboBox);
108     
109     CommandNodeFlavor flavor = commandNode.getFlavor();
110     if (!flavor.equalsOrExtends(BasicCommandNode.RADIOMUTEX_FLAVOR))
111     {
112       throw new IllegalArgumentException("Not a RADIOMUTEX CommandNode: " +
113         commandNode.getClass().getName());
114     }
115     
116     // Children already built at this point. Set the initial combo box selection.
117     final JComboBox combo = getJComboBox();
118     CommandNode curItem = (CommandNode)commandNode.getValue(BasicCommandNode.SELECTED_CHILD_PROPERTY);
119     combo.setSelectedItem(curItem);
120     combo.addActionListener(
121       new ActionListener()
122       {
123         public void actionPerformed(
124           ActionEvent ev)
125         {
126           CommandNode newSelection = (CommandNode)combo.getSelectedItem();
127           commandNode.putValue(BasicCommandNode.SELECTED_CHILD_PROPERTY, newSelection);
128           if (newSelection != null)
129           {
130             AbstractCommandNode.invoke(newSelection);
131           }
132         }
133       });
134     combo.setRenderer(new ActionComboBoxRenderer());
135   }
136   
137   
138   /**
139    * Model changed
140    */
141   public void propertySetChanged(
142     PropertySetEvent ev)
143   {
144     if (ev.getKey().equals(BasicCommandNode.SELECTED_CHILD_PROPERTY))
145     {
146       getJComboBox().setSelectedItem(ev.getNewValue());
147     }
148     super.propertySetChanged(ev);
149   }
150   
151   
152   /**
153    * Override this method to create the initial element object.
154    */
155   protected Object createInitialElement()
156   {
157     JComboBox comboBox = new JComboBox();
158     comboBox.setEnabled(false);
159     return comboBox;
160   }
161   
162   
163   /**
164    * Create a child node
165    */
166   protected AbstractBuilderNode createChildNode(
167     CommandNode cn,
168     int index)
169   {
170     AbstractBuilderNode child = null;
171     CommandNodeFlavor flavor = cn.getFlavor();
172     if (flavor.equalsOrExtends(BasicCommandNode.TOGGLEITEM_FLAVOR))
173     {
174       child = new ComboBoxMemberBuilderNode(cn, this, index);
175     }
176     else if (flavor.equalsOrExtends(BasicCommandNode.GROUP_FLAVOR))
177     {
178       child = new ComboBoxMemberGroupBuilderNode(cn, this, index);
179     }
180     return child;
181   }
182   
183   
184   /**
185    * Add indexed element
186    */
187   protected void addElementAt(
188     int pos,
189     Object element)
190   {
191     JComboBox comboBox = (JComboBox)getElement();
192     comboBox.insertItemAt(element, pos);
193     comboBox.setEnabled(true);
194   }
195   
196   
197   /**
198    * Remove indexed element
199    */
200   protected void removeElementAt(
201     int pos)
202   {
203     JComboBox comboBox = (JComboBox)getElement();
204     comboBox.removeItemAt(pos);
205     if (comboBox.getItemCount() == 0)
206     {
207       comboBox.setEnabled(false);
208     }
209   }
210   
211   
212   /**
213    * Remove all elements
214    */
215   protected void removeAllElements()
216   {
217     JComboBox comboBox = (JComboBox)getElement();
218     comboBox.removeAllItems();
219     comboBox.setEnabled(false);
220   }
221   
222   
223   /**
224    * Get the built combo box
225    */
226   public JComboBox getJComboBox()
227   {
228     return (JComboBox)getElement();
229   }
230   
231   
232   /**
233    * A renderer for these types of combo boxes
234    */
235   public static class ActionComboBoxRenderer
236   extends BasicComboBoxRenderer
237   {
238     public Component getListCellRendererComponent(
239       JList list,
240       Object value,
241       int index,
242       boolean isSelected,
243       boolean cellHasFocus)
244     {
245       Component ret = super.getListCellRendererComponent(list, value,
246         index, isSelected, cellHasFocus);
247       if (value instanceof CommandNode)
248       {
249         CommandNode node = (CommandNode)value;
250         setText((String)node.getValue(BasicCommandNode.NAME_PROPERTY));
251       }
252       return ret;
253     }
254   }
255 }