001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project.ui.customizer;
043:
044: import java.io.IOException;
045: import java.util.Arrays;
046: import java.util.Collections;
047: import java.util.Map;
048: import java.util.Set;
049: import java.util.SortedSet;
050: import java.util.StringTokenizer;
051: import java.util.TreeSet;
052: import org.netbeans.api.java.platform.JavaPlatform;
053: import org.netbeans.modules.apisupport.project.NbModuleProject;
054: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
055: import org.netbeans.modules.apisupport.project.ui.customizer.CustomizerComponentFactory.SuiteSubModulesListModel;
056: import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
057: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
058: import org.netbeans.spi.project.support.ant.AntProjectHelper;
059: import org.netbeans.spi.project.support.ant.EditableProperties;
060: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
061:
062: /**
063: * Provides convenient access to a lot of Suite Module's properties.
064: *
065: * @author Martin Krauskopf
066: */
067: public final class SuiteProperties extends ModuleProperties {
068:
069: public static final String DISABLED_MODULES_PROPERTY = "disabled.modules"; // NOI18N
070: public static final String ENABLED_CLUSTERS_PROPERTY = "enabled.clusters"; // NOI18N
071: public static final String DISABLED_CLUSTERS_PROPERTY = "disabled.clusters"; // NOI18N
072:
073: public static final String NB_PLATFORM_PROPERTY = "nbPlatform"; // NOI18N
074: public static final String JAVA_PLATFORM_PROPERTY = "nbjdk.active"; // NOI18N
075:
076: private NbPlatform activePlatform;
077: private JavaPlatform activeJavaPlatform;
078:
079: /** Project the current properties represents. */
080: private SuiteProject project;
081:
082: /** Represent original set of sub-modules. */
083: private Set<NbModuleProject> origSubModules;
084:
085: /** Represent currently set set of sub-modules. */
086: private Set<NbModuleProject> subModules;
087:
088: // models
089: private SuiteSubModulesListModel moduleListModel;
090:
091: /** disabled modules */
092: private String[] disabledModules;
093: /** enabled clusters */
094: private String[] enabledClusters;
095: /** boolean variable to remember whether there were some changes */
096: private boolean changedDisabledModules, changedEnabledClusters;
097:
098: /** keeps all information related to branding*/
099: private final BasicBrandingModel brandingModel;
100:
101: /**
102: * Creates a new instance of SuiteProperties
103: */
104: public SuiteProperties(SuiteProject project,
105: AntProjectHelper helper, PropertyEvaluator evaluator,
106: Set<NbModuleProject> subModules) {
107: super (helper, evaluator);
108: this .project = project;
109: refresh(subModules);
110: this .disabledModules = getArrayProperty(evaluator,
111: DISABLED_MODULES_PROPERTY);
112: this .enabledClusters = getArrayProperty(evaluator,
113: ENABLED_CLUSTERS_PROPERTY);
114: if (enabledClusters.length == 0) {
115: // Compatibility.
116: SortedSet<String> clusters = new TreeSet<String>();
117: for (ModuleEntry module : activePlatform.getModules()) {
118: clusters.add(module.getClusterDirectory().getName());
119: }
120: clusters.removeAll(Arrays.asList(getArrayProperty(
121: evaluator, DISABLED_CLUSTERS_PROPERTY)));
122: enabledClusters = clusters.toArray(new String[clusters
123: .size()]);
124: }
125: brandingModel = new BasicBrandingModel(this );
126: }
127:
128: void refresh(Set<NbModuleProject> subModules) {
129: reloadProperties();
130: this .origSubModules = Collections.unmodifiableSet(subModules);
131: this .subModules = subModules;
132: this .moduleListModel = null;
133: activePlatform = project.getPlatform(true);
134: activeJavaPlatform = ModuleProperties
135: .findJavaPlatformByID(getEvaluator().getProperty(
136: "nbjdk.active")); // NOI18N
137: firePropertiesRefreshed();
138: }
139:
140: public SuiteProject getProject() {
141: return project;
142: }
143:
144: Map<String, String> getDefaultValues() {
145: return Collections.emptyMap(); // no default value (yet)
146: }
147:
148: public NbPlatform getActivePlatform() {
149: return activePlatform;
150: }
151:
152: void setActivePlatform(NbPlatform newPlaf) {
153: NbPlatform oldPlaf = this .activePlatform;
154: this .activePlatform = newPlaf;
155: firePropertyChange(NB_PLATFORM_PROPERTY, oldPlaf, newPlaf);
156: }
157:
158: JavaPlatform getActiveJavaPlatform() {
159: return activeJavaPlatform;
160: }
161:
162: void setActiveJavaPlatform(JavaPlatform nue) {
163: JavaPlatform old = activeJavaPlatform;
164: if (nue != old) {
165: activeJavaPlatform = nue;
166: firePropertyChange(JAVA_PLATFORM_PROPERTY, old, nue);
167: }
168: }
169:
170: String[] getEnabledClusters() {
171: return enabledClusters;
172: }
173:
174: String[] getDisabledModules() {
175: return disabledModules;
176: }
177:
178: void setEnabledClusters(String[] value) {
179: if (Arrays.asList(enabledClusters).equals(Arrays.asList(value))) {
180: return;
181: }
182: this .enabledClusters = value;
183: this .changedEnabledClusters = true;
184: }
185:
186: void setDisabledModules(String[] value) {
187: if (Arrays.asList(disabledModules).equals(Arrays.asList(value))) {
188: return;
189: }
190: this .disabledModules = value;
191: this .changedDisabledModules = true;
192: }
193:
194: public static String[] getArrayProperty(
195: PropertyEvaluator evaluator, String p) {
196: String s = evaluator.getProperty(p);
197: String[] arr = null;
198: if (s != null) {
199: StringTokenizer tok = new StringTokenizer(s, ","); // NOI18N
200: arr = new String[tok.countTokens()];
201: for (int i = 0; i < arr.length; i++) {
202: arr[i] = tok.nextToken().trim();
203: }
204: }
205: return arr == null ? new String[0] : arr;
206: }
207:
208: public void storeProperties() throws IOException {
209: ModuleProperties
210: .storePlatform(getHelper(), getActivePlatform());
211: ModuleProperties.storeJavaPlatform(getHelper(), getEvaluator(),
212: getActiveJavaPlatform(), false);
213: getBrandingModel().store();
214:
215: // store submodules if they've changed
216: SuiteSubModulesListModel model = getModulesListModel();
217: if (model.isChanged()) {
218: SuiteUtils.replaceSubModules(this );
219: }
220:
221: if (changedDisabledModules || changedEnabledClusters) {
222: EditableProperties ep = getHelper().getProperties(
223: "nbproject/platform.properties"); // NOI18N
224: if (changedDisabledModules) {
225: String[] separated = disabledModules.clone();
226: for (int i = 0; i < disabledModules.length - 1; i++) {
227: separated[i] = disabledModules[i] + ',';
228: }
229: ep.setProperty(DISABLED_MODULES_PROPERTY, separated);
230: // Do not want it left in project.properties if it was there before (from 5.0):
231: setProperty(DISABLED_MODULES_PROPERTY, (String) null);
232: }
233: if (changedEnabledClusters) {
234: String[] separated = enabledClusters.clone();
235: for (int i = 0; i < enabledClusters.length - 1; i++) {
236: separated[i] = enabledClusters[i] + ',';
237: }
238: ep.setProperty(ENABLED_CLUSTERS_PROPERTY, separated);
239: setProperty(ENABLED_CLUSTERS_PROPERTY, (String) null);
240: // Compatibility.
241: SortedSet<String> disabledClusters = new TreeSet<String>();
242: ModuleEntry[] modules = activePlatform.getModules();
243: for (int i = 0; i < modules.length; i++) {
244: disabledClusters.add(modules[i]
245: .getClusterDirectory().getName());
246: }
247: disabledClusters.removeAll(Arrays
248: .asList(enabledClusters));
249: separated = disabledClusters
250: .toArray(new String[disabledClusters.size()]);
251: for (int i = 0; i < separated.length - 1; i++) {
252: separated[i] = separated[i] + ',';
253: }
254: ep.setProperty(DISABLED_CLUSTERS_PROPERTY, separated);
255: ep
256: .setComment(
257: DISABLED_CLUSTERS_PROPERTY,
258: new String[] { "# Deprecated since 5.0u1; for compatibility with 5.0:" },
259: false); // NOI18N
260: }
261: getHelper().putProperties("nbproject/platform.properties",
262: ep); // NOI18N
263: }
264:
265: super .storeProperties();
266: }
267:
268: Set<NbModuleProject> getSubModules() {
269: return getModulesListModel().getSubModules();
270: }
271:
272: Set<NbModuleProject> getOrigSubModules() {
273: return origSubModules;
274: }
275:
276: /**
277: * Returns list model of module's dependencies regarding the currently
278: * selected platform.
279: */
280: SuiteSubModulesListModel getModulesListModel() {
281: if (moduleListModel == null) {
282: moduleListModel = new SuiteSubModulesListModel(subModules);
283: }
284: return moduleListModel;
285: }
286:
287: public BasicBrandingModel getBrandingModel() {
288: return brandingModel;
289: }
290:
291: }
|