001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form;
043:
044: import java.awt.*;
045: import java.beans.*;
046: import java.lang.ref.Reference;
047: import java.lang.ref.SoftReference;
048: import java.util.*;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051:
052: import org.netbeans.modules.form.fakepeer.FakePeerSupport;
053:
054: /**
055: * BeanSupport is a utility class with various static methods supporting
056: * operations with JavaBeans.
057: *
058: * @author Ian Formanek, Jan Stola
059: */
060: public class BeanSupport {
061: public static final Object NO_VALUE = new Object();
062:
063: // -----------------------------------------------------------------------------
064: // Private variables
065:
066: private static Map<Class, Object> instancesCache = new HashMap<Class, Object>(
067: 30);
068:
069: // -----------------------------------------------------------------------------
070: // Public methods
071:
072: /**
073: * Utility method to create an instance of given class. Returns null on
074: * error.
075: * @param beanClass the class to create inctance of
076: * @return new instance of specified class or null if an error occured
077: * during instantiation
078: */
079: public static Object createBeanInstance(Class beanClass) {
080: try {
081: return CreationFactory.createDefaultInstance(beanClass);
082: } catch (Exception ex) {
083: Logger.getLogger(BeanSupport.class.getName()).log(
084: Level.INFO,
085: "Cannot create default instance of: "
086: + beanClass.getName(), ex); // NOI18N
087: return null;
088: } catch (LinkageError ex) {
089: Logger.getLogger(BeanSupport.class.getName()).log(
090: Level.INFO,
091: "Cannot create default instance of: "
092: + beanClass.getName(), ex); // NOI18N
093: return null;
094: }
095: }
096:
097: /**
098: * Utility method to obtain an instance of specified beanClass. The
099: * instance is reused, and thus should only be used to obtain info about
100: * settings of default instances of the specified class.
101: * @param beanClass the class to create inctance of
102: * @return instance of specified class or null if an error occured during
103: * instantiation
104: */
105: public static Object getDefaultInstance(Class beanClass) {
106: Object defInstance = instancesCache.get(beanClass);
107: if (defInstance == null) {
108: defInstance = createBeanInstance(beanClass);
109: if (defInstance instanceof Component) {
110: FakePeerSupport.attachFakePeer((Component) defInstance);
111: if (defInstance instanceof Container)
112: FakePeerSupport
113: .attachFakePeerRecursively((Container) defInstance);
114: }
115: instancesCache.put(beanClass, defInstance);
116: }
117: return defInstance;
118: }
119:
120: /** Utility method that obtains icon for a bean class.
121: * (This method is currently used only for obtaining default icons for AWT
122: * components. Other icons should be provided by BeanInfo.)
123: *
124: * @param beanClass class of the bean
125: * @param iconType not used
126: * @return icon for the bean class
127: */
128: public static Image getBeanIcon(Class beanClass, int iconType) {
129: return getIconForDefault(beanClass);
130: }
131:
132: /** A utility method that returns a class of event adapter for
133: * specified listener. It works only on known listeners from java.awt.event.
134: * Null is returned for unknown listeners.
135: *
136: * @param listener class of the listener
137: * @return class of an adapter for specified listener or null if
138: * unknown/does not exist
139: */
140: public static Class getAdapterForListener(Class listener) {
141: if (java.awt.event.ComponentListener.class.equals(listener))
142: return java.awt.event.ComponentAdapter.class;
143: else if (java.awt.event.ContainerListener.class
144: .equals(listener))
145: return java.awt.event.ContainerAdapter.class;
146: else if (java.awt.event.FocusListener.class.equals(listener))
147: return java.awt.event.FocusAdapter.class;
148: else if (java.awt.event.KeyListener.class.equals(listener))
149: return java.awt.event.KeyAdapter.class;
150: else if (java.awt.event.MouseListener.class.equals(listener))
151: return java.awt.event.MouseAdapter.class;
152: else if (java.awt.event.MouseMotionListener.class
153: .equals(listener))
154: return java.awt.event.MouseMotionAdapter.class;
155: else if (java.awt.event.WindowListener.class.equals(listener))
156: return java.awt.event.WindowAdapter.class;
157: else
158: return null; // not found
159: }
160:
161: // -----------------------------------------------------------------------------
162: // Private methods
163:
164: static Reference<Map<String, Object>> imageCache;
165:
166: private static synchronized Image getIconForDefault(Class klass) {
167: Map<String, Object> icons;
168: if ((imageCache == null)
169: || ((icons = imageCache.get()) == null)) {
170: icons = createImageCache();
171: imageCache = new SoftReference<Map<String, Object>>(icons);
172: }
173:
174: String name = klass.getName();
175: Object img = icons.get(name);
176:
177: if (img == null) {
178: return null;
179: }
180:
181: if (img instanceof Image) {
182: return (Image) img;
183: } else {
184: Image image = java.awt.Toolkit
185: .getDefaultToolkit()
186: .createImage(
187: BeanSupport.class.getResource((String) img));
188: icons.put(name, image);
189: return image;
190: }
191: }
192:
193: private static Map<String, Object> createImageCache() {
194: Map<String, Object> map = new HashMap<String, Object>();
195:
196: map.put("java.awt.Label",
197: "/org/netbeans/modules/form/beaninfo/awt/label.gif"); // NOI18N
198: map.put("java.awt.Button",
199: "/org/netbeans/modules/form/beaninfo/awt/button.gif"); // NOI18N
200: map
201: .put("java.awt.TextField",
202: "/org/netbeans/modules/form/beaninfo/awt/textfield.gif"); // NOI18N
203: map.put("java.awt.TextArea",
204: "/org/netbeans/modules/form/beaninfo/awt/textarea.gif"); // NOI18N
205: map.put("java.awt.Checkbox",
206: "/org/netbeans/modules/form/beaninfo/awt/checkbox.gif"); // NOI18N
207: map.put("java.awt.Choice",
208: "/org/netbeans/modules/form/beaninfo/awt/choice.gif"); // NOI18N
209: map.put("java.awt.List",
210: "/org/netbeans/modules/form/beaninfo/awt/list.gif"); // NOI18N
211: map
212: .put("java.awt.Scrollbar",
213: "/org/netbeans/modules/form/beaninfo/awt/scrollbar.gif"); // NOI18N
214: map
215: .put("java.awt.ScrollPane",
216: "/org/netbeans/modules/form/beaninfo/awt/scrollpane.gif"); // NOI18N
217: map.put("java.awt.Panel",
218: "/org/netbeans/modules/form/beaninfo/awt/panel.gif"); // NOI18N
219: map.put("java.awt.Canvas",
220: "/org/netbeans/modules/form/beaninfo/awt/canvas.gif"); // NOI18N
221: map.put("java.awt.MenuBar",
222: "/org/netbeans/modules/form/beaninfo/awt/menubar.gif"); // NOI18N
223: map
224: .put("java.awt.PopupMenu",
225: "/org/netbeans/modules/form/beaninfo/awt/popupmenu.gif"); // NOI18N
226: map.put("java.awt.Menu",
227: "/org/netbeans/modules/form/resources/menu.gif"); // NOI18N
228: map.put("java.awt.MenuItem",
229: "/org/netbeans/modules/form/resources/menuItem.gif"); // NOI18N
230: map
231: .put("java.awt.CheckboxMenuItem",
232: "/org/netbeans/modules/form/resources/menuItemCheckbox.gif"); // NOI18N
233: map
234: .put("org.netbeans.modules.form.Separator",
235: "/org/netbeans/modules/form/resources/menuSeparator.gif"); // NOI18N
236:
237: map
238: .put("java.applet.Applet",
239: "/org/netbeans/modules/form/resources/palette/applet.gif"); // NOI18N
240: map
241: .put("java.awt.Dialog",
242: "/org/netbeans/modules/form/resources/palette/dialog_16.png"); // NOI18N
243: map
244: .put("java.awt.Frame",
245: "/org/netbeans/modules/form/resources/palette/frame_16.png"); // NOI18N
246:
247: map
248: .put("javax.swing.JApplet",
249: "/org/netbeans/modules/form/resources/palette/applet.gif"); // NOI18N
250:
251: return map;
252: }
253:
254: }
|