| 12 | = | 12 | ||
| 13 | import java.util.Collections; | 13 | import java.util.Collections; | |
| 14 | import java.util.HashSet; | 14 | import java.util.HashSet; | |
| 15 | import java.util.Iterator; | 15 | import java.util.Iterator; | |
| 16 | import java.util.Set; | 16 | import java.util.Set; | |
| 17 | 17 | |||
| -+ | 18 | import org.eclipse.core.runtime.IProgressMonitor; | ||
| 19 | import org.eclipse.core.runtime.IStatus; | |||
| 20 | import org.eclipse.core.runtime.Status; | |||
| 21 | import org.eclipse.core.runtime.jobs.Job; | |||
| 18 | import org.eclipse.jface.preference.IPreferenceStore; | = | 22 | import org.eclipse.jface.preference.IPreferenceStore; |
| 19 | import org.eclipse.jface.util.IPropertyChangeListener; | 23 | import org.eclipse.jface.util.IPropertyChangeListener; | |
| 20 | import org.eclipse.jface.util.PropertyChangeEvent; | 24 | import org.eclipse.jface.util.PropertyChangeEvent; | |
| 21 | import org.eclipse.ui.PlatformUI; | 25 | import org.eclipse.ui.PlatformUI; | |
| 22 | import org.eclipse.ui.activities.ActivityManagerEvent; | 26 | import org.eclipse.ui.activities.ActivityManagerEvent; | |
| 23 | import org.eclipse.ui.activities.IActivity; | 27 | import org.eclipse.ui.activities.IActivity; | |
| 65 | // whatever is still in delta are new activities - restore their | = | 69 | // whatever is still in delta are new activities - restore their |
| 66 | // state | 70 | // state | |
| 67 | loadEnabledStates(activityManagerEvent | 71 | loadEnabledStates(activityManagerEvent | |
| 68 | .getActivityManager().getEnabledActivityIds(), delta); | 72 | .getActivityManager().getEnabledActivityIds(), delta); | |
| 69 | } | 73 | } | |
| 70 | if (activityManagerEvent.haveEnabledActivityIdsChanged()) { | 74 | if (activityManagerEvent.haveEnabledActivityIdsChanged()) { | |
| 71 | saveEnabledStates(); | <> | 75 | saveEnabledStates(true); |
| 72 | } | = | 76 | } |
| 73 | } | 77 | } | |
| 74 | }; | 78 | }; | |
| 75 | 79 | |||
| 76 | /** | 80 | /** | |
| 77 | * The listener that responds to preference changes | 81 | * The listener that responds to preference changes | |
| 83 | * | = | 87 | * |
| 84 | * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent) | 88 | * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent) | |
| 85 | */ | 89 | */ | |
| 86 | public void propertyChange(PropertyChangeEvent event) { | 90 | public void propertyChange(PropertyChangeEvent event) { | |
| 87 | // dont process property events if we're in the process of | 91 | // dont process property events if we're in the process of | |
| 88 | // serializing state. | 92 | // serializing state. | |
| <> | 93 | synchronized(savingLock){ | ||
| 89 | if (!saving && event.getProperty().startsWith(PREFIX)) { | 94 | if (event.getProperty().startsWith(PREFIX)) { | |
| 90 | String activityId = event.getProperty().substring(PREFIX.length()); | 95 | String activityId = event.getProperty().substring(PREFIX.length()); | |
| 91 | IWorkbenchActivitySupport support = PlatformUI.getWorkbench().getActivitySupport(); | 96 | IWorkbenchActivitySupport support = PlatformUI.getWorkbench().getActivitySupport(); | |
| 92 | IActivityManager activityManager = support.getActivityManager(); | 97 | IActivityManager activityManager = support.getActivityManager(); | |
| 93 | 98 | |||
| 94 | boolean enabled = Boolean.valueOf(event.getNewValue().toString()).booleanValue(); | 99 | 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. | 100 | // 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 set = new HashSet(activityManager.getEnabledActivityIds()); | 101 | Set set = new HashSet(activityManager.getEnabledActivityIds()); | |
| 97 | if (enabled == false) { | 102 | if (enabled == false) { | |
| 98 | Set dependencies = buildDependencies(activityManager, activityId); | 103 | Set dependencies = buildDependencies(activityManager, activityId); | |
| 99 | set.removeAll(dependencies); | 104 | set.removeAll(dependencies); | |
| 100 | } | 105 | } | |
| 101 | else { | 106 | else { | |
| 102 | set.add(activityId); | 107 | set.add(activityId); | |
| 103 | } | 108 | } | |
| 104 | support.setEnabledActivityIds(set); | 109 | support.setEnabledActivityIds(set); | |
| 105 | } | 110 | } | |
| 106 | } | 111 | } | |
| 112 | } | |||
| 107 | }; | = | 113 | }; |
| 108 | 114 | |||
| 109 | /** | 115 | /** | |
| 110 | * Whether we are currently saving the state of activities to the preference | 116 | * Whether we are currently saving the state of activities to the preference | |
| 111 | * store. | 117 | * store. | |
| 112 | */ | 118 | */ | |
| 113 | protected boolean saving = false; | <> | 119 | protected Object savingLock = new Object(); |
| 114 | = | 120 | ||
| 115 | /** | 121 | /** | |
| 116 | * Get the singleton instance of this class. | 122 | * Get the singleton instance of this class. | |
| 117 | * | 123 | * | |
| 118 | * @return the singleton instance of this class. | 124 | * @return the singleton instance of this class. | |
| 119 | */ | 125 | */ | |
| 259 | } | = | 265 | } |
| 260 | 266 | |||
| 261 | /** | 267 | /** | |
| 262 | * Save the enabled states in the preference store. | 268 | * Save the enabled states in the preference store. | |
| 263 | */ | 269 | */ | |
| 264 | protected void saveEnabledStates() { | 270 | protected void saveEnabledStates() { | |
| 265 | try { | <> | 271 | saveEnabledStates(false); |
| 266 | saving = true; | 272 | } | |
| 267 | 273 | |||
| 274 | private void saveEnabledStates(boolean saveInBackground) { | |||
| 275 | synchronized(savingLock){ | |||
| 268 | IPreferenceStore store = WorkbenchPlugin.getDefault() | = | 276 | IPreferenceStore store = WorkbenchPlugin.getDefault() |
| 269 | .getPreferenceStore(); | 277 | .getPreferenceStore(); | |
| 270 | <> | 278 | ||
| 271 | IWorkbenchActivitySupport support = PlatformUI.getWorkbench() | 279 | IWorkbenchActivitySupport support = PlatformUI.getWorkbench() | |
| 272 | .getActivitySupport(); | 280 | .getActivitySupport(); | |
| 273 | IActivityManager activityManager = support.getActivityManager(); | 281 | IActivityManager activityManager = support.getActivityManager(); | |
| 274 | Iterator values = activityManager.getDefinedActivityIds().iterator(); | 282 | Iterator values = activityManager.getDefinedActivityIds().iterator(); | |
| 275 | while (values.hasNext()) { | 283 | while (values.hasNext()) { | |
| 276 | IActivity activity = activityManager.getActivity((String) values | 284 | IActivity activity = activityManager.getActivity((String) values | |
| 277 | .next()); | 285 | .next()); | |
| 278 | if (activity.getExpression() != null) { | 286 | if (activity.getExpression() != null) { | |
| 279 | continue; | 287 | continue; | |
| 280 | } | 288 | } | |
| 281 | 289 | |||
| 282 | store.setValue(createPreferenceKey(activity.getId()), activity | 290 | store.setValue(createPreferenceKey(activity.getId()), activity | |
| 283 | .isEnabled()); | 291 | .isEnabled()); | |
| 284 | } | 292 | } | |
| 293 | ||||
| 294 | if (saveInBackground) { | |||
| 295 | Job job = new Job("Save Activity to Plugin Preference Job") { //$NON-NLS-1$ | |||
| 296 | protected IStatus run(IProgressMonitor arg0) { | |||
| 297 | synchronized(savingLock){ | |||
| 285 | WorkbenchPlugin.getDefault().savePluginPreferences(); | 298 | WorkbenchPlugin.getDefault().savePluginPreferences(); | |
| 286 | } | 299 | } | |
| 300 | return Status.OK_STATUS; | |||
| 287 | finally { | 301 | } | |
| 288 | saving = false; | 302 | }; | |
| 303 | job.schedule(); | |||
| 304 | } else { | |||
| 305 | WorkbenchPlugin.getDefault().savePluginPreferences(); | |||
| 289 | } | 306 | } | |
| 307 | } | |||
| 290 | } | = | 308 | } |
| 291 | 309 | |||
| 292 | /** | 310 | /** | |
| 293 | * Save the enabled state of all activities. | 311 | * Save the enabled state of all activities. | |
| 294 | */ | 312 | */ | |
| 295 | public void shutdown() { | 313 | public void shutdown() { | |
| 296 | unhookListeners(); | 314 | unhookListeners(); | |
| 297 | saveEnabledStates(); | <> | 315 | saveEnabledStates(false); |
| 298 | } | = | 316 | } |
| 299 | } | 317 | } | |