1 19 20 package org.netbeans.modules.xml.wsdl.ui.view.treeeditor; 21 22 import java.awt.Image ; 23 import java.awt.datatransfer.Transferable ; 24 import java.beans.BeanInfo ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.Set ; 30 import java.util.concurrent.atomic.AtomicReference ; 31 32 import javax.swing.Action ; 33 34 import org.netbeans.modules.xml.wsdl.model.WSDLComponent; 35 import org.netbeans.modules.xml.wsdl.ui.actions.ActionHelper; 36 import org.netbeans.modules.xml.wsdl.ui.cookies.SaveCookieDelegate; 37 import org.netbeans.modules.xml.xam.Component; 38 import org.netbeans.modules.xml.xam.Model; 39 import org.netbeans.modules.xml.xam.ui.ComponentPasteType; 40 import org.netbeans.modules.xml.xam.ui.XAMUtils; 41 import org.netbeans.modules.xml.xam.ui.cookies.CountChildrenCookie; 42 import org.netbeans.modules.xml.xam.ui.highlight.Highlight; 43 import org.netbeans.modules.xml.xam.ui.highlight.HighlightManager; 44 import org.netbeans.modules.xml.xam.ui.highlight.Highlighted; 45 import org.openide.actions.NewAction; 46 import org.openide.actions.PasteAction; 47 import org.openide.filesystems.Repository; 48 import org.openide.loaders.DataFolder; 49 import org.openide.loaders.DataObject; 50 import org.openide.nodes.AbstractNode; 51 import org.openide.nodes.Children; 52 import org.openide.nodes.Node; 53 import org.openide.util.HelpCtx; 54 import org.openide.util.Utilities; 55 import org.openide.util.actions.SystemAction; 56 import org.openide.util.datatransfer.PasteType; 57 import org.openide.util.lookup.AbstractLookup; 58 import org.openide.util.lookup.InstanceContent; 59 60 63 public abstract class FolderNode extends AbstractNode 64 implements Highlighted, Node.Cookie, CountChildrenCookie { 65 private Set <Component> referenceSet; 66 67 private List <Highlight> highlights; 68 private Class <? extends WSDLComponent> childType; 69 private WSDLComponent mElement; 70 private InstanceContent mLookupContents; 71 protected Image BADGE_ICON; 72 73 private static final SystemAction[] ACTIONS = new SystemAction[] { 74 SystemAction.get(PasteAction.class), 75 null, 76 SystemAction.get(NewAction.class), 77 }; 78 79 protected FolderNode(Children children, WSDLComponent comp, 80 Class <? extends WSDLComponent> childType) { 81 this(children, new InstanceContent(), comp, childType); 82 } 83 84 protected FolderNode(Children children, InstanceContent contents, 85 WSDLComponent comp, Class <? extends WSDLComponent> childType) { 86 super(children, new AbstractLookup(contents)); 87 mLookupContents = contents; 88 this.childType = childType; 89 this.mElement = comp; 90 contents.add(this); 91 DataObject dobj = ActionHelper.getDataObject(comp); 92 if (dobj != null) { 93 contents.add(dobj); 94 } 95 96 contents.add(new SaveCookieDelegate(dobj)); 97 98 referenceSet = new HashSet <Component>(); 99 highlights = new LinkedList <Highlight>(); 100 HighlightManager hm = HighlightManager.getDefault(); 101 List <? extends WSDLComponent> subcomps = comp.getChildren(childType); 104 Iterator <? extends WSDLComponent> iter = subcomps.iterator(); 105 while (iter.hasNext()) { 106 referenceSet.add(iter.next()); 107 } 108 hm.addHighlighted(this); 109 } 110 111 protected InstanceContent getLookupContents() { 112 return mLookupContents; 113 } 114 115 @Override 116 public Image getIcon(int type) { 117 Image folderIcon = FolderIcon.getClosedIcon(); 118 if (BADGE_ICON != null) { 119 return Utilities.mergeImages(folderIcon, BADGE_ICON, 8, 8); 120 } 121 return folderIcon; 122 } 123 124 @Override 125 public Image getOpenedIcon(int type) { 126 127 Image folderIcon = FolderIcon.getOpenedIcon(); 128 if (BADGE_ICON != null) { 129 return Utilities.mergeImages(folderIcon, BADGE_ICON, 8, 8); 130 } 131 return folderIcon; 132 } 133 134 @Override 135 public Action [] getActions(boolean context) { 136 return ACTIONS; 137 } 138 139 public abstract Class getType(); 140 141 146 public Class <? extends WSDLComponent> getChildType() { 147 return childType; 148 } 149 150 public int getChildCount() { 151 return mElement.getChildren(getChildType()).size(); 152 } 153 154 160 161 protected boolean isEditable() { 162 Model model = mElement.getModel(); 163 return model != null && XAMUtils.isWritable(model); 164 } 165 166 167 @Override 168 public boolean canCopy() { 169 return false; 170 } 171 172 @Override 173 public boolean canCut() { 174 return isEditable(); 175 } 176 177 178 179 @Override 180 protected void createPasteTypes(Transferable transferable, List list) { 181 if (mElement != null && mElement.getModel() != null && isEditable()) { 183 PasteType type = ComponentPasteType.getPasteType( 184 mElement, transferable, childType); 185 if (type != null) { 186 list.add(type); 187 } 188 } 189 } 190 191 @Override 192 public PasteType getDropType(Transferable transferable, int action, int index) { 193 if (mElement != null && mElement.getModel() != null && isEditable()) { 195 PasteType type = ComponentPasteType.getDropType( 196 mElement, transferable, childType, action, index); 197 if (type != null) { 198 return type; 199 } 200 } 201 return null; 202 } 203 204 public boolean canHold(WSDLComponent comp) { 205 return getType().isInstance(comp); 206 } 207 208 public Set <Component> getComponents() { 209 return referenceSet; 210 } 211 212 public void highlightAdded(Highlight hl) { 213 highlights.add(hl); 214 fireDisplayNameChange("TempName", getDisplayName()); 215 } 216 217 public void highlightRemoved(Highlight hl) { 218 highlights.remove(hl); 219 fireDisplayNameChange("TempName", getDisplayName()); 220 } 221 222 230 protected String applyHighlights(String name) { 231 int count = highlights.size(); 232 if (count > 0) { 233 String code = null; 235 Highlight hl = highlights.get(count - 1); 236 String type = hl.getType(); 237 if (type.equals(Highlight.SEARCH_RESULT) || 238 type.equals(Highlight.SEARCH_RESULT_PARENT)) { 239 code = "ffc73c"; 242 } else if (type.equals(Highlight.FIND_USAGES_RESULT) || 243 type.equals(Highlight.FIND_USAGES_RESULT_PARENT)) { 244 code = "c7ff3c"; 248 } 249 250 name = "<strong><font color=\"#" + code + "\">" + name + 251 "</font></strong>"; 252 } 253 return name; 254 } 255 256 @Override 257 public String getHtmlDisplayName() { 258 String name = getDisplayName(); 259 if (name != null) { 261 name = name.replace("<", "<").replace(">", ">"); 262 } 263 return applyHighlights(name); 264 } 265 266 @Override 267 public HelpCtx getHelpCtx() { 268 return new HelpCtx(getClass().getName()); 269 } 270 271 277 public static class FolderIcon { 278 279 private static AtomicReference <Image > CLOSED_FOLDER_ICON = 280 new AtomicReference <Image >(); 281 282 private static AtomicReference <Image > OPENED_FOLDER_ICON = 283 new AtomicReference <Image >(); 284 285 private FolderIcon() { 286 } 287 288 public static Image getOpenedIcon() { 289 if (OPENED_FOLDER_ICON.get() == null) { 290 Image image = getSystemFolderImage(true); 291 OPENED_FOLDER_ICON.compareAndSet(null,image); 292 } 293 return OPENED_FOLDER_ICON.get(); 294 } 295 296 public static Image getClosedIcon() { 297 if (CLOSED_FOLDER_ICON.get() == null) { 298 Image image = getSystemFolderImage(false); 299 CLOSED_FOLDER_ICON.compareAndSet(null,image); 300 } 301 return CLOSED_FOLDER_ICON.get(); 302 } 303 304 private static Image getSystemFolderImage(boolean isOpened) { 305 Node n = DataFolder.findFolder(Repository.getDefault() 306 .getDefaultFileSystem().getRoot()).getNodeDelegate(); 307 return isOpened ? n.getOpenedIcon(BeanInfo.ICON_COLOR_16x16) : 308 n.getIcon(BeanInfo.ICON_COLOR_16x16); 309 } 310 311 public static Image getIcon(int type) { 312 return getSystemFolderImage(false); 313 } 314 315 public static Image getOpenedIcon(int type) { 316 return getSystemFolderImage(true); 317 } 318 } 319 } 320 | Popular Tags |