KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > AntSettings


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.apache.tools.ant.module;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.io.File JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.SortedMap JavaDoc;
31 import java.util.TreeMap JavaDoc;
32 import java.util.prefs.Preferences JavaDoc;
33 import java.util.regex.Pattern JavaDoc;
34 import org.apache.tools.ant.module.api.IntrospectedInfo;
35 import org.apache.tools.ant.module.bridge.AntBridge;
36 import org.apache.tools.ant.module.spi.AntEvent;
37 import org.apache.tools.ant.module.spi.AutomaticExtraClasspathProvider;
38 import org.openide.ErrorManager;
39 import org.openide.modules.InstalledFileLocator;
40 import org.openide.util.Lookup;
41 import org.openide.util.LookupEvent;
42 import org.openide.util.LookupListener;
43 import org.openide.util.NbPreferences;
44
45 public class AntSettings {
46
47     private static final String JavaDoc PROP_VERBOSITY = "verbosity"; // NOI18N
48
private static final String JavaDoc PROP_PROPERTIES = "properties"; // NOI18N
49
private static final String JavaDoc PROP_SAVE_ALL = "saveAll"; // NOI18N
50
private static final String JavaDoc PROP_CUSTOM_DEFS = "customDefs"; // NOI18N
51
private static final String JavaDoc PROP_ANT_VERSION = "antVersion"; // NOI18N
52
public static final String JavaDoc PROP_ANT_HOME = "antHome"; // NOI18N
53
public static final String JavaDoc PROP_EXTRA_CLASSPATH = "extraClasspath"; // NOI18N
54
public static final String JavaDoc PROP_AUTOMATIC_EXTRA_CLASSPATH = "automaticExtraClasspath"; // NOI18N
55
private static final String JavaDoc PROP_AUTO_CLOSE_TABS = "autoCloseTabs"; // NOI18N
56
private static final String JavaDoc PROP_ALWAYS_SHOW_OUTPUT = "alwaysShowOutput"; // NOI18N
57

58     private AntSettings() {}
59
60     private static Preferences JavaDoc prefs() {
61         return NbPreferences.forModule(AntSettings.class);
62     }
63
64     public static int getVerbosity() {
65         return prefs().getInt(PROP_VERBOSITY, AntEvent.LOG_INFO);
66     }
67
68     public static void setVerbosity(int v) {
69         prefs().putInt(PROP_VERBOSITY, v);
70     }
71
72     public static Map JavaDoc<String JavaDoc,String JavaDoc> getProperties() {
73         Map JavaDoc<String JavaDoc,String JavaDoc> p = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
74         // Enable hyperlinking for Jikes by default:
75
for (String JavaDoc pair : prefs().get(PROP_PROPERTIES, "build.compiler.emacs=true").split("\n")) { // NOI18N
76
String JavaDoc[] nameval = pair.split("=", 2); // NOI18N
77
p.put(nameval[0], nameval[1]);
78         }
79         return p;
80     }
81
82     public static void setProperties(Map JavaDoc<String JavaDoc,String JavaDoc> p) {
83         if (!(p instanceof SortedMap JavaDoc)) {
84             p = new TreeMap JavaDoc<String JavaDoc,String JavaDoc>(p);
85         }
86         StringBuilder JavaDoc b = new StringBuilder JavaDoc();
87         for (Map.Entry JavaDoc<String JavaDoc,String JavaDoc> pair : p.entrySet()) {
88             if (b.length() > 0) {
89                 b.append('\n');
90             }
91             b.append(pair.getKey());
92             b.append('=');
93             b.append(pair.getValue());
94         }
95         prefs().put(PROP_PROPERTIES, b.toString());
96     }
97
98     public static boolean getSaveAll() {
99         return prefs().getBoolean(PROP_SAVE_ALL, true);
100     }
101
102     public static void setSaveAll(boolean sa) {
103         prefs().putBoolean(PROP_SAVE_ALL, sa);
104     }
105
106     private static IntrospectedInfo customDefs;
107     static {
108         new IntrospectedInfo(); // trigger IntrospectedInfo static block
109
}
110     public static synchronized IntrospectedInfo getCustomDefs() {
111         if (customDefs == null) {
112             customDefs = IntrospectedInfoSerializer.instance.load(prefs().node(PROP_CUSTOM_DEFS));
113         }
114         return customDefs;
115     }
116
117     public static synchronized void setCustomDefs(IntrospectedInfo ii) {
118         IntrospectedInfoSerializer.instance.store(prefs().node(PROP_CUSTOM_DEFS), ii);
119         customDefs = ii;
120     }
121
122     private static String JavaDoc antVersion;
123     // #14993: read-only property for the version of Ant
124
public static String JavaDoc getAntVersion() {
125         if (antVersion == null) {
126             antVersion = AntBridge.getInterface().getAntVersion();
127         }
128         return antVersion;
129     }
130
131     /**
132      * Transient value of ${ant.home} unless otherwise set.
133      * @see "#43522"
134      */

135     private static File JavaDoc defaultAntHome = null;
136
137     private static synchronized File JavaDoc getDefaultAntHome() {
138         if (defaultAntHome == null) {
139             File JavaDoc antJar = InstalledFileLocator.getDefault().locate("ant/lib/ant.jar", "org.apache.tools.ant.module", false); // NOI18N
140
if (antJar == null) {
141                 return null;
142             }
143             defaultAntHome = antJar.getParentFile().getParentFile();
144             if (AntModule.err.isLoggable(ErrorManager.INFORMATIONAL)) {
145                 AntModule.err.log("getDefaultAntHome: " + defaultAntHome);
146             }
147         }
148         assert defaultAntHome != null;
149         return defaultAntHome;
150     }
151
152     /**
153      * Get the Ant installation to use.
154      * Might be null!
155      */

156     public static File JavaDoc getAntHome() {
157         String JavaDoc h = prefs().get(PROP_ANT_HOME, null);
158         if (AntModule.err.isLoggable(ErrorManager.INFORMATIONAL)) {
159             AntModule.err.log("getAntHomeWithDefault: antHome=" + h);
160         }
161         if (h != null) {
162             return new File JavaDoc(h);
163         } else {
164             // Not explicitly configured. Check default.
165
return getDefaultAntHome();
166         }
167     }
168
169     public static void setAntHome(File JavaDoc f) {
170         if (f != null && f.equals(getDefaultAntHome())) {
171             f = null;
172         }
173         if (AntModule.err.isLoggable(ErrorManager.INFORMATIONAL)) {
174             AntModule.err.log("setAntHome: " + f);
175         }
176         if (f != null) {
177             prefs().put(PROP_ANT_HOME, f.getAbsolutePath());
178         } else {
179             prefs().remove(PROP_ANT_HOME);
180         }
181         antVersion = null;
182         firePropertyChange(PROP_ANT_HOME);
183     }
184
185     public static List JavaDoc<File JavaDoc> getExtraClasspath() {
186         // XXX could perhaps populate with xerces.jar:dom-ranges.jar
187
// However currently there is no sure way to get the "good" Xerces
188
// from libs/xerces (rather than the messed-up one from xml/tax)
189
// without hardcoding the JAR name, which seems unwise since it is
190
// definitely subject to change.
191
List JavaDoc<File JavaDoc> files = new ArrayList JavaDoc<File JavaDoc>();
192         for (String JavaDoc f : prefs().get(PROP_EXTRA_CLASSPATH, "").split(Pattern.quote(File.pathSeparator))) {
193             files.add(new File JavaDoc(f));
194         }
195         return files;
196     }
197
198     public static void setExtraClasspath(List JavaDoc<File JavaDoc> p) {
199         StringBuilder JavaDoc b = new StringBuilder JavaDoc();
200         for (File JavaDoc f : p) {
201             if (b.length() > 0) {
202                 b.append(File.pathSeparatorChar);
203             }
204             b.append(f);
205         }
206         prefs().put(PROP_EXTRA_CLASSPATH, b.toString());
207         firePropertyChange(PROP_EXTRA_CLASSPATH);
208     }
209
210     private static List JavaDoc<File JavaDoc> defAECP = null;
211     private static Lookup.Result<AutomaticExtraClasspathProvider> aecpResult = null;
212
213     public static synchronized List JavaDoc<File JavaDoc> getAutomaticExtraClasspath() {
214         if (aecpResult == null) {
215             aecpResult = Lookup.getDefault().lookupResult(AutomaticExtraClasspathProvider.class);
216             aecpResult.addLookupListener(new LookupListener() {
217                 public void resultChanged(LookupEvent ev) {
218                     defAECP = null;
219                     firePropertyChange(PROP_AUTOMATIC_EXTRA_CLASSPATH);
220                 }
221             });
222         }
223         if (defAECP == null) {
224             defAECP = new ArrayList JavaDoc<File JavaDoc>();
225             for (AutomaticExtraClasspathProvider provider : aecpResult.allInstances()) {
226                 defAECP.addAll(Arrays.asList(provider.getClasspathItems()));
227             }
228         }
229         return defAECP;
230     }
231
232     public static boolean getAutoCloseTabs() {
233         return prefs().getBoolean(PROP_AUTO_CLOSE_TABS, /*#47753*/ true);
234     }
235
236     public static void setAutoCloseTabs(boolean b) {
237         prefs().putBoolean(PROP_AUTO_CLOSE_TABS, b);
238     }
239
240     public static boolean getAlwaysShowOutput() {
241         return prefs().getBoolean(PROP_ALWAYS_SHOW_OUTPUT, /* #87801 */false);
242     }
243
244     public static void setAlwaysShowOutput(boolean b) {
245         prefs().putBoolean(PROP_ALWAYS_SHOW_OUTPUT, b);
246     }
247
248     private static final PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(AntSettings.class);
249
250     public static void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
251         pcs.addPropertyChangeListener(l);
252     }
253
254     public static void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
255         pcs.removePropertyChangeListener(l);
256     }
257
258     private static void firePropertyChange(String JavaDoc prop) {
259         pcs.firePropertyChange(prop, null, null);
260     }
261
262     public static abstract class IntrospectedInfoSerializer {
263         public static IntrospectedInfoSerializer instance;
264         public abstract IntrospectedInfo load(Preferences JavaDoc node);
265         public abstract void store(Preferences JavaDoc node, IntrospectedInfo info);
266     }
267
268 }
269
Popular Tags