1 19 20 package org.netbeans.modules.form.palette; 21 22 import java.beans.*; 23 import java.awt.Image ; 24 25 import org.openide.ErrorManager; 26 import org.openide.nodes.Node; 27 28 import org.netbeans.modules.form.FormUtils; 29 import org.netbeans.modules.form.project.*; 30 31 37 38 public final class PaletteItem implements Node.Cookie { 39 40 private PaletteItemDataObject itemDataObject; 41 42 ClassSource componentClassSource; 44 String componentType_explicit; 46 47 private Class componentClass; 49 private Throwable lastError; private int componentType = -1; 52 53 private static final int LAYOUT = 1; 55 private static final int BORDER = 2; 56 private static final int VISUAL = 4; private static final int MENU = 8; private static final int TYPE_MASK = 15; 59 60 62 PaletteItem(PaletteItemDataObject dobj) { 63 itemDataObject = dobj; 64 } 65 66 public PaletteItem(ClassSource componentClassSource) { 67 this.componentClassSource = componentClassSource; 68 } 69 70 public void setComponentClassSource(String className, 71 String [] cpTypes, 72 String [] cpNames) 73 { 74 componentClass = null; 75 lastError = null; 76 componentType = -1; 77 componentClassSource = new ClassSource(className, cpTypes, cpNames); 78 } 79 80 void setComponentExplicitType(String type) { 81 componentType_explicit = type; 82 } 83 84 86 87 public Node getNode() { 88 return (itemDataObject == null) ? null : itemDataObject.getNodeDelegate(); 89 } 90 91 92 public String getId() { 93 return getComponentClassName(); 94 } 95 96 public String getComponentClassName() { 97 return componentClassSource.getClassName(); 98 } 99 100 public ClassSource getComponentClassSource() { 101 return componentClassSource; 102 } 103 104 106 public Class getComponentClass() { 107 if (componentClass == null && lastError == null) 108 componentClass = loadComponentClass(); 109 return componentClass; 110 } 111 112 114 public Throwable getError() { 115 return lastError; 116 } 117 118 120 public String getExplicitComponentType() { 121 return componentType_explicit; 122 } 123 124 126 public boolean isVisual() { 127 if (componentType == -1) 128 resolveComponentType(); 129 return (componentType & VISUAL) != 0; 130 } 131 132 133 public boolean isMenu() { 134 if (componentType == -1) 135 resolveComponentType(); 136 return (componentType & MENU) != 0; 137 } 138 139 141 public boolean isLayout() { 142 if (componentType == -1) 143 resolveComponentType(); 144 return (componentType & TYPE_MASK) == LAYOUT; 145 } 146 147 149 public boolean isBorder() { 150 if (componentType == -1) 151 resolveComponentType(); 152 return (componentType & TYPE_MASK) == BORDER; 153 } 154 155 175 public String toString() { 176 return PaletteUtils.getItemComponentDescription(this); 177 } 178 179 String getDisplayName() { 180 BeanDescriptor bd = getBeanDescriptor(); 181 return bd != null ? bd.getDisplayName() : null; 182 } 183 184 String getTooltip() { 185 BeanDescriptor bd = getBeanDescriptor(); 186 return bd != null ? bd.getShortDescription() : null; 187 } 188 189 Image getIcon(int type) { 190 BeanInfo bi = getBeanInfo(); 191 return bi != null ? bi.getIcon(type) : null; 192 } 193 194 void reset() { 195 componentClass = null; 196 lastError = null; 197 componentType = -1; 199 200 itemDataObject.displayName = null; 201 itemDataObject.tooltip = null; 202 itemDataObject.icon16 = null; 203 itemDataObject.icon32 = null; 204 } 205 206 208 private Class loadComponentClass() { 209 try { 210 return FormUtils.loadSystemClass(getComponentClassName()); 211 } catch (ClassNotFoundException cnfex) {} 212 213 try { 214 return ClassPathUtils.loadClass(getComponentClassSource()); 215 } 216 catch (Exception ex) { 217 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 218 lastError = ex; 219 } 220 catch (LinkageError ex) { 221 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 222 lastError = ex; 223 } 224 return null; 225 } 226 227 private BeanInfo getBeanInfo() { 228 Class compClass = getComponentClass(); 229 if (compClass != null) { 230 try { 231 return FormUtils.getBeanInfo(compClass); 232 } 233 catch (Exception ex) {} catch (Error er) {} } 237 return null; 238 } 239 240 private BeanDescriptor getBeanDescriptor() { 241 Class compClass = getComponentClass(); 242 if (compClass != null) { 243 try { 244 return FormUtils.getBeanInfo(compClass).getBeanDescriptor(); 245 } 246 catch (Exception ex) {} catch (Error er) {} } 250 return null; 251 } 252 253 private void resolveComponentType() { 254 if (componentType_explicit == null) { 255 componentType = 0; 256 257 Class compClass = getComponentClass(); 258 if (compClass == null) 259 return; 260 261 if (java.awt.LayoutManager .class.isAssignableFrom(compClass)) { 262 componentType = LAYOUT; 264 return; 265 } 266 267 if (javax.swing.border.Border .class.isAssignableFrom(compClass)) { 268 componentType = BORDER; 269 return; 270 } 271 272 if (java.awt.Component .class.isAssignableFrom(compClass)) 273 componentType |= VISUAL; 274 275 if (java.awt.MenuComponent .class.isAssignableFrom(compClass) 276 || javax.swing.JMenuItem .class.isAssignableFrom(compClass) 277 || javax.swing.JMenuBar .class.isAssignableFrom(compClass) 278 || javax.swing.JPopupMenu .class.isAssignableFrom(compClass)) 279 componentType |= MENU; 280 } 281 else if ("visual".equalsIgnoreCase(componentType_explicit)) componentType = VISUAL; 283 else if ("layout".equalsIgnoreCase(componentType_explicit)) componentType = LAYOUT; 285 else if ("border".equalsIgnoreCase(componentType_explicit)) componentType = BORDER; 287 else if ("menu".equalsIgnoreCase(componentType_explicit)) componentType = MENU | VISUAL; 289 else 290 componentType = 0; 291 } 292 } 293 | Popular Tags |