KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > FreeformProject


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.ant.freeform;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import javax.swing.Icon JavaDoc;
27 import javax.swing.ImageIcon JavaDoc;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectInformation;
30 import org.netbeans.api.project.ProjectManager;
31 import org.netbeans.modules.ant.freeform.spi.ProjectNature;
32 import org.netbeans.modules.ant.freeform.spi.support.Util;
33 import org.netbeans.modules.ant.freeform.ui.ProjectCustomizerProvider;
34 import org.netbeans.modules.ant.freeform.ui.View;
35 import org.netbeans.spi.project.AuxiliaryConfiguration;
36 import org.netbeans.spi.project.support.LookupProviderSupport;
37 import org.netbeans.spi.project.support.ant.AntProjectHelper;
38 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
39 import org.netbeans.spi.project.support.ant.PropertyUtils;
40 import org.netbeans.spi.project.ui.support.UILookupMergerSupport;
41 import org.openide.filesystems.FileObject;
42 import org.openide.util.Lookup;
43 import org.openide.util.Mutex;
44 import org.openide.util.Utilities;
45 import org.openide.util.lookup.Lookups;
46 import org.w3c.dom.Element JavaDoc;
47 import org.w3c.dom.NodeList JavaDoc;
48
49 /**
50  * One freeform project.
51  * @author Jesse Glick
52  */

