KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ActivityPersistanceHelper


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.ui.internal;
12
13 import java.util.Collections JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.jface.preference.IPreferenceStore;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.activities.ActivityManagerEvent;
23 import org.eclipse.ui.activities.IActivity;
24 import org.eclipse.ui.activities.IActivityManager;
25 import org.eclipse.ui.activities.IActivityManagerListener;
26 import org.eclipse.ui.activities.IActivityRequirementBinding;
27 import org.eclipse.ui.activities.IWorkbenchActivitySupport;
28 import org.eclipse.ui.activities.NotDefinedException;
29
30 /**
31  * Utility class that manages the persistance of enabled activities.
32  *
33  * @since 3.0
34  */

35 final class ActivityPersistanceHelper {
36
37     /**
38      * Prefix for all activity preferences
39      */

40     protected final static String JavaDoc PREFIX = "UIActivities."; //$NON-NLS-1$
41

42     /**
43      * Singleton instance.
44      */

45     private static ActivityPersistanceHelper singleton;
46
47     /**
48      * The listener that responds to changes in the <code>IActivityManager</code>
49      */

50     private final IActivityManagerListener activityManagerListener = new IActivityManagerListener() {
51
52         /*
53          * (non-Javadoc)
54          *
55          * @see org.eclipse.ui.activities.IActivityManagerListener#activityManagerChanged(org.eclipse.ui.activities.ActivityManagerEvent)
56          */

57         public void activityManagerChanged(
58                 ActivityManagerEvent activityManagerEvent) {
59             //process newly defined activities.
60
if (activityManagerEvent.haveDefinedActivityIdsChanged()) {
61                 Set JavaDoc delta = new HashSet JavaDoc(activityManagerEvent
62                         .getActivityManager().getDefinedActivityIds());
63                 delta.removeAll(activityManagerEvent
64                         .getPreviouslyDefinedActivityIds());
65                 // whatever is still in delta are new activities - restore their
66
// state
67
loadEnabledStates(activityManagerEvent
68                         .getActivityManager().getEnabledActivityIds(), delta);
69             }
70             if (activityManagerEvent.haveEnabledActivityIdsChanged()) {
71                 saveEnabledStates();
72             }
73         }
74     };
75     
76     /**
77      * The listener that responds to preference changes
78      */

79     private final IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() {
80
81         /*
82          * (non-Javadoc)
83          *
84          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
85          */

86         public void propertyChange(PropertyChangeEvent event) {
87             // dont process property events if we're in the process of
88
// serializing state.
89
if (!saving && event.getProperty().startsWith(PREFIX)) {
90                 String JavaDoc activityId = event.getProperty().substring(PREFIX.length());
91                 IWorkbenchActivitySupport support = PlatformUI.getWorkbench().getActivitySupport();
92                 IActivityManager activityManager = support.getActivityManager();
93                 
94                 boolean enabled = Boolean.valueOf(event.getNewValue().toString()).booleanValue();
95                 // if we're turning an activity off we'll need to create its dependency tree to ensuure that all dependencies are also disabled.
96
Set JavaDoc set = new HashSet JavaDoc(activityManager.getEnabledActivityIds());
97                 if (enabled == false) {
98                     Set JavaDoc dependencies = buildDependencies(activityManager, activityId);
99                     set.removeAll(dependencies);
100                 }
101                 else {
102                     set.add(activityId);
103                 }
104                 support.setEnabledActivityIds(set);
105             }
106         }
107     };
108
109     /**
110      * Whether we are currently saving the state of activities to the preference
111      * store.
112      */

113     protected boolean saving = false;
114
115     /**
116      * Get the singleton instance of this class.
117      *
118      * @return the singleton instance of this class.
119      */

120     public static ActivityPersistanceHelper getInstance() {
121         if (singleton == null) {
122             singleton = new ActivityPersistanceHelper();
123         }
124         return singleton;
125     }
126
127     /**
128      * Returns a set of activity IDs that depend on the provided ID in order to be enabled.
129      *
130      * @param activityManager the activity manager to query
131      * @param activityId the activity whos dependencies should be added
132      * @return a set of activity IDs
133      */

134     protected Set JavaDoc buildDependencies(IActivityManager activityManager, String JavaDoc activityId) {
135         Set JavaDoc set = new HashSet JavaDoc();
136         for (Iterator JavaDoc i = activityManager.getDefinedActivityIds().iterator(); i.hasNext(); ) {
137             IActivity activity = activityManager.getActivity((String JavaDoc) i.next());
138             for (Iterator JavaDoc j = activity.getActivityRequirementBindings().iterator(); j.hasNext(); ) {
139                 IActivityRequirementBinding binding = (IActivityRequirementBinding) j.next();
140                 if (activityId.equals(binding.getRequiredActivityId())) {
141                     set.addAll(buildDependencies(activityManager, activity.getId()));
142                 }
143             }
144         }
145         set.add(activityId);
146         return set;
147     }
148
149     /**
150      * Create a new <code>ActivityPersistanceHelper</code> which will restore
151      * previously enabled activity states.
152      */

153     private ActivityPersistanceHelper() {
154         loadEnabledStates();
155         hookListeners();
156     }
157
158     /**
159      * Hook the listener that will respond to any activity state changes.
160      */

161     private void hookListeners() {
162         IWorkbenchActivitySupport support = PlatformUI.getWorkbench()
163                 .getActivitySupport();
164
165         IActivityManager activityManager = support.getActivityManager();
166
167         activityManager.addActivityManagerListener(activityManagerListener);
168
169         IPreferenceStore store = WorkbenchPlugin.getDefault()
170                 .getPreferenceStore();
171         
172         store.addPropertyChangeListener(propertyChangeListener);
173     }
174
175     /**
176      * Hook the listener that will respond to any activity state changes.
177      */

178     private void unhookListeners() {
179         IWorkbenchActivitySupport support = PlatformUI.getWorkbench()
180                 .getActivitySupport();
181
182         IActivityManager activityManager = support.getActivityManager();
183
184         activityManager.removeActivityManagerListener(activityManagerListener);
185         
186         IPreferenceStore store = WorkbenchPlugin.getDefault()
187                 .getPreferenceStore();
188         
189         store.removePropertyChangeListener(propertyChangeListener);
190     }
191     
192     /**
193      * Create the preference key for the activity.
194      *
195      * @param activityId the activity id.
196      * @return String a preference key representing the activity.
197      */

198     private String JavaDoc createPreferenceKey(String JavaDoc activityId) {
199         return PREFIX + activityId;
200     }
201
202     /**
203      * Loads the enabled states from the preference store.
204      */

205     void loadEnabledStates() {
206         loadEnabledStates(Collections.EMPTY_SET, PlatformUI.getWorkbench()
207                 .getActivitySupport().getActivityManager()
208                 .getDefinedActivityIds());
209     }
210
211     /**
212      * Load the enabled states for the given activity IDs.
213      *
214      * @param previouslyEnabledActivities the activity states to maintain. This set must be writabe.
215      * @param activityIdsToProcess the activity ids to process
216      */

217     protected void loadEnabledStates(Set JavaDoc previouslyEnabledActivities, Set JavaDoc activityIdsToProcess) {
218         if (activityIdsToProcess.isEmpty()) {
219             return;
220         }
221         
222         Set JavaDoc enabledActivities = new HashSet JavaDoc(previouslyEnabledActivities);
223         IPreferenceStore store = WorkbenchPlugin.getDefault()
224                 .getPreferenceStore();
225
226         IWorkbenchActivitySupport support = PlatformUI.getWorkbench()
227                 .getActivitySupport();
228
229         IActivityManager activityManager = support.getActivityManager();
230
231         for (Iterator JavaDoc i = activityIdsToProcess.iterator(); i
232                 .hasNext();) {
233             String JavaDoc activityId = (String JavaDoc) i.next();
234             String JavaDoc preferenceKey = createPreferenceKey(activityId);
235             try {
236                 IActivity activity = activityManager.getActivity(activityId);
237                 if ("".equals(store.getDefaultString(preferenceKey))) { //$NON-NLS-1$ // no override has been provided in the customization file
238
store // the default should be whatever the XML specifies
239
.setDefault(preferenceKey, activity
240                             .isDefaultEnabled());
241                     
242                 }
243
244             } catch (NotDefinedException e) {
245                 // can't happen - we're iterating over defined activities
246
}
247
248             if (store.getBoolean(preferenceKey)) {
249                 enabledActivities.add(activityId);
250             } else {
251                 enabledActivities.remove(activityId);
252             }
253         }
254
255         support.setEnabledActivityIds(enabledActivities);
256     }
257
258     /**
259      * Save the enabled states in the preference store.
260      */

261     protected void saveEnabledStates() {
262         try {
263             saving = true;
264             
265             IPreferenceStore store = WorkbenchPlugin.getDefault()
266                     .getPreferenceStore();
267     
268             IWorkbenchActivitySupport support = PlatformUI.getWorkbench()
269                     .getActivitySupport();
270             IActivityManager activityManager = support.getActivityManager();
271             Iterator JavaDoc values = activityManager.getDefinedActivityIds().iterator();
272             while (values.hasNext()) {
273                 IActivity activity = activityManager.getActivity((String JavaDoc) values
274                         .next());
275     
276                 store.setValue(createPreferenceKey(activity.getId()), activity
277                         .isEnabled());
278             }
279             WorkbenchPlugin.getDefault().savePluginPreferences();
280         }
281         finally {
282             saving = false;
283         }
284     }
285
286     /**
287      * Save the enabled state of all activities.
288      */

289     public void shutdown() {
290         unhookListeners();
291         saveEnabledStates();
292     }
293 }
294
Popular Tags