KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > treeeditor > FolderNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.ui.view.treeeditor;
21
22 import java.awt.Image JavaDoc;
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import java.beans.BeanInfo JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.concurrent.atomic.AtomicReference JavaDoc;
31
32 import javax.swing.Action JavaDoc;
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 /**
61  * @author skini
62  */

63 public abstract class FolderNode extends AbstractNode
64         implements Highlighted, Node.Cookie, CountChildrenCookie {
65     private Set JavaDoc<Component> referenceSet;
66     /** Ordered list of highlights applied to this node. */
67     private List JavaDoc<Highlight> highlights;
68     private Class JavaDoc<? extends WSDLComponent> childType;
69     private WSDLComponent mElement;
70     private InstanceContent mLookupContents;
71     protected Image JavaDoc 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 JavaDoc<? extends WSDLComponent> childType) {
81         this(children, new InstanceContent(), comp, childType);
82     }
83
84     protected FolderNode(Children children, InstanceContent contents,
85             WSDLComponent comp, Class JavaDoc<? 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 JavaDoc<Component>();
99         highlights = new LinkedList JavaDoc<Highlight>();
100         HighlightManager hm = HighlightManager.getDefault();
101         // Must check for the existence of the highlight manager
102
// since the component selection panel does not highlight.
103
List JavaDoc<? extends WSDLComponent> subcomps = comp.getChildren(childType);
104         Iterator JavaDoc<? 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 JavaDoc
116     public Image JavaDoc getIcon(int type) {
117         Image JavaDoc 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 JavaDoc
125     public Image JavaDoc getOpenedIcon(int type) {
126        
127         Image JavaDoc 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 JavaDoc
135     public Action JavaDoc[] getActions(boolean context) {
136         return ACTIONS;
137     }
138
139     public abstract Class JavaDoc getType();
140
141     /**
142      * Gets the type of child nodes this folder contains.
143      *
144      * @return WSDL component type.
145      */

146     public Class JavaDoc<? extends WSDLComponent> getChildType() {
147         return childType;
148     }
149
150     public int getChildCount() {
151         return mElement.getChildren(getChildType()).size();
152     }
153
154     /**
155      * Determines if this node represents a component that is contained
156      * is editable
157      *
158      * @return true if component is editable, false otherwise.
159      */

160     
161     protected boolean isEditable() {
162         Model model = mElement.getModel();
163         return model != null && XAMUtils.isWritable(model);
164     }
165     
166     
167     @Override JavaDoc
168     public boolean canCopy() {
169         return false;
170     }
171
172     @Override JavaDoc
173     public boolean canCut() {
174         return isEditable();
175     }
176
177     
178     
179     @Override JavaDoc
180     protected void createPasteTypes(Transferable JavaDoc transferable, List JavaDoc list) {
181         // Make sure this node is still valid.
182
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 JavaDoc
192     public PasteType getDropType(Transferable JavaDoc transferable, int action, int index) {
193         // Make sure this node is still valid.
194
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 JavaDoc<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     /**
223      * Given a display name, add the appropriate HTML tags to highlight
224      * the display name as dictated by any Highlights associated with
225      * this node.
226      *
227      * @param name current display name.
228      * @return marked up display name.
229      */

230     protected String JavaDoc applyHighlights(String JavaDoc name) {
231         int count = highlights.size();
232         if (count > 0) {
233             // Apply the last highlight that was added to our list.
234
String JavaDoc code = null;
235             Highlight hl = highlights.get(count - 1);
236             String JavaDoc type = hl.getType();
237             if (type.equals(Highlight.SEARCH_RESULT) ||
238                     type.equals(Highlight.SEARCH_RESULT_PARENT)) {
239                 // Always use the parent color for search results, as
240
// a category cannot possibly be a search result.
241
code = "ffc73c";
242             } else if (type.equals(Highlight.FIND_USAGES_RESULT) ||
243                     type.equals(Highlight.FIND_USAGES_RESULT_PARENT)) {
244                 // Always use the parent color for search results, as
245
// a category cannot possibly be a search result.
246
// color = chartreuse
247
code = "c7ff3c";
248             }
249
250             name = "<strong><font color=\"#" + code + "\">" + name +
251                     "</font></strong>";
252         }
253         return name;
254     }
255
256     @Override JavaDoc
257     public String JavaDoc getHtmlDisplayName() {
258         String JavaDoc name = getDisplayName();
259         // Need to escape any HTML meta-characters in the name.
260
if (name != null) {
261             name = name.replace("<", "&lt;").replace(">", "&gt;");
262         }
263         return applyHighlights(name);
264     }
265
266     @Override JavaDoc
267     public HelpCtx getHelpCtx() {
268         return new HelpCtx(getClass().getName());
269     }
270     
271     /**
272      * Copied from bpel.
273      * @author Vitaly Bychkov
274      * @version 1.0
275      *
276      */

277     public static class FolderIcon {
278
279         private static AtomicReference JavaDoc<Image JavaDoc> CLOSED_FOLDER_ICON =
280                 new AtomicReference JavaDoc<Image JavaDoc>();
281         
282         private static AtomicReference JavaDoc<Image JavaDoc> OPENED_FOLDER_ICON =
283                 new AtomicReference JavaDoc<Image JavaDoc>();
284
285         private FolderIcon() {
286         }
287         
288         public static Image JavaDoc getOpenedIcon() {
289             if (OPENED_FOLDER_ICON.get() == null) {
290                 Image JavaDoc image = getSystemFolderImage(true);
291                 OPENED_FOLDER_ICON.compareAndSet(null,image);
292             }
293             return OPENED_FOLDER_ICON.get();
294         }
295         
296         public static Image JavaDoc getClosedIcon() {
297             if (CLOSED_FOLDER_ICON.get() == null) {
298                 Image JavaDoc image = getSystemFolderImage(false);
299                 CLOSED_FOLDER_ICON.compareAndSet(null,image);
300             }
301             return CLOSED_FOLDER_ICON.get();
302         }
303         
304         private static Image JavaDoc 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 JavaDoc getIcon(int type) {
312             return getSystemFolderImage(false);
313         }
314
315         public static Image JavaDoc getOpenedIcon(int type) {
316             return getSystemFolderImage(true);
317         }
318     }
319 }
320
Popular Tags