KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > uihandler > UINode


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 package org.netbeans.modules.uihandler;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.MissingResourceException JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.LogRecord JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.logging.SimpleFormatter JavaDoc;
30 import javax.swing.JButton JavaDoc;
31 import org.openide.awt.Actions;
32 import org.openide.nodes.AbstractNode;
33 import org.openide.nodes.Children;
34 import org.openide.nodes.Node;
35 import org.openide.nodes.PropertySupport;
36 import org.openide.nodes.Sheet;
37 import org.openide.util.NbBundle;
38 import org.openide.util.lookup.Lookups;
39
40 /**
41  *
42  * @author Jaroslav Tulach
43  */

44 final class UINode extends AbstractNode implements VisualData {
45     private static final SimpleFormatter JavaDoc FORMATTER = new SimpleFormatter JavaDoc();
46     private LogRecord JavaDoc log;
47     private String JavaDoc htmlKey;
48
49     private UINode(LogRecord JavaDoc r, Children ch) {
50         super(ch, Lookups.fixed(r));
51         log = r;
52         if (r.getMessage() == null) {
53             setName("Seq: " + r.getSequenceNumber());
54         } else {
55             setName(r.getMessage());
56         }
57         if (r.getResourceBundle() != null) {
58             try {
59                 String JavaDoc msg = r.getResourceBundle().getString(r.getMessage());
60                 if (r.getParameters() != null) {
61                     msg = MessageFormat.format(msg, r.getParameters());
62                 }
63                 setDisplayName(msg);
64             } catch (MissingResourceException JavaDoc ex) {
65                 Logger.getAnonymousLogger().log(Level.INFO, null, ex);
66             }
67             
68             
69             try {
70                 String JavaDoc iconBase = r.getResourceBundle().getString(r.getMessage() + "_ICON_BASE"); // NOI18N
71
setIconBaseWithExtension(iconBase);
72             } catch (MissingResourceException JavaDoc ex) {
73                 // ok, use default
74
setIconBaseWithExtension("org/netbeans/modules/uihandler/def.png");
75             }
76         }
77         
78         
79         
80         if (ch != Children.LEAF) {
81             setIconBaseWithExtension("org/netbeans/modules/uihandler/exception.gif");
82             htmlKey = "HTML_exception";
83         }
84         
85         if ("UI_ACTION_BUTTON_PRESS".equals(r.getMessage())) { // NOI18N
86
setDisplayName(Actions.cutAmpersand(getParam(r, 4)));
87             String JavaDoc thru = (String JavaDoc)r.getParameters()[1];
88             if (thru.contains("Toolbar") || r.getParameters()[0] instanceof JButton JavaDoc) {
89                 setIconBaseWithExtension("org/netbeans/modules/uihandler/toolbars.gif");
90                 htmlKey = "HTML_toolbar";
91             } else if (thru.contains("MenuItem")) {
92                 setIconBaseWithExtension("org/netbeans/modules/uihandler/menus.gif");
93                 htmlKey = "HTML_menu";
94             }
95         } else if ("UI_ACTION_KEY_PRESS".equals(r.getMessage())) { // NOI18N
96
setDisplayName(Actions.cutAmpersand(getParam(r, 4)));
97             setIconBaseWithExtension("org/netbeans/modules/uihandler/key.png");
98             htmlKey = "HTML_key";
99         } else if ("UI_ACTION_EDITOR".equals(r.getMessage())) { // NOI18N
100
setDisplayName(Actions.cutAmpersand(getParam(r, 4)));
101             setIconBaseWithExtension("org/netbeans/modules/uihandler/key.png");
102             htmlKey = "HTML_key";
103         } else if ("UI_ENABLED_MODULES".equals(r.getMessage())) { // NOI18N
104
setDisplayName(NbBundle.getMessage(UINode.class, "MSG_EnabledModules"));
105             setIconBaseWithExtension("org/netbeans/modules/uihandler/module.gif");
106             htmlKey = null;
107         } else if ("UI_DISABLED_MODULES".equals(r.getMessage())) { // NOI18N
108
setDisplayName(NbBundle.getMessage(UINode.class, "MSG_DisabledModules"));
109             setIconBaseWithExtension("org/netbeans/modules/uihandler/module.gif");
110             htmlKey = null;
111         } else if (Installer.USER_CONFIGURATION.equals(r.getMessage())){// NOI18N
112
setDisplayName(NbBundle.getMessage(UINode.class, "MSG_USER_CONFIGURATION"));
113             htmlKey = null;
114         }
115
116         
117         Sheet.Set s = Sheet.createPropertiesSet();
118         s.put(createPropertyDate(this));
119         s.put(createPropertyLogger(this));
120         s.put(createPropertyMessage(this));
121         getSheet().put(s);
122         
123         if (r.getParameters() != null && r.getParameters().length > 0) {
124             Sheet.Set paramSheet = new Sheet.Set();
125             paramSheet.setName("parameters"); // NOI18N
126
paramSheet.setDisplayName(NbBundle.getMessage(UINode.class, "MSG_DisplayNameParameters"));
127             for (int i = 0; i < r.getParameters().length; i++) {
128                 paramSheet.put(createProperty(i, r.getParameters()[i]));
129             }
130             getSheet().put(paramSheet);
131         }
132         
133         setShortDescription(FORMATTER.format(log));
134     }
135
136     public long getMillis() {
137         return log.getMillis();
138     }
139     
140     public String JavaDoc getLoggerName() {
141         return log.getLoggerName();
142     }
143     
144     public String JavaDoc getMessage() {
145         return FORMATTER.format(log);
146     }
147     
148     @Override JavaDoc
149     public String JavaDoc getHtmlDisplayName() {
150         if (htmlKey == null) {
151             return null;
152         } else {
153             return NbBundle.getMessage(UINode.class, htmlKey, getDisplayName());
154         }
155     }
156     
157     static String JavaDoc getParam(LogRecord JavaDoc r, int index) {
158         Object JavaDoc[] arr = r.getParameters();
159         if (arr == null || arr.length <= index || !(arr[index] instanceof String JavaDoc)) {
160             return "";
161         }
162         return (String JavaDoc)arr[index];
163     }
164     
165     static Node create(LogRecord JavaDoc r) {
166         Children ch;
167         if (r.getThrown() != null) {
168             ch = new StackTraceChildren(r.getThrown());
169         } else if ("UI_ENABLED_MODULES".equals(r.getMessage()) ||
170             "UI_DISABLED_MODULES".equals(r.getMessage())) {
171             ch = new ModulesChildren(r.getParameters());
172         } else {
173             ch = Children.LEAF;
174         }
175         
176         
177         return new UINode(r, ch);
178     }
179     
180     static Node.Property createPropertyDate(final VisualData source) {
181         class NP extends PropertySupport.ReadOnly<Date JavaDoc> {
182             public NP() {
183                 super(
184                     "date", Date JavaDoc.class,
185                     NbBundle.getMessage(UINode.class, "MSG_DateDisplayName"),
186                     NbBundle.getMessage(UINode.class, "MSG_DateShortDescription")
187                 );
188             }
189
190             public Date JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
191                 return source == null ? null : new Date JavaDoc(source.getMillis());
192             }
193             
194             public int hashCode() {
195                 return getClass().hashCode();
196             }
197             public boolean equals(Object JavaDoc o) {
198                 return o != null && o.getClass().equals(getClass());
199             }
200         }
201         return new NP();
202     }
203
204     static Node.Property createPropertyLogger(final VisualData source) {
205         class NP extends PropertySupport.ReadOnly<String JavaDoc> {
206             public NP() {
207                 super(
208                     "logger", String JavaDoc.class,
209                     NbBundle.getMessage(UINode.class, "MSG_LoggerDisplayName"),
210                     NbBundle.getMessage(UINode.class, "MSG_LoggerShortDescription")
211                 );
212             }
213
214             public String JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
215                 if (source == null) {
216                     return null;
217                 }
218                 String JavaDoc full = source.getLoggerName();
219                 if (full == null) {
220                     return null;
221                 }
222                 if (full.startsWith("org.netbeans.ui")) {
223                     if (full.equals("org.netbeans.ui")) {
224                         return "UI General";
225                     }
226                     
227                     return full.substring("org.netbeans.ui.".length());
228                 }
229                 return full;
230             }
231             
232             public int hashCode() {
233                 return getClass().hashCode();
234             }
235             public boolean equals(Object JavaDoc o) {
236                 return o != null && o.getClass().equals(getClass());
237             }
238         }
239         return new NP();
240     }
241     static Node.Property createPropertyMessage(final VisualData source) {
242         class NP extends PropertySupport.ReadOnly<String JavaDoc> {
243             public NP() {
244                 super(
245                     "message", String JavaDoc.class,
246                     NbBundle.getMessage(UINode.class, "MSG_MessageDisplayName"),
247                     NbBundle.getMessage(UINode.class, "MSG_MessageShortDescription")
248                 );
249             }
250
251             public String JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
252                 return source == null ? null : source.getMessage();
253             }
254             
255             public int hashCode() {
256                 return getClass().hashCode();
257             }
258             public boolean equals(Object JavaDoc o) {
259                 return o != null && o.getClass().equals(getClass());
260             }
261         }
262         return new NP();
263     }
264     private Node.Property<?> createProperty(final int index, final Object JavaDoc object) {
265         class NP extends PropertySupport.ReadOnly<String JavaDoc> {
266             public NP() {
267                 super(
268                     "param #" + index, String JavaDoc.class,
269                     NbBundle.getMessage(UINode.class, "MSG_ParameterDisplayName", index, object),
270                     NbBundle.getMessage(UINode.class, "MSG_ParameterShortDescription", index, object)
271                 );
272             }
273
274             public String JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
275                 return object == null ? null : object.toString();
276             }
277             
278             private int getIndex() {
279                 return index;
280             }
281             
282             public int hashCode() {
283                 return getClass().hashCode();
284             }
285             public boolean equals(Object JavaDoc o) {
286                 if (o == null || !o.getClass().equals(getClass())) {
287                     return false;
288                 }
289                 NP np = (NP)o;
290                 return getIndex() == np.getIndex();
291             }
292         }
293         return new NP();
294     }
295
296     
297     private static final class StackTraceChildren extends Children.Keys<StackTraceElement JavaDoc> {
298         private Throwable JavaDoc throwable;
299         public StackTraceChildren(Throwable JavaDoc t) {
300             throwable = t;
301         }
302         
303         protected void addNotify() {
304             setKeys(throwable.getStackTrace());
305         }
306         
307         protected Node[] createNodes(StackTraceElement JavaDoc key) {
308             AbstractNode an = new AbstractNode(Children.LEAF);
309             an.setName(key.getClassName() + "." + key.getMethodName());
310             an.setDisplayName(NbBundle.getMessage(UINode.class, "MSG_StackTraceElement",
311                 new Object JavaDoc[] {
312                     key.getFileName(),
313                     key.getClassName(),
314                     key.getMethodName(),
315                     key.getLineNumber(),
316                     afterLastDot(key.getClassName()),
317                 }
318             ));
319             an.setIconBaseWithExtension("org/netbeans/modules/uihandler/stackframe.gif"); // NOI18N
320
return new Node[] { an };
321         }
322         
323     } // end of StackTraceElement
324

325     
326     private static final class ModulesChildren extends Children.Keys<Object JavaDoc> {
327         private Object JavaDoc[] modules;
328         public ModulesChildren(Object JavaDoc[] m) {
329             modules = m;
330         }
331         
332         protected void addNotify() {
333             setKeys(modules);
334         }
335         
336         protected Node[] createNodes(Object JavaDoc key) {
337             AbstractNode an = new AbstractNode(Children.LEAF);
338             an.setName((String JavaDoc)key);
339             an.setIconBaseWithExtension("org/netbeans/modules/uihandler/module.gif"); // NOI18N
340
return new Node[] { an };
341         }
342         
343     } // end of StackTraceElement
344

345     private static String JavaDoc afterLastDot(String JavaDoc s) {
346         int index = s.lastIndexOf('.');
347         if (index == -1) {
348             return s;
349         }
350         return s.substring(index + 1);
351     }
352
353     
354
355 }
356
Popular Tags