53 public final class FreeformProject implements Project {
54     
55     public static final Lookup.Result<ProjectNature> PROJECT_NATURES = Lookup.getDefault().lookupResult(ProjectNature.class);
56     
57     private final AntProjectHelper helper;
58     private final PropertyEvaluator eval;
59     private final Lookup lookup;
60     private AuxiliaryConfiguration aux;
61     
62     public FreeformProject(AntProjectHelper helper) throws IOException JavaDoc {
63         this.helper = helper;
64         eval = new FreeformEvaluator(this);
65         lookup = initLookup();
66         Logger.getLogger(FreeformProject.class.getName()).log(Level.FINER, "Initializing project in {0} with {1}", new Object JavaDoc[] {helper, lookup});
67         new ProjectXmlValidator(helper.resolveFileObject(AntProjectHelper.PROJECT_XML_PATH));
68     }
69     
70     public AntProjectHelper helper() {
71         return helper;
72     }
73
74     /**
75      * @see Util#getPrimaryConfigurationData
76      */

77     public Element JavaDoc getPrimaryConfigurationData() {
78         return Util.getPrimaryConfigurationData(helper);
79     }
80
81     /**
82      * @see Util#putPrimaryConfigurationData
83      */

84     public void putPrimaryConfigurationData(Element JavaDoc data) {
85         Util.putPrimaryConfigurationData(helper, data);
86     }
87
88     private Lookup initLookup() throws IOException JavaDoc {
89         aux = helper().createAuxiliaryConfiguration(); // AuxiliaryConfiguration
90
Lookup baseLookup = Lookups.fixed(
91             this,
92             new Info(), // ProjectInformation
93
new FreeformSources(this), // Sources
94
new Actions(this), // ActionProvider
95
new View(this), // LogicalViewProvider
96
new ProjectCustomizerProvider(this), // CustomizerProvider
97
aux, // AuxiliaryConfiguration
98
helper().createCacheDirectoryProvider(), // CacheDirectoryProvider
99
new Subprojects(this), // SubprojectProvider
100
new ArtifactProvider(this), // AntArtifactProvider
101
new LookupMergerImpl(), // LookupMerger or ActionProvider
102
UILookupMergerSupport.createPrivilegedTemplatesMerger(),
103             UILookupMergerSupport.createRecommendedTemplatesMerger(),
104             new FreeformProjectOperations(this),
105         new FreeformSharabilityQuery(helper()), //SharabilityQueryImplementation
106
Accessor.DEFAULT.createProjectAccessor(this) //Access to AntProjectHelper and PropertyEvaluator
107
);
108         return LookupProviderSupport.createCompositeLookup(baseLookup, "Projects/org-netbeans-modules-ant-freeform/Lookup"); //NOI18N
109
}
110     
111     public FileObject getProjectDirectory() {
112         return helper.getProjectDirectory();
113     }
114     
115     public Lookup getLookup() {
116         return lookup;
117     }
118     
119     public PropertyEvaluator evaluator() {
120         return eval;
121     }
122
123     public String JavaDoc toString() {
124         return "FreeformProject[" + getProjectDirectory() + "]"; // NOI18N
125
}
126     
127     /** Store configured project name. */
128     public void setName(final String JavaDoc name) {
129         ProjectManager.mutex().writeAccess(new Mutex.Action<Void JavaDoc>() {
130             public Void JavaDoc run() {
131                 Element JavaDoc data = getPrimaryConfigurationData();
132                 // XXX replace by XMLUtil when that has findElement, findText, etc.
133
NodeList JavaDoc nl = data.getElementsByTagNameNS(FreeformProjectType.NS_GENERAL, "name");
134                 Element JavaDoc nameEl;
135                 if (nl.getLength() == 1) {
136                     nameEl = (Element JavaDoc) nl.item(0);
137                     NodeList JavaDoc deadKids = nameEl.getChildNodes();
138                     while (deadKids.getLength() > 0) {
139                         nameEl.removeChild(deadKids.item(0));
140                     }
141                 } else {
142                     nameEl = data.getOwnerDocument().createElementNS(FreeformProjectType.NS_GENERAL, "name");
143                     data.insertBefore(nameEl, /* OK if null */data.getChildNodes().item(0));
144                 }
145                 nameEl.appendChild(data.getOwnerDocument().createTextNode(name));
146                 putPrimaryConfigurationData(data);
147                 return null;
148             }
149         });
150     }
151     
152     private final class Info implements ProjectInformation {
153         
154         public Info() {}
155         
156         public String JavaDoc getName() {
157             return PropertyUtils.getUsablePropertyName(getDisplayName());
158         }
159         
160         public String JavaDoc getDisplayName() {
161             return ProjectManager.mutex().readAccess(new Mutex.Action<String JavaDoc>() {
162                 public String JavaDoc run() {
163                     Element JavaDoc genldata = getPrimaryConfigurationData();
164                     Element JavaDoc nameEl = Util.findElement(genldata, "name", FreeformProjectType.NS_GENERAL); // NOI18N
165
if (nameEl == null) {
166                         // Corrupt. Cf. #48267 (cause unknown).
167
return "???"; // NOI18N
168
}
169                     return Util.findText(nameEl);
170                 }
171             });
172         }
173         
174         public Icon JavaDoc getIcon() {
175             if (usesAntScripting()) {
176                 return new ImageIcon JavaDoc(Utilities.loadImage("org/netbeans/modules/ant/freeform/resources/freeform-project.png", true)); // NOI18N
177
} else {
178                 return new ImageIcon JavaDoc(Utilities.loadImage("org/netbeans/modules/project/ui/resources/projectTab.gif", true)); // NOI18N
179
}
180         }
181         
182         public Project getProject() {
183             return FreeformProject.this;
184         }
185         
186         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
187             // XXX
188
}
189         
190         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
191             // XXX
192
}
193         
194     }
195     
196  
197     
198     /**
199      * Utility method to decide if the project actually uses Ant scripting.
200      * It does if at least one of these hold:
201      * <ol>
202      * <li>There is a <code>build.xml</code> at top level.
203      * <li>The property <code>ant.script</code> is defined.
204      * <li>There are any <code>&lt;action&gt;</code>s bound.
205      * </ol>
206      */

207     public boolean usesAntScripting() {
208         return getProjectDirectory().getFileObject("build.xml") != null || // NOI18N
209
evaluator().getProperty("ant.script") != null || // NOI18N
210
Util.getPrimaryConfigurationData(helper).getElementsByTagName("action").getLength() > 0; // NOI18N
211
}
212
213 }
214
Popular Tags