KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javawebstart > ui > customizer > JWSCompositeCategoryProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.javawebstart.ui.customizer;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30 import javax.swing.JComponent JavaDoc;
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 JavaDoc;
51 import org.w3c.dom.Element JavaDoc;
52 import org.w3c.dom.Node JavaDoc;
53 import org.w3c.dom.NodeList JavaDoc;
54 import org.xml.sax.InputSource JavaDoc;
55 import org.xml.sax.SAXException JavaDoc;
56
57 /**
58  *
59  * @author Milan Kubec
60  */

61 public class JWSCompositeCategoryProvider implements ProjectCustomizer.CompositeCategoryProvider {
62     
63     private static final String JavaDoc CAT_WEBSTART = "WebStart";
64     private String JavaDoc catName = null;
65     
66     private static JWSProjectProperties jwsProps = null;
67     
68     public JWSCompositeCategoryProvider(String JavaDoc name) {
69         catName = name;
70     }
71     
72     public ProjectCustomizer.Category createCategory(Lookup context) {
73         ResourceBundle JavaDoc 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 JavaDoc createComponent(ProjectCustomizer.Category category, Lookup context) {
83         String JavaDoc name = category.getName();
84         JComponent JavaDoc 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     // ----------
94

95     public static JWSCompositeCategoryProvider createWebStart() {
96         return new JWSCompositeCategoryProvider(CAT_WEBSTART);
97     }
98     
99     // ----------
100

101     private static class SavePropsListener implements ActionListener JavaDoc {
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 JavaDoc e) {
112             // log("Saving Properties " + jwsProps + " ...");
113
try {
114                 jwsProps.store();
115             } catch (IOException JavaDoc 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                     // XXX logging
124
// test if the file already exists, if so do not generate, just set as active
125
J2SEProjectConfigurations.createConfigurationFiles(j2seProject, "JWS_generated",
126                             prepareSharedProps(), null /*or new Properties()*/); // NOI18N
127
setActiveConfig(configProvider, "Web Start"); // XXX from bundle
128
copyTemplate(j2seProject);
129                     modifyBuildXml(j2seProject);
130                 } else {
131                     setActiveConfig(configProvider, "<default>"); // XXX from bundle?
132
}
133             } catch (IOException JavaDoc ioe) {
134                 ErrorManager.getDefault().notify(ioe);
135             }
136         }
137         
138         private void setActiveConfig(final ProjectConfigurationProvider provider, String JavaDoc displayName) throws IOException JavaDoc {
139             Collection JavaDoc<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 JavaDoc>() {
144                             public Void JavaDoc run() throws Exception JavaDoc {
145                                 provider.setActiveConfiguration(c);
146                                 return null;
147                             }
148                         });
149                     } catch (MutexException mex) {
150                         throw (IOException JavaDoc) mex.getException();
151                     }
152                 }
153             }
154         }
155         
156         private void copyTemplate(Project proj) throws IOException JavaDoc {
157             FileObject projDir = proj.getProjectDirectory();
158             FileObject jnlpBuildFile = projDir.getFileObject("nbproject/jnlp-impl.xml"); // NOI18N
159
if (jnlpBuildFile == null) {
160                 FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
161                 FileObject templateFO = sfs.findResource("Templates/JWS/jnlp-impl.xml"); // NOI18N
162
if (templateFO != null) {
163                     FileUtil.copyFile(templateFO, projDir.getFileObject("nbproject"), "jnlp-impl"); // NOI18N
164
}
165             }
166         }
167         
168         private void modifyBuildXml(Project proj) throws IOException JavaDoc {
169             FileObject projDir = proj.getProjectDirectory();
170             final FileObject buildXmlFO = projDir.getFileObject("build.xml"); // NOI18N
171
File JavaDoc buildXmlFile = FileUtil.toFile(buildXmlFO);
172             Document JavaDoc xmlDoc = null;
173             try {
174                 xmlDoc = XMLUtil.parse(new InputSource JavaDoc(buildXmlFile.toURI().toString()), false, true, null, null);
175             } catch (SAXException JavaDoc ex) {
176                 ErrorManager.getDefault().notify(ex);
177             }
178             // XXX test if it's already modified - then do not save!
179
Element JavaDoc docElem = xmlDoc.getDocumentElement();
180             NodeList JavaDoc nl = docElem.getElementsByTagName("import"); // NOI18N
181
if (nl.getLength() == 1) {
182                 Element JavaDoc importElem = xmlDoc.createElement("import"); // NOI18N
183
importElem.setAttribute("file", "nbproject/jnlp-impl.xml"); // NOI18N
184
Node JavaDoc n = nl.item(0).getNextSibling();
185                 docElem.insertBefore(importElem, n);
186                 Element JavaDoc targetElem = xmlDoc.createElement("target"); // NOI18N
187
targetElem.setAttribute("name", "-post-jar"); // NOI18N
188
targetElem.setAttribute("depends", "jnlp"); // NOI18N
189
n = importElem.getNextSibling();
190                 docElem.insertBefore(targetElem, n);
191                 final Document JavaDoc fdoc = xmlDoc;
192                 try {
193                     ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void JavaDoc>() {
194                         public Void JavaDoc run() throws Exception JavaDoc {
195                             FileLock lock = buildXmlFO.lock();
196                             try {
197                                 OutputStream JavaDoc os = buildXmlFO.getOutputStream(lock);
198                                 try {
199                                     XMLUtil.write(fdoc, os, "UTF-8"); // NOI18N
200
} finally {
201                                     os.close();
202                                 }
203                             } finally {
204                                 lock.releaseLock();
205                             }
206                             return null;
207                         }
208                     });
209                 } catch (MutexException mex) {
210                     throw (IOException JavaDoc) mex.getException();
211                 }
212             }
213         }
214         
215         private Properties JavaDoc prepareSharedProps() {
216             Properties JavaDoc props = new Properties JavaDoc();
217             props.setProperty("$label", "Web Start"); // XXX get from bundle
218
props.setProperty("$target.run", "jws-run"); // NOI18N
219
props.setProperty("$target.debug", "jws-debug"); // NOI18N
220
return props;
221         }
222         
223     }
224     
225 }
226
Popular Tags