1 19 20 package org.netbeans.modules.project.ui; 21 22 import java.io.IOException ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.io.File ; 29 import java.util.prefs.Preferences ; 30 import javax.swing.filechooser.FileSystemView ; 31 import org.openide.filesystems.FileUtil; 32 import org.openide.util.NbBundle; 33 import org.openide.util.NbPreferences; 34 35 37 public class OpenProjectListSettings { 38 39 private static OpenProjectListSettings INSTANCE = new OpenProjectListSettings(); 40 41 private static final String RECENT_PROJECTS_DISPLAY_NAMES = "RecentProjectsDisplayNames"; private static final String RECENT_PROJECTS_DISPLAY_ICONS = "RecentProjectsIcons"; private static final String LAST_OPEN_PROJECT_DIR = "lastOpenProjectDir"; private static final String PROP_PROJECT_CATEGORY = "lastSelectedProjectCategory"; private static final String PROP_PROJECT_TYPE = "lastSelectedProjectType"; private static final String MAIN_PROJECT_URL = "mainProjectURL"; private static final String OPEN_AS_MAIN = "openAsMain"; private static final String OPEN_PROJECTS_URLS = "openProjectsURLs"; private static final String OPEN_SUBPROJECTS = "openSubprojects"; private static final String PROP_PROJECTS_FOLDER = "projectsFolder"; private static final String RECENT_PROJECTS_URLS = "recentProjectsURLs"; private static final String RECENT_TEMPLATES = "recentTemplates"; 54 55 private OpenProjectListSettings() { 56 } 57 58 public static OpenProjectListSettings getInstance() { 59 return INSTANCE; 60 } 61 62 protected final String putProperty(String key, String value, boolean notify) { 63 String 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 getProperty(String key) { 73 return getPreferences().get(key, null); 74 } 75 76 protected final List <URL > getURLList(String key) { 77 List <String > strs = getStringList(key); 78 List <URL > toRet = new ArrayList <URL >(); 79 for (String val : strs) { 80 try { 81 toRet.add(new URL (val)); 82 } catch (MalformedURLException ex) { 83 ex.printStackTrace(); 84 } 85 } 86 return toRet; 87 } 88 89 protected final List <String > getStringList(String key) { 90 Preferences pref = getPreferences(); 91 int count = 0; 92 String val = pref.get(key + "." + count, null); 93 List <String > toRet = new ArrayList <String >(); 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 <ExtIcon> getIconList(String key) { 103 Preferences pref = getPreferences(); 104 int count = 0; 105 byte[] val = pref.getByteArray(key + "." + count, null); 106 List <ExtIcon> toRet = new ArrayList <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 basekey, List <ExtIcon> list) throws IOException { 116 assert list != null; 117 Preferences pref = getPreferences(); 118 int count = 0; 119 String key = basekey + "." + count; 120 String val = pref.get(key, null); 121 Iterator <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 basekey, List <String > list) { 136 assert list != null; 137 Preferences pref = getPreferences(); 138 int count = 0; 139 String key = basekey + "." + count; 140 String val = pref.get(key, null); 141 Iterator <String > 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 basekey, List <URL > list) { 155 assert list != null; 156 List <String > strs = new ArrayList <String >(list.size()); 157 for (URL url : list) { 158 strs.add(url.toExternalForm()); 159 } 160 setStringList(basekey, strs); 161 } 162 163 protected final Preferences getPreferences() { 164 return NbPreferences.forModule(OpenProjectListSettings.class); 165 } 166 167 public List <URL > getOpenProjectsURLs() { 168 return getURLList(OPEN_PROJECTS_URLS); 169 } 170 171 public void setOpenProjectsURLs( List <URL > 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 getMainProjectURL() { 192 String str = getProperty(MAIN_PROJECT_URL); 193 if (str != null) { 194 try { 195 return new URL (str); 196 } catch (MalformedURLException ex) { 197 ex.printStackTrace(); 198 } 199 } 200 return null; 201 } 202 203 public void setMainProjectURL( URL mainProjectURL ) { 204 putProperty( MAIN_PROJECT_URL, mainProjectURL != null ? mainProjectURL.toString() : null, true ); 205 } 206 207 public String getLastOpenProjectDir() { 208 String result = getProperty( LAST_OPEN_PROJECT_DIR ); 209 if (result == null) { 210 result = getProjectsFolder(false).getAbsolutePath(); 211 } 212 return result; 213 } 214 215 public void setLastOpenProjectDir( String path ) { 216 putProperty( LAST_OPEN_PROJECT_DIR, path, true ); 217 } 218 219 public List <URL > getRecentProjectsURLs() { 220 return getURLList(RECENT_PROJECTS_URLS); 221 } 222 223 public List <String > getRecentProjectsDisplayNames() { 224 return getStringList(RECENT_PROJECTS_DISPLAY_NAMES); 225 } 226 227 public List <ExtIcon> getRecentProjectsIcons() { 228 return getIconList(RECENT_PROJECTS_DISPLAY_ICONS); 229 } 230 231 public void setRecentProjectsURLs( List <URL > list ) { 232 setURLList(RECENT_PROJECTS_URLS, list); 233 } 234 235 public void setRecentProjectsDisplayNames(List <String > list) { 236 setStringList(RECENT_PROJECTS_DISPLAY_NAMES, list); 237 } 238 239 public void setRecentProjectsIcons(List <ExtIcon> list) { 240 try { 241 setIconList(RECENT_PROJECTS_DISPLAY_ICONS, list); 242 } catch (IOException ex) { 243 ex.printStackTrace(); 244 } 245 } 246 247 public File getProjectsFolder(boolean create) { 248 String result = getProperty (PROP_PROJECTS_FOLDER); 249 if (result == null) { 250 String userPrjDir = System.getProperty("netbeans.projects.dir"); if (userPrjDir != null) { 253 File f = new File (userPrjDir); 254 if (f.exists() && f.isDirectory()) { 255 return FileUtil.normalizeFile(f); 256 } 257 } 258 File defaultDir = FileSystemView.getFileSystemView().getDefaultDirectory(); 259 if (defaultDir != null && defaultDir.exists() && defaultDir.isDirectory()) { 260 String nbPrjDirName = NbBundle.getMessage(OpenProjectListSettings.class, "DIR_NetBeansProjects"); 261 File nbPrjDir = new File (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"); } 271 return FileUtil.normalizeFile(new File (result)); 272 } 273 274 public void setProjectsFolder (File folder) { 275 if (folder == null) { 276 putProperty(PROP_PROJECTS_FOLDER, (String )null, true); 277 } 278 else { 279 putProperty(PROP_PROJECTS_FOLDER, folder.getAbsolutePath(), true); 280 } 281 } 282 283 public List <String > getRecentTemplates() { 284 return getStringList(RECENT_TEMPLATES); 285 } 286 287 public void setRecentTemplates( List <String > templateNames ) { 288 setStringList( RECENT_TEMPLATES, templateNames ); 289 } 290 291 public String getLastSelectedProjectCategory () { 292 return getProperty (PROP_PROJECT_CATEGORY); 293 } 294 295 public void setLastSelectedProjectCategory (String category) { 296 putProperty(PROP_PROJECT_CATEGORY,category,true); 297 } 298 299 public String getLastSelectedProjectType () { 300 return getProperty (PROP_PROJECT_TYPE); 301 } 302 303 public void setLastSelectedProjectType (String type) { 304 putProperty(PROP_PROJECT_TYPE,type,true); 305 } 306 307 } 308 | Popular Tags |