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.util.Vector;
27 import java.util.StringTokenizer;
28 
29 import java.io.*;
30 import java.beans.PropertyVetoException;
31 import java.awt.Dialog;
32 
33 import org.w3c.dom.Document;
34 import org.xml.sax.InputSource;
35 import org.xml.sax.SAXException;
36 
37 import org.openide.TopManager;
38 import org.openide.DialogDescriptor;
39 import org.openide.ErrorManager;
40 import org.openide.xml.XMLUtil;
41 import org.openide.execution.NbClassPath;
42 
43 import org.openide.cookies.OpenCookie;
44 import org.openide.loaders.DataObject;
45 import org.openide.loaders.DataNode;
46 
47 import org.openide.filesystems.FileObject;
48 import org.openide.filesystems.FileSystem;
49 import org.openide.filesystems.JarFileSystem;
50 import org.openide.filesystems.FileUtil;
51 import org.openide.filesystems.Repository;
52 
53 import org.openide.nodes.*;
54 import org.openide.windows.OutputWriter;
55 
56 import org.openoffice.netbeans.modules.office.loader.ParcelFolder;
57 import org.openoffice.netbeans.modules.office.options.OfficeSettings;
58 import org.openoffice.netbeans.modules.office.utils.ManifestParser;
59 
60 import com.sun.star.script.framework.container.ParcelDescriptor;
61 
62 import org.openoffice.idesupport.zip.ParcelZipper;
63 import org.openoffice.idesupport.filter.FileFilter;
64 import org.openoffice.idesupport.ui.ConfigurePanel;
65 
66 public class ParcelFolderSupport implements ParcelFolderCookie
67 {
68     protected ParcelFolder parcelFolder;
69     private ConfigurePanel configuror = null;
70 
ParcelFolderSupport(ParcelFolder parcelFolder)71     public ParcelFolderSupport(ParcelFolder parcelFolder) {
72         this.parcelFolder = parcelFolder;
73     }
74 
getLanguage()75     public String getLanguage() {
76         ParcelDescriptor descriptor = getParcelDescriptor();
77 
78         if (descriptor == null) {
79             return "";
80         }
81         else {
82             return descriptor.getLanguage();
83         }
84     }
85 
getClasspath()86     public String getClasspath() {
87         ParcelDescriptor descriptor = getParcelDescriptor();
88 
89         if (descriptor == null) {
90             return "";
91         }
92         else {
93             return descriptor.getLanguageProperty("classpath");
94         }
95     }
96 
setClasspath(String value)97     public void setClasspath(String value) {
98         ParcelDescriptor descriptor = getParcelDescriptor();
99 
100         if (descriptor != null) {
101             descriptor.setLanguageProperty("classpath", value);
102 
103             try {
104                 descriptor.write();
105             }
106             catch (IOException ioe) {
107                 ErrorManager.getDefault().notify(ioe);
108             }
109         }
110     }
111 
getParcelDescriptor()112     private ParcelDescriptor getParcelDescriptor() {
113         FileObject primary = parcelFolder.getPrimaryFile();
114 
115         File contents = FileUtil.toFile(
116             primary.getFileObject(ParcelZipper.CONTENTS_DIRNAME));
117 
118         return ParcelDescriptor.getParcelDescriptor(contents);
119     }
120 
generate()121     public void generate() {
122         ParcelFolder.ParcelFolderNode node =
123             (ParcelFolder.ParcelFolderNode)parcelFolder.getNodeDelegate();
124 
125         FileObject parcelBase = parcelFolder.getPrimaryFile();
126         FileObject contentsBase =
127             parcelBase.getFileObject(ParcelZipper.CONTENTS_DIRNAME);
128 
129         File parcelDir = FileUtil.toFile(parcelBase);
130         File contentsDir = FileUtil.toFile(contentsBase);
131 
132         // The Location property is not displayed so just
133         // use the Parcel Recipe directory as the target directory
134         File targetDir = FileUtil.toFile(parcelFolder.getPrimaryFile());
135         File targetfile = new File(targetDir, File.separator +
136             parcelBase.getName() + "." + ParcelZipper.PARCEL_EXTENSION);
137 
138         boolean proceed = configure();
139         if (proceed == false) {
140             return;
141         }
142 
143         final OutputWriter out =
144             ParcelSupport.getOutputWindowWriter(parcelDir.getName() + " (generating)");
145         try {
146             out.println("Generating: " + parcelDir.getName(), null);
147             ParcelZipper.getParcelZipper().zipParcel(contentsDir, targetfile, node.getFileFilter());
148             out.println("\nGENERATION SUCCESSFUL.");
149             out.println("\nRight click on the generated parcel to deploy it");
150 
151             if (targetDir.equals(parcelDir)) {
152                 parcelBase.refresh(true);
153             }
154         }
155         catch (IOException ioe) {
156             out.println("GENERATION FAILED: reason: " + ioe.getClass().getName() + ": "+ ioe.getMessage());
157         }
158         finally
159         {
160             if( out != null)
161             {
162                 out.close();
163             }
164         }
165     }
166 
configure()167     public boolean configure() {
168 
169         FileObject primary = parcelFolder.getPrimaryFile();
170 
171         File contents = FileUtil.toFile(
172             primary.getFileObject(ParcelZipper.CONTENTS_DIRNAME));
173 
174         Vector classpath = getConfigureClasspath();
175         classpath.addElement(contents.getAbsolutePath());
176 
177         try {
178             ParcelDescriptor descriptor = getParcelDescriptor();
179             if (descriptor == null) {
180                 descriptor = ParcelDescriptor.createParcelDescriptor(contents);
181             }
182 
183             if (configuror == null) {
184                 configuror = new ConfigurePanel(contents.getAbsolutePath(),
185                     classpath, descriptor);
186             }
187             else {
188                 configuror.reload(contents.getAbsolutePath(), classpath,
189                     descriptor);
190             }
191         }
192         catch (IOException ioe) {
193             ErrorManager.getDefault().notify(ioe);
194             return false;
195         }
196 
197         DialogDescriptor dd = new DialogDescriptor(configuror,
198             ConfigurePanel.DIALOG_TITLE);
199 
200         Dialog dialog = TopManager.getDefault().createDialog(dd);
201         dialog.show();
202 
203         if (dd.getValue() == DialogDescriptor.OK_OPTION) {
204             try {
205                 ParcelDescriptor descriptor = configuror.getConfiguration();
206                 descriptor.write();
207             }
208             catch (Exception e) {
209                 ErrorManager.getDefault().notify(e);
210             }
211         }
212         else {
213             return false;
214         }
215         return true;
216     }
217 
getConfigureClasspath()218     private Vector getConfigureClasspath() {
219         Vector result = new Vector();
220 
221         String classpath = NbClassPath.createRepositoryPath().getClassPath();
222         if ( System.getProperty( "os.name" ).startsWith( "Windows" ) )
223         {
224             // under windows path is enclosed by quotes
225             // e.g. C:\path1;d:\path2 would appear as
226             // "C:\path1;d:\path2" therefore for us
227             // we need to remove 1 character at either end of the
228             // classpath returned from "createRepositoryPath().getClassPath()"
229 
230             if ( classpath.startsWith("\"") && classpath.endsWith("\"") )
231             {
232                StringBuffer buff = new StringBuffer(classpath);
233                buff.delete(0,1);
234                buff.delete( buff.length() - 1, buff.length() );
235                classpath = buff.toString();
236             }
237         }
238         StringTokenizer tokens = new StringTokenizer(classpath, File.pathSeparator);
239 
240         while(tokens.hasMoreTokens())
241             result.addElement(tokens.nextToken());
242 
243         OfficeSettings settings = OfficeSettings.getDefault();
244         File classesDir = new File(settings.getOfficeDirectory().getPath(
245             File.separator + "program" + File.separator + "classes"));
246         File[] jarfiles = classesDir.listFiles();
247 
248         for (int i = 0; i < jarfiles.length; i++)
249             result.addElement(jarfiles[i].getAbsolutePath());
250 
251         return result;
252     }
253 }
254