| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package net.sf.webphotos.netbeans.project.empty; |
| 17 | |
|
| 18 | |
import java.awt.Component; |
| 19 | |
import java.io.ByteArrayInputStream; |
| 20 | |
import java.io.ByteArrayOutputStream; |
| 21 | |
import java.io.File; |
| 22 | |
import java.io.IOException; |
| 23 | |
import java.io.InputStream; |
| 24 | |
import java.io.OutputStream; |
| 25 | |
import java.text.MessageFormat; |
| 26 | |
import java.util.Enumeration; |
| 27 | |
import java.util.LinkedHashSet; |
| 28 | |
import java.util.NoSuchElementException; |
| 29 | |
import java.util.Set; |
| 30 | |
import java.util.zip.ZipEntry; |
| 31 | |
import java.util.zip.ZipInputStream; |
| 32 | |
import javax.swing.JComponent; |
| 33 | |
import javax.swing.event.ChangeListener; |
| 34 | |
import net.sf.webphotos.netbeans.project.Constants.ProjectFactory.EmptyProject; |
| 35 | |
import org.netbeans.api.project.ProjectManager; |
| 36 | |
import org.netbeans.api.templates.TemplateRegistration; |
| 37 | |
import org.netbeans.spi.project.ui.support.ProjectChooser; |
| 38 | |
import org.netbeans.spi.project.ui.templates.support.Templates; |
| 39 | |
import org.openide.WizardDescriptor; |
| 40 | |
import org.openide.filesystems.FileObject; |
| 41 | |
import org.openide.filesystems.FileUtil; |
| 42 | |
import org.openide.util.Exceptions; |
| 43 | |
import org.openide.util.NbBundle; |
| 44 | |
import org.openide.xml.XMLUtil; |
| 45 | |
import org.w3c.dom.Document; |
| 46 | |
import org.w3c.dom.Element; |
| 47 | |
import org.w3c.dom.NodeList; |
| 48 | |
import org.xml.sax.InputSource; |
| 49 | |
|
| 50 | |
public class EmptyWebPhotosProjectWizardIterator implements WizardDescriptor.InstantiatingIterator { |
| 51 | |
public static final String PROP_PROJECT_DIR = "projdir"; |
| 52 | |
public static final String PROP_PROJECT_NAME = "name"; |
| 53 | |
|
| 54 | |
private int index; |
| 55 | |
private WizardDescriptor.Panel[] panels; |
| 56 | |
private WizardDescriptor wiz; |
| 57 | |
|
| 58 | 0 | public EmptyWebPhotosProjectWizardIterator() { |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
@TemplateRegistration( |
| 62 | |
folder = EmptyProject.PROJECT_FOLDER, |
| 63 | |
displayName = EmptyProject.PROJECT_DISPLAY_MANE, |
| 64 | |
iconBase = EmptyProject.PROJECT_ICON_BASE, |
| 65 | |
description = EmptyProject.PROJECT_DESCRIPTION) |
| 66 | |
public static EmptyWebPhotosProjectWizardIterator createIterator() { |
| 67 | 0 | return new EmptyWebPhotosProjectWizardIterator(); |
| 68 | |
} |
| 69 | |
|
| 70 | |
private WizardDescriptor.Panel[] createPanels() { |
| 71 | 0 | return new WizardDescriptor.Panel[]{ |
| 72 | |
new EmptyWebPhotosProjectWizardPanel(),}; |
| 73 | |
} |
| 74 | |
|
| 75 | |
private String[] createSteps() { |
| 76 | 0 | return new String[]{ |
| 77 | |
NbBundle.getMessage(EmptyWebPhotosProjectWizardIterator.class, "LBL_CreateProjectStep") |
| 78 | |
}; |
| 79 | |
} |
| 80 | |
|
| 81 | |
@Override |
| 82 | |
public Set<FileObject> instantiate() throws IOException { |
| 83 | 0 | Set<FileObject> resultSet = new LinkedHashSet<FileObject>(); |
| 84 | 0 | File dirF = FileUtil.normalizeFile((File) wiz.getProperty(PROP_PROJECT_DIR)); |
| 85 | 0 | dirF.mkdirs(); |
| 86 | |
|
| 87 | 0 | FileObject template = Templates.getTemplate(wiz); |
| 88 | 0 | FileObject dir = FileUtil.toFileObject(dirF); |
| 89 | 0 | unZipFile(template.getInputStream(), dir); |
| 90 | |
|
| 91 | |
|
| 92 | 0 | resultSet.add(dir); |
| 93 | |
|
| 94 | 0 | Enumeration<? extends FileObject> e = dir.getFolders(true); |
| 95 | 0 | while (e.hasMoreElements()) { |
| 96 | 0 | FileObject subfolder = e.nextElement(); |
| 97 | 0 | if (ProjectManager.getDefault().isProject(subfolder)) { |
| 98 | 0 | resultSet.add(subfolder); |
| 99 | |
} |
| 100 | 0 | } |
| 101 | |
|
| 102 | 0 | File parent = dirF.getParentFile(); |
| 103 | 0 | if (parent != null && parent.exists()) { |
| 104 | 0 | ProjectChooser.setProjectsFolder(parent); |
| 105 | |
} |
| 106 | |
|
| 107 | 0 | return resultSet; |
| 108 | |
} |
| 109 | |
|
| 110 | |
@Override |
| 111 | |
public void initialize(WizardDescriptor wiz) { |
| 112 | 0 | this.wiz = wiz; |
| 113 | 0 | index = 0; |
| 114 | 0 | panels = createPanels(); |
| 115 | |
|
| 116 | 0 | String[] steps = createSteps(); |
| 117 | 0 | for (int i = 0; i < panels.length; i++) { |
| 118 | 0 | Component c = panels[i].getComponent(); |
| 119 | 0 | if (steps[i] == null) { |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | 0 | steps[i] = c.getName(); |
| 124 | |
} |
| 125 | 0 | if (c instanceof JComponent) { |
| 126 | 0 | JComponent jc = (JComponent) c; |
| 127 | |
|
| 128 | 0 | jc.putClientProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, new Integer(i)); |
| 129 | |
|
| 130 | 0 | jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DATA, steps); |
| 131 | |
} |
| 132 | |
} |
| 133 | 0 | } |
| 134 | |
|
| 135 | |
@Override |
| 136 | |
public void uninitialize(WizardDescriptor wiz) { |
| 137 | 0 | this.wiz.putProperty(PROP_PROJECT_DIR, null); |
| 138 | 0 | this.wiz.putProperty(PROP_PROJECT_NAME, null); |
| 139 | 0 | this.wiz = null; |
| 140 | 0 | panels = null; |
| 141 | 0 | } |
| 142 | |
|
| 143 | |
@Override |
| 144 | |
public String name() { |
| 145 | 0 | return MessageFormat.format("{0} of {1}", |
| 146 | |
new Object[]{new Integer(index + 1), new Integer(panels.length)}); |
| 147 | |
} |
| 148 | |
|
| 149 | |
@Override |
| 150 | |
public boolean hasNext() { |
| 151 | 0 | return index < panels.length - 1; |
| 152 | |
} |
| 153 | |
|
| 154 | |
@Override |
| 155 | |
public boolean hasPrevious() { |
| 156 | 0 | return index > 0; |
| 157 | |
} |
| 158 | |
|
| 159 | |
@Override |
| 160 | |
public void nextPanel() { |
| 161 | 0 | if (!hasNext()) { |
| 162 | 0 | throw new NoSuchElementException(); |
| 163 | |
} |
| 164 | 0 | index++; |
| 165 | 0 | } |
| 166 | |
|
| 167 | |
@Override |
| 168 | |
public void previousPanel() { |
| 169 | 0 | if (!hasPrevious()) { |
| 170 | 0 | throw new NoSuchElementException(); |
| 171 | |
} |
| 172 | 0 | index--; |
| 173 | 0 | } |
| 174 | |
|
| 175 | |
@Override |
| 176 | |
public WizardDescriptor.Panel current() { |
| 177 | 0 | return panels[index]; |
| 178 | |
} |
| 179 | |
|
| 180 | |
|
| 181 | |
@Override |
| 182 | |
public final void addChangeListener(ChangeListener l) { |
| 183 | 0 | } |
| 184 | |
|
| 185 | |
@Override |
| 186 | |
public final void removeChangeListener(ChangeListener l) { |
| 187 | 0 | } |
| 188 | |
|
| 189 | |
private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException { |
| 190 | |
try { |
| 191 | 0 | ZipInputStream str = new ZipInputStream(source); |
| 192 | |
ZipEntry entry; |
| 193 | 0 | while ((entry = str.getNextEntry()) != null) { |
| 194 | 0 | if (entry.isDirectory()) { |
| 195 | 0 | FileUtil.createFolder(projectRoot, entry.getName()); |
| 196 | |
} else { |
| 197 | 0 | FileObject fo = FileUtil.createData(projectRoot, entry.getName()); |
| 198 | 0 | if ("nbproject/project.xml".equals(entry.getName())) { |
| 199 | |
|
| 200 | 0 | filterProjectXML(fo, str, projectRoot.getName()); |
| 201 | |
} else { |
| 202 | 0 | writeFile(str, fo); |
| 203 | |
} |
| 204 | 0 | } |
| 205 | |
} |
| 206 | |
} finally { |
| 207 | 0 | source.close(); |
| 208 | 0 | } |
| 209 | 0 | } |
| 210 | |
|
| 211 | |
private static void writeFile(ZipInputStream str, FileObject fo) throws IOException { |
| 212 | 0 | OutputStream out = fo.getOutputStream(); |
| 213 | |
try { |
| 214 | 0 | FileUtil.copy(str, out); |
| 215 | |
} finally { |
| 216 | 0 | out.close(); |
| 217 | 0 | } |
| 218 | 0 | } |
| 219 | |
|
| 220 | |
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException { |
| 221 | |
try { |
| 222 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 223 | 0 | FileUtil.copy(str, baos); |
| 224 | 0 | Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null); |
| 225 | 0 | NodeList nl = doc.getDocumentElement().getElementsByTagName(PROP_PROJECT_NAME); |
| 226 | 0 | if (nl != null) { |
| 227 | 0 | for (int i = 0; i < nl.getLength(); i++) { |
| 228 | 0 | Element el = (Element) nl.item(i); |
| 229 | 0 | if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) { |
| 230 | 0 | NodeList nl2 = el.getChildNodes(); |
| 231 | 0 | if (nl2.getLength() > 0) { |
| 232 | 0 | nl2.item(0).setNodeValue(name); |
| 233 | |
} |
| 234 | |
break; |
| 235 | |
} |
| 236 | |
} |
| 237 | |
} |
| 238 | 0 | OutputStream out = fo.getOutputStream(); |
| 239 | |
try { |
| 240 | 0 | XMLUtil.write(doc, out, "UTF-8"); |
| 241 | |
} finally { |
| 242 | 0 | out.close(); |
| 243 | 0 | } |
| 244 | 0 | } catch (Exception ex) { |
| 245 | 0 | Exceptions.printStackTrace(ex); |
| 246 | 0 | writeFile(str, fo); |
| 247 | 0 | } |
| 248 | |
|
| 249 | 0 | } |
| 250 | |
} |