1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.netbeans.modules.office.actions;
25 
26 import java.io.IOException;
27 
28 import org.openide.cookies.*;
29 import org.openide.filesystems.FileLock;
30 import org.openide.filesystems.FileObject;
31 import org.openide.loaders.DataObject;
32 import org.openide.text.DataEditorSupport;
33 import org.openide.windows.CloneableOpenSupport;
34 
35 import org.openoffice.netbeans.modules.office.loader.ParcelDescriptorDataObject;
36 
37 /** Support for editing a data object as text.
38  *
39  * @author tomaso
40  */
41 // Replace OpenCookie with EditCookie or maybe ViewCookie as desired:
42 public class ParcelDescriptorEditorSupport extends DataEditorSupport implements EditorCookie, OpenCookie, CloseCookie, PrintCookie {
43 
44     /** Create a new editor support.
45      * @param obj the data object whose primary file will be edited as text
46      */
ParcelDescriptorEditorSupport(ParcelDescriptorDataObject obj)47     public ParcelDescriptorEditorSupport(ParcelDescriptorDataObject obj) {
48         super(obj, new ParcelDescriptorEnv(obj));
49         // Set a MIME type as needed, e.g.:
50         setMIMEType("text/xml");
51     }
52 
53     /** Called when the document is modified.
54      * Here, adding a save cookie to the object and marking it modified.
55      * @return true if the modification is acceptable
56      */
notifyModified()57     protected boolean notifyModified() {
58         if (!super.notifyModified()) {
59             return false;
60         }
61         ParcelDescriptorDataObject obj = (ParcelDescriptorDataObject)getDataObject();
62         if (obj.getCookie(SaveCookie.class) == null) {
63             obj.setModified(true);
64             // You must implement this method on the object:
65             obj.addSaveCookie(new Save());
66         }
67         return true;
68     }
69 
70     /** Called when the document becomes unmodified.
71      * Here, removing the save cookie from the object and marking it unmodified.
72      */
notifyUnmodified()73     protected void notifyUnmodified() {
74         ParcelDescriptorDataObject obj = (ParcelDescriptorDataObject)getDataObject();
75         SaveCookie save = (SaveCookie)obj.getCookie(SaveCookie.class);
76         if (save != null) {
77             // You must implement this method on the object:
78             obj.removeSaveCookie(save);
79             obj.setModified(false);
80         }
81         super.notifyUnmodified();
82     }
83 
84     /** A save cookie to use for the editor support.
85      * When saved, saves the document to disk and marks the object unmodified.
86      */
87     private class Save implements SaveCookie {
save()88         public void save() throws IOException {
89             saveDocument();
90             getDataObject().setModified(false);
91         }
92     }
93 
94     /** A description of the binding between the editor support and the object.
95      * Note this may be serialized as part of the window system and so
96      * should be static, and use the transient modifier where needed.
97      */
98     private static class ParcelDescriptorEnv extends DataEditorSupport.Env {
99 
100         //private static final long serialVersionUID = ...L;
101 
102         /** Create a new environment based on the data object.
103          * @param obj the data object to edit
104          */
ParcelDescriptorEnv(ParcelDescriptorDataObject obj)105         public ParcelDescriptorEnv(ParcelDescriptorDataObject obj) {
106             super(obj);
107         }
108 
109         /** Get the file to edit.
110          * @return the primary file normally
111          */
getFile()112         protected FileObject getFile() {
113             return getDataObject().getPrimaryFile();
114         }
115 
116         /** Lock the file to edit.
117          * Should be taken from the file entry if possible, helpful during
118          * e.g. deletion of the file.
119          * @return a lock on the primary file normally
120          * @throws IOException if the lock could not be taken
121          */
takeLock()122         protected FileLock takeLock() throws IOException {
123             return ((ParcelDescriptorDataObject)getDataObject()).getPrimaryEntry().takeLock();
124         }
125 
126         /** Find the editor support this environment represents.
127          * Note that we have to look it up, as keeping a direct
128          * reference would not permit this environment to be serialized.
129          * @return the editor support
130          */
findCloneableOpenSupport()131         public CloneableOpenSupport findCloneableOpenSupport() {
132             return (ParcelDescriptorEditorSupport)getDataObject().getCookie(ParcelDescriptorEditorSupport.class);
133         }
134 
135     }
136 
137 }
138