KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.io.File JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.OutputStreamWriter JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.SortedSet JavaDoc;
23 import java.util.TreeSet JavaDoc;
24
25 import org.eclipse.core.commands.common.EventManager;
26 import org.eclipse.core.runtime.Assert;
27 import org.eclipse.core.runtime.IAdaptable;
28 import org.eclipse.core.runtime.IExtension;
29 import org.eclipse.core.runtime.IExtensionPoint;
30 import org.eclipse.core.runtime.ISafeRunnable;
31 import org.eclipse.core.runtime.Platform;
32 import org.eclipse.core.runtime.SafeRunner;
33 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
34 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
35 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
36 import org.eclipse.jface.util.IPropertyChangeListener;
37 import org.eclipse.jface.util.PropertyChangeEvent;
38 import org.eclipse.swt.widgets.Display;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.ui.IElementFactory;
41 import org.eclipse.ui.IMemento;
42 import org.eclipse.ui.IPersistableElement;
43 import org.eclipse.ui.IWorkingSet;
44 import org.eclipse.ui.IWorkingSetElementAdapter;
45 import org.eclipse.ui.IWorkingSetManager;
46 import org.eclipse.ui.IWorkingSetUpdater;
47 import org.eclipse.ui.PlatformUI;
48 import org.eclipse.ui.XMLMemento;
49 import org.eclipse.ui.dialogs.IWorkingSetEditWizard;
50 import org.eclipse.ui.dialogs.IWorkingSetNewWizard;
51 import org.eclipse.ui.dialogs.IWorkingSetPage;
52 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
53 import org.eclipse.ui.internal.dialogs.WorkingSetEditWizard;
54 import org.eclipse.ui.internal.dialogs.WorkingSetNewWizard;
55 import org.eclipse.ui.internal.dialogs.WorkingSetSelectionDialog;
56 import org.eclipse.ui.internal.misc.StatusUtil;
57 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
58 import org.eclipse.ui.internal.registry.WorkingSetDescriptor;
59 import org.eclipse.ui.internal.registry.WorkingSetRegistry;
60 import org.eclipse.ui.statushandlers.StatusManager;
61 import org.osgi.framework.Bundle;
62 import org.osgi.framework.BundleContext;
63 import org.osgi.framework.BundleEvent;
64 import org.osgi.framework.BundleListener;
65
66
67 /**
68  * Abstract implementation of <code>IWorkingSetManager</code>.
69  */

