KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > actions > ChangeSetActionGroup


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.team.internal.ui.synchronize.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.compare.structuremergeviewer.IDiffElement;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.jface.action.*;
21 import org.eclipse.jface.dialogs.IDialogSettings;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.viewers.*;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.team.core.diff.IDiff;
27 import org.eclipse.team.core.subscribers.Subscriber;
28 import org.eclipse.team.core.synchronize.FastSyncInfoFilter;
29 import org.eclipse.team.core.synchronize.SyncInfo;
30 import org.eclipse.team.core.synchronize.FastSyncInfoFilter.SyncInfoDirectionFilter;
31 import org.eclipse.team.internal.core.subscribers.*;
32 import org.eclipse.team.internal.ui.TeamUIMessages;
33 import org.eclipse.team.internal.ui.TeamUIPlugin;
34 import org.eclipse.team.internal.ui.synchronize.*;
35 import org.eclipse.team.ui.synchronize.*;
36 import org.eclipse.ui.actions.BaseSelectionListenerAction;
37
38 /**
39  * This action group contributes actions that support the management
40  * of Change sets to a synchronize page.
41  *
42  * @since 3.1
43  */

44 public class ChangeSetActionGroup extends SynchronizePageActionGroup {
45
46     /**
47      * Menu group that can be added to the context menu
48      */

49     public final static String JavaDoc CHANGE_SET_GROUP = "change_set_group"; //$NON-NLS-1$
50

51     // Constants for persisting sorting options
52
private static final String JavaDoc P_LAST_COMMENTSORT = TeamUIPlugin.ID + ".P_LAST_COMMENT_SORT"; //$NON-NLS-1$
53

54     public static final FastSyncInfoFilter OUTGOING_RESOURCE_FILTER = new SyncInfoDirectionFilter(
55             new int[] { SyncInfo.OUTGOING, SyncInfo.CONFLICTING });
56     
57     private class CreateChangeSetAction extends SynchronizeModelAction {
58         
59         public CreateChangeSetAction(ISynchronizePageConfiguration configuration) {
60             super(TeamUIMessages.ChangeLogModelProvider_0, configuration);
61         }
62         
63         /* (non-Javadoc)
64          * @see org.eclipse.team.ui.synchronize.SynchronizeModelAction#needsToSaveDirtyEditors()
65          */

66         protected boolean needsToSaveDirtyEditors() {
67             return false;
68         }
69         
70         /* (non-Javadoc)
71          * @see org.eclipse.team.ui.synchronize.SynchronizeModelAction#getSyncInfoFilter()
72          */

73         protected FastSyncInfoFilter getSyncInfoFilter() {
74             return OUTGOING_RESOURCE_FILTER;
75         }
76         
77         /* (non-Javadoc)
78          * @see org.eclipse.team.ui.synchronize.SynchronizeModelAction#getSubscriberOperation(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration, org.eclipse.compare.structuremergeviewer.IDiffElement[])
79          */

80         protected SynchronizeModelOperation getSubscriberOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements) {
81             return new SynchronizeModelOperation(configuration, elements) {
82                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
83                     syncExec(new Runnable JavaDoc() {
84                         public void run() {
85                             ActiveChangeSet set = createChangeSet(getDiffs(getSyncInfoSet().getResources()));
86                             if (set != null) {
87                                 getActiveChangeSetManager().add(set);
88                             }
89                         }
90                     });
91                 }
92             };
93         }
94     }
95
96     private abstract class ChangeSetAction extends BaseSelectionListenerAction {
97
98         public ChangeSetAction(String JavaDoc title, ISynchronizePageConfiguration configuration) {
99             super(title);
100         }
101         
102         /* (non-Javadoc)
103          * @see org.eclipse.ui.actions.BaseSelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
104          */

105         protected boolean updateSelection(IStructuredSelection selection) {
106             return getSelectedSet() != null;
107         }
108
109         protected ActiveChangeSet getSelectedSet() {
110             IStructuredSelection selection = getStructuredSelection();
111             if (selection.size() == 1) {
112                 Object JavaDoc first = selection.getFirstElement();
113                 if (first instanceof IAdaptable) {
114                     Object JavaDoc adapter = ((IAdaptable)first).getAdapter(ChangeSet.class);
115                     if (adapter instanceof ActiveChangeSet) {
116                         return (ActiveChangeSet)adapter;
117                     }
118                 }
119             }
120             return null;
121         }
122     }
123     
124     private class EditChangeSetAction extends ChangeSetAction {
125
126         public EditChangeSetAction(ISynchronizePageConfiguration configuration) {
127             super(TeamUIMessages.ChangeLogModelProvider_6, configuration);
128         }
129         
130         public void run() {
131             ActiveChangeSet set = getSelectedSet();
132             if (set == null) return;
133             editChangeSet(set);
134         }
135     }
136     
137     private class RemoveChangeSetAction extends ChangeSetAction {
138
139         public RemoveChangeSetAction(ISynchronizePageConfiguration configuration) {
140             super("Remove Change Set", configuration); //$NON-NLS-1$
141
}
142         
143         public void run() {
144             ActiveChangeSet set = getSelectedSet();
145             if (set == null) return;
146             if (MessageDialog.openConfirm(getConfiguration().getSite().getShell(), TeamUIMessages.ChangeSetActionGroup_0, NLS.bind(TeamUIMessages.ChangeSetActionGroup_1, new String JavaDoc[] { set.getTitle() }))) { //
147
getActiveChangeSetManager().remove(set);
148             }
149         }
150     }
151     
152     private class MakeDefaultChangeSetAction extends ChangeSetAction {
153
154         public MakeDefaultChangeSetAction(ISynchronizePageConfiguration configuration) {
155             super(TeamUIMessages.ChangeLogModelProvider_9, configuration);
156         }
157         
158         public void run() {
159             ActiveChangeSet set = getSelectedSet();
160             if (set == null) return;
161             getActiveChangeSetManager().makeDefault(set);
162         }
163         
164     }
165     
166     private class AddToChangeSetAction extends SynchronizeModelAction {
167      
168         private final ActiveChangeSet set;
169         
170         public AddToChangeSetAction(ISynchronizePageConfiguration configuration, ActiveChangeSet set, ISelection selection) {
171             super(set == null ? TeamUIMessages.ChangeSetActionGroup_2 : set.getTitle(), configuration);
172             this.set = set;
173             selectionChanged(selection);
174         }
175         
176         /* (non-Javadoc)
177          * @see org.eclipse.team.ui.synchronize.SynchronizeModelAction#getSyncInfoFilter()
178          */

179         protected FastSyncInfoFilter getSyncInfoFilter() {
180             return OUTGOING_RESOURCE_FILTER;
181         }
182         
183         protected boolean needsToSaveDirtyEditors() {
184             return false;
185         }
186         
187         /* (non-Javadoc)
188          * @see org.eclipse.team.ui.synchronize.SynchronizeModelAction#getSubscriberOperation(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration, org.eclipse.compare.structuremergeviewer.IDiffElement[])
189          */

190         protected SynchronizeModelOperation getSubscriberOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements) {
191             return new SynchronizeModelOperation(configuration, elements) {
192                 public void run(IProgressMonitor monitor)
193                         throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
194                     IResource[] resources = getSyncInfoSet().getResources();
195                     if (set != null) {
196                         IDiff[] diffArray = getDiffs(resources);
197                         set.add(diffArray);
198                     } else {
199                         ChangeSet[] sets = getActiveChangeSetManager().getSets();
200                         for (int i = 0; i < sets.length; i++) {
201                             ActiveChangeSet activeSet = (ActiveChangeSet)sets[i];
202                             activeSet.remove(resources);
203                         }
204                     }
205                 }
206             };
207         }
208     }
209     
210     /* *****************************************************************************
211      * Action that allows changing the model providers sort order.
212      */

