KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > suite > SuiteProject


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.suite;
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.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.swing.Icon JavaDoc;
33 import javax.swing.ImageIcon JavaDoc;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.api.project.ProjectInformation;
36 import org.netbeans.api.project.ProjectManager;
37 import org.netbeans.modules.apisupport.project.SuiteProvider;
38 import org.netbeans.modules.apisupport.project.Util;
39 import org.netbeans.modules.apisupport.project.ui.SuiteActions;
40 import org.netbeans.modules.apisupport.project.ui.SuiteLogicalView;
41 import org.netbeans.modules.apisupport.project.ui.SuiteOperations;
42 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteCustomizer;
43 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteProperties;
44 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
45 import org.netbeans.spi.project.support.LookupProviderSupport;
46 import org.netbeans.spi.project.support.ant.AntProjectEvent;
47 import org.netbeans.spi.project.support.ant.AntProjectHelper;
48 import org.netbeans.spi.project.support.ant.AntProjectListener;
49 import org.netbeans.spi.project.support.ant.EditableProperties;
50 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
51 import org.netbeans.spi.project.support.ant.ProjectXmlSavedHook;
52 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
53 import org.netbeans.spi.project.support.ant.PropertyProvider;
54 import org.netbeans.spi.project.support.ant.PropertyUtils;
55 import org.netbeans.spi.project.ui.PrivilegedTemplates;
56 import org.netbeans.spi.project.ui.ProjectOpenedHook;
57 import org.netbeans.spi.project.ui.RecommendedTemplates;
58 import org.netbeans.spi.project.ui.support.UILookupMergerSupport;
59 import org.openide.ErrorManager;
60 import org.openide.filesystems.FileObject;
61 import org.openide.filesystems.FileUtil;
62 import org.openide.util.Lookup;
63 import org.openide.util.Mutex;
64 import org.openide.util.Utilities;
65 import org.openide.util.lookup.Lookups;
66 import org.w3c.dom.Element JavaDoc;
67
68 /**
69  * Represents one module suite project.
70  * @author Jesse Glick
71  */

