KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > WorkspacePreferences


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
19  * This class provides the same interface as <code>WorkspaceDescription</code>
20  * but instead of changing/obtaining values from its internal state, it
21  * changes/obtains properties in/from the workspace plug-in's preferences.
22  *
23  * Obs.: for performance reasons, some frequently called acessor methods are
24  * reading a cached value from the super class instead of reading the
25  * corresponding property preference store. To keep the cache synchronized with
26  * the preference store, a property change listener is used.
27  */

28
29 public class WorkspacePreferences extends WorkspaceDescription {
30
31     public final static String JavaDoc PROJECT_SEPARATOR = "/"; //$NON-NLS-1$
32

33     private Preferences preferences;
34
35     /**
36      * Helper method that converts a string string array {"string1","
37      * string2",..."stringN"} to a string in the form "string1,string2,...
38      * stringN".
39      */

40     public static String JavaDoc convertStringArraytoString(String JavaDoc[] array) {
41         if (array == null || array.length == 0)
42             return ""; //$NON-NLS-1$
43
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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     /**
53      * Helper method that converts a string in the form "string1,string2,...
54      * stringN" to a string array {"string1","string2",..."stringN"}.
55      */

56     public static String JavaDoc[] convertStringToStringArray(String JavaDoc string, String JavaDoc separator) {
57         List list = new ArrayList();
58         for (StringTokenizer tokenizer = new StringTokenizer(string, separator); tokenizer.hasMoreTokens();)
59             list.add(tokenizer.nextToken());
60         return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
61     }
62
63     /**
64      * Helper method that copies all attributes from a workspace description
65      * object to another.
66      */

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"); //$NON-NLS-1$
81
this.preferences = ResourcesPlugin.getPlugin().getPluginPreferences();
82
83         final String JavaDoc version = preferences.getString(ICoreConstants.PREF_VERSION_KEY);
84         if (!ICoreConstants.PREF_VERSION.equals(version))
85             upgradeVersion(version);
86
87         // initialize cached preferences (for better performance)
88
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         // This property listener ensures we are being updated properly when changes
98
// are done directly to the preference store.
99
preferences.addPropertyChangeListener(new Preferences.IPropertyChangeListener() {
100             public void propertyChange(PropertyChangeEvent event) {
101                 synchronizeWithPreferences(event.getProperty());
102             }
103         });
104     }
105
106     public Object JavaDoc clone() {
107         // should never be called - throws an exception to avoid using a
108
// WorkspacePreferences when using WorkspaceDescription was the real
109
// intention (this class offers a different protocol for copying state).
110
throw new UnsupportedOperationException JavaDoc("clone() is not supported in " + getClass().getName()); //$NON-NLS-1$
111
}
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     /**
122      * @see org.eclipse.core.resources.IWorkspaceDescription#getBuildOrder()
123      */

124     public String JavaDoc[] 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     /**
132      * @see org.eclipse.core.internal.resources.
133      * WorkspaceDescription#getBuildOrder(boolean)
134      */

135     public String JavaDoc[] getBuildOrder(boolean makeCopy) {
136         //note that since this is stored in the preference store, we are creating
137
//a new copy of the string array on every access anyway
138
return getBuildOrder();
139     }
140
141     /**
142      * @see org.eclipse.core.resources.IWorkspaceDescription#setAutoBuilding(boolean)
143      */

144     public void setAutoBuilding(boolean value) {
145         preferences.setValue(ResourcesPlugin.PREF_AUTO_BUILDING, value);
146     }
147
148     /**
149      * @see org.eclipse.core.resources.IWorkspaceDescription#setBuildOrder(String[])
150      */

151     public void setBuildOrder(String JavaDoc[] 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     /**
161      * @see org.eclipse.core.resources.IWorkspaceDescription#setFileStateLongevity(long)
162      */

163     public void setFileStateLongevity(long time) {
164         preferences.setValue(ResourcesPlugin.PREF_FILE_STATE_LONGEVITY, time);
165     }
166
167     /**
168      * @see org.eclipse.core.resources.IWorkspaceDescription#setMaxBuildIterations(int)
169      */

170     public void setMaxBuildIterations(int number) {
171         preferences.setValue(ResourcesPlugin.PREF_MAX_BUILD_ITERATIONS, number);
172     }
173
174     /**
175      * @see org.eclipse.core.resources.IWorkspaceDescription#setMaxFileStates(int)
176      */

177     public void setMaxFileStates(int number) {
178         preferences.setValue(ResourcesPlugin.PREF_MAX_FILE_STATES, number);
179     }
180
181     /**
182      * @see org.eclipse.core.resources.IWorkspaceDescription#setMaxFileStateSize(long)
183      */

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     /**
193      * @see org.eclipse.core.resources.IWorkspaceDescription#setSnapshotInterval(long)
194      */

195     public void setSnapshotInterval(long delay) {
196         preferences.setValue(ResourcesPlugin.PREF_SNAPSHOT_INTERVAL, delay);
197     }
198
199     protected void synchronizeWithPreferences(String JavaDoc property) {
200         // do not use the value in the event - may be a string instead
201
// of the expected type. Retrieve it from the preferences store
202
// using the type-specific method
203
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 JavaDoc oldVersion) {
222         if (oldVersion.length() == 0) {
223             //only need to convert the build order if we are not using the default order
224
if (!preferences.getBoolean(ResourcesPlugin.PREF_DEFAULT_BUILD_ORDER)) {
225                 String JavaDoc oldOrder = preferences.getString(ResourcesPlugin.PREF_BUILD_ORDER);
226                 setBuildOrder(convertStringToStringArray(oldOrder, ":")); //$NON-NLS-1$
227
}
228         }
229         preferences.setValue(ICoreConstants.PREF_VERSION_KEY, ICoreConstants.PREF_VERSION);
230     }
231 }
Popular Tags