KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > util > bean > propertyeditor > PropertyEditorUI


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 package com.nightlabs.util.bean.propertyeditor;
32
33 import java.awt.*;
34 import java.lang.reflect.*;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.beans.*;
38
39 import javax.swing.*;
40 import com.nightlabs.util.bean.BeanUtil;
41 import com.nightlabs.util.bean.DefaultPropertyEditorContext;
42 import com.nightlabs.util.bean.PropertyEditorMan;
43
44 /**
45  * Encapsulate a Component object used to configure a Beans property value.
46  * There are 3 possibilities:<p>
47  * 1. The property supports a custom editor. In this case, the editor UI
48  * simply behaves as a container for the custom editor.
49  * 2. The property is paintable. In this case, the editor UI creates a
50  * PaintableComponent which will delegate painting to the property editor.
51  * 3. The property is text based. In this case, the editor UI creates a
52  * text field for the property.
53  */

54 public class PropertyEditorUI
55 extends JPanel
56 {
57     private boolean differentValues = false;
58     public boolean isDifferentValues() {
59         return differentValues;
60     }
61         
62     private boolean saveAllValues = false;
63     public boolean isSaveAllValues() {
64         return saveAllValues;
65     }
66     public void setSaveAllValues(boolean b) {
67         saveAllValues = b;
68     }
69     
70     private boolean readOnly = false;
71     public boolean isReadOnly() {
72         return readOnly;
73     }
74     
75     private Object JavaDoc firstBean;
76     private Object JavaDoc firstValue;
77     
78   /*
79    * Methods to access the beans property
80    */

81   private Method getMethod, setMethod;
82
83   /** Property editor */
84   private PropertyEditor editor;
85     public PropertyEditor getPropertyEditor() {
86         return editor;
87     }
88
89   /** Propertye editor type */
90   private int editorType;
91
92   /** Component displaying the value */
93   private Component component;
94   public Component getComponent() {
95     return component;
96   }
97
98   private String JavaDoc propertyName;
99   public String JavaDoc getPropertyName() {
100     return propertyName;
101   }
102   
103     /**
104      * @return the property name for a given set method
105      * @param methodName method name. If the name is not the one of a set or get method
106      * name, the result is not guaranteed.
107      */

108     public String JavaDoc getPropertyName(String JavaDoc methodName)
109     {
110         return BeanUtil.getPropertyName(methodName, false);
111     }
112  
113   /*
114    * Editor types
115    */

116   static final int EDITOR_CUSTOM = 0;
117   static final int EDITOR_PAINTABLE = 1;
118   static final int EDITOR_STRING = 2;
119
120   public PropertyEditorUI(PropertyDescriptor pd)
121     {
122 // this.editor = PropertyEditorManager.findEditor(pd.getPropertyType());
123
this.editor = PropertyEditorMan.getPropertyEditor(DefaultPropertyEditorContext.class, pd.getPropertyType());
124     this.getMethod = pd.getReadMethod();
125     this.setMethod = pd.getWriteMethod();
126     
127     this.propertyName = pd.getName();
128     
129     init();
130   }
131   
132   /**
133    * @param editor the property editor for which a UI is provided.
134    * @param getMethod method to use to get the property value from a beans
135    * @param setMethod method to use to save the property value into a beans
136    */

137   public PropertyEditorUI(PropertyEditor editor, Method getMethod, Method setMethod)
138   {
139     if(editor==null || getMethod==null)
140       throw new IllegalArgumentException JavaDoc();
141
142     this.editor = editor;
143     this.getMethod = getMethod;
144     this.setMethod = setMethod;
145
146         this.propertyName = getPropertyName(getMethod.getName());
147         
148         init();
149   }
150
151   private void init()
152   {
153     if(editor.supportsCustomEditor())
154       editorType = EDITOR_CUSTOM;
155     else if(editor.isPaintable())
156       editorType = EDITOR_PAINTABLE;
157     else
158       editorType = EDITOR_STRING;
159
160     if (setMethod == null)
161         readOnly = true;
162     
163     switch(editorType){
164     case EDITOR_CUSTOM:
165       component = editor.getCustomEditor();
166       if (readOnly)
167         component.setEnabled(false);
168       break;
169     case EDITOR_PAINTABLE:
170       component = new PaintableEditor(editor);
171       if (readOnly)
172         component.setEnabled(false);
173       break;
174     case EDITOR_STRING:
175       component = new JTextField();
176       if (readOnly)
177         component.setEnabled(false);
178       break;
179     }
180
181     setLayout(new BorderLayout());
182     add(component);
183   }
184   
185   public int getEditorType(){
186     return editorType;
187   }
188
189   /**
190    * @param beans the Vector of all Beans which have this Property
191    * but only the property of one bean will be displayed by this editor.
192    */

193   public void load(List JavaDoc beans)
194   throws PropertyEditorUIException
195     {
196     // Iterate through all beans
197
for (int i=0; i<beans.size(); i++)
198     {
199         Object JavaDoc bean = beans.get(i);
200     
201         try
202             {
203             Object JavaDoc value = getMethod.invoke(bean, null);
204     
205             // if this is the first bean only set the Value
206
// once for the firstBean
207
if (firstValue == null)
208             {
209                 firstValue = value;
210                 firstBean = bean;
211                 editor.setValue(firstValue);
212                 
213                 if(editorType==EDITOR_STRING){
214                     ((JTextField)component).setText(editor.getAsText());
215                 }
216
217                 component.repaint();
218             }
219             // if there exist different values for some beans
220
// set differentValues true
221
else if (!firstValue.equals(value))
222                 differentValues = true;
223                             
224         }catch(IllegalAccessException JavaDoc e1){
225             throw new PropertyEditorUIException(e1);
226         }catch(IllegalArgumentException JavaDoc e2){
227             throw new PropertyEditorUIException(e2);
228         }catch(InvocationTargetException e3){
229             throw new PropertyEditorUIException(e3);
230         }
231     } // for (int i=0; i<beans.size(); i++)
232

233   }
234   
235   /**
236    * @param bean object whose property is to be displayed by this editor.
237    */

238   public void load(Object JavaDoc bean)
239   throws PropertyEditorUIException
240   {
241     try
242         {
243       Object JavaDoc value = getMethod.invoke(bean, null);
244         if (firstValue == null)
245         {
246             firstValue = value;
247         firstBean = bean;
248         editor.setValue(firstValue);
249         
250         if(editorType==EDITOR_STRING){
251           ((JTextField)component).setText(editor.getAsText());
252         }
253
254         component.repaint();
255         }
256     }catch(IllegalAccessException JavaDoc e1){
257         throw new PropertyEditorUIException(e1);
258     }catch(IllegalArgumentException JavaDoc e2){
259         throw new PropertyEditorUIException(e2);
260     }catch(InvocationTargetException e3){
261         throw new PropertyEditorUIException(e3);
262     }
263   }
264
265   /**
266    * @param beans the Vector of Beans which should save the Value
267    * of this editor
268    */

269   public void save(List JavaDoc beans)
270     throws PropertyEditorUIException
271     {
272     boolean commitValue = false;
273     if(editorType==EDITOR_STRING)
274         editor.setAsText(((JTextField)component).getText());
275
276     Object JavaDoc value = editor.getValue();
277     
278     if (firstValue == null)
279         throw new IllegalStateException JavaDoc("you must first load values before you can save!");
280
281     if (firstValue.equals(value))
282         commitValue = false;
283     else
284         commitValue = true;
285     
286     try
287         {
288         if (commitValue)
289         {
290         if (saveAllValues)
291         {
292             for (Iterator JavaDoc it = beans.iterator(); it.hasNext(); )
293             {
294                 Object JavaDoc bean = it.next();
295                 if (setMethod != null)
296                 {
297                     setMethod.invoke(bean, new Object JavaDoc[]{value});
298                 }
299             }
300         } // if (saveAllValues)
301
else
302         {
303             if (setMethod != null)
304                 setMethod.invoke(firstBean, new Object JavaDoc[]{value});
305         }
306         }
307     }catch(IllegalAccessException JavaDoc e1){
308         throw new PropertyEditorUIException(e1);
309     }catch(IllegalArgumentException JavaDoc e2){
310         throw new PropertyEditorUIException(e2);
311     }catch(InvocationTargetException e3){
312         throw new PropertyEditorUIException(e3);
313     }
314   }
315   
316   /**
317    * @param bean object into which the property value displayed
318    * in UI component should be saved.
319    */

320   public void save(Object JavaDoc bean)
321   throws PropertyEditorUIException
322     {
323     if(editorType==EDITOR_STRING)
324       editor.setAsText(((JTextField)component).getText());
325
326     Object JavaDoc value = editor.getValue();
327
328     boolean commitValue = false;
329     
330     if (firstValue == null)
331         throw new IllegalStateException JavaDoc("you must first load values before you can save!");
332
333     if (firstValue.equals(value))
334         commitValue = false;
335     else
336         commitValue = true;
337     
338     try
339         {
340         if (commitValue)
341         {
342         if (setMethod != null)
343         {
344           setMethod.invoke(bean, new Object JavaDoc[]{value});
345         }
346         }
347     }catch(IllegalAccessException JavaDoc e1){
348         throw new PropertyEditorUIException(e1);
349     }catch(IllegalArgumentException JavaDoc e2){
350         throw new PropertyEditorUIException(e2);
351     }catch(InvocationTargetException e3){
352         throw new PropertyEditorUIException(e3);
353     }
354   }
355 }
356
Popular Tags