1 11 package org.eclipse.ui.internal.ide; 12 13 import java.io.File ; 14 import java.io.FileReader ; 15 import java.io.IOException ; 16 import java.io.Reader ; 17 import java.net.URL ; 18 import java.util.StringTokenizer ; 19 20 import org.eclipse.core.runtime.Path; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.core.runtime.preferences.ConfigurationScope; 23 import org.eclipse.jface.preference.IPreferenceStore; 24 import org.eclipse.osgi.service.datalocation.Location; 25 import org.eclipse.ui.IMemento; 26 import org.eclipse.ui.WorkbenchException; 27 import org.eclipse.ui.XMLMemento; 28 import org.eclipse.ui.ide.IDE; 29 import org.eclipse.ui.preferences.ScopedPreferenceStore; 30 import org.osgi.service.prefs.BackingStoreException; 31 import org.osgi.service.prefs.Preferences; 32 33 37 public class ChooseWorkspaceData { 38 41 private static final int RECENT_MAX_LENGTH = 5; 42 43 47 private static final String PERS_FOLDER = "org.eclipse.ui.ide"; 49 54 private static final String PERS_FILENAME = "recentWorkspaces.xml"; 56 60 private static final int PERS_ENCODING_VERSION = 1; 61 62 67 private static final int PERS_ENCODING_VERSION_CONFIG_PREFS = 2; 68 69 77 private static final int PERS_ENCODING_VERSION_CONFIG_PREFS_NO_COMMAS = 3; 78 79 private boolean showDialog = true; 80 81 private String initialDefault; 82 83 private String selection; 84 85 private String [] recentWorkspaces; 86 87 private static interface XML { 89 public static final String PROTOCOL = "protocol"; 91 public static final String VERSION = "version"; 93 public static final String ALWAYS_ASK = "alwaysAsk"; 95 public static final String SHOW_DIALOG = "showDialog"; 97 public static final String WORKSPACE = "workspace"; 99 public static final String RECENT_WORKSPACES = "recentWorkspaces"; 101 public static final String MAX_LENGTH = "maxLength"; 103 public static final String PATH = "path"; } 105 106 109 public ChooseWorkspaceData(String initialDefault) { 110 readPersistedData(); 111 setInitialDefault(initialDefault); 112 } 113 114 117 public ChooseWorkspaceData(URL instanceUrl) { 118 readPersistedData(); 119 if (instanceUrl != null) { 120 setInitialDefault(new File (instanceUrl.getFile()).toString()); 121 } 122 } 123 124 128 public String getInitialDefault() { 129 if (initialDefault == null) { 130 setInitialDefault(System.getProperty("user.dir") + File.separator + "workspace"); } 133 return initialDefault; 134 } 135 136 142 private void setInitialDefault(String dir) { 143 if (dir == null || dir.length() <= 0) { 144 initialDefault = null; 145 return; 146 } 147 148 dir = new Path(dir).toOSString(); 149 while (dir.charAt(dir.length() - 1) == File.separatorChar) { 150 dir = dir.substring(0, dir.length() - 1); 151 } 152 initialDefault = dir; 153 } 154 155 158 public String getSelection() { 159 return selection; 160 } 161 162 165 public boolean getShowDialog() { 166 return showDialog; 167 } 168 169 173 public String [] getRecentWorkspaces() { 174 return recentWorkspaces; 175 } 176 177 181 public void workspaceSelected(String dir) { 182 selection = dir; 185 } 186 187 190 public void toggleShowDialog() { 191 showDialog = !showDialog; 192 } 193 194 198 public void writePersistedData() { 199 Preferences node = new ConfigurationScope().getNode(IDEWorkbenchPlugin.IDE_WORKBENCH); 201 202 node.putBoolean( 204 IDE.Preferences.SHOW_WORKSPACE_SELECTION_DIALOG, 205 showDialog); 206 207 node.putInt(IDE.Preferences.MAX_RECENT_WORKSPACES, 209 recentWorkspaces.length); 210 211 if (selection != null) { 213 String oldEntry = recentWorkspaces[0]; 214 recentWorkspaces[0] = selection; 215 for (int i = 1; i < recentWorkspaces.length && oldEntry != null; ++i) { 216 if (selection.equals(oldEntry)) { 217 break; 218 } 219 String tmp = recentWorkspaces[i]; 220 recentWorkspaces[i] = oldEntry; 221 oldEntry = tmp; 222 } 223 } 224 225 String encodedRecentWorkspaces = encodeStoredWorkspacePaths(recentWorkspaces); 227 node.put(IDE.Preferences.RECENT_WORKSPACES, 228 encodedRecentWorkspaces); 229 230 node.putInt(IDE.Preferences.RECENT_WORKSPACES_PROTOCOL, 232 PERS_ENCODING_VERSION_CONFIG_PREFS_NO_COMMAS); 233 234 try { 236 node.flush(); 237 } catch (BackingStoreException e) { 238 } 240 } 241 242 249 private boolean readPersistedData_file() { 250 URL persUrl = null; 251 252 Location configLoc = Platform.getConfigurationLocation(); 253 if (configLoc != null) { 254 persUrl = getPersistenceUrl(configLoc.getURL(), false); 255 } 256 257 try { 258 if (persUrl == null) { 261 return false; 262 } 263 264 274 Reader reader = new FileReader (persUrl.getFile()); 275 XMLMemento memento = XMLMemento.createReadRoot(reader); 276 if (memento == null || !compatibleFileProtocol(memento)) { 277 return false; 278 } 279 280 IMemento alwaysAskTag = memento.getChild(XML.ALWAYS_ASK); 281 showDialog = alwaysAskTag == null ? true : alwaysAskTag.getInteger( 282 XML.SHOW_DIALOG).intValue() == 1; 283 284 IMemento recent = memento.getChild(XML.RECENT_WORKSPACES); 285 if (recent == null) { 286 return false; 287 } 288 289 Integer maxLength = recent.getInteger(XML.MAX_LENGTH); 290 int max = RECENT_MAX_LENGTH; 291 if (maxLength != null) { 292 max = maxLength.intValue(); 293 } 294 295 IMemento indices[] = recent.getChildren(XML.WORKSPACE); 296 if (indices == null || indices.length <= 0) { 297 return false; 298 } 299 300 max = Math.max(max, indices.length); 304 305 recentWorkspaces = new String [max]; 306 for (int i = 0; i < indices.length; ++i) { 307 String path = indices[i].getString(XML.PATH); 308 if (path == null) { 309 break; 310 } 311 recentWorkspaces[i] = path; 312 } 313 } catch (IOException e) { 314 return false; 316 } catch (WorkbenchException e) { 317 return false; 319 } finally { 320 if (recentWorkspaces == null) { 322 recentWorkspaces = new String [RECENT_MAX_LENGTH]; 323 } 324 } 325 326 return true; 327 } 328 329 333 public static boolean getShowDialogValue() { 334 338 ChooseWorkspaceData data = new ChooseWorkspaceData(""); 340 return data.readPersistedData() ? data.showDialog : true; 343 } 344 345 349 public static void setShowDialogValue(boolean showDialog) { 350 354 ChooseWorkspaceData data = new ChooseWorkspaceData(""); 356 data.showDialog = showDialog; 358 data.writePersistedData(); 359 } 360 361 371 public boolean readPersistedData() { 372 IPreferenceStore store = new ScopedPreferenceStore( 373 new ConfigurationScope(), IDEWorkbenchPlugin.IDE_WORKBENCH); 374 375 383 387 int protocol = store 388 .getInt(IDE.Preferences.RECENT_WORKSPACES_PROTOCOL); 389 if (protocol == IPreferenceStore.INT_DEFAULT_DEFAULT 390 && readPersistedData_file()) { 391 return true; 392 } 393 394 showDialog = store 396 .getBoolean(IDE.Preferences.SHOW_WORKSPACE_SELECTION_DIALOG); 397 398 int max = store 400 .getInt(IDE.Preferences.MAX_RECENT_WORKSPACES); 401 max = Math.max(max, RECENT_MAX_LENGTH); 402 403 String workspacePathPref = store 405 .getString(IDE.Preferences.RECENT_WORKSPACES); 406 recentWorkspaces = decodeStoredWorkspacePaths(protocol, max, workspacePathPref); 407 408 return true; 409 } 410 411 414 private static String encodeStoredWorkspacePaths(String [] recent) { 415 StringBuffer buff = new StringBuffer (); 416 417 String path = null; 418 for (int i = 0; i < recent.length; ++i) { 419 if (recent[i] == null) { 420 break; 421 } 422 423 if (path != null) { 425 buff.append("\n"); } 427 428 path = recent[i]; 429 buff.append(path); 430 } 431 432 return buff.toString(); 433 } 434 435 439 private static String [] decodeStoredWorkspacePaths(int protocol, int max, 440 String prefValue) { 441 String [] paths = new String [max]; 442 if (prefValue == null || prefValue.length() <= 0) { 443 return paths; 444 } 445 446 String tokens = null; 449 switch (protocol) { 450 case PERS_ENCODING_VERSION_CONFIG_PREFS_NO_COMMAS : 451 tokens = "\n"; break; 453 case PERS_ENCODING_VERSION_CONFIG_PREFS : 454 tokens = ","; break; 456 } 457 if (tokens == null) return new String [0]; 460 461 462 StringTokenizer tokenizer = new StringTokenizer (prefValue, tokens); 463 for (int i = 0; i < paths.length && tokenizer.hasMoreTokens(); ++i) { 464 paths[i] = tokenizer.nextToken(); 465 } 466 467 return paths; 468 } 469 470 474 private static boolean compatibleFileProtocol(IMemento memento) { 475 IMemento protocolMemento = memento.getChild(XML.PROTOCOL); 476 if (protocolMemento == null) { 477 return false; 478 } 479 480 Integer version = protocolMemento.getInteger(XML.VERSION); 481 return version != null && version.intValue() == PERS_ENCODING_VERSION; 482 } 483 484 492 private static URL getPersistenceUrl(URL baseUrl, boolean create) { 493 if (baseUrl == null) { 494 return null; 495 } 496 497 try { 498 URL url = new URL (baseUrl, PERS_FOLDER); 500 File dir = new File (url.getFile()); 501 if (!dir.exists() && (!create || !dir.mkdir())) { 502 return null; 503 } 504 505 url = new URL (dir.toURL(), PERS_FILENAME); 507 File persFile = new File (url.getFile()); 508 if (!persFile.exists() && (!create || !persFile.createNewFile())) { 509 return null; 510 } 511 512 return persFile.toURL(); 513 } catch (IOException e) { 514 return null; 516 } 517 } 518 } 519 | Popular Tags |