KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > resources > actions > WorkingSetActionProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 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
12 package org.eclipse.ui.internal.navigator.resources.actions;
13
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.jface.util.IPropertyChangeListener;
16 import org.eclipse.jface.util.PropertyChangeEvent;
17 import org.eclipse.jface.viewers.StructuredViewer;
18 import org.eclipse.ui.IActionBars;
19 import org.eclipse.ui.IMemento;
20 import org.eclipse.ui.IWorkingSet;
21 import org.eclipse.ui.IWorkingSetManager;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.actions.WorkingSetFilterActionGroup;
24 import org.eclipse.ui.internal.navigator.workingsets.WorkingSetsContentProvider;
25 import org.eclipse.ui.navigator.CommonActionProvider;
26 import org.eclipse.ui.navigator.ICommonActionExtensionSite;
27 import org.eclipse.ui.navigator.IExtensionActivationListener;
28 import org.eclipse.ui.navigator.IExtensionStateModel;
29 import org.eclipse.ui.navigator.INavigatorContentService;
30
31 /**
32  * @since 3.2
33  *
34  */

35 public class WorkingSetActionProvider extends CommonActionProvider {
36
37     private static final String JavaDoc TAG_CURRENT_WORKING_SET_NAME = "currentWorkingSetName"; //$NON-NLS-1$
38

39     private boolean contributedToViewMenu = false;
40
41     private StructuredViewer viewer;
42
43     private INavigatorContentService contentService;
44
45     private WorkingSetFilterActionGroup workingSetActionGroup;
46     private WorkingSetRootModeActionGroup workingSetRootModeActionGroup;
47
48     private Object JavaDoc originalViewerInput = ResourcesPlugin.getWorkspace().getRoot();
49
50     private IExtensionStateModel extensionStateModel;
51
52     /**
53      * Provides a smart listener to monitor changes to the Working Set Manager.
54      *
55      */

56     public class WorkingSetManagerListener implements IPropertyChangeListener {
57
58         private boolean listening = false;
59
60         /*
61          * (non-Javadoc)
62          *
63          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
64          */

65         public void propertyChange(PropertyChangeEvent event) {
66
67             Object JavaDoc input = viewer.getInput();
68             if (input instanceof IWorkingSet) {
69                 IWorkingSet workingSet = (IWorkingSet) input;
70
71                 String JavaDoc property = event.getProperty();
72                 Object JavaDoc newValue = event.getNewValue();
73                 Object JavaDoc oldValue = event.getOldValue();
74
75                 if (IWorkingSetManager.CHANGE_WORKING_SET_REMOVE.equals(property) && oldValue == workingSet) {
76                     // setWorkingSet(null);
77
if (viewer != null) {
78                         viewer.setInput(originalViewerInput);
79                     }
80                 } else if (IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE.equals(property) && newValue == workingSet) {
81                 } else if (IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE.equals(property) && newValue == workingSet) {
82                     // if (workingSet.isAggregateWorkingSet() && workingSet.isEmpty()) {
83
// // act as if the working set has been made null
84
// if (!emptyWorkingSet) {
85
// emptyWorkingSet = true;
86
// workingSetFilter.setWorkingSet(null);
87
// }
88
// } else {
89
// // we've gone from empty to non-empty on our set.
90
// // Restore it.
91
// if (emptyWorkingSet) {
92
// emptyWorkingSet = false;
93
// workingSetFilter.setWorkingSet(workingSet);
94
// }
95
// }
96
if (viewer != null) {
97                         viewer.refresh();
98                     }
99                 }
100             }
101
102         }
103
104         /**
105          * Begin listening to the correct source if not already listening.
106          */

107         public synchronized void listen() {
108             if (!listening) {
109                 PlatformUI.getWorkbench().getWorkingSetManager().addPropertyChangeListener(managerChangeListener);
110                 listening = true;
111             }
112         }
113
114         /**
115          * Begin listening to the correct source if not already listening.
116          */

117         public synchronized void ignore() {
118             if (listening) {
119                 PlatformUI.getWorkbench().getWorkingSetManager().removePropertyChangeListener(managerChangeListener);
120                 listening = false;
121             }
122         }
123     }
124
125     private IPropertyChangeListener filterChangeListener = new IPropertyChangeListener() {
126         /*
127          * (non-Javadoc)
128          *
129          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
130          */

131         public void propertyChange(PropertyChangeEvent event) {
132             IWorkingSet oldWorkingSet = (IWorkingSet) event.getOldValue();
133             IWorkingSet newWorkingSet = (IWorkingSet) event.getNewValue();
134
135
136             if (newWorkingSet != null && !contentService.isActive(WorkingSetsContentProvider.EXTENSION_ID)) {
137                 contentService.getActivationService().activateExtensions(new String JavaDoc[]{WorkingSetsContentProvider.EXTENSION_ID}, false);
138                 contentService.getActivationService().persistExtensionActivations();
139             }
140
141             if (viewer != null) {
142                 if (newWorkingSet == null) {
143                     viewer.setInput(ResourcesPlugin.getWorkspace().getRoot());
144                 } else if(oldWorkingSet != newWorkingSet) {
145                     viewer.setInput(newWorkingSet);
146                 }
147             }
148
149         }
150     };
151
152     private WorkingSetManagerListener managerChangeListener = new WorkingSetManagerListener();
153
154     private IExtensionActivationListener activationListener = new IExtensionActivationListener() {
155
156         private IWorkingSet workingSet;
157
158         public void onExtensionActivation(String JavaDoc aViewerId, String JavaDoc[] theNavigatorExtensionIds, boolean isActive) {
159
160             for (int i = 0; i < theNavigatorExtensionIds.length; i++) {
161                 if (WorkingSetsContentProvider.EXTENSION_ID.equals(theNavigatorExtensionIds[i])) {
162                     if (isActive) {
163                         extensionStateModel = contentService.findStateModel(WorkingSetsContentProvider.EXTENSION_ID);
164                         workingSetRootModeActionGroup.setStateModel(extensionStateModel);
165
166                         if (workingSet != null) {
167                             viewer.setInput(workingSet);
168                             workingSetActionGroup.setWorkingSet(workingSet);
169                             workingSetRootModeActionGroup.setShowTopLevelWorkingSets(true);
170                         }
171                         managerChangeListener.listen();
172
173
174                     } else {
175                         Object JavaDoc input = viewer.getInput();
176                         if (input instanceof IWorkingSet) {
177                             workingSet = (IWorkingSet) input;
178                             if (viewer != null && input != originalViewerInput) {
179                                 viewer.setInput(originalViewerInput);
180                             }
181                         } else {
182                             workingSet = null;
183                         }
184                         managerChangeListener.ignore();
185                         workingSetActionGroup.setWorkingSet(null);
186                         workingSetRootModeActionGroup.setShowTopLevelWorkingSets(false);
187
188                     }
189                 }
190             }
191         }
192
193     };
194
195      
196
197     /*
198      * (non-Javadoc)
199      *
200      * @see org.eclipse.ui.navigator.CommonActionProvider#init(org.eclipse.ui.navigator.ICommonActionExtensionSite)
201      */

202     public void init(ICommonActionExtensionSite aSite) {
203         viewer = aSite.getStructuredViewer();
204         contentService = aSite.getContentService();
205
206         extensionStateModel = contentService.findStateModel(WorkingSetsContentProvider.EXTENSION_ID);
207
208         workingSetActionGroup = new WorkingSetFilterActionGroup(aSite.getViewSite().getShell(), filterChangeListener);
209
210         if (extensionStateModel != null) {
211             workingSetRootModeActionGroup = new WorkingSetRootModeActionGroup(viewer, extensionStateModel);
212         }
213
214
215         if (contentService.isActive(WorkingSetsContentProvider.EXTENSION_ID)) {
216             managerChangeListener.listen();
217         }
218
219         contentService.getActivationService().addExtensionActivationListener(activationListener);
220     }
221     
222     /* (non-Javadoc)
223      * @see org.eclipse.ui.navigator.CommonActionProvider#restoreState(org.eclipse.ui.IMemento)
224      */

225     public void restoreState(IMemento aMemento) {
226         super.restoreState(aMemento);
227         
228         boolean showWorkingSets = true;
229         if(aMemento != null) {
230             Integer JavaDoc showWorkingSetsInt = aMemento.getInteger(WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS);
231             showWorkingSets = showWorkingSetsInt == null || showWorkingSetsInt.intValue() == 1;
232             extensionStateModel.setBooleanProperty(WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS, showWorkingSets);
233             workingSetRootModeActionGroup.setShowTopLevelWorkingSets(showWorkingSets);
234
235             if(viewer != null) {
236                 String JavaDoc lastWorkingSetName = aMemento.getString(TAG_CURRENT_WORKING_SET_NAME);
237                 IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
238                 IWorkingSet lastWorkingSet = workingSetManager.getWorkingSet(lastWorkingSetName);
239                 viewer.setInput(lastWorkingSet);
240                 workingSetActionGroup.setWorkingSet(lastWorkingSet);
241             }
242         } else {
243             showWorkingSets = false;
244             
245             extensionStateModel.setBooleanProperty(WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS, showWorkingSets);
246             workingSetRootModeActionGroup.setShowTopLevelWorkingSets(showWorkingSets);
247         }
248     }
249     
250     /* (non-Javadoc)
251      * @see org.eclipse.ui.navigator.CommonActionProvider#saveState(org.eclipse.ui.IMemento)
252      */

253     public void saveState(IMemento aMemento) {
254         super.saveState(aMemento);
255             
256         if(aMemento != null) {
257             int showWorkingSets = extensionStateModel.getBooleanProperty(WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS) ? 1 : 0;
258             aMemento.putInteger(WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS, showWorkingSets);
259             
260             if(viewer != null) {
261                 Object JavaDoc input = viewer.getInput();
262                 if(input instanceof IWorkingSet) {
263                     IWorkingSet workingSet = (IWorkingSet) input;
264                     aMemento.putString(TAG_CURRENT_WORKING_SET_NAME, workingSet.getName());
265                 }
266             }
267         }
268         
269     }
270
271     /*
272      * (non-Javadoc)
273      *
274      * @see org.eclipse.ui.actions.ActionGroup#fillActionBars(org.eclipse.ui.IActionBars)
275      */

276     public void fillActionBars(IActionBars actionBars) {
277         if (!contributedToViewMenu) {
278             try {
279                 super.fillActionBars(actionBars);
280                 workingSetActionGroup.fillActionBars(actionBars);
281                 if (workingSetRootModeActionGroup != null) {
282                     workingSetRootModeActionGroup.fillActionBars(actionBars);
283                 }
284             } finally {
285                 contributedToViewMenu = true;
286             }
287         }
288     }
289
290     /*
291      * (non-Javadoc)
292      *
293      * @see org.eclipse.ui.actions.ActionGroup#dispose()
294      */

295     public void dispose() {
296         super.dispose();
297         workingSetActionGroup.dispose();
298         if (workingSetRootModeActionGroup != null) {
299             workingSetRootModeActionGroup.dispose();
300         }
301
302         managerChangeListener.ignore();
303
304         contentService.getActivationService().removeExtensionActivationListener(activationListener);
305     }
306 }
307
Popular Tags