KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > customizer > NamespaceChildren


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.xam.ui.customizer;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileUtil;
29 import org.openide.loaders.DataObject;
30 import org.openide.loaders.DataObjectNotFoundException;
31 import org.openide.nodes.Children;
32 import org.openide.nodes.Node;
33 import org.openide.util.NbBundle;
34 import org.openide.util.RequestProcessor;
35 import org.netbeans.modules.xml.retriever.catalog.Utilities;
36 import org.netbeans.modules.xml.xam.Model;
37
38 /**
39  * Represents the collection of files belonging to a common namespace.
40  *
41  * @author Ajit Bhate
42  */

43 public class NamespaceChildren extends Children.Keys {
44     /** Map of namespace to a list of files in that namespace. */
45     private HashMap JavaDoc<String JavaDoc, List JavaDoc<FileObject>> nsFilesMap;
46     /** Set of folders containing referencable files. */
47     private FileObject[] rootFolders;
48     /** Controls the appearance of child nodes. */
49     private ExternalReferenceDecorator decorator;
50
51     /**
52      * Creates a new instance of NamespaceChildren.
53      *
54      * @param roots set of root folders.
55      * @param decorator used to decorate the nodes.
56      */

57     public NamespaceChildren(FileObject[] roots,
58             ExternalReferenceDecorator decorator) {
59         super();
60         rootFolders = roots;
61         this.decorator = decorator;
62         nsFilesMap = new HashMap JavaDoc<String JavaDoc, List JavaDoc<FileObject>>();
63     }
64
65     protected Node[] createNodes(Object JavaDoc key) {
66         if (key == WaitNode.WAIT_KEY) {
67             return WaitNode.createNode();
68         } else if (key instanceof String JavaDoc) {
69             List JavaDoc<FileObject> fobjs = nsFilesMap.get(key);
70             if (fobjs != null && !fobjs.isEmpty()) {
71                 Node[] filterNodes = new Node[fobjs.size()];
72                 int i = 0;
73                 for (FileObject fobj:fobjs) {
74                     try {
75                         Node node = DataObject.find(fobj).getNodeDelegate();
76                         filterNodes[i++] = decorator.createExternalReferenceNode(node);
77                     } catch (DataObjectNotFoundException donfe) {
78                     }
79                 }
80                 Children.Array children = new Children.Array();
81                 children.add(filterNodes);
82                 Node node = new NamespaceNode(children, (String JavaDoc) key, decorator);
83                 return new Node[] { node };
84             }
85         }
86         return new Node[] { };
87     }
88
89     protected void addNotify() {
90         super.addNotify();
91         setKeys(WaitNode.getKeys());
92         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
93             public void run() {
94                 for (FileObject root : rootFolders) {
95                     java.util.Map JavaDoc<FileObject, String JavaDoc> map =
96                             Utilities.getFiles2NSMappingInProj(
97                             FileUtil.toFile(root), decorator.getDocumentType());
98                     for (java.util.Map.Entry<FileObject, String JavaDoc> entry : map.entrySet()) {
99                         String JavaDoc ns = entry.getValue();
100                         List JavaDoc<FileObject> fobjs = nsFilesMap.get(ns);
101                         if (fobjs == null) {
102                             fobjs = new ArrayList JavaDoc<FileObject>();
103                         }
104                         fobjs.add(entry.getKey());
105                         nsFilesMap.put(ns, fobjs);
106                     }
107                 }
108                 // Set the keys on the EDT to avoid clobbering the JTree
109
// and causing an AIOOBE (issue 94498).
110
EventQueue.invokeLater(new Runnable JavaDoc() {
111                     public void run() {
112                         setKeys(nsFilesMap.keySet());
113                     }
114                 });
115             }
116         });
117     }
118
119     @Override JavaDoc
120     protected void removeNotify() {
121         setKeys(Collections.emptySet());
122     }
123
124     private static class NamespaceNode extends FolderNode
125             implements ExternalReferenceNode {
126         /** Controls the appearance of this node. */
127         private ExternalReferenceDecorator decorator;
128
129         NamespaceNode(Children children, String JavaDoc myNamespace,
130                 ExternalReferenceDecorator decorator) {
131             super(children);
132             this.decorator = decorator;
133             setName(myNamespace);
134             if (Utilities.NO_NAME_SPACE.equals(myNamespace)) {
135                 setDisplayName(NbBundle.getMessage(NamespaceNode.class,
136                         "LBL_NoTargetNamespace"));
137             }
138         }
139
140         public String JavaDoc getHtmlDisplayName() {
141             String JavaDoc name = getDisplayName();
142             if (decorator != null) {
143                 name = decorator.getHtmlDisplayName(name, this);
144             }
145             return name;
146         }
147
148         public Model getModel() {
149             return null;
150         }
151
152         public String JavaDoc getNamespace() {
153             // Our name is our namespace.
154
return getName();
155         }
156     
157         public boolean hasModel() {
158             return false;
159         }
160     }
161 }
162
Popular Tags