1 19 20 package org.apache.tools.ant.module; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.File ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.SortedMap ; 31 import java.util.TreeMap ; 32 import java.util.prefs.Preferences ; 33 import java.util.regex.Pattern ; 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 PROP_VERBOSITY = "verbosity"; private static final String PROP_PROPERTIES = "properties"; private static final String PROP_SAVE_ALL = "saveAll"; private static final String PROP_CUSTOM_DEFS = "customDefs"; private static final String PROP_ANT_VERSION = "antVersion"; public static final String PROP_ANT_HOME = "antHome"; public static final String PROP_EXTRA_CLASSPATH = "extraClasspath"; public static final String PROP_AUTOMATIC_EXTRA_CLASSPATH = "automaticExtraClasspath"; private static final String PROP_AUTO_CLOSE_TABS = "autoCloseTabs"; private static final String PROP_ALWAYS_SHOW_OUTPUT = "alwaysShowOutput"; 58 private AntSettings() {} 59 60 private static Preferences 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 <String ,String > getProperties() { 73 Map <String ,String > p = new HashMap <String ,String >(); 74 for (String pair : prefs().get(PROP_PROPERTIES, "build.compiler.emacs=true").split("\n")) { String [] nameval = pair.split("=", 2); p.put(nameval[0], nameval[1]); 78 } 79 return p; 80 } 81 82 public static void setProperties(Map <String ,String > p) { 83 if (!(p instanceof SortedMap )) { 84 p = new TreeMap <String ,String >(p); 85 } 86 StringBuilder b = new StringBuilder (); 87 for (Map.Entry <String ,String > 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(); } 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 antVersion; 123 public static String getAntVersion() { 125 if (antVersion == null) { 126 antVersion = AntBridge.getInterface().getAntVersion(); 127 } 128 return antVersion; 129 } 130 131 135 private static File defaultAntHome = null; 136 137 private static synchronized File getDefaultAntHome() { 138 if (defaultAntHome == null) { 139 File antJar = InstalledFileLocator.getDefault().locate("ant/lib/ant.jar", "org.apache.tools.ant.module", false); 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 156 public static File getAntHome() { 157 String 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 (h); 163 } else { 164 return getDefaultAntHome(); 166 } 167 } 168 169 public static void setAntHome(File 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 <File > getExtraClasspath() { 186 List <File > files = new ArrayList <File >(); 192 for (String f : prefs().get(PROP_EXTRA_CLASSPATH, "").split(Pattern.quote(File.pathSeparator))) { 193 files.add(new File (f)); 194 } 195 return files; 196 } 197 198 public static void setExtraClasspath(List <File > p) { 199 StringBuilder b = new StringBuilder (); 200 for (File 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 <File > defAECP = null; 211 private static Lookup.Result<AutomaticExtraClasspathProvider> aecpResult = null; 212 213 public static synchronized List <File > 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 <File >(); 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, 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, 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 pcs = new PropertyChangeSupport (AntSettings.class); 249 250 public static void addPropertyChangeListener(PropertyChangeListener l) { 251 pcs.addPropertyChangeListener(l); 252 } 253 254 public static void removePropertyChangeListener(PropertyChangeListener l) { 255 pcs.removePropertyChangeListener(l); 256 } 257 258 private static void firePropertyChange(String 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 node); 265 public abstract void store(Preferences node, IntrospectedInfo info); 266 } 267 268 } 269 | Popular Tags |