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.loader;
25 
26 import java.io.IOException;
27 
28 import org.openide.TopManager;
29 import org.openide.NotifyDescriptor;
30 import org.openide.ErrorManager;
31 
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileLock;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.filesystems.FileSystem;
36 import org.openide.filesystems.Repository;
37 
38 import org.openide.loaders.DataObject;
39 import org.openide.loaders.DataFolder;
40 import org.openide.loaders.DataObjectExistsException;
41 
42 import org.openide.nodes.Node;
43 import org.openide.util.datatransfer.NewType;
44 
45 import org.openoffice.netbeans.modules.office.actions.ParcelFolderCookie;
46 import org.openoffice.netbeans.modules.office.utils.PackageRemover;
47 
48 public class ParcelContentsFolder extends DataFolder {
ParcelContentsFolder(FileObject pf, ParcelContentsFolderDataLoader loader)49     public ParcelContentsFolder(FileObject pf, ParcelContentsFolderDataLoader loader)
50         throws DataObjectExistsException {
51         super(pf, loader);
52     }
53 
createNodeDelegate()54     public Node createNodeDelegate() {
55         return new DataFolder.FolderNode() {
56             public NewType[] getNewTypes() {
57                 NewType[] newtypes = new NewType[1];
58                 newtypes[0] = new NewType() {
59                     public String getName() {
60                         return "New Script";
61                     }
62 
63                     public void create() {
64                         DataFolder contents = (DataFolder)getDataObject();
65                         ParcelFolderCookie cookie =
66                             (ParcelFolderCookie)contents.getFolder().
67                                 getCookie(ParcelFolderCookie.class);
68 
69                         String language = cookie.getLanguage();
70                         ParcelContentsFolder.createEmptyScript(contents,
71                             language);
72                     }
73                 };
74                 return newtypes;
75             }
76         };
77     }
78 
79     public static void createEmptyScript(DataFolder parent, String language) {
80         String sourceFile = "Templates/OfficeScripting/EmptyScript/Empty";
81 
82         if (language.toLowerCase().equals("java")) {
83             sourceFile += ".java";
84         }
85         else if (language.toLowerCase().equals("beanshell")) {
86             sourceFile += ".bsh";
87         }
88         else {
89             NotifyDescriptor d = new NotifyDescriptor.Message(
90                 "Language not defined for this Parcel Folder");
91             TopManager.getDefault().notify(d);
92             return;
93         }
94 
95         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
96         DataObject result = null;
97         try {
98             DataObject dObj = DataObject.find(fs.findResource(sourceFile));
99             result = dObj.createFromTemplate(parent);
100         }
101         catch (IOException ioe) {
102             ErrorManager.getDefault().notify(ioe);
103         }
104 
105         FileObject fo = result.getPrimaryFile();
106         if (fo.getExt().equals("java")) {
107             FileLock lock = null;
108             try {
109                 PackageRemover.removeDeclaration(FileUtil.toFile(fo));
110 
111                 // IssueZilla 11986 - rename the FileObject
112                 // so the JavaNode is resynchronized
113                 lock = fo.lock();
114                 if (lock != null) {
115                     fo.rename(lock, fo.getName(), fo.getExt());
116                 }
117             }
118             catch (IOException ioe) {
119                 NotifyDescriptor d = new NotifyDescriptor.Message(
120                  "Error removing package declaration from file: " +
121                  fo.getNameExt() +
122                  ". You should manually remove this declaration " +
123                  "before building the Parcel Recipe");
124                 TopManager.getDefault().notify(d);
125             }
126             finally {
127                 if (lock != null) {
128                     lock.releaseLock();
129                 }
130             }
131         }
132     }
133 }
134