summaryrefslogtreecommitdiff
path: root/scripting/java/org/openoffice/netbeans/modules/office/loader/ParcelFolder.java
blob: cb03c0c8f35d6677b827137c75255f3ae19f4768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package org.openoffice.netbeans.modules.office.loader;

import java.io.File;
import java.io.IOException;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport;

import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataFilter;
import org.openide.loaders.DataObjectExistsException;

import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;

import org.openide.nodes.CookieSet;
import org.openide.nodes.Node;
import org.openide.nodes.PropertySupport;
import org.openide.nodes.Sheet;
import org.openide.util.HelpCtx;

import org.openoffice.idesupport.filter.*;
import org.openoffice.idesupport.zip.ParcelZipper;
import org.openoffice.netbeans.modules.office.actions.ParcelFolderCookie;
import org.openoffice.netbeans.modules.office.actions.ParcelFolderSupport;

public class ParcelFolder extends DataFolder {

    public static final String LANGUAGE_ATTRIBUTE = "language";

    public ParcelFolder(FileObject pf, ParcelFolderDataLoader loader)
        throws DataObjectExistsException {
        super(pf, loader);
        CookieSet cookies = getCookieSet();
        cookies.add(new ParcelFolderSupport(this));
    }

    public Node createNodeDelegate() {
        return new ParcelFolderNode(this, new ParcelFolderFilter());
    }

    public class ParcelFolderNode extends DataFolder.FolderNode {
        private static final String LOCATION = "location";
        private static final String FILTER = "filter";
        private static final String LANGUAGE = LANGUAGE_ATTRIBUTE;
        private static final String CLASSPATH = "classpath";

        private File location;
        private FileFilter filter;
        private String language;
        private String classpath;

        private final FileFilter DEFAULT_FILTER = BinaryOnlyFilter.getInstance();

        public ParcelFolderNode(ParcelFolder pf, DataFilter dataFilter) {
            super(pf.createNodeChildren(dataFilter));

            location = (File)pf.getPrimaryFile().getAttribute(LOCATION);
            if (location == null)
                location = FileUtil.toFile(pf.getPrimaryFile());

            String name = (String)pf.getPrimaryFile().getAttribute(FILTER);
            if (name == null)
                filter = DEFAULT_FILTER;
            else {
                for (int i = 0; i < availableFilters.length; i++)
                    if (name.equals(availableFilters[i].toString()))
                        filter = availableFilters[i];
            }

            language = (String)pf.getPrimaryFile().getAttribute(LANGUAGE);

            ParcelFolderCookie cookie =
                (ParcelFolderCookie)pf.getCookie(ParcelFolderCookie.class);
            String s = cookie.getClasspath();
            if (s != null) {
                classpath = s;
            }
            else {
                classpath = ".";
                cookie.setClasspath(classpath);
            }
        }

        public File getTargetDir() {
            return location;
        }

        public FileFilter getFileFilter() {
            return filter;
        }

        public String getLanguage() {
            if (language == null)
                language = (String)getPrimaryFile().getAttribute(LANGUAGE);
            return language;
        }

        public Sheet createSheet() {
            Sheet sheet;
            Sheet.Set props;
            Node.Property prop;

            sheet = super.createSheet();
            props = sheet.get(Sheet.PROPERTIES);
            if (props == null) {
                props = Sheet.createPropertiesSet();
                sheet.put(props);
            }

            prop = createFilterProperty();
            props.put(prop);

            prop = createFilterProperty();
            props.put(prop);

            prop = createClasspathProperty();
            props.put(prop);

            return sheet;
        }

        private Node.Property createLocationProperty() {
           Node.Property prop =
               new PropertySupport.ReadWrite(LOCATION, File.class,
                   "Location", "Output location of Parcel Zip File") {
                    public void setValue(Object obj) {
                        if (obj instanceof File) {
                            location = (File)obj;
                            try {
                                getPrimaryFile().setAttribute(LOCATION, location);
                            }
                            catch (IOException ioe) {
                            }
                        }
                    }

                    public Object getValue() {
                        return location;
                    }
                };
            prop.setValue("files", Boolean.FALSE);
            return prop;
        }

        private String[] languages = {"Java", "BeanShell"};

        private Node.Property createLanguageProperty() {
            Node.Property prop =
               new PropertySupport.ReadWrite(LANGUAGE, String.class,
                   "Parcel Language", "Language of scripts in this Parcel") {
                    public void setValue(Object obj) {
                        if (obj instanceof String) {
                            language = (String)obj;

                            try {
                                getPrimaryFile().setAttribute(LANGUAGE, language);
                            }
                            catch (IOException ioe) {
                            }
                        }
                    }

                    public Object getValue() {
                        if (language == null)
                            language = (String)getPrimaryFile().getAttribute(LANGUAGE);
                        return language;
                    }

                    public PropertyEditor getPropertyEditor() {
                        return new PropertyEditorSupport() {
                            public String[] getTags() {
                                return languages;
                            }

                            public void setAsText(String text) {
                                for (int i = 0; i < languages.length; i++)
                                    if (text.equals(languages[i]))
                                        this.setValue(languages[i]);
                            }

                            public String getAsText() {
                                return (String)this.getValue();
                            }
                        };
                    }
                };
            return prop;
        }

        private FileFilter[] availableFilters = new FileFilter[] {
            BinaryOnlyFilter.getInstance(), AllFilesFilter.getInstance()};

        private Node.Property createFilterProperty() {
            Node.Property prop =
               new PropertySupport.ReadWrite(FILTER, String.class,
                   "File Filter", "Files to be included in Parcel") {
                    public void setValue(Object obj) {
                        if (obj instanceof FileFilter) {
                            filter = (FileFilter)obj;

                            try {
                                getPrimaryFile().setAttribute(FILTER, filter.toString());
                            }
                            catch (IOException ioe) {
                            }
                        }
                    }

                    public Object getValue() {
                        return filter;
                    }

                    public PropertyEditor getPropertyEditor() {
                        return new PropertyEditorSupport() {
                            public String[] getTags() {
                                String[] tags = new String[availableFilters.length];

                                for (int i = 0; i < availableFilters.length; i++)
                                    tags[i] = availableFilters[i].toString();

                                return tags;
                            }

                            public void setAsText(String text) {
                                for (int i = 0; i < availableFilters.length; i++)
                                    if (text.equals(availableFilters[i].toString()))
                                        this.setValue(availableFilters[i]);
                            }

                            public String getAsText() {
                                return this.getValue().toString();
                            }
                        };
                    }
                };
            return prop;
        }

        private Node.Property createClasspathProperty() {
           Node.Property prop =
               new PropertySupport.ReadWrite(CLASSPATH, String.class,
                   "Classpath", "Classpath property for scripts in this parcel") {
                    public void setValue(Object obj) {
                        if (obj instanceof String) {
                            classpath = (String)obj;

                            ParcelFolderCookie cookie = (ParcelFolderCookie)
                                getDataObject().getCookie(ParcelFolderCookie.class);
                            cookie.setClasspath(classpath);
                        }
                    }

                    public Object getValue() {
                        return classpath;
                    }
                };
            return prop;
        }
    }

    private class ParcelFolderFilter implements DataFilter {
        public boolean acceptDataObject(DataObject dobj) {
            String name = dobj.getPrimaryFile().getNameExt();
            if (name.equals(ParcelZipper.PARCEL_DESCRIPTOR_XML))
                return false;
            return true;
        }
    }
}