KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > customizer > ModuleProperties


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.apisupport.project.ui.customizer;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import org.netbeans.api.java.platform.JavaPlatform;
31 import org.netbeans.api.java.platform.JavaPlatformManager;
32 import org.netbeans.api.project.ProjectManager;
33 import org.netbeans.modules.apisupport.project.NbModuleProjectGenerator;
34 import org.netbeans.modules.apisupport.project.Util;
35 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
36 import org.netbeans.spi.project.support.ant.AntProjectHelper;
37 import org.netbeans.spi.project.support.ant.EditableProperties;
38 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
39 import org.openide.DialogDescriptor;
40 import org.openide.DialogDisplayer;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.util.Mutex;
44 import org.openide.util.MutexException;
45 import org.openide.util.NbBundle;
46 import org.openide.util.RequestProcessor;
47
48 /**
49  * Basic support for storing general module's properties.
50  *
51  * @author Martin Krauskopf
52  */

53 public abstract class ModuleProperties {
54     
55     public static final String JavaDoc PROPERTIES_REFRESHED = "propertiesRefreshed"; // NOI18N
56

57     static final RequestProcessor RP = new RequestProcessor(ModuleProperties.class.getName());
58     
59     // XXX better to inject own DialogDisplayer when running tests or similar trick
60
static boolean runFromTests;
61     
62     // Helpers for storing and retrieving real values currently stored on the disk
63
private final AntProjectHelper helper;
64     private final PropertyEvaluator evaluator;
65     
66     /** Represent main module's properties (nbproject/project.properties). */
67     private EditableProperties projectProperties;
68     
69     /** Represent module's private properties (nbproject/private/private.properties). */
70     private EditableProperties privateProperties;
71     
72     private final PropertyChangeSupport JavaDoc changeSupport = new PropertyChangeSupport JavaDoc(this);
73     
74     /** Creates a new instance of ModuleProperties */
75     ModuleProperties(AntProjectHelper helper, PropertyEvaluator evaluator) {
76         this.helper = helper;
77         this.evaluator = evaluator;
78         reloadProperties();
79     }
80     
81     protected void reloadProperties() {
82         this.projectProperties = helper.getProperties(
83                 AntProjectHelper.PROJECT_PROPERTIES_PATH);
84         this.privateProperties = helper.getProperties(
85                 AntProjectHelper.PRIVATE_PROPERTIES_PATH);
86     }
87     
88     /**
89      * Returns map of keys form main module properties and their default values.
90      * Must be overriden by a subclass.
91      */

92     abstract Map JavaDoc<String JavaDoc, String JavaDoc> getDefaultValues();
93     
94     AntProjectHelper getHelper() {
95         return helper;
96     }
97     
98     PropertyEvaluator getEvaluator() {
99         return evaluator;
100     }
101     
102     EditableProperties getProjectProperties() {
103         return projectProperties;
104     }
105     
106     EditableProperties getPrivateProperties() {
107         return privateProperties;
108     }
109     
110     final String JavaDoc getProperty(String JavaDoc key) {
111         String JavaDoc value = getProjectProperties().getProperty(key);
112         return value != null ? value : (String JavaDoc) getDefaultValues().get(key);
113     }
114     
115     final boolean getBooleanProperty(String JavaDoc key) {
116         String JavaDoc bValue = getProperty(key);
117         return bValue != null && (bValue.equalsIgnoreCase("true") || // NOI18N
118
bValue.equalsIgnoreCase("yes")); // NOI18N
119
}
120     
121     final String JavaDoc removeProperty(String JavaDoc key) {
122         return (String JavaDoc) getProjectProperties().remove(key);
123     }
124     
125     final String JavaDoc removePrivateProperty(String JavaDoc key) {
126         return (String JavaDoc) getPrivateProperties().remove(key);
127     }
128     
129     /**
130      * The given property will be stored into the project's properties. If the
131      * given value is equals to the default value it will be removed from the
132      * properties.
133      */

134     final void setProperty(String JavaDoc key, String JavaDoc value) {
135         String JavaDoc def = (String JavaDoc) getDefaultValues().get(key);
136         if (def == null) {
137             def = ""; // NOI18N
138
}
139         if (value == null || def.equals(value)) {
140             getProjectProperties().remove(key);
141         } else {
142             getProjectProperties().setProperty(key, value);
143         }
144         firePropertyChange(key, null, value);
145     }
146     
147     /**
148      * The given property will be stored into the project's properties. If the
149      * given value is equals to the default value it will be removed from the
150      * properties.
151      */

152     final void setPrivateProperty(String JavaDoc key, String JavaDoc value) {
153         String JavaDoc def = (String JavaDoc) getDefaultValues().get(key);
154         if (def == null) {
155             def = ""; // NOI18N
156
}
157         if (def.equals(value)) {
158             getPrivateProperties().remove(key);
159         } else {
160             getPrivateProperties().setProperty(key, value);
161         }
162         firePropertyChange(key, null, value);
163     }
164     
165     void setProperty(String JavaDoc key, String JavaDoc[] value) {
166         getProjectProperties().setProperty(key, value);
167         firePropertyChange(key, null, null);
168     }
169     
170     final void setBooleanProperty(String JavaDoc key, boolean bProp) {
171         setProperty(key, Boolean.toString(bProp));
172     }
173     
174     String JavaDoc getProjectDisplayName() {
175         return Util.getDisplayName(getHelper().getProjectDirectory());
176     }
177     
178     final File JavaDoc getProjectDirectoryFile() {
179         return FileUtil.toFile(getHelper().getProjectDirectory());
180     }
181     
182     final String JavaDoc getProjectDirectory() {
183         return getProjectDirectoryFile().getAbsolutePath();
184     }
185     
186     /**
187      * Stores cached properties. This is called when the user press <em>OK</em>
188      * button in the properties customizer. If <em>Cancel</em> button is
189      * pressed properties will not be saved. Be sure this method is called
190      * whitin {@link ProjectManager#mutex}. However, you are well advised to
191      * explicitly enclose a <em>complete</em> operation within write access to
192      * prevent race conditions.
193      */

194     void storeProperties() throws IOException JavaDoc {
195         // Store changes into in nbproject/project.properties
196
getHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,
197                 getProjectProperties());
198         // Store changes into in nbproject/private/private.properties
199
getHelper().putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH,
200                 getPrivateProperties());
201     }
202     
203     /**
204      * Helper method for storing platform into the appropriate place.
205      */

