KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > catalog > CatalogEntryNode


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.catalog;
21
22 import java.beans.IntrospectionException JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import org.netbeans.modules.xml.catalog.lib.URLEnvironment;
29 import org.netbeans.modules.xml.catalog.spi.CatalogReader;
30 import org.netbeans.modules.xml.catalog.spi.CatalogWriter;
31 import org.netbeans.modules.xml.catalog.user.UserXMLCatalog;
32 import org.openide.ErrorManager;
33 import org.openide.NotifyDescriptor;
34 import org.openide.actions.DeleteAction;
35 import org.openide.actions.EditAction;
36 import org.openide.actions.PropertiesAction;
37 import org.openide.actions.ViewAction;
38 import org.openide.cookies.EditCookie;
39 import org.openide.cookies.ViewCookie;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.loaders.DataObject;
43 import org.openide.nodes.BeanNode;
44 import org.openide.nodes.Node;
45 import org.openide.text.CloneableEditor;
46 import org.openide.text.CloneableEditorSupport;
47 import org.openide.text.CloneableEditorSupport.Env;
48 import org.openide.util.HelpCtx;
49 import org.openide.util.actions.SystemAction;
50
51 /**
52  * Node representing single catalog entry. It can be viewed.
53  *
54  * @author Petr Kuzel
55  * @version 1.0
56  */

