KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > mappings > ChangeSetActionProvider


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  * Matt McCutchen <hashproduct+eclipse@gmail.com> - Bug 94808 [Change Sets] "&" not showing up in dropdown menu
11  *******************************************************************************/

12 package org.eclipse.team.internal.ccvs.ui.mappings;
13
14 import java.util.*;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.resources.mapping.ResourceTraversal;
18 import org.eclipse.jface.action.*;
19 import org.eclipse.jface.dialogs.IDialogSettings;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.viewers.*;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.team.core.diff.*;
25 import org.eclipse.team.core.mapping.IResourceDiffTree;
26 import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
27 import org.eclipse.team.internal.ccvs.core.mapping.ChangeSetModelProvider;
28 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
29 import org.eclipse.team.internal.core.subscribers.*;
30 import org.eclipse.team.internal.ui.*;
31 import org.eclipse.team.internal.ui.mapping.ResourceModelActionProvider;
32 import org.eclipse.team.internal.ui.mapping.ResourceModelTraversalCalculator;
33 import org.eclipse.team.internal.ui.synchronize.*;
34 import org.eclipse.team.ui.synchronize.*;
35 import org.eclipse.ui.actions.ActionContext;
36 import org.eclipse.ui.actions.BaseSelectionListenerAction;
37 import org.eclipse.ui.navigator.INavigatorContentExtension;
38 import org.eclipse.ui.navigator.INavigatorContentService;
39
40 public class ChangeSetActionProvider extends ResourceModelActionProvider {
41
42     /**
43      * Menu group that can be added to the context menu
44      */

45     public final static String JavaDoc CHANGE_SET_GROUP = "changeSetActions"; //$NON-NLS-1$
46

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

50     private MenuManager sortByComment;
51     private CreateChangeSetAction createChangeSet;
52     private MenuManager addToChangeSet;
53     private EditChangeSetAction editChangeSet;
54     private RemoveChangeSetAction removeChangeSet;
55     private MakeDefaultChangeSetAction makeDefault;
56
57     private OpenChangeSetAction openCommitSet;
58     
59     private class CreateChangeSetAction extends ModelParticipantAction {
60         
61         public CreateChangeSetAction(ISynchronizePageConfiguration configuration) {
62             super(TeamUIMessages.ChangeLogModelProvider_0, configuration);
63         }
64
65         public void run() {
66             final IDiff[] diffs = getLocalChanges(getStructuredSelection());
67             syncExec(new Runnable JavaDoc() {
68                 public void run() {
69                     createChangeSet(diffs);
70                 }
71             });
72         }
73         
74         private void createChangeSet(IDiff[] diffs) {
75             ActiveChangeSet set = getChangeSetCapability().createChangeSet(getConfiguration(), diffs);
76             if (set != null) {
77                 getActiveChangeSetManager().add(set);
78             }
79         }
80         
81         protected boolean isEnabledForSelection(IStructuredSelection selection) {
82             return isContentProviderEnabled() && containsLocalChanges(selection);
83         }
84     }
85     
86     /**
87      * Escape a string so it can be used as an action text without '&'
88      * being interpreted as a mnemonic. Specifically, turn each '&' into '&&'.
89      */

90     private static String JavaDoc escapeActionText(String JavaDoc x) {
91         // Loosely based on org.eclipse.jface.action.LegacyActionTools#removeMnemonics
92
int ampersandIndex = x.indexOf('&');
93         if (ampersandIndex == -1)
94             return x;
95         
96         int len = x.length();
97         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(2 * len + 1);
98         int doneIndex = 0;
99         while (ampersandIndex != -1) {
100             sb.append(x.substring(doneIndex, ampersandIndex));
101             sb.append("&&"); //$NON-NLS-1$
102
doneIndex = ampersandIndex + 1;
103             ampersandIndex = x.indexOf('&', doneIndex);
104         }
105         if (doneIndex < len)
106             sb.append(x.substring(doneIndex, len));
107         return sb.toString();
108     }
109
110     private class AddToChangeSetAction extends ModelParticipantAction {
111          
112         private final ActiveChangeSet set;
113         
114         public AddToChangeSetAction(ISynchronizePageConfiguration configuration, ActiveChangeSet set, ISelection selection) {
115             super(set == null ? TeamUIMessages.ChangeSetActionGroup_2 : escapeActionText(set.getTitle()), configuration);
116             this.set = set;
117             selectionChanged(selection);
118         }
119         
120         public void run() {
121             IDiff[] diffArray = getLocalChanges(getStructuredSelection());
122             if (set != null) {
123                 set.add(diffArray);
124             } else {
125                 ChangeSet[] sets = getActiveChangeSetManager().getSets();
126                 IResource[] resources = getResources(diffArray);
127                 for (int i = 0; i < sets.length; i++) {
128                     ActiveChangeSet activeSet = (ActiveChangeSet)sets[i];
129                     activeSet.remove(resources);
130                 }
131             }
132         }
133
134         private IResource[] getResources(IDiff[] diffArray) {
135             List result = new ArrayList();
136             for (int i = 0; i < diffArray.length; i++) {
137                 IDiff diff = diffArray[i];
138                 IResource resource = ResourceDiffTree.getResourceFor(diff);
139                 if (resource != null) {
140                     result.add(resource);
141                 }
142             }
143             return (IResource[]) result.toArray(new IResource[result.size()]);
144         }
145
146         protected boolean isEnabledForSelection(IStructuredSelection selection) {
147             return isContentProviderEnabled() && containsLocalChanges(selection);
148         }
149     }
150
151     private abstract class ChangeSetAction extends BaseSelectionListenerAction {
152
153         public ChangeSetAction(String JavaDoc title, ISynchronizePageConfiguration configuration) {
154             super(title);
155         }
156         
157         /* (non-Javadoc)
158          * @see org.eclipse.ui.actions.BaseSelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
159          */

160         protected boolean updateSelection(IStructuredSelection selection) {
161             return getSelectedSet() != null;
162         }
163
164         protected ActiveChangeSet getSelectedSet() {
165             IStructuredSelection selection = getStructuredSelection();
166             if (selection.size() == 1) {
167                 Object JavaDoc first = selection.getFirstElement();
168                 if (first instanceof ActiveChangeSet) {
169                     ActiveChangeSet activeChangeSet = (ActiveChangeSet) first;
170                     if (activeChangeSet.isUserCreated())
171                         return activeChangeSet;
172                 }
173             }
174             return null;
175         }
176     }
177     
178     private class EditChangeSetAction extends ChangeSetAction {
179
180         public EditChangeSetAction(ISynchronizePageConfiguration configuration) {
181             super(TeamUIMessages.ChangeLogModelProvider_6, configuration);
182         }
183         
184         public void run() {
185             ActiveChangeSet set = getSelectedSet();
186             if (set == null) return;
187             getChangeSetCapability().editChangeSet(getSynchronizePageConfiguration(), set);
188         }
189     }
190     
191     private class RemoveChangeSetAction extends ChangeSetAction {
192
193         public RemoveChangeSetAction(ISynchronizePageConfiguration configuration) {
194             super("Remove Change Set", configuration); //$NON-NLS-1$
195
}
196         
197         public void run() {
198             ActiveChangeSet set = getSelectedSet();
199             if (set == null) return;
200             if (MessageDialog.openConfirm(getSynchronizePageConfiguration().getSite().getShell(), TeamUIMessages.ChangeSetActionGroup_0, NLS.bind(TeamUIMessages.ChangeSetActionGroup_1, new String JavaDoc[] { set.getTitle() }))) { //
201
getActiveChangeSetManager().remove(set);
202             }
203         }
204     }
205     
206     private class MakeDefaultChangeSetAction extends ChangeSetAction {
207         public MakeDefaultChangeSetAction(ISynchronizePageConfiguration configuration) {
208             super(TeamUIMessages.ChangeLogModelProvider_9, configuration);
209         }
210         
211         public void run() {
212             ActiveChangeSet set = getSelectedSet();
213             if (set == null) return;
214             getActiveChangeSetManager().makeDefault(set);
215         }
216     }
217     
218     /* *****************************************************************************
219      * Action that allows changing the model providers sort order.
220      */

221     private class ToggleSortOrderAction extends Action {
222         private int criteria;
223         protected ToggleSortOrderAction(String JavaDoc name, int criteria) {
224             super(name, IAction.AS_RADIO_BUTTON);
225             this.criteria = criteria;
226             update();
227         }
228
229         public void run() {
230             int sortCriteria = getSortCriteria(getSynchronizePageConfiguration());
231             if (isChecked() && sortCriteria != criteria) {
232                 setSortCriteria(getSynchronizePageConfiguration(), criteria);
233                 update();
234                 ((SynchronizePageConfiguration)getSynchronizePageConfiguration()).getPage().getViewer().refresh();
235             }
236         }
237         
238         public void update() {
239             setChecked(criteria == getSortCriteria(getSynchronizePageConfiguration()));
240         }
241     }
242     
243     public static int getSortCriteria(ISynchronizePageConfiguration configuration) {
244         int sortCriteria = ChangeSetSorter.DATE;
245         if (configuration != null) {
246             Object JavaDoc o = configuration.getProperty(P_LAST_COMMENTSORT);
247             if (o instanceof Integer JavaDoc) {
248                 Integer JavaDoc wrapper = (Integer JavaDoc) o;
249                 sortCriteria = wrapper.intValue();
250             } else {
251                 try {
252                     IDialogSettings pageSettings = configuration.getSite().getPageSettings();
253                     if(pageSettings != null) {
254                         sortCriteria = pageSettings.getInt(P_LAST_COMMENTSORT);
255                     }
256                 } catch(NumberFormatException JavaDoc e) {
257                     // ignore and use the defaults.
258
}
259             }
260         }
261         switch (sortCriteria) {
262         case ChangeSetSorter.COMMENT:
263         case ChangeSetSorter.DATE:
264         case ChangeSetSorter.USER:
265             break;
266         default:
267             sortCriteria = ChangeSetSorter.DATE;
268             break;
269         }
270         return sortCriteria;
271     }
272     
273     public static void setSortCriteria(ISynchronizePageConfiguration configuration, int criteria) {
274         configuration.setProperty(P_LAST_COMMENTSORT, new Integer JavaDoc(criteria));
275         IDialogSettings pageSettings = configuration.getSite().getPageSettings();
276         if(pageSettings != null) {
277             pageSettings.put(P_LAST_COMMENTSORT, criteria);
278         }
279     }
280     
281     public ChangeSetActionProvider() {
282         super();
283     }
284
285     protected void initialize() {
286         super.initialize();
287         if (getChangeSetCapability().supportsCheckedInChangeSets()) {
288             sortByComment = new MenuManager(TeamUIMessages.ChangeLogModelProvider_0a);
289             sortByComment.add(new ToggleSortOrderAction(TeamUIMessages.ChangeLogModelProvider_1a, ChangeSetSorter.COMMENT));
290             sortByComment.add(new ToggleSortOrderAction(TeamUIMessages.ChangeLogModelProvider_2a, ChangeSetSorter.DATE));
291             sortByComment.add(new ToggleSortOrderAction(TeamUIMessages.ChangeLogModelProvider_3a, ChangeSetSorter.USER));
292             openCommitSet = new OpenChangeSetAction(getSynchronizePageConfiguration());
293         }
294         if (getChangeSetCapability().supportsActiveChangeSets()) {
295             addToChangeSet = new MenuManager(TeamUIMessages.ChangeLogModelProvider_12);
296             addToChangeSet.setRemoveAllWhenShown(true);
297             addToChangeSet.addMenuListener(new IMenuListener() {
298                 public void menuAboutToShow(IMenuManager manager) {
299                     addChangeSets(manager);
300                 }
301             });
302             createChangeSet = new CreateChangeSetAction(getSynchronizePageConfiguration());
303             addToChangeSet.add(createChangeSet);
304             addToChangeSet.add(new Separator());
305             editChangeSet = new EditChangeSetAction(getSynchronizePageConfiguration());
306             makeDefault = new MakeDefaultChangeSetAction(getSynchronizePageConfiguration());
307             removeChangeSet = new RemoveChangeSetAction(getSynchronizePageConfiguration());
308         }
309     }
310
311     public void fillContextMenu(IMenuManager menu) {
312         if (isContentProviderEnabled()) {
313             super.fillContextMenu(menu);
314             if (getChangeSetCapability().enableCheckedInChangeSetsFor(getSynchronizePageConfiguration())) {
315                 appendToGroup(
316                         menu,
317                         "file-bottom", //$NON-NLS-1$
318
openCommitSet);
319                 appendToGroup(menu, ISynchronizePageConfiguration.SORT_GROUP, sortByComment);
320             }
321             if (getChangeSetCapability().enableActiveChangeSetsFor(getSynchronizePageConfiguration())) {
322                 appendToGroup(
323                         menu,
324                         CHANGE_SET_GROUP,
325                         addToChangeSet);
326                 appendToGroup(
327                         menu,
328                         CHANGE_SET_GROUP,
329                         editChangeSet);
330                 appendToGroup(
331                         menu,
332                         CHANGE_SET_GROUP,
333                         removeChangeSet);
334                 appendToGroup(
335                         menu,
336                         CHANGE_SET_GROUP,
337                         makeDefault);
338             }
339         }
340     }
341     
342     public void dispose() {
343         if (addToChangeSet != null) {
344             addToChangeSet.dispose();
345             addToChangeSet.removeAll();
346         }
347         if (sortByComment != null) {
348             sortByComment.dispose();
349             sortByComment.removeAll();
350         }
351         super.dispose();
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(getSynchronizePageConfiguration(), set, selection);
363             manager.add(action);
364         }
365         addToChangeSet.add(new Separator());
366         // Action that removes change set resources
367
addToChangeSet.add(new AddToChangeSetAction(getSynchronizePageConfiguration(), null, selection));
368     }
369     
370     private boolean appendToGroup(IContributionManager manager, String JavaDoc groupId, IContributionItem item) {
371         if (manager == null || item == null) return false;
372         IContributionItem group = manager.find(groupId);
373         if (group != null) {
374             manager.appendToGroup(group.getId(), item);
375             return true;
376         }
377         return false;
378     }
379     
380     private boolean appendToGroup(IContributionManager manager, String JavaDoc groupId, IAction action) {
381         if (manager == null || action == null) return false;
382         IContributionItem group = manager.find(groupId);
383         if (group != null) {
384             manager.appendToGroup(group.getId(), action);
385             //registerActionWithWorkbench(action);
386
return true;
387         }
388         return false;
389     }
390     
391     public ChangeSetCapability getChangeSetCapability() {
392         ISynchronizeParticipant participant = getSynchronizePageConfiguration().getParticipant();
393         if (participant instanceof IChangeSetProvider) {
394             IChangeSetProvider provider = (IChangeSetProvider) participant;
395             return provider.getChangeSetCapability();
396         }
397         return null;
398     }
399     
400     private void syncExec(final Runnable JavaDoc runnable) {
401         final Control ctrl = getSynchronizePageConfiguration().getPage().getViewer().getControl();
402         Utils.syncExec(runnable, ctrl);
403     }
404
405     private ActiveChangeSetManager getActiveChangeSetManager() {
406         return CVSUIPlugin.getPlugin().getChangeSetManager();
407     }
408
409     
410     public IDiff[] getLocalChanges(IStructuredSelection selection) {
411         if (selection instanceof ITreeSelection) {
412             ITreeSelection ts = (ITreeSelection) selection;
413             TreePath[] paths = ts.getPaths();
414             List result = new ArrayList();
415             for (int i = 0; i < paths.length; i++) {
416                 TreePath path = paths[i];
417                 IDiff[] diffs = getLocalChanges(path);
418                 for (int j = 0; j < diffs.length; j++) {
419                     IDiff diff = diffs[j];
420                     result.add(diff);
421                 }
422             }
423             return (IDiff[]) result.toArray(new IDiff[result.size()]);
424         }
425         return new IDiff[0];
426     }
427
428     private IDiff[] getLocalChanges(TreePath path) {
429         IResourceDiffTree tree = getDiffTree(path);
430         if (path.getSegmentCount() == 1 && path.getLastSegment() instanceof IDiffTree) {
431             return ((ResourceDiffTree)tree).getDiffs();
432         }
433         ResourceTraversal[] traversals = getTraversals(path.getLastSegment());
434         return tree.getDiffs(traversals);
435     }
436
437     private IResourceDiffTree getDiffTree(TreePath path) {
438         return getContentProvider().getDiffTree(path);
439     }
440
441     public boolean containsLocalChanges(IStructuredSelection selection) {
442         if (selection instanceof ITreeSelection) {
443             ITreeSelection ts = (ITreeSelection) selection;
444             TreePath[] paths = ts.getPaths();
445             for (int i = 0; i < paths.length; i++) {
446                 TreePath path = paths[i];
447                 if (containsLocalChanges(path)) {
448                     return true;
449                 }
450             }
451         }
452         return false;
453     }
454
455     private boolean containsLocalChanges(TreePath path) {
456         IResourceDiffTree tree = getDiffTree(path);
457         ResourceTraversal[] traversals = getTraversals(path.getLastSegment());
458         return tree.hasMatchingDiffs(traversals, getVisibleLocalChangesFilter());
459     }
460
461     private ResourceTraversal[] getTraversals(Object JavaDoc element) {
462         if (element instanceof ChangeSet) {
463             ChangeSet set = (ChangeSet) element;
464             return new ResourceTraversal[] { new ResourceTraversal(set.getResources(), IResource.DEPTH_ZERO, IResource.NONE) };
465         }
466         if (element instanceof IProject) {
467             IProject project = (IProject) element;
468             return new ResourceTraversal[] { new ResourceTraversal(new IResource[] { project }, IResource.DEPTH_INFINITE, IResource.NONE) };
469         }
470         if (element instanceof IFile) {
471             IFile file = (IFile) element;
472             return new ResourceTraversal[] { new ResourceTraversal(new IResource[] { file }, IResource.DEPTH_ZERO, IResource.NONE) };
473         }
474         if (element instanceof IFolder) {
475             IFolder folder = (IFolder) element;
476             if (getLayout().equals(IPreferenceIds.COMPRESSED_LAYOUT)) {
477                 return new ResourceTraversal[] { new ResourceTraversal(new IResource[] { folder }, IResource.DEPTH_ONE, IResource.NONE) };
478             } else if (getLayout().equals(IPreferenceIds.TREE_LAYOUT)) {
479                 return new ResourceTraversal[] { new ResourceTraversal(new IResource[] { folder }, IResource.DEPTH_INFINITE, IResource.NONE) };
480             } else if (getLayout().equals(IPreferenceIds.FLAT_LAYOUT)) {
481                 return new ResourceTraversal[] { new ResourceTraversal(new IResource[] { folder }, IResource.DEPTH_ZERO, IResource.NONE) };
482             }
483         }
484         return new ResourceTraversal[0];
485     }
486     
487     private FastDiffFilter getVisibleLocalChangesFilter() {
488         return new FastDiffFilter() {
489             public boolean select(IDiff diff) {
490                 if (diff instanceof IThreeWayDiff && isVisible(diff)) {
491                     IThreeWayDiff twd = (IThreeWayDiff) diff;
492                     if (twd.getDirection() == IThreeWayDiff.OUTGOING || twd.getDirection() == IThreeWayDiff.CONFLICTING) {
493                         return true;
494                     }
495                 }
496                 return false;
497             }
498         };
499     }
500
501     private boolean isVisible(IDiff diff) {
502         return ((SynchronizePageConfiguration)getSynchronizePageConfiguration()).isVisible(diff);
503     }
504     
505     protected ResourceModelTraversalCalculator getTraversalCalculator() {
506         return ResourceModelTraversalCalculator.getTraversalCalculator(getSynchronizePageConfiguration());
507     }
508     
509     private ChangeSetContentProvider getContentProvider() {
510         INavigatorContentExtension extension = getExtension();
511         if (extension != null) {
512             ITreeContentProvider provider = extension.getContentProvider();
513             if (provider instanceof ChangeSetContentProvider) {
514                 return (ChangeSetContentProvider) provider;
515             }
516         }
517         return null;
518     }
519
520     private INavigatorContentExtension getExtension() {
521         INavigatorContentService service = getActionSite().getContentService();
522         Set set = service.findContentExtensionsByTriggerPoint(getModelProvider());
523         for (Iterator iter = set.iterator(); iter.hasNext();) {
524             INavigatorContentExtension extension = (INavigatorContentExtension) iter.next();
525             return extension;
526         }
527         return null;
528     }
529
530     private Object JavaDoc getModelProvider() {
531         return ChangeSetModelProvider.getProvider();
532     }
533
534     private String JavaDoc getLayout() {
535         return TeamUIPlugin.getPlugin().getPreferenceStore().getString(IPreferenceIds.SYNCVIEW_DEFAULT_LAYOUT);
536     }
537     
538     public void setContext(ActionContext context) {
539         super.setContext(context);
540         if (context != null) {
541             if (editChangeSet != null)
542                 editChangeSet.selectionChanged((IStructuredSelection)getContext().getSelection());
543             if (removeChangeSet != null)
544                 removeChangeSet.selectionChanged((IStructuredSelection)getContext().getSelection());
545             if (makeDefault != null)
546                 makeDefault.selectionChanged((IStructuredSelection)getContext().getSelection());
547         }
548     }
549
550     protected boolean isContentProviderEnabled() {
551         ChangeSetContentProvider provider = getContentProvider();
552         if (provider != null) {
553             return provider.isEnabled();
554         }
555         return false;
556     }
557 }
558
Popular Tags