72 public final class SuiteProject implements Project {
73     
74     public static final String JavaDoc SUITE_ICON_PATH =
75             "org/netbeans/modules/apisupport/project/suite/resources/suite.gif"; // NOI18N
76

77     private final AntProjectHelper helper;
78     private Lookup lookup;
79     private final PropertyEvaluator eval;
80     private final GeneratedFilesHelper genFilesHelper;
81     
82     public SuiteProject(AntProjectHelper helper) throws IOException JavaDoc {
83         this.helper = helper;
84         eval = createEvaluator();
85         genFilesHelper = new GeneratedFilesHelper(helper);
86         Util.err.log("Loading suite project in " + getProjectDirectory());
87         lookup = Lookups.fixed(new Object JavaDoc[] {
88             this,
89             new Info(),
90             helper.createAuxiliaryConfiguration(),
91             helper.createCacheDirectoryProvider(),
92             new SavedHook(),
93             new OpenedHook(),
94             helper.createSharabilityQuery(eval, new String JavaDoc[0], new String JavaDoc[] {"build", "dist"}), // NOI18N
95
new SuiteSubprojectProviderImpl(helper, eval),
96             new SuiteProviderImpl(),
97             new SuiteActions(this),
98             new SuiteLogicalView(this),
99             new SuiteCustomizer(this, helper, eval),
100             new PrivilegedTemplatesImpl(),
101             new SuiteOperations(this),
102         });
103         lookup = LookupProviderSupport.createCompositeLookup(lookup, "Projects/org-netbeans-modules-apisupport-project-suite/Lookup");
104     }
105     
106     public String JavaDoc toString() {
107         return "SuiteProject[" + getProjectDirectory() + "]"; // NOI18N
108
}
109     
110     public Lookup getLookup() {
111         return lookup;
112     }
113     
114     public FileObject getProjectDirectory() {
115         return helper.getProjectDirectory();
116     }
117     
118     public File JavaDoc getProjectDirectoryFile() {
119         return FileUtil.toFile(getProjectDirectory());
120     }
121     
122     /** For unit tests purpose only. */
123     public AntProjectHelper getHelper() {
124         return helper;
125     }
126     
127     /** For unit tests purpose only. */
128     public PropertyEvaluator getEvaluator() {
129         return eval;
130     }
131     
132     /**
133      * Get the platform selected for use with this suite.
134      * @param fallback if true, fall back to the default platform if necessary
135      * @return the current platform; or null if fallback is false and there is no
136      * platform specified, or an invalid platform is specified, or even if
137      * fallback is true but even the default platform is not available
138      */

139     public NbPlatform getPlatform(boolean fallback) {
140         NbPlatform p;
141         // #65652: more reliable to use the dest dir, in case nbplatform.active is not set.
142
String JavaDoc destdir = getEvaluator().getProperty("netbeans.dest.dir"); // NOI18N
143
if (destdir != null) {
144             p = NbPlatform.getPlatformByDestDir(getHelper().resolveFile(destdir));
145         } else {
146             p = null;
147         }
148         if (fallback && (p == null || !p.isValid())) {
149             p = NbPlatform.getDefaultPlatform();
150         }
151         return p;
152     }
153     
154     private PropertyEvaluator createEvaluator() {
155         PropertyProvider predefs = helper.getStockPropertyPreprovider();
156         File JavaDoc dir = getProjectDirectoryFile();
157         List JavaDoc<PropertyProvider> providers = new ArrayList JavaDoc();
158         providers.add(helper.getPropertyProvider("nbproject/private/platform-private.properties")); // NOI18N
159
providers.add(helper.getPropertyProvider("nbproject/platform.properties")); // NOI18N
160
PropertyEvaluator baseEval = PropertyUtils.sequentialPropertyEvaluator(predefs, (PropertyProvider[]) providers.toArray(new PropertyProvider[providers.size()]));
161         providers.add(new Util.UserPropertiesFileProvider(baseEval, dir));
162         baseEval = PropertyUtils.sequentialPropertyEvaluator(predefs, (PropertyProvider[]) providers.toArray(new PropertyProvider[providers.size()]));
163         class DestDirProvider extends Util.ComputedPropertyProvider {
164             public DestDirProvider(PropertyEvaluator eval) {
165                 super(eval);
166             }
167             protected Map JavaDoc<String JavaDoc,String JavaDoc> getProperties(Map JavaDoc<String JavaDoc,String JavaDoc> inputPropertyValues) {
168                 String JavaDoc platformS = (String JavaDoc) inputPropertyValues.get("nbplatform.active"); // NOI18N
169
if (platformS != null) {
170                     return Collections.singletonMap("netbeans.dest.dir", "${nbplatform." + platformS + ".netbeans.dest.dir}"); // NOI18N
171
} else {
172                     return Collections.EMPTY_MAP;
173                 }
174             }
175             protected Set JavaDoc inputProperties() {
176                 return Collections.singleton("nbplatform.active"); // NOI18N
177
}
178         }
179         providers.add(new DestDirProvider(baseEval));
180         providers.add(helper.getPropertyProvider(AntProjectHelper.PRIVATE_PROPERTIES_PATH));
181         providers.add(helper.getPropertyProvider(AntProjectHelper.PROJECT_PROPERTIES_PATH));
182         Map JavaDoc<String JavaDoc,String JavaDoc> fixedProps = new HashMap JavaDoc();
183         // synchronize with suite.xml
184
fixedProps.put(SuiteProperties.ENABLED_CLUSTERS_PROPERTY, "");
185         fixedProps.put(SuiteProperties.DISABLED_CLUSTERS_PROPERTY, "");
186         fixedProps.put(SuiteProperties.DISABLED_MODULES_PROPERTY, "");
187         fixedProps.put(BrandingSupport.BRANDING_DIR_PROPERTY, "branding"); // NOI18N
188
providers.add(PropertyUtils.fixedPropertyProvider(fixedProps));
189         return PropertyUtils.sequentialPropertyEvaluator(predefs, (PropertyProvider[]) providers.toArray(new PropertyProvider[providers.size()]));
190     }
191     
192     private final class Info implements ProjectInformation, AntProjectListener {
193         
194         private final PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
195         
196         Info() {
197             helper.addAntProjectListener(this);
198         }
199         
200         private String JavaDoc getSimpleName() {
201             Element JavaDoc nameEl = Util.findElement(helper.getPrimaryConfigurationData(true), "name", SuiteProjectType.NAMESPACE_SHARED); // NOI18N
202
String JavaDoc text = (nameEl != null) ? Util.findText(nameEl) : null;
203             return (text != null) ? text : "???"; // NOI18N
204
}
205         
206         public String JavaDoc getName() {
207             return PropertyUtils.getUsablePropertyName(getSimpleName());
208         }
209         
210         public String JavaDoc getDisplayName() {
211             String JavaDoc appTitle = getEvaluator().getProperty("app.title"); // NOI18N
212
if (appTitle != null) {
213                 return appTitle;
214             } else {
215                 return getSimpleName();
216             }
217         }
218         
219         public Icon JavaDoc getIcon() {
220             return new ImageIcon JavaDoc(Utilities.loadImage(SUITE_ICON_PATH));
221         }
222         
223         public Project getProject() {
224             return SuiteProject.this;
225         }
226         
227         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
228             pcs.addPropertyChangeListener(listener);
229         }
230         
231         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
232             pcs.removePropertyChangeListener(listener);
233         }
234         
235         public void configurationXmlChanged(AntProjectEvent ev) {
236             fireNameChange();
237         }
238         
239         public void propertiesChanged(AntProjectEvent ev) {
240             fireNameChange();
241         }
242         
243         private void fireNameChange() {
244             pcs.firePropertyChange(ProjectInformation.PROP_NAME, null, getName());
245             pcs.firePropertyChange(ProjectInformation.PROP_DISPLAY_NAME, null, getDisplayName());
246         }
247         
248     }
249     
250     public final class OpenedHook extends ProjectOpenedHook {
251         
252         OpenedHook() {}
253         
254         public void projectOpened() {
255             // XXX skip this in case nbplatform.active is not defined
256
ProjectManager.mutex().writeAccess(new Mutex.Action() {
257                 public Object JavaDoc run() {
258                     String JavaDoc path = "nbproject/private/platform-private.properties"; // NOI18N
259
EditableProperties ep = helper.getProperties(path);
260                     File JavaDoc buildProperties = new File JavaDoc(System.getProperty("netbeans.user"), "build.properties"); // NOI18N
261
ep.setProperty("user.properties.file", buildProperties.getAbsolutePath()); //NOI18N
262
helper.putProperties(path, ep);
263                     try {
264                         ProjectManager.getDefault().saveProject(SuiteProject.this);
265                     } catch (IOException JavaDoc e) {
266                         ErrorManager.getDefault().notify(e);
267                     }
268                     return null;
269                 }
270             });
271             // refresh build.xml and build-impl.xml
272
try {
273                 genFilesHelper.refreshBuildScript(
274                         GeneratedFilesHelper.BUILD_IMPL_XML_PATH,
275                         SuiteProject.class.getResource("resources/build-impl.xsl"),
276                         true);
277                 genFilesHelper.refreshBuildScript(
278                         GeneratedFilesHelper.BUILD_XML_PATH,
279                         SuiteProject.class.getResource("resources/build.xsl"),
280                         true);
281             } catch (IOException JavaDoc e) {
282                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
283             }
284         }
285         
286         protected void projectClosed() {
287             try {
288                 ProjectManager.getDefault().saveProject(SuiteProject.this);
289             } catch (IOException JavaDoc e) {
290                 Util.err.notify(e);
291             }
292         }
293         
294     }
295     
296     private final class SavedHook extends ProjectXmlSavedHook {
297         
298         SavedHook() {}
299         
300         protected void projectXmlSaved() throws IOException JavaDoc {
301             // refresh build.xml and build-impl.xml
302
genFilesHelper.refreshBuildScript(
303                     GeneratedFilesHelper.BUILD_IMPL_XML_PATH,
304                     SuiteProject.class.getResource("resources/build-impl.xsl"),
305                     false);
306             genFilesHelper.refreshBuildScript(
307                     GeneratedFilesHelper.BUILD_XML_PATH,
308                     SuiteProject.class.getResource("resources/build.xsl"),
309                     false);
310         }
311         
312     }
313     
314     private final class SuiteProviderImpl implements SuiteProvider {
315         
316         public File JavaDoc getSuiteDirectory() {
317             return getProjectDirectoryFile();
318         }
319         
320     }
321     
322     private static final class PrivilegedTemplatesImpl implements PrivilegedTemplates, RecommendedTemplates {
323         
324         private static final String JavaDoc[] PRIVILEGED_NAMES = new String JavaDoc[] {
325             "Templates/Ant/Project.xml", // NOI18N
326
"Templates/Other/properties.properties", // NOI18N
327
};
328         
329         private static final String JavaDoc[] RECOMMENDED_TYPES = new String JavaDoc[] {
330             "oasis-XML-catalogs", // NOI18N
331
"XML", // NOI18N
332
"ant-script", // NOI18N
333
"simple-files", // NOI18N
334
};
335         
336         public String JavaDoc[] getPrivilegedTemplates() {
337             return PRIVILEGED_NAMES;
338         }
339         
340         public String JavaDoc[] getRecommendedTypes() {
341             return RECOMMENDED_TYPES;
342         }
343     }
344     
345 }
346
Popular Tags