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