70 public abstract class AbstractWorkingSetManager extends EventManager implements
71         IWorkingSetManager, BundleListener, IExtensionChangeHandler {
72     
73     private SortedSet JavaDoc workingSets = new TreeSet JavaDoc(WorkingSetComparator.INSTANCE);
74     
75     /**
76      * Size of the list of most recently used working sets.
77      */

78     private static final int MRU_SIZE = 5;
79     private List JavaDoc recentWorkingSets = new ArrayList JavaDoc();
80
81     private BundleContext bundleContext;
82     private Map JavaDoc/*<String, IWorkingSetUpdater>*/ updaters= new HashMap JavaDoc();
83
84     private Map JavaDoc/*<String, IWorkingSetElementAdapter>*/ elementAdapters = new HashMap JavaDoc();
85     
86     private static final IWorkingSetUpdater NULL_UPDATER= new IWorkingSetUpdater() {
87         public void add(IWorkingSet workingSet) {
88         }
89         public boolean remove(IWorkingSet workingSet) {
90             return true;
91         }
92         public boolean contains(IWorkingSet workingSet) {
93             return true;
94         }
95         public void dispose() {
96         }
97     };
98     
99     private static final IWorkingSetElementAdapter IDENTITY_ADAPTER = new IWorkingSetElementAdapter() {
100
101         public IAdaptable[] adaptElements(IWorkingSet ws, IAdaptable[] elements) {
102             return elements;
103         }
104
105         public void dispose() {
106         }
107     };
108         
109     /**
110      * Returns the descriptors for the given editable working set ids. If an id
111      * refers to a missing descriptor, or one that is non-editable, it is
112      * skipped. If <code>null</code> is passed, all editable descriptors are
113      * returned.
114      *
115      * @param supportedWorkingSetIds
116      * the ids for the working set descriptors, or <code>null</code>
117      * for all editable descriptors
118      * @return the descriptors corresponding to the given editable working set
119      * ids
120      */

121     private static WorkingSetDescriptor[] getSupportedEditableDescriptors(
122             String JavaDoc[] supportedWorkingSetIds) {
123         WorkingSetRegistry registry = WorkbenchPlugin.getDefault()
124                 .getWorkingSetRegistry();
125         if (supportedWorkingSetIds == null) {
126             return registry.getNewPageWorkingSetDescriptors();
127         }
128         List JavaDoc result = new ArrayList JavaDoc(supportedWorkingSetIds.length);
129         for (int i = 0; i < supportedWorkingSetIds.length; i++) {
130             WorkingSetDescriptor desc = registry
131                     .getWorkingSetDescriptor(supportedWorkingSetIds[i]);
132             if (desc != null && desc.isEditable()) {
133                 result.add(desc);
134             }
135         }
136         return (WorkingSetDescriptor[]) result
137                 .toArray(new WorkingSetDescriptor[result.size()]);
138     }
139     
140     protected AbstractWorkingSetManager(BundleContext context) {
141         bundleContext= context;
142         bundleContext.addBundleListener(this);
143         PlatformUI.getWorkbench().getExtensionTracker().registerHandler(this, ExtensionTracker
144                 .createExtensionPointFilter(getExtensionPointFilter()));
145     }
146     
147     /**
148      *
149      * @return
150      * @since 3.3
151      */

152     private IExtensionPoint getExtensionPointFilter() {
153         return Platform.getExtensionRegistry().getExtensionPoint(
154                 PlatformUI.PLUGIN_ID,
155                 IWorkbenchRegistryConstants.PL_WORKINGSETS);
156     }
157     
158     public void dispose() {
159         bundleContext.removeBundleListener(this);
160         for (Iterator JavaDoc iter= updaters.values().iterator(); iter.hasNext();) {
161             ((IWorkingSetUpdater)iter.next()).dispose();
162         }
163         
164         for (Iterator JavaDoc iter= elementAdapters.values().iterator(); iter.hasNext();) {
165             ((IWorkingSetElementAdapter)iter.next()).dispose();
166         }
167     }
168     
169     //---- working set creation -----------------------------------------------------
170

171     /* (non-Javadoc)
172      * @see org.eclipse.ui.IWorkingSetManager
173      */

174     public IWorkingSet createWorkingSet(String JavaDoc name, IAdaptable[] elements) {
175         return new WorkingSet(name, name, elements);
176     }
177     
178     public IWorkingSet createAggregateWorkingSet(String JavaDoc name, String JavaDoc label,
179             IWorkingSet[] components) {
180         return new AggregateWorkingSet(name, label, components);
181     }
182
183     /*
184      * (non-Javadoc)
185      *
186      * @see org.eclipse.ui.IWorkingSetManager
187      */

188     public IWorkingSet createWorkingSet(IMemento memento) {
189         return restoreWorkingSet(memento);
190     }
191
192     //---- working set management ---------------------------------------------------
193

194     /* (non-Javadoc)
195      * @see org.eclipse.ui.IWorkingSetManager
196      */

197     public void addWorkingSet(IWorkingSet workingSet) {
198         Assert.isTrue(!workingSets.contains(workingSet),
199                 "working set already registered"); //$NON-NLS-1$
200
internalAddWorkingSet(workingSet);
201     }
202
203     private void internalAddWorkingSet(IWorkingSet workingSet) {
204         workingSets.add(workingSet);
205         ((AbstractWorkingSet)workingSet).connect(this);
206         addToUpdater(workingSet);
207         firePropertyChange(CHANGE_WORKING_SET_ADD, null, workingSet);
208     }
209
210     /* (non-Javadoc)
211      * @see org.eclipse.ui.IWorkingSetManager
212      */

213     protected boolean internalRemoveWorkingSet(IWorkingSet workingSet) {
214         boolean workingSetRemoved = workingSets.remove(workingSet);
215         boolean recentWorkingSetRemoved = recentWorkingSets.remove(workingSet);
216         
217         if (workingSetRemoved) {
218             ((AbstractWorkingSet)workingSet).disconnect();
219             removeFromUpdater(workingSet);
220             firePropertyChange(CHANGE_WORKING_SET_REMOVE, workingSet, null);
221         }
222         return workingSetRemoved || recentWorkingSetRemoved;
223     }
224
225     /* (non-Javadoc)
226      * @see org.eclipse.ui.IWorkingSetManager
227      */

228     public IWorkingSet[] getWorkingSets() {
229             SortedSet JavaDoc visibleSubset = new TreeSet JavaDoc(WorkingSetComparator.INSTANCE);
230             for (Iterator JavaDoc i = workingSets.iterator(); i.hasNext();) {
231                 IWorkingSet workingSet = (IWorkingSet) i.next();
232                 if (workingSet.isVisible()) {
233                     visibleSubset.add(workingSet);
234                 }
235             }
236         return (IWorkingSet[]) visibleSubset.toArray(new IWorkingSet[visibleSubset.size()]);
237     }
238     
239     public IWorkingSet[] getAllWorkingSets() {
240             return (IWorkingSet[]) workingSets.toArray(new IWorkingSet[workingSets.size()]);
241     }
242
243     /* (non-Javadoc)
244      * @see org.eclipse.ui.IWorkingSetManager
245      */

246     public IWorkingSet getWorkingSet(String JavaDoc name) {
247         if (name == null || workingSets == null) {
248             return null;
249         }
250
251         Iterator JavaDoc iter = workingSets.iterator();
252         while (iter.hasNext()) {
253             IWorkingSet workingSet = (IWorkingSet) iter.next();
254             if (name.equals(workingSet.getName())) {
255                 return workingSet;
256             }
257         }
258         return null;
259     }
260     
261     // ---- recent working set management --------------------------------------
262

263     /* (non-Javadoc)
264      * @see org.eclipse.ui.IWorkingSetManager
265      */

266     public IWorkingSet[] getRecentWorkingSets() {
267         return (IWorkingSet[]) recentWorkingSets.toArray(new IWorkingSet[recentWorkingSets.size()]);
268     }
269
270     /**
271      * Adds the specified working set to the list of recently used
272      * working sets.
273      *
274      * @param workingSet working set to added to the list of recently
275      * used working sets.
276      */

277     protected void internalAddRecentWorkingSet(IWorkingSet workingSet) {
278             if (!workingSet.isVisible()) {
279                 return;
280             }
281         recentWorkingSets.remove(workingSet);
282         recentWorkingSets.add(0, workingSet);
283         if (recentWorkingSets.size() > MRU_SIZE) {
284             recentWorkingSets.remove(MRU_SIZE);
285         }
286     }
287
288     //---- equals and hash code -----------------------------------------------
289

290     /**
291      * Tests the receiver and the object for equality
292      *
293      * @param object object to compare the receiver to
294      * @return true=the object equals the receiver, it has the same
295      * working sets. false otherwise
296      */

297     public boolean equals(Object JavaDoc object) {
298         if (this == object) {
299             return true;
300         }
301         if (!getClass().getName().equals(object.getClass().getName())) {
302             return false;
303         }
304         AbstractWorkingSetManager other= (AbstractWorkingSetManager)object;
305         return other.workingSets.equals(workingSets);
306     }
307
308     /**
309      * Returns the hash code.
310      *
311      * @return the hash code.
312      */

313     public int hashCode() {
314         return workingSets.hashCode();
315     }
316
317     //---- property listeners -------------------------------------------------
318

319     /* (non-Javadoc)
320      * @see org.eclipse.ui.IWorkingSetManager
321      */

322     public void addPropertyChangeListener(IPropertyChangeListener listener) {
323         addListenerObject(listener);
324     }
325
326     /* (non-Javadoc)
327      * @see org.eclipse.ui.IWorkingSetManager
328      */

329     public void removePropertyChangeListener(IPropertyChangeListener listener) {
330         removeListenerObject(listener);
331     }
332
333     /**
334      * Notify property change listeners about a change to the list of
335      * working sets.
336      *
337      * @param changeId one of
338      * IWorkingSetManager#CHANGE_WORKING_SET_ADD
339      * IWorkingSetManager#CHANGE_WORKING_SET_REMOVE
340      * IWorkingSetManager#CHANGE_WORKING_SET_CONTENT_CHANGE
341      * IWorkingSetManager#CHANGE_WORKING_SET_NAME_CHANGE
342      * @param oldValue the removed working set or null if a working set
343      * was added or changed.
344      * @param newValue the new or changed working set or null if a working
345      * set was removed.
346      */

347     protected void firePropertyChange(String JavaDoc changeId, Object JavaDoc oldValue,
348             Object JavaDoc newValue) {
349         final Object JavaDoc[] listeners = getListeners();
350         
351         if (listeners.length == 0) {
352             return;
353         }
354         
355         final PropertyChangeEvent event = new PropertyChangeEvent(this,
356                 changeId, oldValue, newValue);
357         Runnable JavaDoc notifier = new Runnable JavaDoc() {
358             public void run() {
359                 for (int i = 0; i < listeners.length; i++) {
360                     final IPropertyChangeListener listener = (IPropertyChangeListener) listeners[i];
361                     ISafeRunnable safetyWrapper = new ISafeRunnable() {
362
363                         public void run() throws Exception JavaDoc {
364                             listener.propertyChange(event);
365                         }
366
367                         public void handleException(Throwable JavaDoc exception) {
368                             // logged by the runner
369
}
370                     };
371                     SafeRunner.run(safetyWrapper);
372                 }
373             }
374         };
375         // Notifications are sent on the UI thread.
376
if (Display.getCurrent() != null) {
377             notifier.run();
378         } else {
379             // Use an asyncExec to avoid deadlocks.
380
Display.getDefault().asyncExec(notifier);
381         }
382     }
383     
384     /**
385      * Fires a property change event for the changed working set. Should only be
386      * called by org.eclipse.ui.internal.WorkingSet.
387      *
388      * @param changedWorkingSet
389      * the working set that has changed
390      * @param propertyChangeId
391      * the changed property. one of
392      * CHANGE_WORKING_SET_CONTENT_CHANGE,
393      * CHANGE_WORKING_SET_LABEL_CHANGE, and
394      * CHANGE_WORKING_SET_NAME_CHANGE
395      * @param oldValue
396      * the old value
397      */

398     public void workingSetChanged(IWorkingSet changedWorkingSet,
399             String JavaDoc propertyChangeId, Object JavaDoc oldValue) {
400         firePropertyChange(propertyChangeId, oldValue, changedWorkingSet);
401     }
402     
403     // ---- Persistence
404
// ----------------------------------------------------------------
405

406     /**
407      * Saves all persistable working sets in the persistence store.
408      *
409      * @param memento the persistence store
410      * @see IPersistableElement
411      */

412     public void saveWorkingSetState(IMemento memento) {
413         Iterator JavaDoc iterator = workingSets.iterator();
414         
415         // break the sets into aggregates and non aggregates. The aggregates should be saved after the non-aggregates
416
// so that on restoration all necessary aggregate components can be found.
417

418         ArrayList JavaDoc standardSets = new ArrayList JavaDoc();
419         ArrayList JavaDoc aggregateSets = new ArrayList JavaDoc();
420         while (iterator.hasNext()) {
421                 IWorkingSet set = (IWorkingSet) iterator.next();
422                 if (set instanceof AggregateWorkingSet) {
423                     aggregateSets.add(set);
424                 } else {
425                     standardSets.add(set);
426                 }
427         }
428
429         saveWorkingSetState(memento, standardSets);
430         saveWorkingSetState(memento, aggregateSets);
431     }
432
433     /**
434      * @param memento the memento to save to
435      * @param list the working sets to save
436      * @since 3.2
437      */

438     private void saveWorkingSetState(IMemento memento, List JavaDoc list) {
439         for (Iterator JavaDoc i = list.iterator(); i.hasNext();) {
440             IPersistableElement persistable = (IWorkingSet) i.next();
441             IMemento workingSetMemento = memento
442                     .createChild(IWorkbenchConstants.TAG_WORKING_SET);
443             workingSetMemento.putString(IWorkbenchConstants.TAG_FACTORY_ID,
444                     persistable.getFactoryId());
445             persistable.saveState(workingSetMemento);
446         }
447     }
448     
449     /**
450      * Recreates all working sets from the persistence store
451      * and adds them to the receiver.
452      *
453      * @param memento the persistence store
454      */

455     protected void restoreWorkingSetState(IMemento memento) {
456         IMemento[] children = memento
457                 .getChildren(IWorkbenchConstants.TAG_WORKING_SET);
458         for (int i = 0; i < children.length; i++) {
459             IWorkingSet workingSet = restoreWorkingSet(children[i]);
460             if (workingSet != null) {
461                 internalAddWorkingSet(workingSet);
462             }
463         }
464     }
465     
466     /**
467      * Recreates a working set from the persistence store.
468      *
469      * @param memento the persistence store
470      * @return the working set created from the memento or null if
471      * creation failed.
472      */

473     protected IWorkingSet restoreWorkingSet(IMemento memento) {
474         String JavaDoc factoryID = memento
475                 .getString(IWorkbenchConstants.TAG_FACTORY_ID);
476
477         if (factoryID == null) {
478             // if the factory id was not set in the memento
479
// then assume that the memento was created using
480
// IMemento.saveState, and should be restored using WorkingSetFactory
481
factoryID = AbstractWorkingSet.FACTORY_ID;
482         }
483         IElementFactory factory = PlatformUI.getWorkbench().getElementFactory(
484                 factoryID);
485         if (factory == null) {
486             WorkbenchPlugin
487                     .log("Unable to restore working set - cannot instantiate factory: " + factoryID); //$NON-NLS-1$
488
return null;
489         }
490         IAdaptable adaptable = factory.createElement(memento);
491         if (adaptable == null) {
492             WorkbenchPlugin
493                     .log("Unable to restore working set - cannot instantiate working set: " + factoryID); //$NON-NLS-1$
494
return null;
495         }
496         if ((adaptable instanceof IWorkingSet) == false) {
497             WorkbenchPlugin
498                     .log("Unable to restore working set - element is not an IWorkingSet: " + factoryID); //$NON-NLS-1$
499
return null;
500         }
501         return (IWorkingSet) adaptable;
502     }
503
504     /**
505      * Saves the list of most recently used working sets in the persistence
506      * store.
507      *
508      * @param memento the persistence store
509      */

510     protected void saveMruList(IMemento memento) {
511         Iterator JavaDoc iterator = recentWorkingSets.iterator();
512
513         while (iterator.hasNext()) {
514             IWorkingSet workingSet = (IWorkingSet) iterator.next();
515             IMemento mruMemento = memento
516                     .createChild(IWorkbenchConstants.TAG_MRU_LIST);
517
518             mruMemento.putString(IWorkbenchConstants.TAG_NAME, workingSet
519                     .getName());
520         }
521     }
522
523    /**
524      * Restores the list of most recently used working sets from the
525      * persistence store.
526      *
527      * @param memento the persistence store
528      */

529     protected void restoreMruList(IMemento memento) {
530         IMemento[] mruWorkingSets = memento
531                 .getChildren(IWorkbenchConstants.TAG_MRU_LIST);
532
533         for (int i = mruWorkingSets.length - 1; i >= 0; i--) {
534             String JavaDoc workingSetName = mruWorkingSets[i]
535                     .getString(IWorkbenchConstants.TAG_NAME);
536             if (workingSetName != null) {
537                 IWorkingSet workingSet = getWorkingSet(workingSetName);
538                 if (workingSet != null) {
539                     internalAddRecentWorkingSet(workingSet);
540                 }
541             }
542         }
543     }
544
545     //---- user interface support -----------------------------------------------------
546

547     /**
548      * @see org.eclipse.ui.IWorkingSetManager#createWorkingSetEditWizard(org.eclipse.ui.IWorkingSet)
549      * @since 2.1
550      */

551     public IWorkingSetEditWizard createWorkingSetEditWizard(
552             IWorkingSet workingSet) {
553         String JavaDoc editPageId = workingSet.getId();
554         WorkingSetRegistry registry = WorkbenchPlugin.getDefault()
555                 .getWorkingSetRegistry();
556         IWorkingSetPage editPage = null;
557
558         if (editPageId != null) {
559             editPage = registry.getWorkingSetPage(editPageId);
560         }
561  
562         // the following block kind of defeats IWorkingSet.isEditable() and it
563
// doesn't make sense for there to be a default page in such a case.
564

565          if (editPage == null) {
566             editPage = registry.getDefaultWorkingSetPage();
567             if (editPage == null) {
568                 return null;
569             }
570         }
571          
572         WorkingSetEditWizard editWizard = new WorkingSetEditWizard(editPage);
573         editWizard.setSelection(workingSet);
574         return editWizard;
575     }
576
577     /**
578      * @deprecated use createWorkingSetSelectionDialog(parent, true) instead
579      */

580     public IWorkingSetSelectionDialog createWorkingSetSelectionDialog(
581             Shell parent) {
582         return createWorkingSetSelectionDialog(parent, true);
583     }
584
585     /* (non-Javadoc)
586      * @see org.eclipse.ui.IWorkingSetManager
587      */

588     public IWorkingSetSelectionDialog createWorkingSetSelectionDialog(
589             Shell parent, boolean multi) {
590         return createWorkingSetSelectionDialog(parent, multi, null);
591     }
592     
593     /**
594      * {@inheritDoc}
595      */

596     public IWorkingSetNewWizard createWorkingSetNewWizard(String JavaDoc[] workingSetIds) {
597          WorkingSetDescriptor[] descriptors= getSupportedEditableDescriptors(workingSetIds);
598          if (descriptors.length == 0) {
599             return null;
600         }
601          return new WorkingSetNewWizard(descriptors);
602 }
603
604     //---- working set delta handling -------------------------------------------------
605

606     public void bundleChanged(BundleEvent event) {
607         if (event.getBundle().getSymbolicName() == null)
608             return;
609         // If the workbench isn't running anymore simply return.
610
if (!Workbench.getInstance().isRunning()) {
611             return;
612         }
613         
614         if (event.getBundle().getState() == Bundle.ACTIVE) {
615             WorkingSetDescriptor[] descriptors = WorkbenchPlugin.getDefault()
616                     .getWorkingSetRegistry().getUpdaterDescriptorsForNamespace(
617                             event.getBundle().getSymbolicName());
618             synchronized (updaters) {
619                 for (int i = 0; i < descriptors.length; i++) {
620                     WorkingSetDescriptor descriptor = descriptors[i];
621                     List JavaDoc workingSets = getWorkingSetsForId(descriptor.getId());
622                     if (workingSets.size() == 0) {
623                         continue;
624                     }
625                     IWorkingSetUpdater updater = getUpdater(descriptor);
626                     for (Iterator JavaDoc iter = workingSets.iterator(); iter.hasNext();) {
627                         IWorkingSet workingSet = (IWorkingSet) iter.next();
628                         if (!updater.contains(workingSet)) {
629                             updater.add(workingSet);
630                         }
631                     }
632                 }
633             }
634         }
635     }
636
637     private List JavaDoc getWorkingSetsForId(String JavaDoc id) {
638         List JavaDoc result= new ArrayList JavaDoc();
639         for (Iterator JavaDoc iter= workingSets.iterator(); iter.hasNext();) {
640             IWorkingSet ws= (IWorkingSet)iter.next();
641             if (id.equals(ws.getId())) {
642                 result.add(ws);
643             }
644         }
645         return result;
646     }
647     
648     private void addToUpdater(IWorkingSet workingSet) {
649         WorkingSetDescriptor descriptor= WorkbenchPlugin.getDefault()
650             .getWorkingSetRegistry().getWorkingSetDescriptor(workingSet.getId());
651         if (descriptor == null || !descriptor.isUpdaterClassLoaded()) {
652             return;
653         }
654         synchronized(updaters) {
655             IWorkingSetUpdater updater= getUpdater(descriptor);
656             if (!updater.contains(workingSet)) {
657                 updater.add(workingSet);
658             }
659         }
660     }
661     
662     private IWorkingSetUpdater getUpdater(WorkingSetDescriptor descriptor) {
663         IWorkingSetUpdater updater= (IWorkingSetUpdater)updaters.get(descriptor.getId());
664         if (updater == null) {
665             updater= descriptor.createWorkingSetUpdater();
666             if (updater == null) {
667                 updater= NULL_UPDATER;
668             } else {
669                 firePropertyChange(CHANGE_WORKING_SET_UPDATER_INSTALLED, null, updater);
670                 PlatformUI.getWorkbench().getExtensionTracker().registerObject(
671                         descriptor.getConfigurationElement()
672                                 .getDeclaringExtension(), updater,
673                         IExtensionTracker.REF_WEAK);
674                 
675             }
676             updaters.put(descriptor.getId(), updater);
677         }
678         return updater;
679     }
680     
681     IWorkingSetElementAdapter getElementAdapter(WorkingSetDescriptor descriptor) {
682         IWorkingSetElementAdapter elementAdapter = (IWorkingSetElementAdapter) elementAdapters
683                 .get(descriptor.getId());
684         if (elementAdapter == null) {
685             elementAdapter = descriptor.createWorkingSetElementAdapter();
686             if (elementAdapter == null) {
687                 elementAdapter = IDENTITY_ADAPTER;
688             } else {
689                 elementAdapters.put(descriptor.getId(), elementAdapter);
690             }
691         }
692         return elementAdapter;
693     }
694
695     private void removeFromUpdater(IWorkingSet workingSet) {
696         synchronized (updaters) {
697             IWorkingSetUpdater updater = (IWorkingSetUpdater) updaters
698                     .get(workingSet.getId());
699             if (updater != null) {
700                 updater.remove(workingSet);
701             }
702         }
703     }
704     
705     
706     /* (non-Javadoc)
707      * @see org.eclipse.ui.IWorkingSetManager#createWorkingSetSelectionDialog(org.eclipse.swt.widgets.Shell, boolean, java.lang.String[])
708      */

709     public IWorkingSetSelectionDialog createWorkingSetSelectionDialog(Shell parent, boolean multi, String JavaDoc[] workingsSetIds) {
710         return new WorkingSetSelectionDialog(parent, multi, workingsSetIds);
711     }
712
713     /**
714      * Save the state to the state file.
715      *
716      * @param stateFile
717      * @throws IOException
718      */

719     public void saveState(File JavaDoc stateFile) throws IOException JavaDoc {
720         XMLMemento memento = XMLMemento
721                 .createWriteRoot(IWorkbenchConstants.TAG_WORKING_SET_MANAGER);
722         saveWorkingSetState(memento);
723         saveMruList(memento);
724     
725         FileOutputStream JavaDoc stream = new FileOutputStream JavaDoc(stateFile);
726         OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(stream, "utf-8"); //$NON-NLS-1$
727
memento.save(writer);
728         writer.close();
729     
730     }
731     
732     /* (non-Javadoc)
733      * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamichelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
734      */

735     public void addExtension(IExtensionTracker tracker, IExtension extension) {
736         // nothing - this is handled lazily. These items are only created as needed by the getUpdater() and getElementAdapter() methods
737

738     }
739     
740     /* (non-Javadoc)
741      * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
742      */

743     public void removeExtension(IExtension extension, Object JavaDoc[] objects) {
744         for (int i = 0; i < objects.length; i++) {
745             Object JavaDoc object = objects[i];
746             if (object instanceof IWorkingSetUpdater) {
747                 removeUpdater((IWorkingSetUpdater)object);
748                 
749             }
750             if (object instanceof IWorkingSetElementAdapter) {
751                 removeElementAdapter((IWorkingSetElementAdapter) object);
752             }
753         }
754     }
755
756     /**
757      * Remove the element adapter from the manager and dispose of it.
758      *
759      * @param elementAdapter
760      * @since 3.3
761      */

762     private void removeElementAdapter(
763             final IWorkingSetElementAdapter elementAdapter) {
764         SafeRunner.run(new ISafeRunnable() {
765
766             public void handleException(Throwable JavaDoc exception) {
767                 StatusManager.getManager().handle(
768                         StatusUtil.newStatus(PlatformUI.PLUGIN_ID, exception));
769             }
770
771             public void run() throws Exception JavaDoc {
772                 elementAdapter.dispose();
773
774             }
775         });
776         synchronized (elementAdapters) {
777             elementAdapters.values().remove(elementAdapter);
778         }
779     }
780
781     /**
782      * Remove the updater from the manager and dispose of it.
783      *
784      * @param updater
785      * @since 3.3
786      */

787     private void removeUpdater(final IWorkingSetUpdater updater) {
788         SafeRunner.run(new ISafeRunnable() {
789
790             public void handleException(Throwable JavaDoc exception) {
791                 StatusManager.getManager().handle(
792                         StatusUtil.newStatus(PlatformUI.PLUGIN_ID, exception));
793             }
794
795             public void run() throws Exception JavaDoc {
796                 updater.dispose();
797
798             }
799         });
800         synchronized (updaters) {
801             updaters.values().remove(updater);
802         }
803         firePropertyChange(IWorkingSetManager.CHANGE_WORKING_SET_UPDATER_UNINSTALLED, updater, null);
804     }
805 }
806
Popular Tags