View Javadoc

1   /*
2    * Copyright (c) 2005 Regents of the University of California (Regents). Created
3    * by TELS, Graduate School of Education, University of California at Berkeley.
4    *
5    * This software is distributed under the GNU Lesser General Public License, v2.
6    *
7    * Permission is hereby granted, without written agreement and without license
8    * or royalty fees, to use, copy, modify, and distribute this software and its
9    * documentation for any purpose, provided that the above copyright notice and
10   * the following two paragraphs appear in all copies of this software.
11   *
12   * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14   * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
15   * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
16   * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17   *
18   * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19   * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
20   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21   * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22   */
23  package net.sf.sail.core.util;
24  
25  import java.awt.Image;
26  import java.beans.BeanInfo;
27  import java.beans.IntrospectionException;
28  import java.beans.Introspector;
29  import java.beans.beancontext.BeanContext;
30  import java.beans.beancontext.BeanContextChild;
31  import java.beans.beancontext.BeanContextServices;
32  import java.io.PrintStream;
33  import java.net.URL;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.HashSet;
37  import java.util.logging.Level;
38  import java.util.logging.Logger;
39  
40  import net.sf.sail.common.beansupport.IIconProvider;
41  
42  /**
43   * Our class of utility functions for Beans and BeanContexts. Everything here is
44   * generic to all JavaBean programming. Methods specific to pods go in
45   * {@link PodUtils}.
46   * 
47   * @author turadg
48   */
49  public class SailBeanUtils {
50  	/**
51  	 * Logger for this class
52  	 */
53  	private static final Logger logger = Logger.getLogger(SailBeanUtils.class
54  			.getName());
55  
56  	public static void printTree(PrintStream out, int indent,
57  			Collection<?> collection) {
58  		for (int i = 0; i < indent; i += 1)
59  			out.print(" ");
60  		out.println(collection);
61  		for (Object object : collection) {
62  			if (object instanceof Collection)
63  				printTree(out, indent + 4, (Collection<?>) object);
64  			else {
65  				for (int i = 0; i < indent; i += 1)
66  					out.print(" ");
67  				out.print("\\   (");
68  				out.print(object.getClass().getSimpleName());
69  				out.print(") ");
70  				out.println(object);  // object.toString() representation
71  			}
72  		}
73  	}
74  
75  	public static <T> T firstChildOfType(BeanContext bc, Class<T> type) {
76  		Collection<T> collection = SailBeanUtils.childrenOfType(bc, type);
77  		return collection.iterator().next();
78  	}
79  
80  	public static <T> Collection<T> childrenOfType(BeanContext bc, Class<T> type) {
81  		Collection<T> found = new HashSet<T>();
82  		findChildrenOfType(bc, type, found);
83  		return Collections.unmodifiableCollection(found);
84  	}
85  
86  	@SuppressWarnings("unchecked")
87  	static <T> void findChildrenOfType(BeanContext bc, Class<T> type,
88  			Collection<T> found) {
89  		for (Object object : bc) {
90  			if (type.isInstance(object)) {
91  				final T typedObject = (T) object;
92  				found.add(typedObject);
93  			}
94  			if (object instanceof BeanContext) {
95  				BeanContext child = (BeanContext) object;
96  				findChildrenOfType(child, type, found);
97  			}
98  		}
99  	}
100 
101 	public static Image getLargeBeanIcon(Object bean) {
102 		if (bean instanceof IIconProvider) {
103 			URL iconUrl = ((IIconProvider) bean)
104 					.getIcon(BeanInfo.ICON_COLOR_32x32);
105 			if (iconUrl != null) {
106 				return java.awt.Toolkit.getDefaultToolkit().getImage(iconUrl);
107 			}
108 		}
109 
110 		try {
111 			BeanInfo info = Introspector.getBeanInfo(bean.getClass());
112 			return info.getIcon(BeanInfo.ICON_COLOR_32x32);
113 		} catch (IntrospectionException e) {
114 			logger.log(Level.SEVERE, "Class -", e); //$NON-NLS-1$
115 			return null;
116 		}
117 	}
118 
119 	public static Image getSmallBeanIcon(Object bean) {
120 		if (bean instanceof IIconProvider) {
121 			URL iconUrl = ((IIconProvider) bean)
122 					.getIcon(BeanInfo.ICON_COLOR_16x16);
123 			if (iconUrl != null) {
124 				return java.awt.Toolkit.getDefaultToolkit().getImage(iconUrl);
125 			}
126 		}
127 
128 		try {
129 			BeanInfo info = Introspector.getBeanInfo(bean.getClass());
130 			Image icon = info.getIcon(BeanInfo.ICON_COLOR_16x16);
131 			return icon;
132 		} catch (IntrospectionException e) {
133 			logger.log(Level.SEVERE, "Class -", e); //$NON-NLS-1$
134 			return null;
135 		}
136 	}
137 
138 	@Deprecated
139 	public static String shortNameOf(Class<?> clazz) {
140 		return clazz.getSimpleName();
141 	}
142 
143 	public static BeanContextServices findServicesContextOf(
144 			BeanContextChild child) {
145 		BeanContext bc = child.getBeanContext();
146 
147 		if (bc == null)
148 			throw new RuntimeException("BeanContextChild has no bean context");
149 
150 		if (!(bc instanceof BeanContextServices))
151 			throw new RuntimeException(
152 					"BeanContextChild within a non BCS context");
153 
154 		BeanContextServices bcs = (BeanContextServices) bc;
155 		return bcs;
156 	}
157 
158 	/**
159 	 * Helper method to find the BeanContext containing the source
160 	 * 
161 	 * @param source
162 	 * @return BeanContext containing the source bean
163 	 */
164 	public static BeanContext resolveBeanContext(Object source) {
165 		if (source instanceof BeanContextChild) {
166 			BeanContextChild bcc = (BeanContextChild) source;
167 			BeanContext pod = bcc.getBeanContext();
168 			return pod;
169 		}
170 		return null;
171 	}
172 
173 	/**
174 	 * @param source
175 	 * @return Pod containing the source bean
176 	 * @deprecated use {@link PodUtils} instead
177 	 */
178 	@Deprecated
179 	public static BeanContext resolvePod(Object source) {
180 		return SailBeanUtils.resolvePod(source);
181 	}
182 
183 }