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