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-2007 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.sun.manager.jbi.util;
043:
044: import java.awt.Image;
045: import java.beans.BeanInfo;
046: import java.beans.IntrospectionException;
047: import java.beans.Introspector;
048: import java.beans.PropertyDescriptor;
049: import java.io.StringReader;
050: import java.lang.reflect.Method;
051: import java.util.LinkedHashMap;
052: import java.util.Map;
053: import java.util.TreeMap;
054: import java.util.regex.Matcher;
055: import java.util.regex.Pattern;
056: import javax.management.Attribute;
057: import javax.management.MBeanAttributeInfo;
058: import javax.swing.ImageIcon;
059: import javax.swing.SwingUtilities;
060: import javax.xml.parsers.DocumentBuilder;
061: import javax.xml.parsers.DocumentBuilderFactory;
062: import org.openide.util.Utilities;
063: import org.w3c.dom.Document;
064: import org.xml.sax.InputSource;
065:
066: /**
067: *
068: * @author jqian
069: */
070: public class Utils {
071:
072: public static Image getBadgedIcon(Class clazz, String iconName,
073: String internalBadgeIconName, String externalBadgeIconName) {
074:
075: Image ret = new ImageIcon(clazz.getResource(iconName))
076: .getImage();
077:
078: if (internalBadgeIconName != null) {
079: Image internalBadgeImg = new ImageIcon(clazz
080: .getResource(internalBadgeIconName)).getImage();
081: ret = Utilities.mergeImages(ret, internalBadgeImg, 7, 7);
082: }
083:
084: if (externalBadgeIconName != null) {
085: Image externalBadgeImg = new ImageIcon(clazz
086: .getResource(externalBadgeIconName)).getImage();
087: ret = Utilities.mergeImages(ret, externalBadgeImg, 15, 8);
088: }
089:
090: return ret;
091: }
092:
093: /**
094: * Ensure that the specified ruannable task will run only in the event dispatch
095: * thread.
096: */
097: public static void runInEventDispatchThread(Runnable runnable) {
098: if (SwingUtilities.isEventDispatchThread()) {
099: runnable.run();
100: } else {
101: SwingUtilities.invokeLater(runnable);
102: }
103: }
104:
105: // public static Map<Attribute, MBeanAttributeInfo> getIntrospectedPropertyMap(
106: // Object bean) {
107: // return getIntrospectedPropertyMap(bean, false);
108: // }
109:
110: public static Map<Attribute, MBeanAttributeInfo> getIntrospectedPropertyMap(
111: Object bean, boolean sort) {
112: return getIntrospectedPropertyMap(bean, sort, null);
113: }
114:
115: public static Map<Attribute, MBeanAttributeInfo> getIntrospectedPropertyMap(
116: Object bean, boolean sort, String beanInfoPackageName) {
117:
118: if (bean == null) {
119: return null;
120: }
121:
122: Class beanClass = bean.getClass();
123: BeanInfo beanInfo = null;
124: try {
125: if (beanInfoPackageName != null) {
126: Introspector
127: .setBeanInfoSearchPath(new String[] { beanInfoPackageName });
128: }
129: beanInfo = Introspector
130: .getBeanInfo(beanClass, Object.class);
131: } catch (IntrospectionException ex) {
132: System.err.println("Couldn't introspect "
133: + beanClass.getName()); // NOI18N
134: return null;
135: }
136:
137: Map<Attribute, MBeanAttributeInfo> map = sort ? new TreeMap<Attribute, MBeanAttributeInfo>()
138: : new LinkedHashMap<Attribute, MBeanAttributeInfo>();
139:
140: PropertyDescriptor[] propDescriptors = beanInfo
141: .getPropertyDescriptors();
142:
143: for (int i = 0; i < propDescriptors.length; i++) {
144: Class propertyTypeClass = propDescriptors[i]
145: .getPropertyType();
146: Method readMethod = propDescriptors[i].getReadMethod();
147: Method writeMethod = propDescriptors[i].getWriteMethod();
148:
149: String propertyType = propertyTypeClass.getName();
150: String propertyName = propDescriptors[i].getName();
151: String propertyDesc = propDescriptors[i]
152: .getShortDescription();
153: Object propertyValue = null;
154: try {
155: propertyValue = readMethod
156: .invoke(bean, (Object[]) null);
157: } catch (Exception e) {
158: e.printStackTrace();
159: }
160: Attribute attr = new Attribute(propertyName, propertyValue);
161: if (sort) {
162: attr = new ComparableAttribute(attr);
163: }
164: map.put(attr, new MBeanAttributeInfo(propertyName,
165: propertyType, propertyDesc, readMethod != null,
166: writeMethod != null, readMethod.getName()
167: .startsWith("is"))); // NOI18N
168: }
169:
170: return map;
171: }
172:
173: /**
174: * Word-wrap long string.
175: *
176: * @param input an input string
177: * @param maxLineLength maximum characters per line after word wrapping
178: * @param newLineChars new line characters to be inserted
179: *
180: * @return the word-wrapped string with new line characters inserted.
181: */
182: public static String wordWrapString(String input,
183: int maxLineLength, String newLineChars) {
184: String ret = ""; // NOI18N
185: String regex = "(.{1," + maxLineLength + "}$)|" + // NOI18N
186: "(.{1," + maxLineLength + "}\\b\\s*)|(.{" + // NOI18N
187: maxLineLength + "}\\B)"; // NOI18N
188: Pattern p = Pattern.compile(regex);
189: Matcher m = p.matcher(input);
190: while (m.find()) {
191: ret += input.substring(m.start(), m.end());
192: ret += newLineChars;
193: }
194:
195: return ret;
196: }
197:
198: public static String getTooltip(String input) {
199: if (input == null) {
200: return ""; // NOI18N
201: } else if (input.length() > 80) {
202: String ret = "<HTML>"; // NOI18N
203: ret += Utils.wordWrapString(input, 80, "<br>"); // NOI18N
204: ret += "</HTML>"; // NOI18N
205: return ret;
206: } else {
207: return input;
208: }
209: }
210:
211: private static Document getDocument(String xmlString) {
212: try {
213: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
214: .newInstance();
215: DocumentBuilder documentBuilder = documentBuilderFactory
216: .newDocumentBuilder();
217: return documentBuilder.parse(new InputSource(
218: new StringReader(xmlString)));
219:
220: } catch (Exception e) {
221: System.out.println("Error parsing XML string: " + e); // NOI18N
222: return null;
223: }
224: }
225: }
|