KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URISyntaxException JavaDoc;
26 import java.util.Collections JavaDoc;
27 import org.netbeans.modules.xml.xam.ModelSource;
28 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
29 import org.netbeans.modules.xml.retriever.catalog.CatalogEntry;
30 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
31 import org.netbeans.modules.xml.xam.Model;
32 import org.netbeans.modules.xml.xam.ui.ModelCookie;
33 import org.openide.filesystems.FileObject;
34 import org.openide.loaders.DataObjectNotFoundException;
35 import org.openide.nodes.AbstractNode;
36 import org.openide.nodes.Children;
37 import org.openide.nodes.Node;
38 import org.openide.util.NbBundle;
39 import org.openide.loaders.DataObject;
40
41 /**
42  *
43  * @author Ajit Bhate
44  */

45 public class RetrievedFilesChildren extends Children.Keys {
46     private CatalogWriteModel cwm;
47     private ExternalReferenceDecorator decorator;
48
49     /** Creates a new instance of Children */
50     public RetrievedFilesChildren(CatalogWriteModel cwm,
51             ExternalReferenceDecorator decorator) {
52         super();
53         this.cwm = cwm;
54         this.decorator = decorator;
55     }
56
57     protected Node[] createNodes(Object JavaDoc key) {
58         if (key == WaitNode.WAIT_KEY) {
59             return WaitNode.createNode();
60         } else if (key instanceof CatalogEntry) {
61             CatalogEntry entry = (CatalogEntry) key;
62             try {
63                 ModelSource modelSource = cwm.getModelSource(new URI JavaDoc(entry.getSource()));
64                 if (modelSource == null) {
65                     return new Node[0];
66                 }
67                 FileObject fobj = (FileObject) modelSource.
68                         getLookup().lookup(FileObject.class);
69                 if (fobj == null) {
70                     return new Node[0];
71                 }
72                 DataObject dobj = DataObject.find(fobj);
73                 if (dobj == null) {
74                     return new Node[0];
75                 }
76                 ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class);
77                 if (cookie == null) {
78                     return new Node[0];
79                 }
80                 Model model = cookie.getModel();
81                 if (model == null) {
82                     return new Node[0];
83                 }
84                 String JavaDoc targetNS = decorator.getNamespace(model);
85                 RetrievedFileNode childNode = new RetrievedFileNode(entry,
86                         targetNS, Children.LEAF, decorator);
87                 childNode.setDisplayName(entry.getSource());
88                 Children.Array children = new Children.Array();
89                 children.add(new Node[] { childNode });
90                 return new Node[] {
91                     new RetrievedFileNode(entry, targetNS, children, decorator)
92                 };
93             } catch (URISyntaxException JavaDoc urise) {
94             } catch (CatalogModelException cme) {
95             } catch (DataObjectNotFoundException donfe) {
96             } catch (IOException JavaDoc ioe) {
97             }
98         }
99         return new Node[0];
100     }
101
102     protected void addNotify() {
103         setKeys(WaitNode.getKeys());
104         EventQueue.invokeLater(new Runnable JavaDoc() {
105             public void run() {
106                 setKeys(cwm.getCatalogEntries());
107             }
108         });
109     }
110
111     @Override JavaDoc
112     protected void removeNotify() {
113         setKeys(Collections.emptySet());
114     }
115
116     public static class RetrievedFileNode extends AbstractNode
117             implements ExternalReferenceNode {
118         private boolean valid;
119         private String JavaDoc location;
120         private String JavaDoc targetNS;
121         private ExternalReferenceDecorator decorator;
122
123         /**
124          * Creates a new instance of RetrievedFileNode.
125          */

126         RetrievedFileNode(CatalogEntry entry, String JavaDoc targetNS,
127                 org.openide.nodes.Children children,
128                 ExternalReferenceDecorator decorator) {
129             super(children);
130             this.location = entry.getSource();
131             this.valid = entry.isValid();
132             this.targetNS = targetNS;
133             this.decorator = decorator;
134             setName(targetNS);
135             if (targetNS == null) {
136                 setDisplayName(NbBundle.getMessage(RetrievedFileNode.class,
137                         "LBL_NoTargetNamespace"));
138             }
139             setIconBaseWithExtension(
140                     "org/netbeans/modules/xml/xam/ui/customizer/Schema_File.png"); // NOI18N
141
}
142
143         public String JavaDoc getLocation() {
144             return location;
145         }
146
147         public String JavaDoc getNamespace() {
148             return targetNS;
149         }
150
151         public boolean isValid() {
152             return valid;
153         }
154
155         public String JavaDoc getHtmlDisplayName() {
156             String JavaDoc name = super.getHtmlDisplayName();
157             if (isValid()) {
158                 if (decorator != null) {
159                     if (name == null) {
160                         name = getDisplayName();
161                     }
162                     name = decorator.getHtmlDisplayName(name, this);
163                 }
164             } else {
165                 return "<s>" + name == null ? getDisplayName() : name + "</s>";
166             }
167             return name;
168         }
169
170         public Model getModel() {
171             return null;
172         }
173
174         public boolean hasModel() {
175             return false;
176         }
177     }
178 }
179
Popular Tags