KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > options > BlueJSettings


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.netbeans.bluej.options;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.prefs.Preferences JavaDoc;
30 import org.openide.util.NbPreferences;
31 import org.openide.util.Utilities;
32
33 /**
34  *
35  * @author Milos Kleint
36  */

37 public class BlueJSettings {
38     public static final String JavaDoc PROP_HOME = "home"; // NOI18N
39

40     private static final BlueJSettings INSTANCE = new BlueJSettings();
41     
42     private PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
43     
44     protected final Preferences JavaDoc getPreferences() {
45         return NbPreferences.forModule(BlueJSettings.class);
46     }
47     
48     protected final String JavaDoc putProperty(String JavaDoc key, String JavaDoc value) {
49         String JavaDoc retval = getProperty(key);
50         if (value != null) {
51             getPreferences().put(key, value);
52         } else {
53             getPreferences().remove(key);
54         }
55         support.firePropertyChange(key, retval, value);
56         return retval;
57     }
58     
59     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
60         support.addPropertyChangeListener(listener);
61     }
62     
63     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
64         support.removePropertyChangeListener(listener);
65     }
66
67     protected final String JavaDoc getProperty(String JavaDoc key) {
68         return getPreferences().get(key, null);
69     }
70     
71     public static BlueJSettings getDefault() {
72         return INSTANCE;
73     }
74     
75     public File JavaDoc getHome() {
76         String JavaDoc s = getProperty(PROP_HOME);
77         return s != null ? new File JavaDoc(s) : null;
78     }
79     
80     public void setHome(File JavaDoc home) {
81         putProperty(PROP_HOME, home == null ? null : home.getAbsolutePath());
82     }
83     
84     /**
85      * There is a bluej.properties file in the user directory. It countains a row of properties
86      * named bluej.userlibrary.*.location, it's value is the path to the library, * is the number starting from
87      * 1. The cycle stops when there is one number missing.
88      * the user directory is in various places on each OS. Windows is "bluej" under user.home, on macosx it's "Library/Preferences/org.bluej" under user.home
89      * any other platform is ".bluej" under user.home.
90      * @return as ant classpath entry.
91      */

92     public String JavaDoc getUserLibrariesAsClassPath() {
93         File JavaDoc userDir = new File JavaDoc(System.getProperty("user.home")); // NOI18N
94
File JavaDoc bluejHome = null;
95         if (Utilities.isWindows()) {
96             bluejHome = new File JavaDoc(userDir, "bluej"); // NOI18N
97
} else if (Utilities.getOperatingSystem() == Utilities.OS_MAC) {
98             bluejHome = new File JavaDoc(userDir, "Library/Preferences/org.bluej"); // NOI18N
99
} else {
100             bluejHome = new File JavaDoc(userDir, ".bluej"); // NOI18N
101
}
102         File JavaDoc prop = new File JavaDoc(bluejHome, "bluej.properties"); // NOI18N
103
String JavaDoc path = "";
104         if (prop.exists()) {
105             FileInputStream JavaDoc str = null;
106             try {
107                 str = new FileInputStream JavaDoc(prop);
108                 Properties JavaDoc properties = new Properties JavaDoc();
109                 properties.load(str);
110                 int index = 1;
111                 while (true) {
112                     String JavaDoc propKey = "bluej.userlibrary." + index + ".location"; // NOI18N
113
String JavaDoc value = properties.getProperty(propKey);
114                     if (value != null) {
115                         path = path + (path.length() == 0 ? "" : ":") + value; // NOI18N
116
} else {
117                         //we're done.
118
break;
119                     }
120                     index = index + 1;
121                 }
122             } catch (FileNotFoundException JavaDoc ex) {
123                 ex.printStackTrace();
124             } catch (IOException JavaDoc ex) {
125                 ex.printStackTrace();
126             }
127                     
128         }
129         return path;
130     }
131     
132 }
133
Popular Tags