1 11 package org.eclipse.ui.internal; 12 13 import java.util.Collections ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Set ; 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 35 final class ActivityPersistanceHelper { 36 37 40 protected final static String PREFIX = "UIActivities."; 42 45 private static ActivityPersistanceHelper singleton; 46 47 50 private final IActivityManagerListener activityManagerListener = new IActivityManagerListener() { 51 52 57 public void activityManagerChanged( 58 ActivityManagerEvent activityManagerEvent) { 59 if (activityManagerEvent.haveDefinedActivityIdsChanged()) { 61 Set delta = new HashSet (activityManagerEvent 62 .getActivityManager().getDefinedActivityIds()); 63 delta.removeAll(activityManagerEvent 64 .getPreviouslyDefinedActivityIds()); 65 loadEnabledStates(activityManagerEvent 68 .getActivityManager().getEnabledActivityIds(), delta); 69 } 70 if (activityManagerEvent.haveEnabledActivityIdsChanged()) { 71 saveEnabledStates(); 72 } 73 } 74 }; 75 76 79 private final IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() { 80 81 86 public void propertyChange(PropertyChangeEvent event) { 87 if (!saving && event.getProperty().startsWith(PREFIX)) { 90 String 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 Set set = new HashSet (activityManager.getEnabledActivityIds()); 97 if (enabled == false) { 98 Set 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 113 protected boolean saving = false; 114 115 120 public static ActivityPersistanceHelper getInstance() { 121 if (singleton == null) { 122 singleton = new ActivityPersistanceHelper(); 123 } 124 return singleton; 125 } 126 127 134 protected Set buildDependencies(IActivityManager activityManager, String activityId) { 135 Set set = new HashSet (); 136 for (Iterator i = activityManager.getDefinedActivityIds().iterator(); i.hasNext(); ) { 137 IActivity activity = activityManager.getActivity((String ) i.next()); 138 for (Iterator 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 153 private ActivityPersistanceHelper() { 154 loadEnabledStates(); 155 hookListeners(); 156 } 157 158 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 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 198 private String createPreferenceKey(String activityId) { 199 return PREFIX + activityId; 200 } 201 202 205 void loadEnabledStates() { 206 loadEnabledStates(Collections.EMPTY_SET, PlatformUI.getWorkbench() 207 .getActivitySupport().getActivityManager() 208 .getDefinedActivityIds()); 209 } 210 211 217 protected void loadEnabledStates(Set previouslyEnabledActivities, Set activityIdsToProcess) { 218 if (activityIdsToProcess.isEmpty()) { 219 return; 220 } 221 222 Set enabledActivities = new HashSet (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 i = activityIdsToProcess.iterator(); i 232 .hasNext();) { 233 String activityId = (String ) i.next(); 234 String preferenceKey = createPreferenceKey(activityId); 235 try { 236 IActivity activity = activityManager.getActivity(activityId); 237 if ("".equals(store.getDefaultString(preferenceKey))) { store .setDefault(preferenceKey, activity 240 .isDefaultEnabled()); 241 242 } 243 244 } catch (NotDefinedException e) { 245 } 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 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 values = activityManager.getDefinedActivityIds().iterator(); 272 while (values.hasNext()) { 273 IActivity activity = activityManager.getActivity((String ) 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 289 public void shutdown() { 290 unhookListeners(); 291 saveEnabledStates(); 292 } 293 } 294 | Popular Tags |