KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > components > DjComboBox


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.specifier.components;
31
32 import java.awt.event.ActionEvent JavaDoc;
33 import java.awt.event.ActionListener JavaDoc;
34 import java.awt.event.FocusEvent JavaDoc;
35 import java.awt.event.FocusListener JavaDoc;
36
37 import javax.swing.DefaultComboBoxModel JavaDoc;
38 import javax.swing.JComboBox JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40
41 import com.genimen.djeneric.repository.DjDomainValue;
42 import com.genimen.djeneric.repository.exceptions.DjenericException;
43 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer;
44 import com.genimen.djeneric.ui.DjFocusMeLater;
45 import com.genimen.djeneric.util.DjLogger;
46
47 public class DjComboBox extends JComboBox JavaDoc implements DjBindable, FocusListener JavaDoc, ActionListener JavaDoc
48 {
49
50   private static final long serialVersionUID = 1L;
51   private BindingMediator _mediator;
52
53   public DjComboBox(ObjectViewer viewer, String JavaDoc propertyName, DjDomainValue[] domainValues) throws DjenericException
54   {
55     super();
56     setModel(new DefaultComboBoxModel JavaDoc(domainValues));
57     setEditable(true);
58
59     if (isEditable())
60     {
61       DjFocusMeLater.getNeededComponent(this).addFocusListener(this);
62     }
63     else addActionListener(this);
64
65     _mediator = new BindingMediator(this, viewer, propertyName);
66   }
67
68   public void actionPerformed(ActionEvent JavaDoc e)
69   {
70     try
71     {
72       apply();
73     }
74     catch (Exception JavaDoc x)
75     {
76       DjLogger.log(x);
77     }
78   }
79
80   public void clear()
81   {
82     setSelectedIndex(-1);
83     setEnabled(_mediator.isComponentWritable());
84   }
85
86   public void apply() throws DjenericException
87   {
88     if (isEditable())
89     {
90       _mediator.setValue(getEditor().getItem());
91     }
92     else
93     {
94       if (getSelectedIndex() != -1)
95       {
96         DjDomainValue dv = (DjDomainValue) getSelectedItem();
97         _mediator.setValue(dv.getValue());
98       }
99     }
100   }
101
102   public void focusGained(FocusEvent JavaDoc e)
103   {
104     if (isEditable()) getEditor().selectAll();
105   }
106
107   public void focusLost(FocusEvent JavaDoc e)
108   {
109     try
110     {
111       apply();
112       synchronize();
113     }
114     catch (Exception JavaDoc x)
115     {
116       DjLogger.log(x);
117     }
118   }
119
120   public void setPropertyName(String JavaDoc propertyName) throws DjenericException
121   {
122     _mediator.setPropertyName(propertyName);
123   }
124
125   public void setViewer(ObjectViewer viewer)
126   {
127     _mediator.setViewer(viewer);
128   }
129
130   public void synchronize() throws DjenericException
131   {
132     setEnabled(_mediator.isComponentWritable());
133     if (isEditable())
134     {
135       try
136       {
137         setSelectedIndex(-1);
138         getEditor().setItem(_mediator.getPropertyValueString());
139       }
140       catch (Exception JavaDoc x)
141       {
142         DjLogger.log(x);
143       }
144     }
145     else
146     {
147       String JavaDoc curVal = _mediator.getPropertyValueString();
148       int idx = -1;
149       if (curVal.length() != 0)
150       {
151         for (int i = 0; i < getItemCount(); i++)
152         {
153           DjDomainValue dv = (DjDomainValue) getItemAt(i);
154
155           if (dv.getValue().equals(curVal))
156           {
157             idx = i;
158             break;
159           }
160         }
161       }
162
163       setSelectedIndex(idx);
164     }
165   }
166
167   public Object JavaDoc getDisplayedValue()
168   {
169     if (getSelectedIndex() != -1) return getEditor().getItem();
170     return null;
171   }
172
173   public String JavaDoc getPropertyName()
174   {
175     return _mediator.getPropertyName();
176   }
177
178   public JComponent JavaDoc getFocussableComponent()
179   {
180     return this;
181   }
182
183   public boolean isComponentWritable()
184   {
185     return _mediator.isComponentWritable();
186   }
187 }
Popular Tags