001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.catalog;
043:
044: import java.beans.IntrospectionException;
045: import java.io.File;
046: import java.io.IOException;
047: import java.net.MalformedURLException;
048: import java.net.URL;
049: import javax.swing.Action;
050: import org.netbeans.modules.xml.catalog.lib.URLEnvironment;
051: import org.netbeans.modules.xml.catalog.spi.CatalogReader;
052: import org.netbeans.modules.xml.catalog.spi.CatalogWriter;
053: import org.netbeans.modules.xml.catalog.user.UserXMLCatalog;
054: import org.openide.ErrorManager;
055: import org.openide.NotifyDescriptor;
056: import org.openide.actions.DeleteAction;
057: import org.openide.actions.EditAction;
058: import org.openide.actions.PropertiesAction;
059: import org.openide.actions.ViewAction;
060: import org.openide.cookies.EditCookie;
061: import org.openide.cookies.ViewCookie;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064: import org.openide.loaders.DataObject;
065: import org.openide.nodes.BeanNode;
066: import org.openide.nodes.Node;
067: import org.openide.text.CloneableEditor;
068: import org.openide.text.CloneableEditorSupport;
069: import org.openide.text.CloneableEditorSupport.Env;
070: import org.openide.util.HelpCtx;
071: import org.openide.util.NbBundle;
072: import org.openide.util.actions.SystemAction;
073:
074: /**
075: * Node representing single catalog entry. It can be viewed.
076: *
077: * @author Petr Kuzel
078: * @version 1.0
079: */
080: final class CatalogEntryNode extends BeanNode implements EditCookie,
081: Node.Cookie {
082:
083: // cached ViewCookie instance
084: private transient ViewCookie view;
085: private boolean isCatalogWriter;
086: private CatalogReader catalogReader;
087:
088: /** Creates new CatalogNode */
089: public CatalogEntryNode(CatalogEntry entry)
090: throws IntrospectionException {
091: super (entry);
092: getCookieSet().add(this );
093: catalogReader = entry.getCatalog();
094: if (catalogReader instanceof CatalogWriter) {
095: isCatalogWriter = true;
096: }
097: }
098:
099: public boolean isCatalogWriter() {
100: return isCatalogWriter;
101: }
102:
103: public javax.swing.Action getPreferredAction() {
104: if (isCatalogWriter)
105: return SystemAction.get(EditAction.class);
106: else
107: return SystemAction.get(ViewAction.class);
108: }
109:
110: public void edit() {
111: UserXMLCatalog catalog = (UserXMLCatalog) getCatalogReader();
112: try {
113: java.net.URI uri = new java.net.URI(getSystemID());
114: File file = new File(uri);
115: FileObject fo = FileUtil.toFileObject(file);
116: boolean editPossible = false;
117: if (fo != null) {
118: DataObject obj = DataObject.find(fo);
119: EditCookie editCookie = (EditCookie) obj
120: .getCookie(EditCookie.class);
121: if (editCookie != null) {
122: editPossible = true;
123: editCookie.edit();
124: }
125: }
126: if (!editPossible)
127: org.openide.DialogDisplayer.getDefault().notify(
128: new NotifyDescriptor.Message(NbBundle
129: .getMessage(CatalogEntryNode.class,
130: "MSG_CannotOpenURI",
131: getSystemID()), //NOI18N
132: NotifyDescriptor.INFORMATION_MESSAGE));
133: } catch (Throwable ex) {
134: ErrorManager.getDefault().notify(ex);
135: }
136: }
137:
138: private CatalogReader getCatalogReader() {
139: return catalogReader;
140: }
141:
142: public Action[] getActions(boolean context) {
143: if (isCatalogWriter)
144: return new Action[] { SystemAction.get(EditAction.class),
145: SystemAction.get(DeleteAction.class), null,
146: SystemAction.get(PropertiesAction.class) };
147: else
148: return new Action[] { SystemAction.get(ViewAction.class),
149: null, SystemAction.get(PropertiesAction.class) };
150: }
151:
152: /**
153: * Provide <code>ViewCookie</code>. Always provide same instance for
154: * entry until its system ID changes.
155: */
156: public Node.Cookie getCookie(Class clazz) {
157:
158: if (ViewCookie.class.equals(clazz)) {
159:
160: try {
161: String sys = getSystemID();
162: if (sys == null)
163: return null;
164:
165: if (view == null) {
166: URL url = new URL(sys);
167: ViewEnv env = new ViewEnv(url);
168: view = new ViewCookieImpl(env);
169: }
170: return view;
171:
172: } catch (MalformedURLException ex) {
173: ErrorManager emgr = ErrorManager.getDefault();
174: emgr.notify(ErrorManager.INFORMATIONAL, ex);
175: return null;
176: } catch (IOException ex) {
177: ErrorManager emgr = ErrorManager.getDefault();
178: emgr.notify(ErrorManager.INFORMATIONAL, ex);
179: return null;
180: }
181:
182: } else {
183: return super .getCookie(clazz);
184: }
185: }
186:
187: public HelpCtx getHelpCtx() {
188: //return new HelpCtx(CatalogEntryNode.class);
189: return HelpCtx.DEFAULT_HELP;
190: }
191:
192: private String getPublicID() {
193: return ((CatalogEntry) getBean()).getPublicID();
194: }
195:
196: private String getSystemID() {
197: return ((CatalogEntry) getBean()).getSystemID();
198: }
199:
200: public String getShortDescription() {
201: return getSystemID();
202: }
203:
204: public void destroy() throws IOException {
205: super .destroy();
206: if (isCatalogWriter) {
207: CatalogWriter catalogWriter = (CatalogWriter) ((CatalogEntry) getBean())
208: .getCatalog();
209: catalogWriter.registerCatalogEntry(getPublicID(), null);
210: }
211: }
212:
213: /**
214: * OpenSupport that is able to open an input stream.
215: * Encoding, coloring, ..., let editor kit takes care
216: */
217: private class ViewCookieImpl extends CloneableEditorSupport
218: implements ViewCookie {
219:
220: ViewCookieImpl(Env env) {
221: super (env);
222: }
223:
224: protected String messageName() {
225: return NbBundle.getMessage(CatalogEntryNode.class,
226: "MSG_opened_entity", getPublicID()); // NOI18N
227: }
228:
229: protected String messageSave() {
230: return NbBundle.getMessage(CatalogEntryNode.class,
231: "MSG_ENTITY_SAVE", getPublicID()); // NOI18N
232: }
233:
234: protected java.lang.String messageToolTip() {
235: return NbBundle.getMessage(CatalogEntryNode.class,
236: "MSG_ENTITY_TOOLTIP", getSystemID()); // NOI18N
237: }
238:
239: protected java.lang.String messageOpening() {
240: return NbBundle.getMessage(CatalogEntryNode.class,
241: "MSG_ENTITY_OPENING", getPublicID()); // NOI18N
242: }
243:
244: protected java.lang.String messageOpened() {
245: return NbBundle.getMessage(CatalogEntryNode.class,
246: "MSG_ENTITY_OPENED", getPublicID()); // NOI18N
247: }
248:
249: //#20646 associate the entry node with editor top component
250: protected CloneableEditor createCloneableEditor() {
251: CloneableEditor editor = super .createCloneableEditor();
252: editor
253: .setActivatedNodes(new Node[] { CatalogEntryNode.this });
254: return editor;
255: }
256:
257: /**
258: * Do not write it down, it is runtime view. #20007
259: */
260: private Object writeReplace() {
261: return null;
262: }
263:
264: }
265:
266: // ~~~~~~~~~~~~~~~~~ environment ~~~~~~~~~~~~~~~~~~~
267:
268: /**
269: * text/xml stream environment.
270: */
271: private class ViewEnv extends URLEnvironment {
272:
273: /** Serial Version UID */
274: private static final long serialVersionUID = -5031004511063404433L;
275:
276: ViewEnv(URL url) {
277: super (url);
278: }
279:
280: public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
281: return (ViewCookieImpl) CatalogEntryNode.this
282: .getCookie(ViewCookieImpl.class);
283: }
284: }
285:
286: }
|