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-2007 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.cnd.settings;
043:
044: import java.io.ByteArrayInputStream;
045: import java.io.File;
046: import java.io.IOException;
047: import java.util.Properties;
048: import java.util.ResourceBundle;
049: import java.util.StringTokenizer;
050: import java.util.prefs.Preferences;
051: import org.netbeans.modules.cnd.api.utils.IpeUtils;
052: import org.netbeans.modules.cnd.api.utils.Path;
053: import org.openide.ErrorManager;
054: import org.openide.util.HelpCtx;
055: import org.openide.util.NbBundle;
056: import org.openide.util.NbPreferences;
057: import org.openide.util.SharedClassObject;
058: import org.openide.util.Utilities;
059:
060: /**
061: * Settings for the C/C++/Fortran. The compile/build options stored
062: * in this class are <B>default</B> options which will be applied to new files.
063: * Once a file has been created its options may diverge from the defaults. A
064: * files options do not change if these default options are changed.
065: */
066:
067: public class CppSettings extends SharedClassObject {
068:
069: /** serial uid */
070: static final long serialVersionUID = -2942467713237077336L;
071:
072: public static final int DEFAULT_PARSING_DELAY = 2000;
073: private static final boolean DEFAULT_FORTRAN_ENABLED = false; // disable Fortran by default
074:
075: // Properties
076: public static final String PROP_PARSING_DELAY = "parsingDelay"; //NOI18N
077: public static final String PROP_REPLACEABLE_STRINGS_TABLE = "replaceableStringsTable"; //NOI18N
078: public static final String PROP_FREE_FORMAT_FORTRAN = "freeFormatFortran"; // NOI18N
079: public static final String PROP_FORTRAN_ENABLED = "fortranEnabled"; // NOI18N
080: public static final String PROP_MAKE_NAME = "makeName"; // NOI18N
081: public static final String PROP_MAKE_PATH = "makePath"; // NOI18N
082: public static final String PROP_GDB_NAME = "gdbName"; // NOI18N
083: public static final String PROP_GDB_PATH = "gdbPath"; // NOI18N
084: public static final String PROP_COMPILER_SET_NAME = "compilerSetName"; // NOI18N
085: public static final String PROP_COMPILER_SET_DIRECTORIES = "compilerSetDirectories"; // NOI18N
086: public static final String PROP_C_COMPILER_NAME = "cCompilerName"; // NOI18N
087: public static final String PROP_CPP_COMPILER_NAME = "cppCompilerName"; // NOI18N
088: public static final String PROP_FORTRAN_COMPILER_NAME = "fortranCompilerName"; // NOI18N
089: public static final String PROP_GDB_REQUIRED = "gdbRequired"; // NOI18N
090: public static final String PROP_C_REQUIRED = "cRequired"; // NOI18N
091: public static final String PROP_CPP_REQUIRED = "cppRequired"; // NOI18N
092: public static final String PROP_FORTRAN_REQUIRED = "fortranRequired"; // NOI18N
093: public static final String PROP_ARRAY_REPEAT_THRESHOLD = "arrayRepeatThreshold"; // NOI18N
094:
095: /** The resource bundle for the form editor */
096: public static ResourceBundle bundle;
097:
098: // private String path = null;
099:
100: private static CppSettings cppSettings = null;
101:
102: /** Initialize each property */
103: @Override
104: protected void initialize() {
105: super .initialize();
106: // if (Boolean.getBoolean("netbeans.cnd.enable_fortran")) { // NOI18N
107: // setFortranEnabled(true); // DEBUG
108: // }
109: }
110:
111: /** Return the signleton cppSettings */
112: public static CppSettings getDefault() {
113: // See IZ 120502
114: if (cppSettings == null) {
115: cppSettings = (CppSettings) findObject(CppSettings.class,
116: true);
117: }
118: return cppSettings;
119: }
120:
121: // /**
122: // * Return the local version of $PATH. This masquerades as a property but isn't!
123: // * The reason it isn't is that we don't want persistance beyond the existing IDE
124: // * session.
125: // *
126: // * @returns Current value of the path (as a String)
127: // */
128: // public String getPath() {
129: // if (path == null) {
130: // path = Path.getPathAsString();
131: // }
132: // return path;
133: // }
134: //
135: // public void setPath(String p) {
136: // if (!p.equals(path) && p.length() > 0) {
137: // path = p;
138: // }
139: // }
140:
141: public String getCompilerSetName() {
142: //String name = (String) getProperty(PROP_COMPILER_SET_NAME);
143: String name = getPreferences()
144: .get(PROP_COMPILER_SET_NAME, null);
145: if (name == null) {
146: return "";
147: } else {
148: return name;
149: }
150: }
151:
152: public void setCompilerSetName(String name) {
153: String n = getCompilerSetName();
154: if (n == null || !n.equals(name)) {
155: //putProperty(PROP_COMPILER_SET_NAME, name, true);
156: getPreferences().put(PROP_COMPILER_SET_NAME, name);
157: firePropertyChange(PROP_COMPILER_SET_NAME, n, name);
158: }
159: }
160:
161: // public String getCompilerSetDirectories() {
162: // //return (String) getProperty(PROP_COMPILER_SET_DIRECTORIES);
163: // return getPreferences().get(PROP_COMPILER_SET_DIRECTORIES, null);
164: // }
165: //
166: // public void setCompilerSetDirectories(String name) {
167: // String n = getCompilerSetDirectories();
168: // if (n == null || !n.equals(name)) {
169: // //putProperty(PROP_COMPILER_SET_DIRECTORIES, name, true);
170: // getPreferences().put(PROP_COMPILER_SET_DIRECTORIES, name);
171: // firePropertyChange(PROP_COMPILER_SET_DIRECTORIES, n, name);
172: // }
173: // }
174: //
175: // public String getMakeName() {
176: // //String name = (String) getProperty(PROP_MAKE_NAME);
177: // String name = getPreferences().get(PROP_MAKE_NAME, null);
178: // if (name == null) {
179: // return "make"; // NOI18N
180: // } else {
181: // return name;
182: // }
183: // }
184: //
185: // public void setMakeName(String name) {
186: // String n = getMakeName();
187: // if (!n.equals(name)) {
188: // //putProperty(PROP_MAKE_NAME, name, true);
189: // getPreferences().put(PROP_MAKE_NAME, name);
190: // firePropertyChange(PROP_MAKE_NAME, n, name);
191: // }
192: // }
193: //
194: // /**
195: // * Get the current make path. If this isnt' set but make name is, do a path search and
196: // * set the make path too.
197: // *
198: // * @returns Path to the make program
199: // */
200: // public String getMakePath() {
201: // String p = getPreferences().get(PROP_MAKE_PATH, null);
202: // if (p == null) {
203: // //String name = (String) getProperty(PROP_MAKE_NAME);
204: // String name = getPreferences().get(PROP_MAKE_NAME, null);
205: // if (name != null) {
206: // StringTokenizer tok = new StringTokenizer(Path.getPathAsString(), File.pathSeparator);
207: // while (tok.hasMoreTokens()) {
208: // String d = tok.nextToken();
209: // File file = new File(d, name);
210: // if (file.exists()) {
211: // p = file.getAbsolutePath();
212: // //putProperty(PROP_MAKE_PATH, p, true);
213: // getPreferences().put(PROP_MAKE_PATH, p);
214: // firePropertyChange(PROP_MAKE_PATH, null, p);
215: // return p;
216: // }
217: // }
218: // }
219: // if (Utilities.isWindows()) {
220: // return "C:\\Cygwin\\bin\\make.exe"; // NOI18N
221: // } else if (Utilities.getOperatingSystem() == Utilities.OS_SOLARIS) {
222: // return "/usr/ccs/bin/make"; // NOI18N
223: // } else { // pick /usr/bin/make as a default value
224: // return "/usr/bin/make"; // NOI18N
225: // }
226: // } else {
227: // return p;
228: // }
229: // }
230: //
231: // public void setMakePath(String path) {
232: // String p = getMakePath();
233: // if (!p.equals(path)) {
234: // //putProperty(PROP_MAKE_PATH, path, true);
235: // getPreferences().put(PROP_MAKE_PATH, path);
236: // firePropertyChange(PROP_MAKE_PATH, p, path);
237: // }
238: // }
239: //
240: // /*
241: // * Returns full path if no spaces otherwise return just base name.
242: // * See IZ 116463 for details
243: // */
244: // public String getMakeCommand() {
245: // String makeCommand = getMakePath();
246: // if (makeCommand.indexOf(' ') >= 0) {
247: // // Strip path
248: // makeCommand = IpeUtils.getBaseName(makeCommand);
249: // }
250: // return makeCommand;
251: // }
252:
253: public String getGdbName() {
254: //String name = (String) getProperty(PROP_GDB_NAME);
255: String name = getPreferences().get(PROP_GDB_NAME, null);
256: if (name == null) {
257: return "gdb"; // NOI18N
258: } else {
259: return name;
260: }
261: }
262:
263: public void setGdbName(String name) {
264: String n = getGdbName();
265: if (!n.equals(name)) {
266: //putProperty(PROP_GDB_NAME, name, true);
267: getPreferences().put(PROP_GDB_NAME, name);
268: firePropertyChange(PROP_GDB_NAME, n, name);
269: }
270: }
271:
272: public String getGdbPath() {
273: //String path = (String) getProperty(PROP_GDB_PATH);
274: String p = getPreferences().get(PROP_GDB_PATH, null);
275: if (p == null) {
276: if (Utilities.isWindows()) {
277: return "C:\\Cygwin\\bin\\gdb.exe"; // NOI18N
278: } else {
279: return "/usr/bin/gdb"; // NOI18N
280: }
281: } else {
282: return p;
283: }
284: }
285:
286: public void setGdbPath(String path) {
287: String p = getGdbPath();
288: if (!p.equals(path)) {
289: //putProperty(PROP_GDB_PATH, path, true);
290: getPreferences().put(PROP_GDB_PATH, path);
291: firePropertyChange(PROP_GDB_PATH, p, path);
292: }
293: }
294:
295: // public String getCCompilerName() {
296: // //String name = (String) getProperty(PROP_C_COMPILER_NAME);
297: // String name = getPreferences().get(PROP_C_COMPILER_NAME, null);
298: // if (name == null) {
299: // return getCompilerSetName().startsWith("Sun") ? "cc" : "gcc"; // NOI18N
300: // } else {
301: // return name;
302: // }
303: // }
304: //
305: // public void setCCompilerName(String name) {
306: // String n = getCCompilerName();
307: // if (!n.equals(name)) {
308: // //putProperty(PROP_C_COMPILER_NAME, name, true);
309: // getPreferences().put(PROP_C_COMPILER_NAME, name);
310: // firePropertyChange(PROP_C_COMPILER_NAME, n, name);
311: // }
312: // }
313: //
314: // public String getCppCompilerName() {
315: // //String name = (String) getProperty(PROP_CPP_COMPILER_NAME);
316: // String name = getPreferences().get(PROP_CPP_COMPILER_NAME, null);
317: // if (name == null) {
318: // return getCompilerSetName().startsWith("Sun") ? "CC" : "g++"; // NOI18N
319: // } else {
320: // return name;
321: // }
322: // }
323: //
324: // public void setCppCompilerName(String name) {
325: // String n = getCppCompilerName();
326: // if (!n.equals(name)) {
327: // //putProperty(PROP_CPP_COMPILER_NAME, name, true);
328: // getPreferences().put(PROP_CPP_COMPILER_NAME, name);
329: // firePropertyChange(PROP_CPP_COMPILER_NAME, n, name);
330: // }
331: // }
332: //
333: // public String getFortranCompilerName() {
334: // //String name = (String) getProperty(PROP_FORTRAN_COMPILER_NAME);
335: // String name = getPreferences().get(PROP_FORTRAN_COMPILER_NAME, null);
336: // if (name == null) {
337: // return getCompilerSetName().startsWith("Sun") ? "f90" : "g77"; // NOI18N
338: // } else {
339: // return name;
340: // }
341: // }
342: //
343: // public void setFortranCompilerName(String name) {
344: // String n = getFortranCompilerName();
345: // if (!n.equals(name)) {
346: // //putProperty(PROP_FORTRAN_COMPILER_NAME, name, true);
347: // getPreferences().put(PROP_FORTRAN_COMPILER_NAME, name);
348: // firePropertyChange(PROP_FORTRAN_COMPILER_NAME, n, name);
349: // }
350: // }
351:
352: /**
353: * Gets the delay time for the start of the parsing.
354: * @return The time in milis
355: */
356: public int getParsingDelay() {
357: //Integer delay = (Integer)getProperty(PROP_PARSING_DELAY);
358: int delay = getPreferences().getInt(PROP_PARSING_DELAY,
359: DEFAULT_PARSING_DELAY);
360: return delay;
361: }
362:
363: /**
364: * Sets the delay time for the start of the parsing.
365: * @param delay The time in milis
366: */
367: public void setParsingDelay(int delay) {
368: if (delay != 0 && delay < 1000) {
369: IllegalArgumentException e = new IllegalArgumentException();
370: ErrorManager.getDefault().annotate(e,
371: getString("INVALID_AUTO_PARSING_DELAY"));
372: //ErrorManager.getDefault().notify(e);
373: throw e;
374: }
375: //putProperty(PROP_PARSING_DELAY, new Integer(delay));
376: int oldValue = getParsingDelay();
377: getPreferences().putInt(PROP_PARSING_DELAY, delay);
378: firePropertyChange(PROP_PARSING_DELAY, new Integer(oldValue),
379: new Integer(delay));
380: }
381:
382: /**
383: * Sets the replaceable strings table - used during instantiating
384: * from template.
385: */
386: public void setReplaceableStringsTable(String table) {
387: String t = getReplaceableStringsTable();
388: if (t.equals(table))
389: return;
390: //putProperty(PROP_REPLACEABLE_STRINGS_TABLE, table, true);
391: getPreferences().put(PROP_REPLACEABLE_STRINGS_TABLE, table);
392: firePropertyChange(PROP_REPLACEABLE_STRINGS_TABLE, t, table);
393: }
394:
395: /**
396: * Gets the replacable strings table - used during instantiating
397: * from template.
398: */
399: public String getReplaceableStringsTable() {
400: //String table = (String)getProperty(PROP_REPLACEABLE_STRINGS_TABLE);
401: String table = getPreferences().get(
402: PROP_REPLACEABLE_STRINGS_TABLE, null);
403: if (table == null) {
404: return "USER=" + System.getProperty("user.name"); // NOI18N
405: } else {
406: return table;
407: }
408: }
409:
410: /**
411: * Gets the replaceable table as the Properties class.
412: * @return the properties
413: */
414: public Properties getReplaceableStringsProps() {
415: Properties props = new Properties();
416:
417: try {
418: props.load(new ByteArrayInputStream(
419: getReplaceableStringsTable().getBytes()));
420: } catch (IOException e) {
421: }
422: return props;
423: }
424:
425: /**
426: * Find out if Fortran is enabled
427: *
428: * @return true if its enabled
429: */
430: public boolean isFortranEnabled() {
431: //Boolean b = (Boolean) getProperty(PROP_FORTRAN_ENABLED);
432: boolean b = getPreferences().getBoolean(PROP_FORTRAN_ENABLED,
433: DEFAULT_FORTRAN_ENABLED);
434: return b;
435: }
436:
437: /**
438: * Set value of PROP_FORTRAN_ENABLED property.
439: *
440: * @param enabled Value to set property to
441: */
442: public void setFortranEnabled(boolean enabled) {
443: //putProperty(PROP_FORTRAN_ENABLED, Boolean.valueOf(enabled), true);
444: boolean oldValue = isFortranEnabled();
445: getPreferences().putBoolean(PROP_FORTRAN_ENABLED, enabled);
446: firePropertyChange(PROP_FORTRAN_ENABLED, new Boolean(oldValue),
447: new Boolean(enabled));
448: }
449:
450: public boolean isFreeFormatFortran() {
451: //Boolean b = (Boolean)getProperty(PROP_FREE_FORMAT_FORTRAN);
452: boolean b = getPreferences().getBoolean(
453: PROP_FREE_FORMAT_FORTRAN, true);
454: // if( b == null ){
455: // try{
456: // // Need to go through SystemClassLoader :(
457: // Class fSettingsDefaults = Class.forName(
458: // "org.netbeans.modules.cnd.editor.fortran.FSettingsDefaults", // NOI18N
459: // true, (ClassLoader) Lookup.getDefault().lookup(ClassLoader.class)
460: // );
461: // java.lang.reflect.Field defaultFreeFormat =
462: // fSettingsDefaults.getField("defaultFreeFormat"); // NOI18N
463: // b = (defaultFreeFormat.getBoolean(null))?Boolean.TRUE:Boolean.FALSE;
464: // }catch(Exception e){
465: // // let's cheat, we know the default is TRUE (from FSettingsDefault)
466: // b = Boolean.TRUE;
467: // }
468: // putProperty(PROP_FREE_FORMAT_FORTRAN, b);
469: // }
470: // return b.booleanValue();'
471: return b;
472: }
473:
474: public void setFreeFormatFortran(boolean state) {
475: //putProperty(PROP_FREE_FORMAT_FORTRAN, state ? Boolean.TRUE : Boolean.FALSE);
476: boolean oldValue = isFreeFormatFortran();
477: getPreferences().putBoolean(PROP_FREE_FORMAT_FORTRAN, state);
478: firePropertyChange(PROP_FREE_FORMAT_FORTRAN, new Boolean(
479: oldValue), new Boolean(state));
480: }
481:
482: public boolean isGdbRequired() {
483: //Boolean b = (Boolean) getProperty(PROP_GDB_REQUIRED);
484: //return b == null ? false : b.booleanValue();
485: return getPreferences().getBoolean(PROP_GDB_REQUIRED, false);
486: }
487:
488: public void setGdbRequired(boolean enabled) {
489: //putProperty(PROP_GDB_REQUIRED, Boolean.valueOf(enabled));
490: boolean oldValue = isGdbRequired();
491: getPreferences().putBoolean(PROP_GDB_REQUIRED, enabled);
492: firePropertyChange(PROP_GDB_REQUIRED, new Boolean(oldValue),
493: new Boolean(enabled));
494: }
495:
496: // public boolean isCRequired() {
497: // //Boolean b = (Boolean) getProperty(PROP_C_REQUIRED);
498: // //return b == null ? true : b.booleanValue();
499: // return getPreferences().getBoolean(PROP_C_REQUIRED, true);
500: // }
501: //
502: // public void setCRequired(boolean enabled) {
503: // //putProperty(PROP_C_REQUIRED, Boolean.valueOf(enabled));
504: // boolean oldValue = isCRequired();
505: // getPreferences().putBoolean(PROP_C_REQUIRED, enabled);
506: // firePropertyChange(PROP_C_REQUIRED, new Boolean(oldValue), new Boolean(enabled));
507: // }
508: //
509: // public boolean isCppRequired() {
510: // //Boolean b = (Boolean) getProperty(PROP_CPP_REQUIRED);
511: // //return b == null ? true : b.booleanValue();
512: // return getPreferences().getBoolean(PROP_CPP_REQUIRED, true);
513: // }
514: //
515: // public void setCppRequired(boolean enabled) {
516: // //putProperty(PROP_CPP_REQUIRED, Boolean.valueOf(enabled));
517: // boolean oldValue = isCppRequired();
518: // getPreferences().putBoolean(PROP_CPP_REQUIRED, enabled);
519: // firePropertyChange(PROP_CPP_REQUIRED, new Boolean(oldValue), new Boolean(enabled));
520: // }
521: //
522: // public boolean isFortranRequired() {
523: // //Boolean b = (Boolean) getProperty(PROP_FORTRAN_REQUIRED);
524: // //return b == null ? false : b.booleanValue();
525: // return getPreferences().getBoolean(PROP_FORTRAN_REQUIRED, false);
526: // }
527: //
528: // public void setFortranRequired(boolean enabled) {
529: // //putProperty(PROP_FORTRAN_REQUIRED, Boolean.valueOf(enabled));
530: // boolean oldValue = isFortranRequired();
531: // getPreferences().putBoolean(PROP_FORTRAN_REQUIRED, enabled);
532: // firePropertyChange(PROP_FORTRAN_REQUIRED, new Boolean(oldValue), new Boolean(enabled));
533: // }
534:
535: public int getArrayRepeatThreshold() {
536: return getPreferences().getInt(PROP_ARRAY_REPEAT_THRESHOLD, 10);
537: }
538:
539: public void setArrayRepeatThreshold(int arrayRepeatThreshold) {
540: int art = getArrayRepeatThreshold();
541: if (art != arrayRepeatThreshold) {
542: getPreferences().putInt(PROP_ARRAY_REPEAT_THRESHOLD,
543: arrayRepeatThreshold);
544: firePropertyChange(PROP_ARRAY_REPEAT_THRESHOLD, art,
545: arrayRepeatThreshold);
546: }
547: }
548:
549: /**
550: * Get the display name.
551: *
552: * @return value of OPTION_CPP_SETTINGS_NAME
553: */
554: public String displayName() {
555: return getString("OPTION_CPP_SETTINGS_NAME"); //NOI18N
556: }
557:
558: public HelpCtx getHelpCtx() {
559: return new HelpCtx("Welcome_opt_editing_sources"); //NOI18N
560: }
561:
562: private Preferences getPreferences() {
563: return NbPreferences.forModule(CppSettings.class);
564: }
565:
566: public ResourceBundle getBundle() {
567: return bundle;
568: }
569:
570: /** @return localized string */
571: static String getString(String s) {
572: if (bundle == null) {
573: bundle = NbBundle.getBundle(CppSettings.class);
574: }
575: return bundle.getString(s);
576: }
577: }
|