KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 import java.util.Collections JavaDoc;
15
16 import org.eclipse.core.commands.*;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.jface.operation.IRunnableWithProgress;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.team.core.diff.*;
23 import org.eclipse.team.internal.ui.Utils;
24 import org.eclipse.team.ui.mapping.SaveableComparison;
25 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
26 import org.eclipse.team.ui.synchronize.ModelParticipantAction;
27 import org.eclipse.ui.PlatformUI;
28
29 /**
30  * Action that performs an optimistic merge
31  */

32 public class MergeIncomingChangesAction extends ModelParticipantAction implements IHandlerListener {
33
34     IHandler handler;
35     
36     public MergeIncomingChangesAction(ISynchronizePageConfiguration configuration) {
37         super(null, configuration);
38         // TODO: We're past the API freeze so we need to access the property by string
39
handler = (IHandler)configuration.getProperty("org.eclipse.team.ui.mergeAll"); //$NON-NLS-1$
40
if (handler == null)
41             handler = new MergeAllActionHandler(configuration);
42         handler.addHandlerListener(this);
43     }
44     
45     public void runWithEvent(Event event) {
46         if (handler == null || !handler.isEnabled())
47             return;
48         try {
49             handleTargetSaveableChange();
50         } catch (InvocationTargetException JavaDoc e) {
51             handle(e);
52             return;
53         } catch (InterruptedException JavaDoc e) {
54             // Canceled so return
55
return;
56         }
57         try {
58             handler.execute(new ExecutionEvent(null, Collections.EMPTY_MAP, event, null));
59         } catch (ExecutionException e) {
60             handle(e);
61         }
62     }
63     
64     private void handle(Throwable JavaDoc throwable) {
65         if (throwable instanceof ExecutionException) {
66             ExecutionException ee = (ExecutionException) throwable;
67             if (ee.getCause() != null) {
68                 throwable = ee.getCause();
69             }
70         }
71         Utils.handle(throwable);
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.team.internal.ui.mapping.ModelProviderAction#isEnabledForSelection(org.eclipse.jface.viewers.IStructuredSelection)
76      */

77     protected boolean isEnabledForSelection(IStructuredSelection selection) {
78         return handler.isEnabled();
79     }
80     
81     /* (non-Javadoc)
82      * @see org.eclipse.team.ui.operations.ModelProviderAction#getDiffFilter()
83      */

84     protected FastDiffFilter getDiffFilter() {
85         return new FastDiffFilter() {
86             public boolean select(IDiff node) {
87                 if (node instanceof IThreeWayDiff) {
88                     IThreeWayDiff twd = (IThreeWayDiff) node;
89                     if (twd.getDirection() == IThreeWayDiff.CONFLICTING || twd.getDirection() == IThreeWayDiff.INCOMING) {
90                         return true;
91                     }
92                 }
93                 return false;
94             }
95         };
96     }
97     
98     protected void handleTargetSaveableChange() throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
99         final SaveableComparison currentBuffer = getActiveSaveable();
100         if (currentBuffer != null && currentBuffer.isDirty()) {
101             PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
102                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
103                         InterruptedException JavaDoc {
104                     try {
105                         handleTargetSaveableChange(getConfiguration().getSite().getShell(), null, currentBuffer, true, monitor);
106                     } catch (CoreException e) {
107                         throw new InvocationTargetException JavaDoc(e);
108                     }
109                 }
110             });
111         }
112         setActiveSaveable(null);
113     }
114
115     public void dispose() {
116         handler.dispose();
117     }
118
119     /**
120      * @param handlerEvent
121      */

122     public void handlerChanged(HandlerEvent handlerEvent) {
123         setEnabled(handler.isEnabled());
124     }
125     
126 }
127
Popular Tags