KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > config > ui > ConfigUtils


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
20 /*
21  * ConfigUtils.java
22  *
23  * Created on August 15, 2001, 4:29 PM
24  */

25
26 package org.netbeans.modules.j2ee.sun.share.config.ui;
27
28 import java.beans.*;
29 import java.lang.reflect.Method JavaDoc;
30 import java.util.*;
31
32 import javax.enterprise.deploy.spi.DConfigBean JavaDoc;
33
34 import org.openide.ErrorManager;
35 import org.openide.nodes.*;
36 import org.openide.util.Lookup;
37
38 import org.netbeans.modules.j2ee.sun.share.config.ConfigBeanStorage;
39
40
41 /*
42  * @author gfink
43  * @version
44  */

45 public class ConfigUtils {
46
47     static Map infoMap = new HashMap();
48
49     public static BeanInfo createBeanInfo(Object JavaDoc bean) {
50         BeanInfo info;
51         if (bean == null) {
52             return null;
53         }
54         if(infoMap.containsKey(bean.getClass())) {
55             return (BeanInfo) infoMap.get(bean.getClass());
56         }
57         try {
58             // Introspector doesn't work!
59
// info = (BeanInfo) Class.forName(bean.getClass().getName() + "BeanInfo").newInstance();
60
// info = Introspector.getBeanInfo(bean.getClass()/*,ConfigBean.class */);
61
// System.out.println("ConfigUtils.createBeanInfo BeanInfo: " + dConfigBean.getClass().getName() + "BeanInfo");
62
info = (BeanInfo)
63                 bean.getClass().getClassLoader().loadClass(bean.getClass().getName() + "BeanInfo").newInstance();
64                 // System.out.println("Creating info " + info);
65

66             
67         } catch (/* Introspection */ Exception JavaDoc ie) {
68 // ie.getMessage();
69
// BeanInfo not found. Must create one?
70
info = new DefaultBeanInfo(bean.getClass());
71         }
72         infoMap.put(bean.getClass(),info);
73         return info;
74     }
75     
76     // don't do the classloader lookup
77
static BeanInfo createDefaultBeanInfo(Object JavaDoc bean) {
78         BeanInfo info = new DefaultBeanInfo(bean.getClass());
79         infoMap.put(bean.getClass(),info);
80         return info;
81     }
82
83     public static Sheet.Set[] createSheets(Object JavaDoc bean) {
84         return null;
85     }
86     
87     public static Sheet.Set createSheet(Object JavaDoc bean) {
88         // System.err.println("Creating sheet for " + bean);
89
Sheet.Set basic = new Sheet.Set();
90         try {
91             BeanInfo info = null;
92             if (bean instanceof ConfigBeanStorage) {
93                 info = createBeanInfo(((ConfigBeanStorage)bean).getConfigBean());
94             }
95             else {
96                 info = createBeanInfo(bean);
97             }
98             if(info == null) return basic;
99             PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
100             if(descriptors == null) {
101                 System.err.println("Beaninfo doesn't have property descriptors for " + bean.getClass() + " info " + info.getClass());
102   /* BeanInfo defInfo = createDefaultBeanInfo(bean);
103                 if(defInfo != null) {
104                     info = defInfo;
105                     descriptors = info.getPropertyDescriptors();
106                 } */

107                 Node.Property property = ConfigProperty.getBraindead(info.getBeanDescriptor());
108                 basic.put(property);
109             }
110             else
111                 for(int i = 0; i < descriptors.length; i++) {
112                     if(descriptors[i].isHidden()) continue;
113                     if(descriptors[i].getName().equals("xpaths")) continue;
114                     if(descriptors[i].getName().equals("standardDDBean")) continue;
115                     // System.err.println("Creating property for " + descriptors[i].getName());
116
DConfigBean JavaDoc dConfigBean = ((ConfigBeanStorage)bean).getConfigBean();
117                     Node.Property property = ConfigProperty.getProperty(dConfigBean,descriptors[i]);
118 // Node.Property property = ConfigProperty.getProperty(bean,descriptors[i]);
119
basic.put(property);
120                 }
121         } catch (Exception JavaDoc e) {
122             ErrorManager em = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class);
123             em.notify(e);
124         }
125         // need some way of returning null if there are no properties in
126
// a set.
127
return basic;
128     }
129     
130     public static Object JavaDoc getBeanPropertyValue(DConfigBean JavaDoc config, String JavaDoc propName) {
131         Class JavaDoc cls = config.getClass();
132         StringBuffer JavaDoc name = new StringBuffer JavaDoc("get"+propName); //NOI18N
133
name.setCharAt(3,Character.toUpperCase(propName.charAt(0)));
134         try {
135             Method JavaDoc method = cls.getDeclaredMethod(name.toString(), new Class JavaDoc[0]);
136             method.setAccessible (true);
137             return method.invoke(config,new Object JavaDoc[0]);
138         } catch (Exception JavaDoc ex) {
139             org.openide.ErrorManager.getDefault().log(ErrorManager.USER, ex.toString());
140         }
141         return null;
142     }
143     
144     public static void setBeanPropertyValue(DConfigBean JavaDoc config, String JavaDoc propName, String JavaDoc value) {
145         Class JavaDoc cls = config.getClass();
146         StringBuffer JavaDoc name = new StringBuffer JavaDoc("set"+propName); //NOI18N
147
name.setCharAt(3,Character.toUpperCase(propName.charAt(0)));
148         try {
149             Method JavaDoc method = cls.getDeclaredMethod(name.toString(), new Class JavaDoc[] { java.lang.String JavaDoc.class });
150             method.setAccessible (true);
151             method.invoke(config, new Object JavaDoc[] { value });
152         } catch (Exception JavaDoc ex) {
153             org.openide.ErrorManager.getDefault().log(ErrorManager.USER, ex.toString());
154         }
155     }}
156
Popular Tags