KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.swing.ComboBoxModel JavaDoc;
32 import javax.swing.DefaultComboBoxModel JavaDoc;
33 import javax.swing.JToggleButton JavaDoc;
34 import javax.swing.text.BadLocationException JavaDoc;
35 import javax.swing.text.DefaultStyledDocument JavaDoc;
36 import javax.swing.text.Document JavaDoc;
37 import javax.swing.text.PlainDocument JavaDoc;
38 import javax.swing.text.StyledDocument JavaDoc;
39
40 import org.netbeans.api.project.Project;
41 import org.netbeans.api.project.ProjectManager;
42 import org.netbeans.modules.java.j2seproject.api.J2SEPropertyEvaluator;
43 import org.netbeans.spi.project.support.ant.EditableProperties;
44 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
45 import org.netbeans.spi.project.support.ant.ui.StoreGroup;
46 import org.openide.filesystems.FileLock;
47 import org.openide.filesystems.FileObject;
48 import org.openide.filesystems.FileSystem;
49 import org.openide.filesystems.FileUtil;
50 import org.openide.util.Lookup;
51 import org.openide.util.Mutex;
52 import org.openide.util.MutexException;
53 import org.openide.util.NbBundle;
54
55 /**
56  *
57  * @author Milan Kubec
58  */

59 public class JWSProjectProperties {
60     
61     public static final String JavaDoc JNLP_ENABLED = "jnlp.enabled";
62     //public static final String JNLP_DESC_SHORT = "jnlp.desc.short";
63
public static final String JavaDoc JNLP_ICON = "jnlp.icon";
64     public static final String JavaDoc JNLP_OFFLINE = "jnlp.offline-allowed";
65     public static final String JavaDoc JNLP_CBASE_TYPE = "jnlp.codebase.type";
66     public static final String JavaDoc JNLP_CBASE_USER = "jnlp.codebase.user";
67     public static final String JavaDoc JNLP_CBASE_URL = "jnlp.codebase.url";
68     
69     public static final String JavaDoc JNLP_SPEC = "jnlp.spec";
70     public static final String JavaDoc JNLP_INIT_HEAP = "jnlp.initial-heap-size";
71     public static final String JavaDoc JNLP_MAX_HEAP = "jnlp.max-heap-size";
72     
73     public static final String JavaDoc JNLP_SIGN_ENABLED = "jnlp.signing.enabled";
74     
75     public static final String JavaDoc CB_TYPE_LOCAL = "local";
76     public static final String JavaDoc CB_TYPE_WEB = "web";
77     public static final String JavaDoc CB_TYPE_USER = "user";
78     
79     public static final String JavaDoc CB_URL_WEB = "$$codebase";
80     
81     // special value to persist Ant script handling
82
public static final String JavaDoc CB_URL_WEB_PROP_VALUE = "$$$$codebase";
83     
84     private StoreGroup jnlpPropGroup = new StoreGroup();
85     
86     private J2SEPropertyEvaluator j2sePropEval;
87     private PropertyEvaluator evaluator;
88     private Project j2seProject;
89     
90     // Models
91
JToggleButton.ToggleButtonModel JavaDoc enabledModel;
92     JToggleButton.ToggleButtonModel JavaDoc allowOfflineModel;
93     ComboBoxModel JavaDoc codebaseModel;
94     
95     // and Documents
96
Document JavaDoc iconDocument;
97     Document JavaDoc codebaseURLDocument;
98     
99     /** Creates a new instance of JWSProjectProperties */
100     public JWSProjectProperties(Lookup context) {
101         
102         j2seProject = (Project) context.lookup(Project.class);
103         if (j2seProject != null) {
104             j2sePropEval = (J2SEPropertyEvaluator) j2seProject.getLookup().lookup(J2SEPropertyEvaluator.class);
105         } else {
106             // XXX
107
}
108         
109         evaluator = j2sePropEval.evaluator();
110         
111         enabledModel = jnlpPropGroup.createToggleButtonModel(evaluator, JNLP_ENABLED);
112         allowOfflineModel = jnlpPropGroup.createToggleButtonModel(evaluator, JNLP_OFFLINE);
113         iconDocument = jnlpPropGroup.createStringDocument(evaluator, JNLP_ICON);
114         
115         codebaseModel = new CodebaseComboBoxModel();
116         codebaseURLDocument = createCBTextFieldDocument();
117         
118     }
119     
120     private void storeRest(EditableProperties editableProps) {
121         String JavaDoc selItem = ((CodebaseComboBoxModel) codebaseModel).getSelectedCodebaseItem();
122         String JavaDoc propName = null;
123         String JavaDoc propValue = null;
124         if (CB_TYPE_USER.equals(selItem)) {
125             propName = JNLP_CBASE_USER;
126             try {
127                 propValue = codebaseURLDocument.getText(0, codebaseURLDocument.getLength());
128             } catch (BadLocationException JavaDoc ex) {
129                 // do not store anything
130
// XXX log the exc
131
return;
132             }
133         } else if (CB_TYPE_LOCAL.equals(selItem)) {
134             propName = JNLP_CBASE_URL;
135             propValue = getProjectDistDir();
136         } else if (CB_TYPE_WEB.equals(selItem)) {
137             propName = JNLP_CBASE_URL;
138             propValue = CB_URL_WEB_PROP_VALUE;
139         }
140         if (propName == null || propValue == null) {
141             return;
142         } else {
143             editableProps.setProperty(JNLP_CBASE_TYPE, selItem);
144             editableProps.setProperty(propName, propValue);
145         }
146     }
147     
148     public void store() throws IOException JavaDoc {
149         
150         final EditableProperties ep = new EditableProperties(true);
151         final FileObject projPropsFO = j2seProject.getProjectDirectory().getFileObject("nbproject/project.properties");
152         
153         try {
154             final InputStream JavaDoc is = projPropsFO.getInputStream();
155             ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void JavaDoc>() {
156                 public Void JavaDoc run() throws Exception JavaDoc {
157                     try {
158                         ep.load(is);
159                     } finally {
160                         if (is != null) {
161                             is.close();
162                         }
163                     }
164                     jnlpPropGroup.store(ep);
165                     storeRest(ep);
166                     OutputStream JavaDoc os = null;
167                     FileLock lock = null;
168                     try {
169                         lock = projPropsFO.lock();
170                         os = projPropsFO.getOutputStream(lock);
171                         ep.store(os);
172                     } finally {
173                         if (lock != null) {
174                             lock.releaseLock();
175                         }
176                         if (os != null) {
177                             os.close();
178                         }
179                     }
180                     return null;
181                 }
182             });
183         } catch (MutexException mux) {
184             throw (IOException JavaDoc) mux.getException();
185         }
186         
187     }
188     
189     private Document JavaDoc createCBTextFieldDocument() {
190         Document JavaDoc doc = new PlainDocument JavaDoc();
191         String JavaDoc valueURL = evaluator.getProperty(JNLP_CBASE_USER);
192         String JavaDoc valueType = evaluator.getProperty(JNLP_CBASE_TYPE);
193         String JavaDoc docString = "";
194         if (CB_TYPE_LOCAL.equals(valueType)) {
195             docString = getProjectDistDir();
196         } else if (CB_TYPE_WEB.equals(valueType)) {
197             docString = CB_URL_WEB;
198         } else if (CB_TYPE_USER.equals(valueType)) {
199             docString = getCodebaseLocation();
200         }
201         try {
202             doc.insertString(0, docString, null);
203         } catch (BadLocationException JavaDoc ex) {
204             // do nothing, just return PlainDocument
205
// XXX log the exc
206
}
207         return doc;
208     }
209     /*
210     private StyledDocument createDescTextAreaDocument() {
211         StyledDocument doc = new DefaultStyledDocument();
212         String docString = "";
213         docString = evaluator.getProperty(JNLP_DESC);
214         try {
215             doc.insertString(0, docString, null);
216         } catch (BadLocationException ex) {
217             // do nothing, just return DefaultStyledDocument
218         }
219         return doc;
220     }
221     */

