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.File;
27 import java.io.IOException;
28 import java.beans.PropertyEditor;
29 import java.beans.PropertyEditorSupport;
30 
31 import org.openide.loaders.DataFolder;
32 import org.openide.loaders.DataObject;
33 import org.openide.loaders.DataFilter;
34 import org.openide.loaders.DataObjectExistsException;
35 
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 
39 import org.openide.nodes.CookieSet;
40 import org.openide.nodes.Node;
41 import org.openide.nodes.PropertySupport;
42 import org.openide.nodes.Sheet;
43 import org.openide.util.HelpCtx;
44 
45 import org.openoffice.idesupport.filter.*;
46 import org.openoffice.idesupport.zip.ParcelZipper;
47 import org.openoffice.netbeans.modules.office.actions.ParcelFolderCookie;
48 import org.openoffice.netbeans.modules.office.actions.ParcelFolderSupport;
49 
50 public class ParcelFolder extends DataFolder {
51 
52     public static final String LANGUAGE_ATTRIBUTE = "language";
53 
ParcelFolder(FileObject pf, ParcelFolderDataLoader loader)54     public ParcelFolder(FileObject pf, ParcelFolderDataLoader loader)
55         throws DataObjectExistsException {
56         super(pf, loader);
57         CookieSet cookies = getCookieSet();
58         cookies.add(new ParcelFolderSupport(this));
59     }
60 
createNodeDelegate()61     public Node createNodeDelegate() {
62         return new ParcelFolderNode(this, new ParcelFolderFilter());
63     }
64 
65     public class ParcelFolderNode extends DataFolder.FolderNode {
66         private static final String LOCATION = "location";
67         private static final String FILTER = "filter";
68         private static final String LANGUAGE = LANGUAGE_ATTRIBUTE;
69         private static final String CLASSPATH = "classpath";
70 
71         private File location;
72         private FileFilter filter;
73         private String language;
74         private String classpath;
75 
76         private final FileFilter DEFAULT_FILTER = BinaryOnlyFilter.getInstance();
77 
ParcelFolderNode(ParcelFolder pf, DataFilter dataFilter)78         public ParcelFolderNode(ParcelFolder pf, DataFilter dataFilter) {
79             super(pf.createNodeChildren(dataFilter));
80 
81             location = (File)pf.getPrimaryFile().getAttribute(LOCATION);
82             if (location == null)
83                 location = FileUtil.toFile(pf.getPrimaryFile());
84 
85             String name = (String)pf.getPrimaryFile().getAttribute(FILTER);
86             if (name == null)
87                 filter = DEFAULT_FILTER;
88             else {
89                 for (int i = 0; i < availableFilters.length; i++)
90                     if (name.equals(availableFilters[i].toString()))
91                         filter = availableFilters[i];
92             }
93 
94             language = (String)pf.getPrimaryFile().getAttribute(LANGUAGE);
95 
96             ParcelFolderCookie cookie =
97                 (ParcelFolderCookie)pf.getCookie(ParcelFolderCookie.class);
98             String s = cookie.getClasspath();
99             if (s != null) {
100                 classpath = s;
101             }
102             else {
103                 classpath = ".";
104                 cookie.setClasspath(classpath);
105             }
106         }
107 
getTargetDir()108         public File getTargetDir() {
109             return location;
110         }
111 
getFileFilter()112         public FileFilter getFileFilter() {
113             return filter;
114         }
115 
getLanguage()116         public String getLanguage() {
117             if (language == null)
118                 language = (String)getPrimaryFile().getAttribute(LANGUAGE);
119             return language;
120         }
121 
createSheet()122         public Sheet createSheet() {
123             Sheet sheet;
124             Sheet.Set props;
125             Node.Property prop;
126 
127             sheet = super.createSheet();
128             props = sheet.get(Sheet.PROPERTIES);
129             if (props == null) {
130                 props = Sheet.createPropertiesSet();
131                 sheet.put(props);
132             }
133 
134             // prop = createLocationProperty();
135             // props.put(prop);
136 
137             prop = createFilterProperty();
138             props.put(prop);
139 
140             prop = createFilterProperty();
141             props.put(prop);
142 
143             // prop = createLanguageProperty();
144             // props.put(prop);
145 
146             prop = createClasspathProperty();
147             props.put(prop);
148 
149             return sheet;
150         }
151 
createLocationProperty()152         private Node.Property createLocationProperty() {
153            Node.Property prop =
154                new PropertySupport.ReadWrite(LOCATION, File.class,
155                    "Location", "Output location of Parcel Zip File") {
156                     public void setValue(Object obj) {
157                         if (obj instanceof File) {
158                             location = (File)obj;
159                             try {
160                                 getPrimaryFile().setAttribute(LOCATION, location);
161                             }
162                             catch (IOException ioe) {
163                             }
164                         }
165                     }
166 
167                     public Object getValue() {
168                         return location;
169                     }
170                 };
171             prop.setValue("files", Boolean.FALSE);
172             return prop;
173         }
174 
175         private String[] languages = {"Java", "BeanShell"};
176 
createLanguageProperty()177         private Node.Property createLanguageProperty() {
178             Node.Property prop =
179                new PropertySupport.ReadWrite(LANGUAGE, String.class,
180                    "Parcel Language", "Language of scripts in this Parcel") {
181                     public void setValue(Object obj) {
182                         if (obj instanceof String) {
183                             language = (String)obj;
184 
185                             try {
186                                 getPrimaryFile().setAttribute(LANGUAGE, language);
187                             }
188                             catch (IOException ioe) {
189                             }
190                         }
191                     }
192 
193                     public Object getValue() {
194                         if (language == null)
195                             language = (String)getPrimaryFile().getAttribute(LANGUAGE);
196                         return language;
197                     }
198 
199                     public PropertyEditor getPropertyEditor() {
200                         return new PropertyEditorSupport() {
201                             public String[] getTags() {
202                                 return languages;
203                             }
204 
205                             public void setAsText(String text) {
206                                 for (int i = 0; i < languages.length; i++)
207                                     if (text.equals(languages[i]))
208                                         this.setValue(languages[i]);
209                             }
210 
211                             public String getAsText() {
212                                 return (String)this.getValue();
213                             }
214                         };
215                     }
216                 };
217             return prop;
218         }
219 
220         private FileFilter[] availableFilters = new FileFilter[] {
221             BinaryOnlyFilter.getInstance(), AllFilesFilter.getInstance()};
222 
createFilterProperty()223         private Node.Property createFilterProperty() {
224             Node.Property prop =
225                new PropertySupport.ReadWrite(FILTER, String.class,
226                    "File Filter", "Files to be included in Parcel") {
227                     public void setValue(Object obj) {
228                         if (obj instanceof FileFilter) {
229                             filter = (FileFilter)obj;
230 
231                             try {
232                                 getPrimaryFile().setAttribute(FILTER, filter.toString());
233                             }
234                             catch (IOException ioe) {
235                             }
236                         }
237                     }
238 
239                     public Object getValue() {
240                         return filter;
241                     }
242 
243                     public PropertyEditor getPropertyEditor() {
244                         return new PropertyEditorSupport() {
245                             public String[] getTags() {
246                                 String[] tags = new String[availableFilters.length];
247 
248                                 for (int i = 0; i < availableFilters.length; i++)
249                                     tags[i] = availableFilters[i].toString();
250 
251                                 return tags;
252                             }
253 
254                             public void setAsText(String text) {
255                                 for (int i = 0; i < availableFilters.length; i++)
256                                     if (text.equals(availableFilters[i].toString()))
257                                         this.setValue(availableFilters[i]);
258                             }
259 
260                             public String getAsText() {
261                                 return this.getValue().toString();
262                             }
263                         };
264                     }
265                 };
266             return prop;
267         }
268 
createClasspathProperty()269         private Node.Property createClasspathProperty() {
270            Node.Property prop =
271                new PropertySupport.ReadWrite(CLASSPATH, String.class,
272                    "Classpath", "Classpath property for scripts in this parcel") {
273                     public void setValue(Object obj) {
274                         if (obj instanceof String) {
275                             classpath = (String)obj;
276 
277                             ParcelFolderCookie cookie = (ParcelFolderCookie)
278                                 getDataObject().getCookie(ParcelFolderCookie.class);
279                             cookie.setClasspath(classpath);
280                         }
281                     }
282 
283                     public Object getValue() {
284                         return classpath;
285                     }
286                 };
287             return prop;
288         }
289     }
290 
291     private class ParcelFolderFilter implements DataFilter {
acceptDataObject(DataObject dobj)292         public boolean acceptDataObject(DataObject dobj) {
293             String name = dobj.getPrimaryFile().getNameExt();
294             if (name.equals(ParcelZipper.PARCEL_DESCRIPTOR_XML))
295                 return false;
296             return true;
297         }
298     }
299 }
300