213     private class ToggleSortOrderAction extends Action {
214         private int criteria;
215         protected ToggleSortOrderAction(String JavaDoc name, int criteria) {
216             super(name, IAction.AS_RADIO_BUTTON);
217             this.criteria = criteria;
218             update();
219         }
220
221         public void run() {
222             if (isChecked() && sortCriteria != criteria) {
223                 sortCriteria = criteria;
224                 String JavaDoc key = getSettingsKey();
225                 IDialogSettings pageSettings = getConfiguration().getSite().getPageSettings();
226                 if(pageSettings != null) {
227                     pageSettings.put(key, criteria);
228                 }
229                 update();
230                 provider.setViewerSorter(getViewerSorter());
231             }
232         }
233         
234         public void update() {
235             setChecked(criteria == sortCriteria);
236         }
237         
238         protected String JavaDoc getSettingsKey() {
239             return P_LAST_COMMENTSORT;
240         }
241     }
242
243     /*
244      * The model provider for this action group
245      */

246     private ChangeSetModelProvider provider;
247     
248     /*
249      * The actions created by this group
250      */

251     private MenuManager sortByComment;
252     private CreateChangeSetAction createChangeSet;
253     private MenuManager addToChangeSet;
254     private EditChangeSetAction editChangeSet;
255     private RemoveChangeSetAction removeChangeSet;
256     private MakeDefaultChangeSetAction makeDefault;
257     
258     private SynchronizePageActionGroup subActions;
259     
260     /*
261      * The currently chosen sort criteria
262      */

