1 11 package org.eclipse.core.internal.resources; 12 13 import java.util.*; 14 import org.eclipse.core.resources.ResourcesPlugin; 15 import org.eclipse.core.runtime.Preferences; 16 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent; 17 18 28 29 public class WorkspacePreferences extends WorkspaceDescription { 30 31 public final static String PROJECT_SEPARATOR = "/"; 33 private Preferences preferences; 34 35 40 public static String convertStringArraytoString(String [] array) { 41 if (array == null || array.length == 0) 42 return ""; StringBuffer sb = new StringBuffer (); 44 for (int i = 0; i < array.length; i++) { 45 sb.append(array[i]); 46 sb.append(PROJECT_SEPARATOR); 47 } 48 sb.deleteCharAt(sb.length() - 1); 49 return sb.toString(); 50 } 51 52 56 public static String [] convertStringToStringArray(String string, String separator) { 57 List list = new ArrayList(); 58 for (StringTokenizer tokenizer = new StringTokenizer(string, separator); tokenizer.hasMoreTokens();) 59 list.add(tokenizer.nextToken()); 60 return (String []) list.toArray(new String [list.size()]); 61 } 62 63 67 private static void copyFromTo(WorkspaceDescription source, WorkspaceDescription target) { 68 target.setAutoBuilding(source.isAutoBuilding()); 69 target.setBuildOrder(source.getBuildOrder()); 70 target.setFileStateLongevity(source.getFileStateLongevity()); 71 target.setMaxBuildIterations(source.getMaxBuildIterations()); 72 target.setMaxFileStates(source.getMaxFileStates()); 73 target.setMaxFileStateSize(source.getMaxFileStateSize()); 74 target.setSnapshotInterval(source.getSnapshotInterval()); 75 target.setOperationsPerSnapshot(source.getOperationsPerSnapshot()); 76 target.setDeltaExpiration(source.getDeltaExpiration()); 77 } 78 79 public WorkspacePreferences() { 80 super("Workspace"); this.preferences = ResourcesPlugin.getPlugin().getPluginPreferences(); 82 83 final String version = preferences.getString(ICoreConstants.PREF_VERSION_KEY); 84 if (!ICoreConstants.PREF_VERSION.equals(version)) 85 upgradeVersion(version); 86 87 super.setAutoBuilding(preferences.getBoolean(ResourcesPlugin.PREF_AUTO_BUILDING)); 89 super.setSnapshotInterval(preferences.getInt(ResourcesPlugin.PREF_SNAPSHOT_INTERVAL)); 90 super.setMaxBuildIterations(preferences.getInt(ResourcesPlugin.PREF_MAX_BUILD_ITERATIONS)); 91 super.setMaxFileStates(preferences.getInt(ResourcesPlugin.PREF_MAX_FILE_STATES)); 92 super.setMaxFileStateSize(preferences.getLong(ResourcesPlugin.PREF_MAX_FILE_STATE_SIZE)); 93 super.setFileStateLongevity(preferences.getLong(ResourcesPlugin.PREF_FILE_STATE_LONGEVITY)); 94 super.setOperationsPerSnapshot(preferences.getInt(PreferenceInitializer.PREF_OPERATIONS_PER_SNAPSHOT)); 95 super.setDeltaExpiration(preferences.getLong(PreferenceInitializer.PREF_DELTA_EXPIRATION)); 96 97 preferences.addPropertyChangeListener(new Preferences.IPropertyChangeListener() { 100 public void propertyChange(PropertyChangeEvent event) { 101 synchronizeWithPreferences(event.getProperty()); 102 } 103 }); 104 } 105 106 public Object clone() { 107 throw new UnsupportedOperationException ("clone() is not supported in " + getClass().getName()); } 112 113 public void copyFrom(WorkspaceDescription source) { 114 copyFromTo(source, this); 115 } 116 117 public void copyTo(WorkspaceDescription target) { 118 copyFromTo(this, target); 119 } 120 121 124 public String [] getBuildOrder() { 125 boolean defaultBuildOrder = preferences.getBoolean(ResourcesPlugin.PREF_DEFAULT_BUILD_ORDER); 126 if (defaultBuildOrder) 127 return null; 128 return convertStringToStringArray(preferences.getString(ResourcesPlugin.PREF_BUILD_ORDER), PROJECT_SEPARATOR); 129 } 130 131 135 public String [] getBuildOrder(boolean makeCopy) { 136 return getBuildOrder(); 139 } 140 141 144 public void setAutoBuilding(boolean value) { 145 preferences.setValue(ResourcesPlugin.PREF_AUTO_BUILDING, value); 146 } 147 148 151 public void setBuildOrder(String [] value) { 152 preferences.setValue(ResourcesPlugin.PREF_DEFAULT_BUILD_ORDER, value == null); 153 preferences.setValue(ResourcesPlugin.PREF_BUILD_ORDER, convertStringArraytoString(value)); 154 } 155 156 public void setDeltaExpiration(long value) { 157 preferences.setValue(PreferenceInitializer.PREF_DELTA_EXPIRATION, value); 158 } 159 160 163 public void setFileStateLongevity(long time) { 164 preferences.setValue(ResourcesPlugin.PREF_FILE_STATE_LONGEVITY, time); 165 } 166 167 170 public void setMaxBuildIterations(int number) { 171 preferences.setValue(ResourcesPlugin.PREF_MAX_BUILD_ITERATIONS, number); 172 } 173 174 177 public void setMaxFileStates(int number) { 178 preferences.setValue(ResourcesPlugin.PREF_MAX_FILE_STATES, number); 179 } 180 181 184 public void setMaxFileStateSize(long size) { 185 preferences.setValue(ResourcesPlugin.PREF_MAX_FILE_STATE_SIZE, size); 186 } 187 188 public void setOperationsPerSnapshot(int value) { 189 preferences.setValue(PreferenceInitializer.PREF_OPERATIONS_PER_SNAPSHOT, value); 190 } 191 192 195 public void setSnapshotInterval(long delay) { 196 preferences.setValue(ResourcesPlugin.PREF_SNAPSHOT_INTERVAL, delay); 197 } 198 199 protected void synchronizeWithPreferences(String property) { 200 if (property.equals(ResourcesPlugin.PREF_AUTO_BUILDING)) 204 super.setAutoBuilding(preferences.getBoolean(ResourcesPlugin.PREF_AUTO_BUILDING)); 205 else if (property.equals(ResourcesPlugin.PREF_SNAPSHOT_INTERVAL)) 206 super.setSnapshotInterval(preferences.getLong(ResourcesPlugin.PREF_SNAPSHOT_INTERVAL)); 207 else if (property.equals(ResourcesPlugin.PREF_MAX_BUILD_ITERATIONS)) 208 super.setMaxBuildIterations(preferences.getInt(ResourcesPlugin.PREF_MAX_BUILD_ITERATIONS)); 209 else if (property.equals(ResourcesPlugin.PREF_MAX_FILE_STATES)) 210 super.setMaxFileStates(preferences.getInt(ResourcesPlugin.PREF_MAX_FILE_STATES)); 211 else if (property.equals(ResourcesPlugin.PREF_MAX_FILE_STATE_SIZE)) 212 super.setMaxFileStateSize(preferences.getLong(ResourcesPlugin.PREF_MAX_FILE_STATE_SIZE)); 213 else if (property.equals(ResourcesPlugin.PREF_FILE_STATE_LONGEVITY)) 214 super.setFileStateLongevity(preferences.getLong(ResourcesPlugin.PREF_FILE_STATE_LONGEVITY)); 215 else if (property.equals(PreferenceInitializer.PREF_OPERATIONS_PER_SNAPSHOT)) 216 super.setOperationsPerSnapshot(preferences.getInt(PreferenceInitializer.PREF_OPERATIONS_PER_SNAPSHOT)); 217 else if (property.equals(PreferenceInitializer.PREF_DELTA_EXPIRATION)) 218 super.setDeltaExpiration(preferences.getLong(PreferenceInitializer.PREF_DELTA_EXPIRATION)); 219 } 220 221 private void upgradeVersion(String oldVersion) { 222 if (oldVersion.length() == 0) { 223 if (!preferences.getBoolean(ResourcesPlugin.PREF_DEFAULT_BUILD_ORDER)) { 225 String oldOrder = preferences.getString(ResourcesPlugin.PREF_BUILD_ORDER); 226 setBuildOrder(convertStringToStringArray(oldOrder, ":")); } 228 } 229 preferences.setValue(ICoreConstants.PREF_VERSION_KEY, ICoreConstants.PREF_VERSION); 230 } 231 } | Popular Tags |