1 19 20 package org.netbeans.modules.palette; 21 22 import java.awt.Image ; 23 import java.awt.datatransfer.DataFlavor ; 24 import java.awt.datatransfer.Transferable ; 25 import java.awt.datatransfer.UnsupportedFlavorException ; 26 import java.beans.BeanInfo ; 27 import java.io.IOException ; 28 import java.util.ArrayList ; 29 import org.openide.ErrorManager; 30 import org.openide.loaders.DataNode; 31 import org.openide.nodes.FilterNode; 32 import org.openide.nodes.Node; 33 import org.openide.text.ActiveEditorDrop; 34 import org.openide.util.HelpCtx; 35 import org.openide.util.Lookup; 36 import org.openide.util.NbBundle; 37 import org.openide.util.Utilities; 38 import org.openide.util.datatransfer.ExTransferable; 39 import org.openide.util.lookup.AbstractLookup; 40 import org.openide.util.lookup.InstanceContent; 41 42 43 44 45 49 public final class PaletteItemNode extends FilterNode { 50 51 private static final Node.PropertySet[] NO_PROPERTIES = new Node.PropertySet[0]; 52 53 private String name; 54 private String bundleName; 55 private String displayNameKey; 56 private String className; 57 private String tooltipKey; 58 private String icon16URL; 59 private String icon32URL; 60 61 private String displayName; 62 private String description; 63 private Image icon16; 64 private Image icon32; 65 66 PaletteItemNode(DataNode original, 67 String name, 68 String bundleName, 69 String displayNameKey, 70 String className, 71 String tooltipKey, 72 String icon16URL, 73 String icon32URL, 74 InstanceContent content) 75 { 76 super(original, Children.LEAF, new AbstractLookup(content)); 77 78 content.add( this ); 79 this.name = name; 80 this.bundleName = bundleName; 81 this.displayNameKey = displayNameKey; 82 this.className = className; 83 this.tooltipKey = tooltipKey; 84 this.icon16URL = icon16URL; 85 this.icon32URL = icon32URL; 86 } 87 88 PaletteItemNode(DataNode original, 89 String name, 90 String displayName, 91 String tooltip, 92 String icon16URL, 93 String icon32URL, 94 InstanceContent content) 95 { 96 super(original, Children.LEAF, new AbstractLookup(content)); 97 98 content.add( this ); 99 this.name = name; 100 this.bundleName = bundleName; 101 assert null != displayName; 102 this.displayName = displayName; 103 this.description = tooltip; 104 if( null == this.description ) 105 description = displayName; 106 this.icon16URL = icon16URL; 107 this.icon32URL = icon32URL; 108 } 109 110 public String getName() { 111 return name; 112 } 113 114 public String getDisplayName() { 115 if (displayName == null) 116 displayName = _getDisplayName(bundleName, displayNameKey, className); 117 118 return displayName; 119 } 120 121 public String getShortDescription() { 122 if (description == null) 123 description = _getShortDescription(bundleName, tooltipKey, className, displayNameKey); 124 125 return description; 126 } 127 128 public Image getIcon(int type) { 129 130 Image icon = null; 131 132 if (type == BeanInfo.ICON_COLOR_16x16 || type == BeanInfo.ICON_MONO_16x16) { 133 if (icon16 == null) { 134 icon16 = _getIcon(icon16URL); 135 if (icon16 == null) 136 icon16 = Utilities.loadImage("org/netbeans/modules/palette/resources/unknown16.gif"); } 138 icon = icon16; 139 } 140 else if (type == BeanInfo.ICON_COLOR_32x32 || type == BeanInfo.ICON_MONO_32x32) { 141 if (icon32 == null) { 142 icon32 = _getIcon(icon32URL); 143 if (icon32 == null) 144 icon32 = Utilities.loadImage("org/netbeans/modules/palette/resources/unknown32.gif"); } 146 icon = icon32; 147 } 148 149 return icon; 150 } 151 152 public boolean canRename() { 153 return false; 154 } 155 156 public Node.PropertySet[] getPropertySets() { 158 return NO_PROPERTIES; 159 } 160 161 public Transferable clipboardCopy() throws IOException { 162 163 ExTransferable t = ExTransferable.create( super.clipboardCopy() ); 164 165 Lookup lookup = getLookup(); 166 ActiveEditorDrop drop = (ActiveEditorDrop) lookup.lookup(ActiveEditorDrop.class); 167 ActiveEditorDropTransferable s = new ActiveEditorDropTransferable(drop); 168 t.put(s); 169 170 return new NoExternalDndTransferable( t ); 173 } 174 175 public Transferable drag() throws IOException { 176 return clipboardCopy(); 177 } 178 179 private static class ActiveEditorDropTransferable extends ExTransferable.Single { 180 181 private ActiveEditorDrop drop; 182 183 ActiveEditorDropTransferable(ActiveEditorDrop drop) { 184 super(ActiveEditorDrop.FLAVOR); 185 186 this.drop = drop; 187 } 188 189 public Object getData () { 190 return drop; 191 } 192 193 } 194 195 public String _getDisplayName( 196 String bundleName, 197 String displayNameKey, 198 String instanceName) 199 { 200 201 String displayName = null; 202 try { 203 displayName = NbBundle.getBundle(bundleName).getString(displayNameKey); 204 205 if (displayName == null && displayNameKey != null) 206 displayName = displayNameKey; 207 208 if (displayName == null) { if (instanceName != null && instanceName.trim().length() > 0) { 210 int dotIndex = instanceName.lastIndexOf('.'); displayName = instanceName.substring(dotIndex); 212 } 213 } 214 215 if (displayName == null) displayName = name; 217 218 } 219 catch (Exception ex) { 220 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 221 } 222 223 return (displayName == null ? "" : displayName); 224 } 225 226 public String _getShortDescription( 227 String bundleName, 228 String tooltipKey, 229 String instanceName, 230 String displayNameKey) 231 { 232 233 String tooltip = null; 234 try { 235 tooltip = NbBundle.getBundle(bundleName).getString(tooltipKey); 236 237 if (tooltip == null && tooltipKey != null) 238 tooltip = tooltipKey; 239 240 if (tooltip == null) { if (instanceName != null && instanceName.trim().length() > 0) { 242 int dotIndex = instanceName.indexOf('.'); tooltip = instanceName.substring(0, dotIndex).replace('-', '.'); } 245 } 246 247 if (tooltip == null) tooltip = _getDisplayName(bundleName, displayNameKey, instanceName); 249 250 } 251 catch (Exception ex) { 252 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 253 } 254 255 return (tooltip == null ? "" : tooltip); 256 } 257 258 public Image _getIcon(String iconURL) { 259 260 Image icon = null; 261 try { 262 icon = Utilities.loadImage(iconURL); 263 } 264 catch (Exception ex) { 265 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 266 } 267 268 return icon; 269 } 270 271 public HelpCtx getHelpCtx() { 272 DataNode dn = (DataNode) getOriginal(); 273 Object helpId = dn.getDataObject().getPrimaryFile().getAttribute("helpId"); return (helpId == null ? super.getHelpCtx() : new HelpCtx(helpId.toString())); 275 } 276 277 281 private static class NoExternalDndTransferable implements Transferable { 282 private Transferable t; 283 private DataFlavor uriListFlavor; 284 public NoExternalDndTransferable( Transferable t ) { 285 this.t = t; 286 } 287 288 public DataFlavor [] getTransferDataFlavors() { 289 DataFlavor [] flavors = t.getTransferDataFlavors(); 290 if( t.isDataFlavorSupported( DataFlavor.javaFileListFlavor ) 291 || t.isDataFlavorSupported( getUriListFlavor() ) ) { 292 ArrayList <DataFlavor > tmp = new ArrayList <DataFlavor >( flavors.length ); 293 for( int i=0; i<flavors.length; i++ ) { 294 if( isDataFlavorSupported( flavors[i] ) ) 295 tmp.add( flavors[i] ); 296 } 297 flavors = tmp.toArray( new DataFlavor [tmp.size()] ); 298 } 299 return flavors; 300 } 301 302 public boolean isDataFlavorSupported( DataFlavor flavor ) { 303 if( DataFlavor.javaFileListFlavor.equals( flavor ) || getUriListFlavor().equals( flavor ) ) 304 return false; 305 return t.isDataFlavorSupported(flavor); 306 } 307 308 public Object getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException , IOException { 309 if( !isDataFlavorSupported(flavor) ) 310 throw new UnsupportedFlavorException ( flavor ); 311 return t.getTransferData( flavor ); 312 } 313 314 private DataFlavor getUriListFlavor () { 315 if( null == uriListFlavor ) { 316 try { 317 uriListFlavor = new DataFlavor ("text/uri-list;class=java.lang.String"); 318 } catch (ClassNotFoundException ex) { 319 throw new AssertionError (ex); 321 } 322 } 323 return uriListFlavor; 324 } 325 } 326 } 327 | Popular Tags |