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-2006 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.project.ui;
043:
044: import java.io.IOException;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import java.util.ArrayList;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.io.File;
051: import java.util.prefs.Preferences;
052: import javax.swing.filechooser.FileSystemView;
053: import org.openide.filesystems.FileUtil;
054: import org.openide.util.Exceptions;
055: import org.openide.util.NbBundle;
056: import org.openide.util.NbPreferences;
057:
058: /** SystemOption to store the list of open projects
059: */
060: public class OpenProjectListSettings {
061:
062: private static OpenProjectListSettings INSTANCE = new OpenProjectListSettings();
063:
064: private static final String RECENT_PROJECTS_DISPLAY_NAMES = "RecentProjectsDisplayNames"; //NOI18N
065: private static final String RECENT_PROJECTS_DISPLAY_ICONS = "RecentProjectsIcons"; //NOI18N
066: private static final String LAST_OPEN_PROJECT_DIR = "lastOpenProjectDir"; //NOI18N - String
067: private static final String PROP_PROJECT_CATEGORY = "lastSelectedProjectCategory"; //NOI18N - String
068: private static final String PROP_PROJECT_TYPE = "lastSelectedProjectType"; //NOI18N - String
069: private static final String MAIN_PROJECT_URL = "mainProjectURL"; //NOI18N -URL
070: private static final String OPEN_AS_MAIN = "openAsMain"; //NOI18N - boolean
071: private static final String OPEN_PROJECTS_URLS = "openProjectsURLs"; //NOI18N - List of URLs
072: private static final String OPEN_PROJECTS_DISPLAY_NAMES = "openProjectsDisplayNames"; //NOI18N - List of names
073: private static final String OPEN_PROJECTS_ICONS = "openProjectsIcons"; //NOI18N - List of icons
074: private static final String OPEN_SUBPROJECTS = "openSubprojects"; //NOI18N - boolean
075: private static final String PROP_PROJECTS_FOLDER = "projectsFolder"; //NOI18N - String
076: private static final String RECENT_PROJECTS_URLS = "recentProjectsURLs"; //NOI18N List of URLs
077: private static final String RECENT_TEMPLATES = "recentTemplates"; // NOI18N -List of Strings
078:
079: public static final String PROP_CREATED_PROJECTS_FOLDER = "createdProjectsFolderInWizard"; // NOI18N
080:
081: private OpenProjectListSettings() {
082: }
083:
084: public static OpenProjectListSettings getInstance() {
085: return INSTANCE;
086: }
087:
088: protected final String putProperty(String key, String value,
089: boolean notify) {
090: String retval = getProperty(key);
091: if (value != null) {
092: getPreferences().put(key, value);
093: } else {
094: getPreferences().remove(key);
095: }
096: return retval;
097: }
098:
099: protected final String getProperty(String key) {
100: return getPreferences().get(key, null);
101: }
102:
103: protected final List<URL> getURLList(String key) {
104: List<String> strs = getStringList(key);
105: List<URL> toRet = new ArrayList<URL>();
106: for (String val : strs) {
107: try {
108: toRet.add(new URL(val));
109: } catch (MalformedURLException ex) {
110: ex.printStackTrace();
111: }
112: }
113: return toRet;
114: }
115:
116: protected final List<String> getStringList(String key) {
117: Preferences pref = getPreferences();
118: int count = 0;
119: String val = pref.get(key + "." + count, null);
120: List<String> toRet = new ArrayList<String>();
121: while (val != null) {
122: toRet.add(val);
123: count = count + 1;
124: val = pref.get(key + "." + count, null);
125: }
126: return toRet;
127: }
128:
129: protected final List<ExtIcon> getIconList(String key) {
130: Preferences pref = getPreferences();
131: int count = 0;
132: byte[] val = pref.getByteArray(key + "." + count, null);
133: List<ExtIcon> toRet = new ArrayList<ExtIcon>();
134: while (val != null) {
135: toRet
136: .add(val.length > 0 ? new ExtIcon(val)
137: : new ExtIcon());
138: count = count + 1;
139: val = pref.getByteArray(key + "." + count, null);
140: }
141: return toRet;
142: }
143:
144: protected final void setIconList(String basekey, List<ExtIcon> list)
145: throws IOException {
146: assert list != null;
147: Preferences pref = getPreferences();
148: int count = 0;
149: String key = basekey + "." + count;
150: String val = pref.get(key, null);
151: Iterator<ExtIcon> it = list.iterator();
152: while (val != null || it.hasNext()) {
153: if (it.hasNext()) {
154: byte[] data = it.next().getBytes();
155: if (data == null) {
156: data = new byte[0];
157: }
158: pref.putByteArray(key, data);
159: } else {
160: pref.remove(key);
161: }
162: count = count + 1;
163: key = basekey + "." + count;
164: val = pref.get(key, null);
165: }
166: }
167:
168: protected final void setStringList(String basekey, List<String> list) {
169: assert list != null;
170: Preferences pref = getPreferences();
171: int count = 0;
172: String key = basekey + "." + count;
173: String val = pref.get(key, null);
174: Iterator<String> it = list.iterator();
175: while (val != null || it.hasNext()) {
176: if (it.hasNext()) {
177: pref.put(key, it.next());
178: } else {
179: pref.remove(key);
180: }
181: count = count + 1;
182: key = basekey + "." + count;
183: val = pref.get(key, null);
184: }
185: }
186:
187: protected final void setURLList(String basekey, List<URL> list) {
188: assert list != null;
189: List<String> strs = new ArrayList<String>(list.size());
190: for (URL url : list) {
191: strs.add(url.toExternalForm());
192: }
193: setStringList(basekey, strs);
194: }
195:
196: protected final Preferences getPreferences() {
197: return NbPreferences.forModule(OpenProjectListSettings.class);
198: }
199:
200: public List<URL> getOpenProjectsURLs() {
201: return getURLList(OPEN_PROJECTS_URLS);
202: }
203:
204: public void setOpenProjectsURLs(List<URL> list) {
205: setURLList(OPEN_PROJECTS_URLS, list);
206: }
207:
208: public List<String> getOpenProjectsDisplayNames() {
209: return getStringList(OPEN_PROJECTS_DISPLAY_NAMES);
210: }
211:
212: public void setOpenProjectsDisplayNames(List<String> list) {
213: setStringList(OPEN_PROJECTS_DISPLAY_NAMES, list);
214: }
215:
216: public List<ExtIcon> getOpenProjectsIcons() {
217: return getIconList(OPEN_PROJECTS_ICONS);
218: }
219:
220: public void setOpenProjectsIcons(List<ExtIcon> list) {
221: try {
222: setIconList(OPEN_PROJECTS_ICONS, list);
223: } catch (IOException ex) {
224: Exceptions.printStackTrace(ex);
225: }
226: }
227:
228: public boolean isOpenSubprojects() {
229: return getPreferences().getBoolean(OPEN_SUBPROJECTS, false);
230: }
231:
232: public void setOpenSubprojects(boolean openSubprojects) {
233: getPreferences().putBoolean(OPEN_SUBPROJECTS, openSubprojects);
234: }
235:
236: public boolean isOpenAsMain() {
237: return getPreferences().getBoolean(OPEN_AS_MAIN, true);
238: }
239:
240: public void setOpenAsMain(boolean openAsMain) {
241: getPreferences().putBoolean(OPEN_AS_MAIN, openAsMain);
242: }
243:
244: public URL getMainProjectURL() {
245: String str = getProperty(MAIN_PROJECT_URL);
246: if (str != null) {
247: try {
248: return new URL(str);
249: } catch (MalformedURLException ex) {
250: ex.printStackTrace();
251: }
252: }
253: return null;
254: }
255:
256: public void setMainProjectURL(URL mainProjectURL) {
257: putProperty(MAIN_PROJECT_URL,
258: mainProjectURL != null ? mainProjectURL.toString()
259: : null, true);
260: }
261:
262: public String getLastOpenProjectDir() {
263: String result = getProperty(LAST_OPEN_PROJECT_DIR);
264: if (result == null) {
265: result = getProjectsFolder(/* #89624 */false)
266: .getAbsolutePath();
267: }
268: return result;
269: }
270:
271: public void setLastOpenProjectDir(String path) {
272: putProperty(LAST_OPEN_PROJECT_DIR, path, true);
273: }
274:
275: public List<URL> getRecentProjectsURLs() {
276: return getURLList(RECENT_PROJECTS_URLS);
277: }
278:
279: public List<String> getRecentProjectsDisplayNames() {
280: return getStringList(RECENT_PROJECTS_DISPLAY_NAMES);
281: }
282:
283: public List<ExtIcon> getRecentProjectsIcons() {
284: return getIconList(RECENT_PROJECTS_DISPLAY_ICONS);
285: }
286:
287: public void setRecentProjectsURLs(List<URL> list) {
288: setURLList(RECENT_PROJECTS_URLS, list);
289: }
290:
291: public void setRecentProjectsDisplayNames(List<String> list) {
292: setStringList(RECENT_PROJECTS_DISPLAY_NAMES, list);
293: }
294:
295: public void setRecentProjectsIcons(List<ExtIcon> list) {
296: try {
297: setIconList(RECENT_PROJECTS_DISPLAY_ICONS, list);
298: } catch (IOException ex) {
299: ex.printStackTrace();
300: }
301: }
302:
303: public File getProjectsFolder(boolean create) {
304: String result = getProperty(PROP_PROJECTS_FOLDER);
305: if (result == null || !(new File(result)).exists()) {
306: // property for overriding default projects dir location
307: String userPrjDir = System
308: .getProperty("netbeans.projects.dir"); // NOI18N
309: if (userPrjDir != null) {
310: File f = new File(userPrjDir);
311: if (f.exists() && f.isDirectory()) {
312: return FileUtil.normalizeFile(f);
313: }
314: }
315: File defaultDir = FileSystemView.getFileSystemView()
316: .getDefaultDirectory();
317: if (defaultDir != null && defaultDir.exists()
318: && defaultDir.isDirectory()) {
319: String nbPrjDirName = NbBundle.getMessage(
320: OpenProjectListSettings.class,
321: "DIR_NetBeansProjects");
322: File nbPrjDir = new File(defaultDir, nbPrjDirName);
323: if (nbPrjDir.exists() && nbPrjDir.canWrite()) {
324: return nbPrjDir;
325: } else {
326: boolean created = create && nbPrjDir.mkdir();
327: if (created) {
328: // #75960 - using Preferences to temporarily save created projects folder path,
329: // folder will be deleted after wizard is finished if nothing was created in it
330: getPreferences().put(
331: PROP_CREATED_PROJECTS_FOLDER,
332: nbPrjDir.getAbsolutePath());
333: return nbPrjDir;
334: }
335: }
336: }
337: result = System.getProperty("user.home"); //NOI18N
338: }
339: return FileUtil.normalizeFile(new File(result));
340: }
341:
342: public void setProjectsFolder(File folder) {
343: if (folder == null) {
344: putProperty(PROP_PROJECTS_FOLDER, (String) null, true);
345: } else {
346: putProperty(PROP_PROJECTS_FOLDER, folder.getAbsolutePath(),
347: true);
348: }
349: }
350:
351: public List<String> getRecentTemplates() {
352: return getStringList(RECENT_TEMPLATES);
353: }
354:
355: public void setRecentTemplates(List<String> templateNames) {
356: setStringList(RECENT_TEMPLATES, templateNames);
357: }
358:
359: public String getLastSelectedProjectCategory() {
360: return getProperty(PROP_PROJECT_CATEGORY);
361: }
362:
363: public void setLastSelectedProjectCategory(String category) {
364: putProperty(PROP_PROJECT_CATEGORY, category, true);
365: }
366:
367: public String getLastSelectedProjectType() {
368: return getProperty(PROP_PROJECT_TYPE);
369: }
370:
371: public void setLastSelectedProjectType(String type) {
372: putProperty(PROP_PROJECT_TYPE, type, true);
373: }
374:
375: }
|