263     private int sortCriteria = ChangeSetModelSorter.DATE;
264     
265     public static int getSortCriteria(ISynchronizePageConfiguration configuration) {
266         int sortCriteria = ChangeSetModelSorter.DATE;
267         try {
268             IDialogSettings pageSettings = configuration.getSite().getPageSettings();
269             if(pageSettings != null) {
270                 sortCriteria = pageSettings.getInt(P_LAST_COMMENTSORT);
271             }
272         } catch(NumberFormatException JavaDoc e) {
273             // ignore and use the defaults.
274
}
275         switch (sortCriteria) {
276         case ChangeSetModelSorter.COMMENT:
277         case ChangeSetModelSorter.DATE:
278         case ChangeSetModelSorter.USER:
279             break;
280         default:
281             sortCriteria = ChangeSetModelSorter.DATE;
282             break;
283         }
284         return sortCriteria;
285     }
286     
287     public ChangeSetActionGroup(ChangeSetModelProvider provider) {
288         this.provider = provider;
289     }
290     
291     public void initialize(ISynchronizePageConfiguration configuration) {
292         super.initialize(configuration);
293         
294         if (getChangeSetCapability().supportsCheckedInChangeSets()) {
295             sortCriteria = getSortCriteria(configuration);
296             sortByComment = new MenuManager(TeamUIMessages.ChangeLogModelProvider_0a);
297             sortByComment.add(new ToggleSortOrderAction(TeamUIMessages.ChangeLogModelProvider_1a, ChangeSetModelSorter.COMMENT));
298             sortByComment.add(new ToggleSortOrderAction(TeamUIMessages.ChangeLogModelProvider_2a, ChangeSetModelSorter.DATE));
299             sortByComment.add(new ToggleSortOrderAction(TeamUIMessages.ChangeLogModelProvider_3a, ChangeSetModelSorter.USER));
300         }
301         
302         if (getChangeSetCapability().supportsActiveChangeSets()) {
303             addToChangeSet = new MenuManager(TeamUIMessages.ChangeLogModelProvider_12);
304             addToChangeSet.setRemoveAllWhenShown(true);
305             addToChangeSet.addMenuListener(new IMenuListener() {
306                 public void menuAboutToShow(IMenuManager manager) {
307                     addChangeSets(manager);
308                 }
309             });
310             createChangeSet = new CreateChangeSetAction(configuration);
311             addToChangeSet.add(createChangeSet);
312             addToChangeSet.add(new Separator());
313             editChangeSet = new EditChangeSetAction(configuration);
314             makeDefault = new MakeDefaultChangeSetAction(configuration);
315             removeChangeSet = new RemoveChangeSetAction(configuration);
316         }
317         
318         subActions = getChangeSetCapability().getActionGroup();
319         if (subActions != null) {
320             subActions.initialize(configuration);
321         }
322     }
323     
324     /* (non-Javadoc)
325      * @see org.eclipse.team.ui.synchronize.SynchronizePageActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
326      */

