KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > OpenProjectListSettings


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.modules.project.ui;
21
22 import java.io.IOException JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.io.File JavaDoc;
29 import java.util.prefs.Preferences JavaDoc;
30 import javax.swing.filechooser.FileSystemView JavaDoc;
31 import org.openide.filesystems.FileUtil;
32 import org.openide.util.NbBundle;
33 import org.openide.util.NbPreferences;
34
35 /** SystemOption to store the list of open projects
36  */

37 public class OpenProjectListSettings {
38
39     private static OpenProjectListSettings INSTANCE = new OpenProjectListSettings();
40     
41     private static final String JavaDoc RECENT_PROJECTS_DISPLAY_NAMES = "RecentProjectsDisplayNames"; //NOI18N
42
private static final String JavaDoc RECENT_PROJECTS_DISPLAY_ICONS = "RecentProjectsIcons"; //NOI18N
43
private static final String JavaDoc LAST_OPEN_PROJECT_DIR = "lastOpenProjectDir"; //NOI18N - String
44
private static final String JavaDoc PROP_PROJECT_CATEGORY = "lastSelectedProjectCategory"; //NOI18N - String
45
private static final String JavaDoc PROP_PROJECT_TYPE = "lastSelectedProjectType"; //NOI18N - String
46
private static final String JavaDoc MAIN_PROJECT_URL = "mainProjectURL"; //NOI18N -URL
47
private static final String JavaDoc OPEN_AS_MAIN = "openAsMain"; //NOI18N - boolean
48
private static final String JavaDoc OPEN_PROJECTS_URLS = "openProjectsURLs"; //NOI18N - List of URLs
49
private static final String JavaDoc OPEN_SUBPROJECTS = "openSubprojects"; //NOI18N - boolean
50
private static final String JavaDoc PROP_PROJECTS_FOLDER = "projectsFolder"; //NOI18N - String
51
private static final String JavaDoc RECENT_PROJECTS_URLS = "recentProjectsURLs"; //NOI18N List of URLs
52
private static final String JavaDoc RECENT_TEMPLATES = "recentTemplates"; // NOI18N -List of Strings
53

54     
55     private OpenProjectListSettings() {
56     }
57     
58     public static OpenProjectListSettings getInstance() {
59         return INSTANCE;
60     }
61     
62     protected final String JavaDoc putProperty(String JavaDoc key, String JavaDoc value, boolean notify) {
63         String JavaDoc retval = getProperty(key);
64         if (value != null) {
65             getPreferences().put(key, value);
66         } else {
67             getPreferences().remove(key);
68         }
69         return retval;
70     }
71
72     protected final String JavaDoc getProperty(String JavaDoc key) {
73         return getPreferences().get(key, null);
74     }
75     
76     protected final List JavaDoc<URL JavaDoc> getURLList(String JavaDoc key) {
77         List JavaDoc<String JavaDoc> strs = getStringList(key);
78         List JavaDoc<URL JavaDoc> toRet = new ArrayList JavaDoc<URL JavaDoc>();
79         for (String JavaDoc val : strs) {
80             try {
81                 toRet.add(new URL JavaDoc(val));
82             } catch (MalformedURLException JavaDoc ex) {
83                 ex.printStackTrace();
84             }
85         }
86         return toRet;
87     }
88     
89     protected final List JavaDoc<String JavaDoc> getStringList(String JavaDoc key) {
90         Preferences JavaDoc pref = getPreferences();
91         int count = 0;
92         String JavaDoc val = pref.get(key + "." + count, null);
93         List JavaDoc<String JavaDoc> toRet = new ArrayList JavaDoc<String JavaDoc>();
94         while (val != null) {
95             toRet.add(val);
96             count = count + 1;
97             val = pref.get(key + "." + count, null);
98         }
99         return toRet;
100     }
101     
102     protected final List JavaDoc<ExtIcon> getIconList(String JavaDoc key) {
103         Preferences JavaDoc pref = getPreferences();
104         int count = 0;
105         byte[] val = pref.getByteArray(key + "." + count, null);
106         List JavaDoc<ExtIcon> toRet = new ArrayList JavaDoc<ExtIcon>();
107         while (val != null) {
108             toRet.add(new ExtIcon(val));
109             count = count + 1;
110             val = pref.getByteArray(key + "." + count, null);
111         }
112         return toRet;
113     }
114     
115     protected final void setIconList(String JavaDoc basekey, List JavaDoc<ExtIcon> list) throws IOException JavaDoc {
116         assert list != null;
117         Preferences JavaDoc pref = getPreferences();
118         int count = 0;
119         String JavaDoc key = basekey + "." + count;
120         String JavaDoc val = pref.get(key, null);
121         Iterator JavaDoc<ExtIcon> it = list.iterator();
122         while (val != null || it.hasNext()) {
123             if (it.hasNext()) {
124                 pref.putByteArray(key, it.next().getBytes());
125             } else {
126                 pref.remove(key);
127             }
128             count = count + 1;
129             key = basekey + "." + count;
130             val = pref.get(key, null);
131         }
132     }
133     
134     
135     protected final void setStringList(String JavaDoc basekey, List JavaDoc<String JavaDoc> list) {
136         assert list != null;
137         Preferences JavaDoc pref = getPreferences();
138         int count = 0;
139         String JavaDoc key = basekey + "." + count;
140         String JavaDoc val = pref.get(key, null);
141         Iterator JavaDoc<String JavaDoc> it = list.iterator();
142         while (val != null || it.hasNext()) {
143             if (it.hasNext()) {
144                 pref.put(key, it.next());
145             } else {
146                 pref.remove(key);
147             }
148             count = count + 1;
149             key = basekey + "." + count;
150             val = pref.get(key, null);
151         }
152     }
153     
154     protected final void setURLList(String JavaDoc basekey, List JavaDoc<URL JavaDoc> list) {
155         assert list != null;
156         List JavaDoc<String JavaDoc> strs = new ArrayList JavaDoc<String JavaDoc>(list.size());
157         for (URL JavaDoc url : list) {
158             strs.add(url.toExternalForm());
159         }
160         setStringList(basekey, strs);
161     }
162     
163     protected final Preferences JavaDoc getPreferences() {
164         return NbPreferences.forModule(OpenProjectListSettings.class);
165     }
166
167     public List JavaDoc<URL JavaDoc> getOpenProjectsURLs() {
168         return getURLList(OPEN_PROJECTS_URLS);
169     }
170
171     public void setOpenProjectsURLs( List JavaDoc<URL JavaDoc> list ) {
172         setURLList( OPEN_PROJECTS_URLS, list);
173     }
174     
175     public boolean isOpenSubprojects() {
176         return getPreferences().getBoolean( OPEN_SUBPROJECTS, true);
177     }
178     
179     public void setOpenSubprojects( boolean openSubprojects ) {
180         getPreferences().putBoolean(OPEN_SUBPROJECTS, openSubprojects);
181     }
182     
183     public boolean isOpenAsMain() {
184         return getPreferences().getBoolean( OPEN_AS_MAIN, true);
185     }
186     
187     public void setOpenAsMain( boolean openAsMain ) {
188         getPreferences().putBoolean(OPEN_AS_MAIN, openAsMain);
189     }
190     
191     public URL JavaDoc getMainProjectURL() {
192         String JavaDoc str = getProperty(MAIN_PROJECT_URL);
193         if (str != null) {
194             try {
195                 return new URL JavaDoc(str);
196             } catch (MalformedURLException JavaDoc ex) {
197                 ex.printStackTrace();
198             }
199         }
200         return null;
201     }
202     
203     public void setMainProjectURL( URL JavaDoc mainProjectURL ) {
204         putProperty( MAIN_PROJECT_URL, mainProjectURL != null ? mainProjectURL.toString() : null, true );
205     }
206     
207     public String JavaDoc getLastOpenProjectDir() {
208         String JavaDoc result = getProperty( LAST_OPEN_PROJECT_DIR );
209         if (result == null) {
210             result = getProjectsFolder(/* #89624 */false).getAbsolutePath();
211         }
212         return result;
213     }
214     
215     public void setLastOpenProjectDir( String JavaDoc path ) {
216         putProperty( LAST_OPEN_PROJECT_DIR, path, true );
217     }
218     
219     public List JavaDoc<URL JavaDoc> getRecentProjectsURLs() {
220         return getURLList(RECENT_PROJECTS_URLS);
221     }
222     
223     public List JavaDoc<String JavaDoc> getRecentProjectsDisplayNames() {
224         return getStringList(RECENT_PROJECTS_DISPLAY_NAMES);
225     }
226     
227     public List JavaDoc<ExtIcon> getRecentProjectsIcons() {
228         return getIconList(RECENT_PROJECTS_DISPLAY_ICONS);
229     }
230     
231     public void setRecentProjectsURLs( List JavaDoc<URL JavaDoc> list ) {
232         setURLList(RECENT_PROJECTS_URLS, list);
233     }
234     
235     public void setRecentProjectsDisplayNames(List JavaDoc<String JavaDoc> list) {
236         setStringList(RECENT_PROJECTS_DISPLAY_NAMES, list);
237     }
238     
239     public void setRecentProjectsIcons(List JavaDoc<ExtIcon> list) {
240         try {
241             setIconList(RECENT_PROJECTS_DISPLAY_ICONS, list);
242         } catch (IOException JavaDoc ex) {
243             ex.printStackTrace();
244         }
245     }
246     
247     public File JavaDoc getProjectsFolder(boolean create) {
248         String JavaDoc result = getProperty (PROP_PROJECTS_FOLDER);
249         if (result == null) {
250             // property for overriding default projects dir location
251
String JavaDoc userPrjDir = System.getProperty("netbeans.projects.dir"); // NOI18N
252
if (userPrjDir != null) {
253                 File JavaDoc f = new File JavaDoc(userPrjDir);
254                 if (f.exists() && f.isDirectory()) {
255                     return FileUtil.normalizeFile(f);
256                 }
257             }
258             File JavaDoc defaultDir = FileSystemView.getFileSystemView().getDefaultDirectory();
259             if (defaultDir != null && defaultDir.exists() && defaultDir.isDirectory()) {
260                 String JavaDoc nbPrjDirName = NbBundle.getMessage(OpenProjectListSettings.class, "DIR_NetBeansProjects");
261                 File JavaDoc nbPrjDir = new File JavaDoc(defaultDir, nbPrjDirName);
262                 if (nbPrjDir.exists() && nbPrjDir.canWrite()) {
263                     return nbPrjDir;
264                 } else {
265                     boolean created = create && nbPrjDir.mkdir();
266                     if (created) return nbPrjDir;
267                 }
268             }
269             result = System.getProperty("user.home"); //NOI18N
270
}
271         return FileUtil.normalizeFile(new File JavaDoc(result));
272     }
273
274     public void setProjectsFolder (File JavaDoc folder) {
275         if (folder == null) {
276             putProperty(PROP_PROJECTS_FOLDER, (String JavaDoc)null, true);
277         }
278         else {
279             putProperty(PROP_PROJECTS_FOLDER, folder.getAbsolutePath(), true);
280         }
281     }
282     
283     public List JavaDoc<String JavaDoc> getRecentTemplates() {
284         return getStringList(RECENT_TEMPLATES);
285     }
286     
287     public void setRecentTemplates( List JavaDoc<String JavaDoc> templateNames ) {
288         setStringList( RECENT_TEMPLATES, templateNames );
289     }
290     
291     public String JavaDoc getLastSelectedProjectCategory () {
292         return getProperty (PROP_PROJECT_CATEGORY);
293     }
294     
295     public void setLastSelectedProjectCategory (String JavaDoc category) {
296         putProperty(PROP_PROJECT_CATEGORY,category,true);
297     }
298     
299     public String JavaDoc getLastSelectedProjectType () {
300         return getProperty (PROP_PROJECT_TYPE);
301     }
302     
303     public void setLastSelectedProjectType (String JavaDoc type) {
304         putProperty(PROP_PROJECT_TYPE,type,true);
305     }
306
307 }
308
Popular Tags