xref: /trunk/main/scripting/java/org/openoffice/netbeans/modules/office/actions/ParcelSupport.java (revision 5226f0b8dea4ef117ddd2bc4948a8601d0ac403b) !
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.File;
27 import java.io.IOException;
28 import java.beans.PropertyVetoException;
29 import java.util.Calendar;
30 
31 import org.openide.TopManager;
32 import org.openide.NotifyDescriptor;
33 import org.openide.windows.OutputWriter;
34 import org.openide.windows.InputOutput;
35 
36 import org.openide.ErrorManager;
37 import org.openide.nodes.Node;
38 import org.openide.filesystems.Repository;
39 import org.openide.filesystems.FileSystem;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.filesystems.FileEvent;
43 
44 import org.openoffice.idesupport.zip.ParcelZipper;
45 
46 import org.openoffice.netbeans.modules.office.options.OfficeSettings;
47 import org.openoffice.netbeans.modules.office.utils.NagDialog;
48 import org.openoffice.netbeans.modules.office.utils.ZipMounter;
49 import org.openoffice.netbeans.modules.office.utils.ManifestParser;
50 
51 public class ParcelSupport implements ParcelCookie
52 {
53     private FileObject fo;
54     private ParcelZipper zipper = ParcelZipper.getParcelZipper();
55     private String language = null;
56 
ParcelSupport(FileObject fo)57     public ParcelSupport(FileObject fo) {
58         this.fo = fo;
59     }
60 
getFile()61     public File getFile() {
62         return FileUtil.toFile(fo);
63     }
64 
getLanguage()65     public String getLanguage() {
66         if (language == null) {
67             try {
68                 language = zipper.getParcelLanguage(getFile());
69             }
70             catch (IOException ioe) {
71                 return null;
72             }
73         }
74         return language;
75     }
76 
mount()77     public void mount()
78     {
79         File parcel = FileUtil.toFile(fo);
80 
81         if (parcel != null) {
82             try {
83                 ZipMounter.getZipMounter().mountZipFile(parcel);
84             }
85             catch (IOException ioe) {
86                 ErrorManager.getDefault().notify(ioe);
87             }
88             catch (PropertyVetoException pve) {
89                 ErrorManager.getDefault().notify(pve);
90             }
91         }
92     }
93 
deploy(File target)94     public boolean deploy(File target) {
95         File source = FileUtil.toFile(fo);
96 
97         if (!target.isDirectory()) {
98             OfficeSettings settings = OfficeSettings.getDefault();
99             String message = "If you already have this document open in " +
100                 "Office, please close it before continuing. Click OK to " +
101                 "continue deployment.";
102 
103             if (settings.getWarnBeforeDocDeploy() == true) {
104                 NagDialog warning = NagDialog.createConfirmationDialog(
105                     message, "Show this message in future", true);
106 
107                 boolean result = warning.show();
108 
109                 if (warning.getState() == false)
110                     settings.setWarnBeforeDocDeploy(false);
111 
112                 if (result == false)
113                     return false;
114             }
115         }
116 
117         OutputWriter out =
118             getOutputWindowWriter(fo.getName() + " (deploying)");
119 
120         try {
121             if (zipper.isOverwriteNeeded(source, target) == true)
122                 if (promptForOverwrite(source, target) == false)
123                     return false;
124         }
125         catch (IOException ioe) {
126            out.println("DEPLOYMENT FAILED: reason: " +
127                ioe.getClass().getName() + ": "+ ioe.getMessage());
128            return false;
129         }
130 
131         try {
132             out.println("Deploying: " + fo.getName() +
133                 "\nTo: " + target.getAbsolutePath(), null);
134 
135             zipper.deployParcel(source, target);
136 
137             out.println("\nDEPLOYMENT SUCCESSFUL.");
138 
139             FileObject[] fileobjs = FileUtil.fromFile(target);
140             if (fileobjs != null) {
141                 for (int i = 0; i < fileobjs.length; i++)
142                     fileobjs[i].refresh(true);
143             }
144         }
145         catch (IOException ioe) {
146            out.println("DEPLOYMENT FAILED: reason: " +
147                ioe.getClass().getName() + ": "+ ioe.getMessage());
148            return false;
149         }
150         finally {
151             if( out != null)
152                 out.close();
153         }
154         return true;
155     }
156 
getOutputWindowWriter(String title)157     public static OutputWriter getOutputWindowWriter(String title) {
158         InputOutput io = TopManager.getDefault().getIO(title, false);
159         io.setFocusTaken(true);
160         io.setOutputVisible(true);
161 
162         OutputWriter out = io.getOut();
163         try {
164             out.reset();
165         }
166         catch( IOException e) {
167             e.printStackTrace(System.err);
168         }
169         out.println(Calendar.getInstance().getTime() + ":\n");
170         return out;
171     }
172 
promptForOverwrite(File source, File target)173     private boolean promptForOverwrite(File source, File target) {
174         String message = source.getName() + " has already been deployed " +
175             "to this target. Do you wish to overwrite it?";
176 
177         NotifyDescriptor d = new NotifyDescriptor.Confirmation(
178             message, NotifyDescriptor.OK_CANCEL_OPTION);
179         TopManager.getDefault().notify(d);
180 
181         if (d.getValue() == NotifyDescriptor.CANCEL_OPTION)
182             return false;
183         else
184             return true;
185     }
186 }
187