327     public void fillContextMenu(IMenuManager menu) {
328         if (getChangeSetCapability().enableCheckedInChangeSetsFor(getConfiguration())) {
329             appendToGroup(menu, ISynchronizePageConfiguration.SORT_GROUP, sortByComment);
330         }
331         if (getChangeSetCapability().enableActiveChangeSetsFor(getConfiguration())) {
332             appendToGroup(
333                     menu,
334                     CHANGE_SET_GROUP,
335                     addToChangeSet);
336             appendToGroup(
337                     menu,
338                     CHANGE_SET_GROUP,
339                     editChangeSet);
340             appendToGroup(
341                     menu,
342                     CHANGE_SET_GROUP,
343                     removeChangeSet);
344             appendToGroup(
345                     menu,
346                     CHANGE_SET_GROUP,
347                     makeDefault);
348         }
349         if (subActions != null) {
350             subActions.fillContextMenu(menu);
351         }
352     }
353     
354     protected void addChangeSets(IMenuManager manager) {
355         ChangeSet[] sets = getActiveChangeSetManager().getSortedSets();
356         ISelection selection = getContext().getSelection();
357         createChangeSet.selectionChanged(selection);
358         addToChangeSet.add(createChangeSet);
359         addToChangeSet.add(new Separator());
360         for (int i = 0; i < sets.length; i++) {
361             ActiveChangeSet set = (ActiveChangeSet)sets[i];
362             AddToChangeSetAction action = new AddToChangeSetAction(getConfiguration(), set, selection);
363             manager.add(action);
364         }
365         addToChangeSet.add(new Separator());
366         // Action that removes change set resources
367
addToChangeSet.add(new AddToChangeSetAction(getConfiguration(), null, selection));
368     }
369
370     /**
371      * Return the change set manager for the current page.
372      * @return the change set manager for the current page
373      */

374     protected ActiveChangeSetManager getActiveChangeSetManager() {
375         return getChangeSetCapability().getActiveChangeSetManager();
376     }
377
378     /* (non-Javadoc)
379      * @see org.eclipse.team.ui.synchronize.SynchronizePageActionGroup#dispose()
380      */

381     public void dispose() {
382         if (addToChangeSet != null) {
383             addToChangeSet.dispose();
384             addToChangeSet.removeAll();
385         }
386         if (sortByComment != null) {
387             sortByComment.dispose();
388             sortByComment.removeAll();
389         }
390         if (subActions != null) {
391             subActions.dispose();
392         }
393         super.dispose();
394     }
395     
396     
397     public void updateActionBars() {
398         if (editChangeSet != null)
399             editChangeSet.selectionChanged((IStructuredSelection)getContext().getSelection());
400         if (removeChangeSet != null)
401             removeChangeSet.selectionChanged((IStructuredSelection)getContext().getSelection());
402         if (makeDefault != null)
403             makeDefault.selectionChanged((IStructuredSelection)getContext().getSelection());
404         super.updateActionBars();
405     }
406     
407     private void syncExec(final Runnable JavaDoc runnable) {
408         final Control ctrl = getConfiguration().getPage().getViewer().getControl();
409         if (ctrl != null && !ctrl.isDisposed()) {
410             ctrl.getDisplay().syncExec(new Runnable JavaDoc() {
411                 public void run() {
412                     if (!ctrl.isDisposed()) {
413                         runnable.run();
414                     }
415                 }
416             });
417         }
418     }
419     
420     /**
421      * Return a viewer sorter that utilizes the sort criteria
422      * selected by the user.
423      * @return a sorter
424      */

425     public ViewerSorter getViewerSorter() {
426         return new ChangeSetModelSorter(provider, sortCriteria);
427     }
428     
429     private ActiveChangeSet createChangeSet(IDiff[] diffs) {
430         return getChangeSetCapability().createChangeSet(getConfiguration(), diffs);
431     }
432     
433     private void editChangeSet(ActiveChangeSet set) {
434         getChangeSetCapability().editChangeSet(getConfiguration(), set);
435     }
436
437     private ChangeSetCapability getChangeSetCapability() {
438         return provider.getChangeSetCapability();
439     }
440
441     private IDiff[] getDiffs(IResource[] resources) {
442         List JavaDoc diffs = new ArrayList JavaDoc();
443         Subscriber s = ((SubscriberParticipant)getConfiguration().getParticipant()).getSubscriber();
444         for (int i = 0; i < resources.length; i++) {
445             IResource resource = resources[i];
446             try {
447                 IDiff diff = s.getDiff(resource);
448                 if (diff != null)
449                     diffs.add(diff);
450             } catch (CoreException e) {
451                 TeamUIPlugin.log(e);
452             }
453         }
454         IDiff[] diffArray = (IDiff[]) diffs
455                 .toArray(new IDiff[diffs.size()]);
456         return diffArray;
457     }
458 }
459
Popular Tags