KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tax > beans > Lib


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.tax.beans;
20
21 import java.beans.*;
22 import java.awt.Component JavaDoc;
23
24 import org.openide.NotifyDescriptor;
25 import org.openide.DialogDescriptor;
26 import org.openide.DialogDisplayer;
27
28 import org.netbeans.tax.*;
29 import javax.swing.JComponent JavaDoc;
30
31 import org.netbeans.modules.xml.tax.util.TAXUtil;
32
33 /**
34  *
35  * @author Libor Kramolis
36  * @version 0.1
37  */

38 public class Lib {
39
40     private static String JavaDoc CREATE_ATTRIBUTE_NAME = Util.THIS.getString ("TEXT_new_attribute_name");
41     private static String JavaDoc CREATE_ATTRIBUTE_VALUE = Util.THIS.getString ("TEXT_new_attribute_value");
42     
43
44
45     /** Returns the customizer component for <CODE>object</CODE>.
46      *
47      * @param <CODE>object</CODE> bean to get its customizer
48      * @return the component or <CODE>null</CODE> if there is no customizer
49      */

50     public static Component JavaDoc getCustomizer (Object JavaDoc object) {
51         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: object = " + object); // NOI18N
52

53         if (object == null)
54             return null;
55         
56         BeanInfo beanInfo;
57         try {
58             beanInfo = Introspector.getBeanInfo (object.getClass());
59         } catch (IntrospectionException e) {
60             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: exception = " + e); // NOI18N
61

62             return null;
63         }
64         Class JavaDoc clazz = beanInfo.getBeanDescriptor().getCustomizerClass();
65         if (clazz == null) {
66             return null;
67         }
68
69         Object JavaDoc o;
70         try {
71             o = clazz.newInstance ();
72         } catch (InstantiationException JavaDoc e) {
73             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: exception = " + e); // NOI18N
74

75             return null;
76         } catch (IllegalAccessException JavaDoc e) {
77             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: exception = " + e); // NOI18N
78

79             return null;
80         }
81
82         if (!!! (o instanceof Customizer) ) {
83             // no customizer => no fun
84
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: is NOT instanceof Customizer: " + o); // NOI18N
85

86             return null;
87         }
88
89         Customizer cust = ((java.beans.Customizer JavaDoc)o);
90
91         // looking for the component
92
Component JavaDoc comp = null;
93         if (o instanceof Component JavaDoc) {
94             comp = (Component JavaDoc)o;
95         } else {
96             // no component provided
97
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: is NOT instanceof Component: " + o); // NOI18N
98

99             return null;
100         }
101
102         cust.setObject (object);
103
104         return comp;
105     }
106
107     /**
108      */

109     public static Component JavaDoc getCustomizer (Class JavaDoc classClass, Object JavaDoc property, String JavaDoc propertyName) {
110         BeanInfo beanInfo;
111         try {
112             beanInfo = Introspector.getBeanInfo (classClass);
113         } catch (IntrospectionException e) {
114             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: exception = " + e); // NOI18N
115

116             return null;
117         }
118         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: beaninfo = " + beanInfo); // NOI18N
119

120     PropertyDescriptor[] propDescrs = beanInfo.getPropertyDescriptors();
121     PropertyDescriptor propertyDescriptor = null;
122     for ( int i = 0; i < propDescrs.length; i++ ) {
123         if ( propertyName.equals (propDescrs[i].getName()) ) {
124         propertyDescriptor = propDescrs[i];
125         break;
126         }
127     }
128     if ( propertyDescriptor == null ) {
129             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: have NOT property: " + propertyName); // NOI18N
130

131         return null;
132     }
133         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: propertyDescriptor: " + propertyDescriptor); // NOI18N
134

135         Class JavaDoc clazz = propertyDescriptor.getPropertyEditorClass();
136
137         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: propertyEditorClass: " + clazz); // NOI18N
138

139         if (clazz == null) {
140             return null;
141         }
142         Object JavaDoc peo;
143         try {
144             peo = clazz.newInstance ();
145         } catch (InstantiationException JavaDoc e) {
146             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: exception = " + e); // NOI18N
147

148             return null;
149         } catch (IllegalAccessException JavaDoc e) {
150             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: exception = " + e); // NOI18N
151

152             return null;
153         }
154     
155         if (!!! (peo instanceof PropertyEditor) ) {
156             // no customizer => no fun
157
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: is NOT instanceof PropertyEditor: " + peo); // NOI18N
158

159             return null;
160         }
161
162         PropertyEditor editor = ((PropertyEditor)peo);
163     editor.setValue (property);
164     Component JavaDoc comp = editor.getCustomEditor();
165     if ( comp == null ) {
166             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: have NOT customizer: " + editor); // NOI18N
167

168         return null;
169     }
170     if (!!! (comp instanceof Customizer) ) {
171         // no customizer => no fun
172
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Lib::getCustomizer: is NOT instanceof Customizer: " + comp); // NOI18N
173

174             return null;
175         }
176         Customizer cust = ((Customizer)comp);
177
178 // cust.setObject (property); // done by editor.setValue (property);
179

180         return comp;
181     }
182
183     /**
184      */

185     public static boolean confirmAction (String JavaDoc message) {
186     NotifyDescriptor nd = new NotifyDescriptor.Confirmation (message, NotifyDescriptor.YES_NO_OPTION);
187
188     Object JavaDoc option = DialogDisplayer.getDefault().notify (nd);
189
190     return ( option == NotifyDescriptor.YES_OPTION );
191     }
192
193
194     /**
195      * Create Attribute dialo gin edit mode.
196      */

197     public static TreeAttribute createAttributeDialog () {
198         return createAttributeDialog(false);
199     }
200     
201     /**
202      * @param mone if true new mode is used instead of edit one
203      */

204     public static TreeAttribute createAttributeDialog (boolean mode) {
205         try {
206             TreeAttribute attr = new TreeAttribute (CREATE_ATTRIBUTE_NAME, CREATE_ATTRIBUTE_VALUE);
207             Component JavaDoc customizer = getCustomizer (attr);
208
209             if ( customizer == null ) {
210                 return null;
211             }
212
213         return (TreeAttribute)customNode (attr, customizer, Util.THIS.getString ("TITLE_new_attribute"), mode);
214     } catch (TreeException exc) {
215         TAXUtil.notifyTreeException (exc);
216             return null;
217         }
218     }
219
220     
221     /**
222      */

223     private static TreeNode customNode (TreeNode treeNode, Component JavaDoc panel, String JavaDoc title, boolean mode) {
224     DialogDescriptor dd = new DialogDescriptor
225         (panel, title, true, DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION,
226          DialogDescriptor.BOTTOM_ALIGN, null, null);
227
228         //??? Warning same code is also in tree.nodes.NodeFactory
229
if (panel instanceof JComponent JavaDoc) {
230             // set hints to the customizer component
231
if (mode) {
232                 ((JComponent JavaDoc)panel).putClientProperty("xml-edit-mode", "new"); // NOI18N
233
} else {
234                 ((JComponent JavaDoc)panel).putClientProperty("xml-edit-mode", "edit"); // NOI18N
235
}
236         }
237         
238     DialogDisplayer.getDefault ().createDialog (dd).setVisible(true);
239
240     if (dd.getValue() != DialogDescriptor.OK_OPTION) {
241         return null;
242         }
243     return treeNode;
244     }
245
246 }
247
Popular Tags