222     public String JavaDoc getCodebaseLocation() {
223         return evaluator.getProperty(JNLP_CBASE_USER);
224     }
225         
226     public String JavaDoc getProjectDistDir() {
227         File JavaDoc distDir = new File JavaDoc(FileUtil.toFile(j2seProject.getProjectDirectory()), evaluator.getProperty("dist.dir"));
228         return distDir.toURI().toString();
229     }
230     
231     // only should return JNLP properties
232
public String JavaDoc getProperty(String JavaDoc propName) {
233         return evaluator.getProperty(propName);
234     }
235     
236     // ----------
237

238     public class CodebaseComboBoxModel extends DefaultComboBoxModel JavaDoc {
239         
240         String JavaDoc localLabel = NbBundle.getBundle(JWSProjectProperties.class).getString("LBL_CB_Combo_Local");
241         String JavaDoc webLabel = NbBundle.getBundle(JWSProjectProperties.class).getString("LBL_CB_Combo_Web");
242         String JavaDoc userLabel = NbBundle.getBundle(JWSProjectProperties.class).getString("LBL_CB_Combo_User");
243         Object JavaDoc visItems[] = new Object JavaDoc[] { localLabel, webLabel, userLabel };
244         String JavaDoc cbItems[] = new String JavaDoc[] { CB_TYPE_LOCAL, CB_TYPE_WEB, CB_TYPE_USER };
245         
246         public CodebaseComboBoxModel() {
247             super();
248             addElement(visItems[0]);
249             addElement(visItems[1]);
250             addElement(visItems[2]);
251             String JavaDoc propValue = evaluator.getProperty(JNLP_CBASE_TYPE);
252             if (cbItems[2].equals(propValue)) {
253                 setSelectedItem(visItems[2]);
254             } else if (cbItems[1].equals(propValue)) {
255                 setSelectedItem(visItems[1]);
256             } else {
257                 setSelectedItem(visItems[0]);
258             }
259         }
260         
261         public String JavaDoc getSelectedCodebaseItem() {
262             return cbItems[getIndexOf(getSelectedItem())];
263         }
264         
265     }
266     
267 }
268
Popular Tags