KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > ResourceMarkAsMergedHandler


1 /*******************************************************************************
2  * Copyright (c) 2006 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.mapping;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IWorkspaceRunnable;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.jobs.ISchedulingRule;
22 import org.eclipse.core.runtime.jobs.MultiRule;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.team.core.diff.*;
26 import org.eclipse.team.core.mapping.IMergeContext;
27 import org.eclipse.team.internal.ui.TeamUIMessages;
28 import org.eclipse.team.ui.mapping.SynchronizationOperation;
29 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
30
31 public class ResourceMarkAsMergedHandler extends ResourceMergeActionHandler {
32
33     private ResourceModelProviderOperation operation;
34
35
36     public ResourceMarkAsMergedHandler(ISynchronizePageConfiguration configuration) {
37         super(configuration);
38     }
39     
40     /* (non-Javadoc)
41      * @see org.eclipse.team.ui.mapping.MergeActionHandler#getOperation()
42      */

43     protected synchronized SynchronizationOperation getOperation() {
44         if (operation == null) {
45             operation = new ResourceModelProviderOperation(getConfiguration(),
46                         getStructuredSelection()) {
47                 /* (non-Javadoc)
48                  * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
49                  */

50                 public void execute(IProgressMonitor monitor)
51                         throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
52                     try {
53                         final IMergeContext context = (IMergeContext) getContext();
54                         final IDiff[] deltas = getTargetDiffs();
55                         ISchedulingRule rule = getMergeRule(context, deltas);
56                         context.run(new IWorkspaceRunnable() {
57                             public void run(IProgressMonitor monitor)
58                                     throws CoreException {
59                                 markAsMerged(deltas, context, monitor);
60                             }
61     
62                         }, rule, IResource.NONE, monitor);
63     
64                     } catch (CoreException e) {
65                         throw new InvocationTargetException JavaDoc(e);
66                     }
67                 }
68     
69                 private ISchedulingRule getMergeRule(IMergeContext context,
70                         IDiff[] deltas) {
71                     ISchedulingRule result = null;
72                     for (int i = 0; i < deltas.length; i++) {
73                         IDiff node = deltas[i];
74                         ISchedulingRule rule = context.getMergeRule(node);
75                         if (result == null) {
76                             result = rule;
77                         } else {
78                             result = MultiRule.combine(result, rule);
79                         }
80                     }
81                     return result;
82                 }
83     
84                 private void markAsMerged(IDiff[] deltas,
85                         final IMergeContext context, IProgressMonitor monitor)
86                         throws CoreException {
87                     context.markAsMerged(deltas, false, monitor);
88                 }
89     
90                 /* (non-Javadoc)
91                  * @see org.eclipse.team.internal.ui.mapping.ResourceModelProviderOperation#getDiffFilter()
92                  */

93                 protected FastDiffFilter getDiffFilter() {
94                     return new FastDiffFilter() {
95                         public boolean select(IDiff node) {
96                             if (node instanceof IThreeWayDiff) {
97                                 IThreeWayDiff twd = (IThreeWayDiff) node;
98                                 if (twd.getDirection() == IThreeWayDiff.CONFLICTING
99                                         || twd.getDirection() == IThreeWayDiff.INCOMING) {
100                                     return true;
101                                 }
102                             }
103                             return false;
104                         }
105                     };
106                 }
107                 protected String JavaDoc getJobName() {
108                     IDiff[] diffs = getTargetDiffs();
109                     if (diffs.length == 1)
110                         return TeamUIMessages.ResourceMarkAsMergedHandler_0;
111                     return NLS.bind(TeamUIMessages.ResourceMarkAsMergedHandler_1, new Integer JavaDoc(diffs.length).toString());
112                 }
113             };
114         }
115         return operation;
116     }
117     
118     
119     /* (non-Javadoc)
120      * @see org.eclipse.team.ui.mapping.MergeActionHandler#updateEnablement(org.eclipse.jface.viewers.IStructuredSelection)
121      */

122     public void updateEnablement(IStructuredSelection selection) {
123         synchronized (this) {
124             operation = null;
125         }
126         super.updateEnablement(selection);
127         int mode = getConfiguration().getMode();
128         if ((mode == ISynchronizePageConfiguration.OUTGOING_MODE
129                 && getSynchronizationContext().getDiffTree().countFor(IThreeWayDiff.CONFLICTING, IThreeWayDiff.DIRECTION_MASK) == 0)
130                 || (getSynchronizationContext().getDiffTree().countFor(IThreeWayDiff.CONFLICTING, IThreeWayDiff.DIRECTION_MASK) == 0
131                         && getSynchronizationContext().getDiffTree().countFor(IThreeWayDiff.INCOMING, IThreeWayDiff.DIRECTION_MASK) == 0)) {
132             setEnabled(false);
133             return;
134         }
135     }
136     
137     public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
138         if (saveDirtyEditors())
139             return super.execute(event);
140         return null;
141     }
142     
143 }
144
Popular Tags