1 19 20 package org.netbeans.modules.form.palette; 21 22 import java.util.*; 23 import java.io.*; 24 import java.beans.*; 25 import javax.swing.Action ; 26 27 import org.xml.sax.*; 28 import org.xml.sax.helpers.DefaultHandler ; 29 30 import org.openide.loaders.*; 31 import org.openide.filesystems.*; 32 import org.openide.nodes.*; 33 import org.openide.xml.XMLUtil; 34 import org.openide.actions.*; 35 import org.openide.util.Utilities; 36 import org.openide.util.NbBundle; 37 import org.openide.ErrorManager; 38 39 import org.netbeans.modules.form.project.ClassSource; 40 41 47 48 class PaletteItemDataObject extends MultiDataObject { 49 50 static final String XML_ROOT = "palette_item"; static final String ATTR_VERSION = "version"; static final String TAG_COMPONENT = "component"; static final String ATTR_CLASSNAME = "classname"; static final String ATTR_TYPE = "type"; static final String TAG_CLASSPATH = "classpath"; static final String TAG_RESOURCE= "resource"; static final String ATTR_NAME = "name"; static final String TAG_DESCRIPTION = "description"; static final String ATTR_BUNDLE = "localizing-bundle"; static final String ATTR_DISPLAY_NAME_KEY = "display-name-key"; static final String ATTR_TOOLTIP_KEY = "tooltip-key"; static final String TAG_ICON16 = "icon16"; static final String ATTR_URL = "urlvalue"; static final String TAG_ICON32 = "icon32"; 69 private static final Node.PropertySet[] NO_PROPERTIES = new Node.PropertySet[0]; 70 71 private boolean fileLoaded; 73 private PaletteItem paletteItem; 74 75 private String displayName_key; 77 private String tooltip_key; 78 private String bundleName; 79 private String icon16URL; 80 private String icon32URL; 81 82 String displayName; 84 String tooltip; 85 java.awt.Image icon16; 86 java.awt.Image icon32; 87 88 90 PaletteItemDataObject(FileObject fo, MultiFileLoader loader) 91 throws DataObjectExistsException 92 { 93 super(fo, loader); 94 } 95 96 boolean isFileRead() { 97 return fileLoaded; 98 } 99 100 boolean isItemValid() { 101 return paletteItem != null; 102 } 103 104 void reloadFile() { 105 if (paletteItem != null) { 106 paletteItem.reset(); 108 paletteItem.componentClassSource = null; 109 paletteItem.componentType_explicit = null; 111 } 112 113 displayName = null; 114 tooltip = null; 115 icon16 = null; 116 icon32 = null; 117 118 displayName_key = null; 119 tooltip_key = null; 120 bundleName = null; 121 icon16URL = null; 122 icon32URL = null; 123 124 loadFile(); 125 } 126 127 129 public Node createNodeDelegate() { 130 return new ItemNode(); 131 } 132 133 public Node.Cookie getCookie(Class cookieClass) { 134 if (PaletteItem.class.equals(cookieClass)) { 135 if (!fileLoaded) 136 loadFile(); 137 return paletteItem; 138 } 139 return super.getCookie(cookieClass); 140 } 141 142 144 private void loadFile() { 145 fileLoaded = true; 146 PaletteItem item = paletteItem; 147 if (item == null) 148 item = new PaletteItem(this); 149 150 FileObject file = getPrimaryFile(); 151 if (file.getSize() == 0L) { item.setComponentClassSource(file.getName().replace('-', '.'), 154 null, null); 155 paletteItem = item; 156 return; 157 } 158 159 try { 161 XMLReader reader = XMLUtil.createXMLReader(); 162 PaletteItemHandler handler = new PaletteItemHandler(); 163 reader.setContentHandler(handler); 164 InputSource input = new InputSource(getPrimaryFile().getURL().toExternalForm()); 165 reader.parse(input); 166 168 item.setComponentExplicitType(handler.componentExplicitType); 169 if (handler.componentClassName != null || displayName_key != null) { 170 String [] cpTypes; 171 String [] cpNames; 172 if (handler.cpTypeList.size() > 0) { 173 cpTypes = new String [handler.cpTypeList.size()]; 174 handler.cpTypeList.toArray(cpTypes); 175 cpNames = new String [handler.cpNameList.size()]; 176 handler.cpNameList.toArray(cpNames); 177 } else { 178 cpTypes = cpNames = null; 179 } 180 181 item.setComponentClassSource(handler.componentClassName, cpTypes, cpNames); 182 183 paletteItem = item; 184 } 185 } catch (SAXException saxex) { 186 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, saxex); 187 } catch (IOException ioex) { 188 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioex); 189 } 190 } 191 192 198 static void createFile(FileObject folder, ClassSource classSource) 199 throws IOException 200 { 201 String classname = classSource.getClassName(); 202 203 int idx = classname.lastIndexOf('.'); 204 String fileName = FileUtil.findFreeFileName( 205 folder, 206 idx >= 0 ? classname.substring(idx+1) : classname, 207 PaletteItemDataLoader.ITEM_EXT); 208 209 FileObject itemFile = folder.createData(fileName, 210 PaletteItemDataLoader.ITEM_EXT); 211 212 StringBuffer buff = new StringBuffer (512); 213 buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"); buff.append("<palette_item version=\"1.0\">\n"); buff.append(" <component classname=\""); buff.append(classname); 217 buff.append("\" />\n"); buff.append(" <classpath>\n"); for (int i=0, n=classSource.getCPRootCount(); i < n; i++) { 220 buff.append(" <resource type=\""); buff.append(classSource.getCPRootType(i)); 222 buff.append("\" name=\""); buff.append(classSource.getCPRootName(i)); 224 buff.append("\" />\n"); buff.append(" </classpath>\n"); buff.append("</palette_item>\n"); } 228 229 FileLock lock = itemFile.lock(); 230 OutputStream os = itemFile.getOutputStream(lock); 231 try { 232 os.write(buff.toString().getBytes()); 233 } 234 finally { 235 os.close(); 236 lock.releaseLock(); 237 } 238 } 239 240 242 243 public static final class PaletteItemDataLoader extends UniFileLoader { 244 245 static final String ITEM_EXT = "palette_item"; 247 PaletteItemDataLoader() { 248 super("org.netbeans.modules.form.palette.PaletteItemDataObject"); 250 ExtensionList ext = new ExtensionList(); 251 ext.addExtension(ITEM_EXT); 252 setExtensions(ext); 253 } 254 255 256 protected String defaultDisplayName() { 257 return NbBundle.getBundle(PaletteItemDataObject.class) 258 .getString("PROP_PaletteItemLoader_Name"); } 260 261 262 protected MultiDataObject createMultiObject(FileObject primaryFile) 263 throws DataObjectExistsException, IOException 264 { 265 return new PaletteItemDataObject(primaryFile, this); 266 } 267 } 268 269 public static final class PaletteItemDataLoaderBeanInfo extends SimpleBeanInfo { 270 private static String iconURL = "org/netbeans/modules/form/resources/palette_manager.png"; 272 public BeanInfo[] getAdditionalBeanInfo() { 273 try { 274 return new BeanInfo[] { Introspector.getBeanInfo(UniFileLoader.class) }; 275 } catch (IntrospectionException ie) { 276 org.openide.ErrorManager.getDefault().notify(ie); 277 return null; 278 } 279 } 280 281 public java.awt.Image getIcon(final int type) { 282 return Utilities.loadImage(iconURL); 283 } 284 285 } 286 287 289 290 class ItemNode extends DataNode { 291 292 ItemNode() { 293 super(PaletteItemDataObject.this, Children.LEAF); 294 } 295 296 public String getDisplayName() { 297 if (!fileLoaded) 298 loadFile(); 299 300 if (displayName == null) { 301 displayName = getExplicitDisplayName(); 302 if (displayName == null) { if (isItemValid()) { 304 displayName = paletteItem.getDisplayName(); 305 if (displayName == null) { String classname = paletteItem.getComponentClassName(); 307 if (classname != null) { 308 int i = classname.lastIndexOf('.'); displayName = i >= 0 ? 310 classname.substring(i+1) : classname; 311 } 312 } 313 } 314 if (displayName == null) displayName = super.getDisplayName(); 316 } 317 } 318 return displayName; 319 } 320 321 public String getShortDescription() { 322 if (!fileLoaded) 323 loadFile(); 324 325 if (tooltip == null) { 326 tooltip = getExplicitTooltip(); 327 if (tooltip == null) { if (isItemValid()) { 329 tooltip = paletteItem.getTooltip(); 330 if (tooltip == null) tooltip = paletteItem.getComponentClassName(); 332 } 333 if (tooltip == null) tooltip = getDisplayName(); 335 } 336 } 337 return tooltip; 338 } 339 340 public boolean canRename() { 341 return false; 342 } 343 344 public java.awt.Image getIcon(int type) { 345 if (!fileLoaded) 346 loadFile(); 347 348 if (type == BeanInfo.ICON_COLOR_32x32 349 || type == BeanInfo.ICON_MONO_32x32) 350 { 351 if (icon32 == null) { 352 icon32 = getExplicitIcon(type); 353 if (icon32 == null && isItemValid()) 354 icon32 = paletteItem.getIcon(type); 355 if (icon32 == null) 356 icon32 = Utilities.loadImage("org/netbeans/modules/form/resources/palette/unknown32.gif"); } 358 return icon32; 359 } 360 else { if (icon16 == null) { 362 icon16 = getExplicitIcon(type); 363 if (icon16 == null && isItemValid()) 364 icon16 = paletteItem.getIcon(type); 365 if (icon16 == null) 366 icon16 = Utilities.loadImage("org/netbeans/modules/form/resources/palette/unknown.gif"); } 368 return icon16; 369 } 370 } 372 373 public Node.PropertySet[] getPropertySets() { 375 return NO_PROPERTIES; 376 } 377 378 380 private String getExplicitDisplayName() { 381 String displayName = null; 382 if (displayName_key != null) { 383 if (bundleName != null) { 384 try { 385 displayName = NbBundle.getBundle(bundleName) 386 .getString(displayName_key); 387 } 388 catch (Exception ex) {} } 390 if (displayName == null) 391 displayName = displayName_key; 392 } 393 return displayName; 394 } 395 396 private String getExplicitTooltip() { 397 String tooltip = null; 398 if (tooltip_key != null) { 399 if (bundleName != null) { 400 try { 401 tooltip = NbBundle.getBundle(bundleName) 402 .getString(tooltip_key); 403 } 404 catch (Exception ex) {} } 406 if (tooltip == null) 407 tooltip = tooltip_key; 408 } 409 return tooltip; 410 } 411 412 private java.awt.Image getExplicitIcon(int type) { 413 if (type == BeanInfo.ICON_COLOR_32x32 414 || type == BeanInfo.ICON_MONO_32x32) 415 { 416 if (icon32URL != null) { try { 418 return java.awt.Toolkit.getDefaultToolkit().getImage( 419 new java.net.URL (icon32URL)); 420 } 421 catch (java.net.MalformedURLException ex) {} } 423 else if (getPrimaryFile().getAttribute("SystemFileSystem.icon32") != null) return super.getIcon(type); 425 } 426 else { if (icon16URL != null) { try { 429 return java.awt.Toolkit.getDefaultToolkit().getImage( 430 new java.net.URL (icon16URL)); 431 } 432 catch (java.net.MalformedURLException ex) {} } 434 else if (getPrimaryFile().getAttribute("SystemFileSystem.icon") != null) return super.getIcon(type); 436 } 437 return null; 438 } 439 } 440 441 private class PaletteItemHandler extends DefaultHandler { 442 List cpTypeList; List cpNameList; String componentClassName; 445 String componentExplicitType; 446 447 public void startDocument() throws SAXException { 448 cpTypeList = new ArrayList(); 449 cpNameList = new ArrayList(); 450 componentClassName = null; 451 componentExplicitType = null; 452 } 453 454 public void startElement(String uri, String localName, String qName, 455 Attributes attributes) throws SAXException { 456 if (XML_ROOT.equals(qName)) { 457 String version = attributes.getValue(ATTR_VERSION); 458 if (version == null) { 459 String message = NbBundle.getBundle(PaletteItemDataObject.class) 460 .getString("MSG_UnknownPaletteItemVersion"); throw new SAXException(message); 462 } else if (!version.startsWith("1.")) { String message = NbBundle.getBundle(PaletteItemDataObject.class) 464 .getString("MSG_UnsupportedPaletteItemVersion"); throw new SAXException(message); 466 } 467 } else if (TAG_COMPONENT.equals(qName)) { 469 String className = attributes.getValue(ATTR_CLASSNAME); 470 componentClassName = className; 471 componentExplicitType = attributes.getValue(ATTR_TYPE); 472 } else if (TAG_CLASSPATH.equals(qName)) { 473 } else if (TAG_RESOURCE.equals(qName)) { 475 String type = attributes.getValue(ATTR_TYPE); 476 String name = attributes.getValue(ATTR_NAME); 477 if ((type != null) && (name != null)) { 478 cpTypeList.add(type); 479 cpNameList.add(name); 480 } 481 } else if (TAG_DESCRIPTION.equals(qName)) { 482 String bundle = attributes.getValue(ATTR_BUNDLE); 483 if (bundle != null) { 484 PaletteItemDataObject.this.bundleName = bundle; 485 } 486 String displayNameKey = attributes.getValue(ATTR_DISPLAY_NAME_KEY); 487 if (displayNameKey != null) { 488 PaletteItemDataObject.this.displayName_key = displayNameKey; 489 } 490 String tooltipKey = attributes.getValue(ATTR_TOOLTIP_KEY); 491 if (tooltipKey != null) { 492 PaletteItemDataObject.this.tooltip_key = tooltipKey; 493 } 494 } else if (TAG_ICON16.equals(qName)) { 495 String url = attributes.getValue(ATTR_URL); 496 if (url != null) { 497 PaletteItemDataObject.this.icon16URL = url; 498 } 499 } else if (TAG_ICON32.equals(qName)) { 501 String url = attributes.getValue(ATTR_URL); 502 if (url != null) { 503 PaletteItemDataObject.this.icon32URL = url; 504 } 505 } 507 } 508 } 509 510 } 511 | Popular Tags |