1 19 20 package org.netbeans.modules.uihandler; 21 22 import java.lang.reflect.InvocationTargetException ; 23 import java.text.MessageFormat ; 24 import java.util.Date ; 25 import java.util.MissingResourceException ; 26 import java.util.logging.Level ; 27 import java.util.logging.LogRecord ; 28 import java.util.logging.Logger ; 29 import java.util.logging.SimpleFormatter ; 30 import javax.swing.JButton ; 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 44 final class UINode extends AbstractNode implements VisualData { 45 private static final SimpleFormatter FORMATTER = new SimpleFormatter (); 46 private LogRecord log; 47 private String htmlKey; 48 49 private UINode(LogRecord 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 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 ex) { 65 Logger.getAnonymousLogger().log(Level.INFO, null, ex); 66 } 67 68 69 try { 70 String iconBase = r.getResourceBundle().getString(r.getMessage() + "_ICON_BASE"); setIconBaseWithExtension(iconBase); 72 } catch (MissingResourceException ex) { 73 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())) { setDisplayName(Actions.cutAmpersand(getParam(r, 4))); 87 String thru = (String )r.getParameters()[1]; 88 if (thru.contains("Toolbar") || r.getParameters()[0] instanceof JButton ) { 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())) { 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())) { 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())) { 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())) { 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())){ 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"); 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 getLoggerName() { 141 return log.getLoggerName(); 142 } 143 144 public String getMessage() { 145 return FORMATTER.format(log); 146 } 147 148 @Override 149 public String getHtmlDisplayName() { 150 if (htmlKey == null) { 151 return null; 152 } else { 153 return NbBundle.getMessage(UINode.class, htmlKey, getDisplayName()); 154 } 155 } 156 157 static String getParam(LogRecord r, int index) { 158 Object [] arr = r.getParameters(); 159 if (arr == null || arr.length <= index || !(arr[index] instanceof String )) { 160 return ""; 161 } 162 return (String )arr[index]; 163 } 164 165 static Node create(LogRecord 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 > { 182 public NP() { 183 super( 184 "date", Date .class, 185 NbBundle.getMessage(UINode.class, "MSG_DateDisplayName"), 186 NbBundle.getMessage(UINode.class, "MSG_DateShortDescription") 187 ); 188 } 189 190 public Date getValue() throws IllegalAccessException , InvocationTargetException { 191 return source == null ? null : new Date (source.getMillis()); 192 } 193 194 public int hashCode() { 195 return getClass().hashCode(); 196 } 197 public boolean equals(Object 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 > { 206 public NP() { 207 super( 208 "logger", String .class, 209 NbBundle.getMessage(UINode.class, "MSG_LoggerDisplayName"), 210 NbBundle.getMessage(UINode.class, "MSG_LoggerShortDescription") 211 ); 212 } 213 214 public String getValue() throws IllegalAccessException , InvocationTargetException { 215 if (source == null) { 216 return null; 217 } 218 String 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 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 > { 243 public NP() { 244 super( 245 "message", String .class, 246 NbBundle.getMessage(UINode.class, "MSG_MessageDisplayName"), 247 NbBundle.getMessage(UINode.class, "MSG_MessageShortDescription") 248 ); 249 } 250 251 public String getValue() throws IllegalAccessException , InvocationTargetException { 252 return source == null ? null : source.getMessage(); 253 } 254 255 public int hashCode() { 256 return getClass().hashCode(); 257 } 258 public boolean equals(Object 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 object) { 265 class NP extends PropertySupport.ReadOnly<String > { 266 public NP() { 267 super( 268 "param #" + index, String .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 getValue() throws IllegalAccessException , InvocationTargetException { 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 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 > { 298 private Throwable throwable; 299 public StackTraceChildren(Throwable t) { 300 throwable = t; 301 } 302 303 protected void addNotify() { 304 setKeys(throwable.getStackTrace()); 305 } 306 307 protected Node[] createNodes(StackTraceElement 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 [] { 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"); return new Node[] { an }; 321 } 322 323 } 325 326 private static final class ModulesChildren extends Children.Keys<Object > { 327 private Object [] modules; 328 public ModulesChildren(Object [] m) { 329 modules = m; 330 } 331 332 protected void addNotify() { 333 setKeys(modules); 334 } 335 336 protected Node[] createNodes(Object key) { 337 AbstractNode an = new AbstractNode(Children.LEAF); 338 an.setName((String )key); 339 an.setIconBaseWithExtension("org/netbeans/modules/uihandler/module.gif"); return new Node[] { an }; 341 } 342 343 } 345 private static String afterLastDot(String 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 |