206     static void storePlatform(AntProjectHelper helper, NbPlatform platform) {
207         if (platform != null && platform.getID() != null) {
208             // store platform properties
209
EditableProperties props = helper.getProperties(
210                     NbModuleProjectGenerator.PLATFORM_PROPERTIES_PATH);
211             props.put("nbplatform.active", platform.getID()); // NOI18N
212
helper.putProperties(
213                     NbModuleProjectGenerator.PLATFORM_PROPERTIES_PATH, props);
214         }
215     }
216     
217     public void addPropertyChangeListener(PropertyChangeListener JavaDoc pchl) {
218         changeSupport.addPropertyChangeListener(pchl);
219     }
220     
221     public void removePropertyChangeListener(PropertyChangeListener JavaDoc pchl) {
222         changeSupport.removePropertyChangeListener(pchl);
223     }
224     
225     protected void firePropertyChange(String JavaDoc propName, Object JavaDoc oldValue, Object JavaDoc newValue) {
226         changeSupport.firePropertyChange(propName, oldValue, newValue);
227     }
228     
229     protected void firePropertiesRefreshed() {
230         firePropertyChange(PROPERTIES_REFRESHED, null, null);
231     }
232     
233     protected static void reportLostPlatform(final NbPlatform lostPlatform) {
234         String JavaDoc plafText = lostPlatform != null
235                 ? '"' + lostPlatform.getLabel() + '"'
236                 : NbBundle.getMessage(ModuleProperties.class, "MSG_PreviouslySet");
237         String JavaDoc message = NbBundle.getMessage(ModuleProperties.class, "MSG_PlatformNotFound", plafText);
238         if (!runFromTests) {
239             DialogDisplayer.getDefault().notify(new DialogDescriptor.Message(message));
240         } else {
241             System.err.println(message);
242         }
243     }
244     
245     static String JavaDoc getPlatformID(JavaPlatform platform) {
246         // XXX why isn't there a real API for this??
247
String JavaDoc id = (String JavaDoc) platform.getProperties().get("platform.ant.name"); // NOI18N
248
// Handle case that a platform is not an Ant platform:
249
return id != null ? id : "default"; // NOI18N
250
}
251     
252     static JavaPlatform findJavaPlatformByID(String JavaDoc id) {
253         if (id == null || id.equals("default")) { // NOI18N
254
return JavaPlatform.getDefault();
255         }
256         JavaPlatform[] platforms = JavaPlatformManager.getDefault().getInstalledPlatforms();
257         for (int i = 0; i < platforms.length; i++) {
258             if (id.equals(getPlatformID(platforms[i]))) {
259                 return platforms[i];
260             }
261         }
262         return null;
263     }
264     
265     static void storeJavaPlatform(AntProjectHelper helper, PropertyEvaluator eval, JavaPlatform platform, boolean isNetBeansOrg) throws IOException JavaDoc {
266         if (isNetBeansOrg) {
267             final boolean isDefault = platform == null || platform == JavaPlatform.getDefault();
268             final File JavaDoc home = isDefault ? null : getPlatformLocation(platform);
269             if (home != null || isDefault) {
270                 final FileObject nbbuild = helper.resolveFileObject(eval.evaluate("${nb_all}/nbbuild")); // NOI18N
271
if (nbbuild != null) {
272                     try {
273                         ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() {
274                             public Object JavaDoc run() throws IOException JavaDoc {
275                                 FileObject userBuildProperties = nbbuild.getFileObject("user.build.properties"); // NOI18N
276
if (userBuildProperties == null) {
277                                     userBuildProperties = nbbuild.createData("user.build.properties"); // NOI18N
278
}
279                                 EditableProperties ep = Util.loadProperties(userBuildProperties);
280                                 if (isDefault) {
281                                     // Have to remove it; no default value.
282
ep.remove("nbjdk.home");
283                                 } else {
284                                     ep.setProperty("nbjdk.home", home.getAbsolutePath());
285                                 }
286                                 Util.storeProperties(userBuildProperties, ep);
287                                 return null;
288                             }
289                         });
290                     } catch (MutexException e) {
291                         throw (IOException JavaDoc) e.getException();
292                     }
293                 }
294             }
295         } else {
296             EditableProperties props = helper.getProperties(NbModuleProjectGenerator.PLATFORM_PROPERTIES_PATH);
297             if (platform == null || platform == JavaPlatform.getDefault()) {
298                 if (props.containsKey("nbjdk.active")) { // NOI18N
299
// Could also just remove it, but probably nicer to set it explicitly to 'default'.
300
props.put("nbjdk.active", "default"); // NOI18N
301
}
302             } else {
303                 props.put("nbjdk.active", getPlatformID(platform)); // NOI18N
304
}
305             helper.putProperties(NbModuleProjectGenerator.PLATFORM_PROPERTIES_PATH, props);
306         }
307     }
308     
309     private static File JavaDoc getPlatformLocation(JavaPlatform platform) {
310         Collection JavaDoc<FileObject> installs = platform.getInstallFolders();
311         if (installs.size() == 1) {
312             return FileUtil.toFile((FileObject) installs.iterator().next());
313         } else {
314             return null;
315         }
316     }
317     
318     public static JavaPlatform findJavaPlatformByLocation(String JavaDoc home) {
319         if (home == null) {
320             return JavaPlatform.getDefault();
321         }
322         JavaPlatform[] platforms = JavaPlatformManager.getDefault().getInstalledPlatforms();
323         for (int i = 0; i < platforms.length; i++) {
324             if (new File JavaDoc(home).equals(getPlatformLocation(platforms[i]))) {
325                 return platforms[i];
326             }
327         }
328         return null;
329     }
330     
331     
332     void addLazyStorage(LazyStorage st) {
333         storages.add(st);
334     }
335     
336     void triggerLazyStorages() {
337         for (LazyStorage stor : storages) {
338             stor.store();
339         }
340     }
341     
342     private List JavaDoc<LazyStorage> storages = new ArrayList JavaDoc<LazyStorage>();
343     /**
344      * Implement this interface when you want your panel to be told that the
345      * properties/customizer are going to be saved.
346      */

347     static interface LazyStorage {
348         
349         /** Called when user pressed <em>ok</em> before storing the model. */
350         void store();
351         
352     }
353     
354 }
355
Popular Tags