KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collections JavaDoc;
15
16 import org.eclipse.core.commands.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.dialogs.ErrorDialog;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.team.internal.ui.*;
23 import org.eclipse.team.ui.mapping.*;
24 import org.eclipse.team.ui.synchronize.*;
25 import org.eclipse.ui.PlatformUI;
26
27 /**
28  * An action that delegates to an appropriate handler when performing
29  * a merge opereration.
30  *
31  * @since 3.2
32  */

33 public class MergeAction extends Action {
34     
35     private final String JavaDoc handlerId;
36     private final CommonMenuManager manager;
37     private final ISynchronizePageConfiguration configuration;
38     private IHandler defaultHandler;
39     
40     public MergeAction(String JavaDoc handlerId, CommonMenuManager manager, ISynchronizePageConfiguration configuration) {
41         Assert.isNotNull(handlerId);
42         Assert.isNotNull(manager);
43         Assert.isNotNull(configuration);
44         this.handlerId = handlerId;
45         this.manager = manager;
46         this.configuration = configuration;
47     }
48     
49     public void runWithEvent(Event event) {
50         IHandler handler = getHandler();
51         if (handler != null && handler.isEnabled()) {
52             final SaveableComparison currentBuffer = ((ModelSynchronizeParticipant)configuration.getParticipant()).getActiveSaveable();
53             if (currentBuffer != null && currentBuffer.isDirty()) {
54                 SaveableComparison targetBuffer = null;
55                 if (handler instanceof MergeActionHandler) {
56                     MergeActionHandler mah = (MergeActionHandler) handler;
57                     targetBuffer = mah.getSaveable();
58                 }
59                 final SaveableComparison target = targetBuffer;
60                 try {
61                     PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
62                         public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
63                                 InterruptedException JavaDoc {
64                             try {
65                                 ModelParticipantAction.handleTargetSaveableChange(configuration.getSite().getShell(), target, currentBuffer, true, monitor);
66                             } catch (CoreException e) {
67                                 throw new InvocationTargetException JavaDoc(e);
68                             }
69                         }
70                     });
71                 } catch (InvocationTargetException JavaDoc e) {
72                     Utils.handle(e);
73                     return;
74                 } catch (InterruptedException JavaDoc e) {
75                     return;
76                 }
77                 ((ModelSynchronizeParticipant)configuration.getParticipant()).setActiveSaveable(targetBuffer);
78             }
79             try {
80                 handler.execute(new ExecutionEvent(null, Collections.EMPTY_MAP, event, null));
81             } catch (ExecutionException e) {
82                 handle(e);
83             }
84         }
85     }
86
87     /**
88      * Handle the given exception. By default, the user is prompted
89      * @param exception the exception
90      */

91     protected void handle(Throwable JavaDoc exception) {
92         if (exception instanceof ExecutionException) {
93             ExecutionException ee = (ExecutionException) exception;
94             if (ee.getCause() != null) {
95                 handle(exception.getCause());
96             }
97         }
98         IStatus status;
99         if (exception instanceof CoreException) {
100             CoreException ce = (CoreException) exception;
101             status = ce.getStatus();
102         } else {
103             status = new Status(IStatus.ERROR, TeamUIPlugin.ID, 0, TeamUIMessages.exception, exception);
104         }
105         ErrorDialog.openError(configuration.getSite().getShell(), null, null, status);
106     }
107
108     private IHandler getHandler() {
109         IHandler handler = manager.getHandler(handlerId);
110         if (handler == null) {
111             if (defaultHandler == null)
112                 defaultHandler = getDefaultHandler();
113             return defaultHandler;
114         }
115         return handler;
116     }
117     
118     private IHandler getDefaultHandler() {
119         return MergeActionHandler.getDefaultHandler(handlerId, configuration);
120     }
121
122     private boolean calculateEnablement() {
123         IHandler handler = getHandler();
124         return handler != null && handler.isEnabled();
125     }
126
127     public void dispose() {
128         if (defaultHandler != null)
129             defaultHandler.dispose();
130     }
131     
132     public void update() {
133         setEnabled(calculateEnablement());
134     }
135
136 }
137
Popular Tags