57 final class CatalogEntryNode extends BeanNode implements EditCookie {
58
59     // cached ViewCookie instance
60
private transient ViewCookie view;
61     private boolean isCatalogWriter;
62     private CatalogReader catalogReader;
63     
64     /** Creates new CatalogNode */
65     public CatalogEntryNode(CatalogEntry entry) throws IntrospectionException JavaDoc {
66         super(entry);
67         //getCookieSet().add(this);
68
catalogReader = entry.getCatalog();
69         if (catalogReader instanceof CatalogWriter) {
70             isCatalogWriter = true;
71         }
72     }
73     
74     public javax.swing.Action JavaDoc getPreferredAction() {
75         if (isCatalogWriter)
76             return SystemAction.get(EditAction.class);
77         else
78             return SystemAction.get(ViewAction.class);
79     }
80     
81     public void edit() {
82         UserXMLCatalog catalog = (UserXMLCatalog)getCatalogReader();
83         try {
84             java.net.URI JavaDoc uri = new java.net.URI JavaDoc(getSystemID());
85             File JavaDoc file = new File JavaDoc(uri);
86             FileObject fo = FileUtil.toFileObject(file);
87             boolean editPossible=false;
88             if (fo!=null) {
89                 DataObject obj = DataObject.find(fo);
90                 EditCookie editCookie = (EditCookie)obj.getCookie(EditCookie.class);
91                 if (editCookie!=null) {
92                     editPossible=true;
93                     editCookie.edit();
94                 }
95             }
96             if (!editPossible)
97                 org.openide.DialogDisplayer.getDefault().notify(
98                         new NotifyDescriptor.Message(
99                             Util.THIS.getString("MSG_CannotOpenURI",getSystemID()), //NOI18N
100
NotifyDescriptor.INFORMATION_MESSAGE));
101         } catch (Throwable JavaDoc ex) {
102             ErrorManager.getDefault().notify(ex);
103         }
104     }
105     
106     private CatalogReader getCatalogReader() {
107         return catalogReader;
108     }
109     
110     public Action JavaDoc[] getActions(boolean context) {
111         if (isCatalogWriter)
112             return new Action JavaDoc[] {
113                 SystemAction.get(EditAction.class),
114                 SystemAction.get(DeleteAction.class),
115                 null,
116                 SystemAction.get(PropertiesAction.class)
117             };
118         else
119             return new Action JavaDoc[] {
120                 SystemAction.get(ViewAction.class),
121                 null,
122                 SystemAction.get(PropertiesAction.class)
123             };
124     }
125     
126     /**
127      * Provide <code>ViewCookie</code>. Always provide same instance for
128      * entry until its system ID changes.
129      */

130     public Node.Cookie getCookie(Class JavaDoc clazz) {
131         
132         if (ViewCookie.class.equals(clazz)) {
133             
134             try {
135                 String JavaDoc sys = getSystemID();
136                 if (sys == null) return null;
137                                 
138                 if (view == null) {
139                     URL JavaDoc url = new URL JavaDoc(sys);
140                     ViewEnv env = new ViewEnv(url);
141                     view = new ViewCookieImpl(env);
142                 }
143                 return view;
144                 
145             } catch (MalformedURLException JavaDoc ex) {
146                 ErrorManager emgr = ErrorManager.getDefault();
147                 emgr.notify(ErrorManager.INFORMATIONAL, ex);
148                 return null;
149             } catch (IOException JavaDoc ex) {
150                 ErrorManager emgr = ErrorManager.getDefault();
151                 emgr.notify(ErrorManager.INFORMATIONAL, ex);
152                 return null;
153             }
154             
155         } else {
156             return super.getCookie(clazz);
157         }
158     }
159
160     
161     public HelpCtx getHelpCtx() {
162         //return new HelpCtx(CatalogEntryNode.class);
163
return HelpCtx.DEFAULT_HELP;
164     }
165
166     private String JavaDoc getPublicID() {
167         return ((CatalogEntry)getBean()).getPublicID();
168     }
169     
170     private String JavaDoc getSystemID() {
171         return ((CatalogEntry)getBean()).getSystemID();
172     }
173     
174     public String JavaDoc getShortDescription() {
175         return getSystemID();
176     }
177
178     public void destroy() throws IOException JavaDoc {
179         super.destroy();
180         if (isCatalogWriter) {
181             CatalogWriter catalogWriter = (CatalogWriter)((CatalogEntry)getBean()).getCatalog();
182             catalogWriter.registerCatalogEntry(getPublicID(),null);
183         }
184     }
185
186     
187     /**
188      * OpenSupport that is able to open an input stream.
189      * Encoding, coloring, ..., let editor kit takes care
190      */

191     private class ViewCookieImpl extends CloneableEditorSupport implements ViewCookie {
192
193         ViewCookieImpl(Env env) {
194             super(env);
195         }
196                                 
197         protected String JavaDoc messageName() {
198             return Util.THIS.getString("MSG_opened_entity", getPublicID()); // NOI18N
199
}
200         
201         protected String JavaDoc messageSave() {
202             return Util.THIS.getString ("MSG_ENTITY_SAVE", getPublicID()); // NOI18N
203
}
204         
205         protected java.lang.String JavaDoc messageToolTip() {
206             return Util.THIS.getString ("MSG_ENTITY_TOOLTIP", getSystemID()); // NOI18N
207
}
208
209         protected java.lang.String JavaDoc messageOpening() {
210             return Util.THIS.getString ("MSG_ENTITY_OPENING", getPublicID()); // NOI18N
211
}
212         
213         protected java.lang.String JavaDoc messageOpened() {
214             return Util.THIS.getString ("MSG_ENTITY_OPENED", getPublicID()); // NOI18N
215
}
216
217         //#20646 associate the entry node with editor top component
218
protected CloneableEditor createCloneableEditor() {
219             CloneableEditor editor = super.createCloneableEditor();
220             editor.setActivatedNodes(new Node[] {CatalogEntryNode.this});
221             return editor;
222         }
223
224         /**
225          * Do not write it down, it is runtime view. #20007
226          */

227         private Object JavaDoc writeReplace() {
228             return null;
229         }
230                 
231     }
232     
233     
234     // ~~~~~~~~~~~~~~~~~ environment ~~~~~~~~~~~~~~~~~~~
235

236     /**
237      * text/xml stream environment.
238      */

239     private class ViewEnv extends URLEnvironment {
240
241         /** Serial Version UID */
242         private static final long serialVersionUID =-5031004511063404433L;
243         
244         ViewEnv (URL JavaDoc url) {
245             super(url);
246         }
247
248         public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
249             return (ViewCookieImpl) CatalogEntryNode.this.getCookie(ViewCookieImpl.class);
250         }
251     }
252     
253 }
254
Popular Tags