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.wizard;
25 
26 import java.awt.Component;
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.ObjectInputStream;
30 import java.util.Collections;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.NoSuchElementException;
34 import java.util.Set;
35 import javax.swing.JComponent;
36 import javax.swing.event.ChangeEvent;
37 import javax.swing.event.ChangeListener;
38 
39 import org.openide.TopManager;
40 import org.openide.NotifyDescriptor;
41 import org.openide.WizardDescriptor;
42 import org.openide.cookies.OpenCookie;
43 import org.openide.cookies.SourceCookie;
44 import org.openide.loaders.*;
45 import org.openide.util.NbBundle;
46 import org.openide.filesystems.*;
47 
48 import com.sun.star.script.framework.container.ParcelDescriptor;
49 import org.openoffice.idesupport.zip.ParcelZipper;
50 import org.openoffice.netbeans.modules.office.loader.ParcelFolder;
51 import org.openoffice.netbeans.modules.office.loader.ParcelContentsFolder;
52 import org.openoffice.netbeans.modules.office.filesystem.OpenOfficeDocFileSystem;
53 
54 /** A template wizard iterator (sequence of panels).
55  * Used to fill in the second and subsequent panels in the New wizard.
56  * Associate this to a template inside a layer using the
57  * Sequence of Panels extra property.
58  * Create one or more panels from template as needed too.
59  *
60  * @author tomaso
61  */
62 public class ParcelContentsIterator implements TemplateWizard.Iterator {
63 
64 
65     // private static final long serialVersionUID = ...L;
66 
67     // You should define what panels you want to use here:
68 
69     public static final String PROP_LANGUAGE =
70         ParcelFolder.LANGUAGE_ATTRIBUTE;
71 
createPanels()72     protected WizardDescriptor.Panel[] createPanels() {
73         return new WizardDescriptor.Panel[] {
74             // keep the default 2nd panel:
75             // wiz.targetChooser(),
76             new ParcelPropertiesPanel(),
77         };
78     }
79 
80     // And the list of step names:
81 
createSteps()82     protected String[] createSteps() {
83         return new String[] {
84             // null,
85             "Parcel Properties",
86         };
87     }
88 
checkTarget(DataFolder folder)89     private DataFolder checkTarget(DataFolder folder) {
90         FileObject fo = folder.getPrimaryFile();
91 
92         try {
93             FileSystem fs = fo.getFileSystem();
94 
95             if (fs instanceof OpenOfficeDocFileSystem && fo.isRoot()) {
96                 FileObject scripts =
97                     fo.getFileObject(OpenOfficeDocFileSystem.SCRIPTS_ROOT);
98                 if (scripts == null)
99                     scripts =
100                         fo.createFolder(OpenOfficeDocFileSystem.SCRIPTS_ROOT);
101 
102                 FileObject javafolder = scripts.getFileObject("java");
103                 if (javafolder == null)
104                     javafolder = scripts.createFolder("java");
105 
106                 DataFolder subfolder = new DataFolder(javafolder);
107                 return subfolder;
108             }
109         }
110         catch (IOException ioe) {
111             /* do nothing, we will just return the folder we were passed in */
112         }
113         return folder;
114     }
115 
instantiate(TemplateWizard wiz)116     public Set instantiate(TemplateWizard wiz) throws IOException {
117         String name = wiz.getTargetName();
118         DataFolder targetFolder = wiz.getTargetFolder();
119         targetFolder = checkTarget(targetFolder);
120 
121         String language = (String)wiz.getProperty(PROP_LANGUAGE);
122 
123         DataObject template = wiz.getTemplate();
124         DataObject result;
125         if (name == null) {
126             // Default name.
127             result = template.createFromTemplate(targetFolder);
128         } else {
129             result = template.createFromTemplate(targetFolder, name);
130         }
131 
132         FileObject recipe = result.getPrimaryFile();
133 
134         FileObject contents =
135             recipe.getFileObject(ParcelZipper.CONTENTS_DIRNAME);
136 
137         if (contents != null) {
138             File f = FileUtil.toFile(contents);
139             ParcelDescriptor pd = ParcelDescriptor.createParcelDescriptor(f);
140             pd.setLanguage(language);
141             pd.write();
142 
143             DataFolder parent = DataFolder.findFolder(contents);
144             ParcelContentsFolder.createEmptyScript(parent, language);
145         }
146 
147         return Collections.singleton(result);
148     }
149 
150     // --- The rest probably does not need to be touched. ---
151 
152     private transient int index;
153     private transient WizardDescriptor.Panel[] panels;
154     private transient TemplateWizard wiz;
155 
156     // You can keep a reference to the TemplateWizard which can
157     // provide various kinds of useful information such as
158     // the currently selected target name.
159     // Also the panels will receive wiz as their "settings" object.
initialize(TemplateWizard wiz)160     public void initialize(TemplateWizard wiz) {
161         this.wiz = wiz;
162         index = 0;
163         panels = createPanels();
164         // Make sure list of steps is accurate.
165         String[] steps = createSteps();
166         for (int i = 0; i < panels.length; i++) {
167             Component c = panels[i].getComponent();
168             if (steps[i] == null) {
169                 // Default step name to component name of panel.
170                 // Mainly useful for getting the name of the target
171                 // chooser to appear in the list of steps.
172                 steps[i] = c.getName();
173             }
174             if (c instanceof JComponent) { // assume Swing components
175                 JComponent jc = (JComponent)c;
176                 // Step #.
177                 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i)); // NOI18N
178                 // Step name (actually the whole list for reference).
179                 jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
180             }
181         }
182     }
uninitialize(TemplateWizard wiz)183     public void uninitialize(TemplateWizard wiz) {
184         this.wiz = null;
185         panels = null;
186     }
187 
188     // --- WizardDescriptor.Iterator METHODS: ---
189     // Note that this is very similar to WizardDescriptor.Iterator, but with a
190     // few more options for customization. If you e.g. want to make panels appear
191     // or disappear dynamically, go ahead.
192 
name()193     public String name() {
194         return "";
195     }
196 
hasNext()197     public boolean hasNext() {
198         return index < panels.length - 1;
199     }
hasPrevious()200     public boolean hasPrevious() {
201         return index > 0;
202     }
nextPanel()203     public void nextPanel() {
204         if (!hasNext()) throw new NoSuchElementException();
205         index++;
206     }
previousPanel()207     public void previousPanel() {
208         if (!hasPrevious()) throw new NoSuchElementException();
209         index--;
210     }
current()211     public WizardDescriptor.Panel current() {
212         return panels[index];
213     }
214 
215     // If nothing unusual changes in the middle of the wizard, simply:
addChangeListener(ChangeListener l)216     public final void addChangeListener(ChangeListener l) {}
removeChangeListener(ChangeListener l)217     public final void removeChangeListener(ChangeListener l) {}
218     // If something changes dynamically (besides moving between panels),
219     // e.g. the number of panels changes in response to user input, then
220     // uncomment the following and call when needed:
221     // fireChangeEvent();
222     /*
223     private transient Set listeners = new HashSet(1); // Set<ChangeListener>
224     public final void addChangeListener(ChangeListener l) {
225         synchronized(listeners) {
226             listeners.add(l);
227         }
228     }
229     public final void removeChangeListener(ChangeListener l) {
230         synchronized(listeners) {
231             listeners.remove(l);
232         }
233     }
234     protected final void fireChangeEvent() {
235         Iterator it;
236         synchronized (listeners) {
237             it = new HashSet(listeners).iterator();
238         }
239         ChangeEvent ev = new ChangeEvent(this);
240         while (it.hasNext()) {
241             ((ChangeListener)it.next()).stateChanged(ev);
242         }
243     }
244     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
245         in.defaultReadObject();
246         listeners = new HashSet(1);
247     }
248      */
249 
250 }
251