1 19 20 package org.netbeans.modules.javawebstart.ui.customizer; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import java.util.Collection ; 28 import java.util.Properties ; 29 import java.util.ResourceBundle ; 30 import javax.swing.JComponent ; 31 32 import org.netbeans.api.project.Project; 33 import org.netbeans.api.project.ProjectManager; 34 import org.netbeans.modules.java.j2seproject.api.J2SEProjectConfigurations; 35 import org.netbeans.spi.project.ProjectConfiguration; 36 import org.netbeans.spi.project.ProjectConfigurationProvider; 37 import org.netbeans.spi.project.ui.support.ProjectCustomizer; 38 import org.openide.ErrorManager; 39 import org.openide.filesystems.FileLock; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileSystem; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.filesystems.Repository; 44 45 import org.openide.util.Lookup; 46 import org.openide.util.Mutex; 47 import org.openide.util.MutexException; 48 import org.openide.util.NbBundle; 49 import org.openide.xml.XMLUtil; 50 import org.w3c.dom.Document ; 51 import org.w3c.dom.Element ; 52 import org.w3c.dom.Node ; 53 import org.w3c.dom.NodeList ; 54 import org.xml.sax.InputSource ; 55 import org.xml.sax.SAXException ; 56 57 61 public class JWSCompositeCategoryProvider implements ProjectCustomizer.CompositeCategoryProvider { 62 63 private static final String CAT_WEBSTART = "WebStart"; 64 private String catName = null; 65 66 private static JWSProjectProperties jwsProps = null; 67 68 public JWSCompositeCategoryProvider(String name) { 69 catName = name; 70 } 71 72 public ProjectCustomizer.Category createCategory(Lookup context) { 73 ResourceBundle bundle = NbBundle.getBundle(JWSCompositeCategoryProvider.class); 74 ProjectCustomizer.Category category = null; 75 if (CAT_WEBSTART.equals(catName)) { 76 category = ProjectCustomizer.Category.create(CAT_WEBSTART, 77 bundle.getString("LBL_Category_WebStart"), null, null); 78 } 79 return category; 80 } 81 82 public JComponent createComponent(ProjectCustomizer.Category category, Lookup context) { 83 String name = category.getName(); 84 JComponent component = null; 85 if (CAT_WEBSTART.equals(name)) { 86 jwsProps = new JWSProjectProperties(context); 87 category.setOkButtonListener(new SavePropsListener(jwsProps, context.lookup(Project.class))); 88 component = new JWSCustomizerPanel(jwsProps); 89 } 90 return component; 91 } 92 93 95 public static JWSCompositeCategoryProvider createWebStart() { 96 return new JWSCompositeCategoryProvider(CAT_WEBSTART); 97 } 98 99 101 private static class SavePropsListener implements ActionListener { 102 103 private JWSProjectProperties jwsProps; 104 private Project j2seProject; 105 106 public SavePropsListener(JWSProjectProperties props, Project proj) { 107 jwsProps = props; 108 j2seProject = proj; 109 } 110 111 public void actionPerformed(ActionEvent e) { 112 try { 114 jwsProps.store(); 115 } catch (IOException ioe) { 116 ErrorManager.getDefault().notify(ioe); 117 } 118 boolean enabled = Boolean.valueOf(jwsProps.getProperty(JWSProjectProperties.JNLP_ENABLED)).booleanValue(); 119 final ProjectConfigurationProvider configProvider = 120 j2seProject.getLookup().lookup(ProjectConfigurationProvider.class); 121 try { 122 if (enabled) { 123 J2SEProjectConfigurations.createConfigurationFiles(j2seProject, "JWS_generated", 126 prepareSharedProps(), null ); setActiveConfig(configProvider, "Web Start"); copyTemplate(j2seProject); 129 modifyBuildXml(j2seProject); 130 } else { 131 setActiveConfig(configProvider, "<default>"); } 133 } catch (IOException ioe) { 134 ErrorManager.getDefault().notify(ioe); 135 } 136 } 137 138 private void setActiveConfig(final ProjectConfigurationProvider provider, String displayName) throws IOException { 139 Collection <ProjectConfiguration> configs = provider.getConfigurations(); 140 for (final ProjectConfiguration c : configs) { 141 if (displayName.equals(c.getDisplayName())) { 142 try { 143 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void >() { 144 public Void run() throws Exception { 145 provider.setActiveConfiguration(c); 146 return null; 147 } 148 }); 149 } catch (MutexException mex) { 150 throw (IOException ) mex.getException(); 151 } 152 } 153 } 154 } 155 156 private void copyTemplate(Project proj) throws IOException { 157 FileObject projDir = proj.getProjectDirectory(); 158 FileObject jnlpBuildFile = projDir.getFileObject("nbproject/jnlp-impl.xml"); if (jnlpBuildFile == null) { 160 FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); 161 FileObject templateFO = sfs.findResource("Templates/JWS/jnlp-impl.xml"); if (templateFO != null) { 163 FileUtil.copyFile(templateFO, projDir.getFileObject("nbproject"), "jnlp-impl"); } 165 } 166 } 167 168 private void modifyBuildXml(Project proj) throws IOException { 169 FileObject projDir = proj.getProjectDirectory(); 170 final FileObject buildXmlFO = projDir.getFileObject("build.xml"); File buildXmlFile = FileUtil.toFile(buildXmlFO); 172 Document xmlDoc = null; 173 try { 174 xmlDoc = XMLUtil.parse(new InputSource (buildXmlFile.toURI().toString()), false, true, null, null); 175 } catch (SAXException ex) { 176 ErrorManager.getDefault().notify(ex); 177 } 178 Element docElem = xmlDoc.getDocumentElement(); 180 NodeList nl = docElem.getElementsByTagName("import"); if (nl.getLength() == 1) { 182 Element importElem = xmlDoc.createElement("import"); importElem.setAttribute("file", "nbproject/jnlp-impl.xml"); Node n = nl.item(0).getNextSibling(); 185 docElem.insertBefore(importElem, n); 186 Element targetElem = xmlDoc.createElement("target"); targetElem.setAttribute("name", "-post-jar"); targetElem.setAttribute("depends", "jnlp"); n = importElem.getNextSibling(); 190 docElem.insertBefore(targetElem, n); 191 final Document fdoc = xmlDoc; 192 try { 193 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void >() { 194 public Void run() throws Exception { 195 FileLock lock = buildXmlFO.lock(); 196 try { 197 OutputStream os = buildXmlFO.getOutputStream(lock); 198 try { 199 XMLUtil.write(fdoc, os, "UTF-8"); } finally { 201 os.close(); 202 } 203 } finally { 204 lock.releaseLock(); 205 } 206 return null; 207 } 208 }); 209 } catch (MutexException mex) { 210 throw (IOException ) mex.getException(); 211 } 212 } 213 } 214 215 private Properties prepareSharedProps() { 216 Properties props = new Properties (); 217 props.setProperty("$label", "Web Start"); props.setProperty("$target.run", "jws-run"); props.setProperty("$target.debug", "jws-debug"); return props; 221 } 222 223 } 224 225 } 226 | Popular Tags |