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