KickJava   Java API By Example, From Geeks To Geeks.

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


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.runtime.*;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.StructuredViewer;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.team.core.diff.*;
25 import org.eclipse.team.core.mapping.IMergeContext;
26 import org.eclipse.team.internal.ui.TeamUIMessages;
27 import org.eclipse.team.internal.ui.Utils;
28 import org.eclipse.team.ui.mapping.SynchronizationOperation;
29 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
30
31 public class ResourceMergeHandler extends ResourceMergeActionHandler {
32     
33     private final boolean overwrite;
34     private ResourceModelProviderOperation operation;
35
36     public ResourceMergeHandler(ISynchronizePageConfiguration configuration, boolean overwrite) {
37         super(configuration);
38         this.overwrite = overwrite;
39     }
40
41     /* (non-Javadoc)
42      * @see org.eclipse.team.ui.mapping.MergeActionHandler#getOperation()
43      */

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

50                 public void execute(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
51                         InterruptedException JavaDoc {
52                     try {
53                         IMergeContext context = (IMergeContext)getContext();
54                         IDiff[] diffs = getTargetDiffs();
55                         if (diffs.length == 0) {
56                             promptForNoChanges();
57                         }
58                         IStatus status = context.merge(diffs, overwrite, monitor);
59                         if (!status.isOK())
60                             throw new CoreException(status);
61                     } catch (CoreException e) {
62                         throw new InvocationTargetException JavaDoc(e);
63                     }
64                 }
65                 /* (non-Javadoc)
66                  * @see org.eclipse.team.internal.ui.mapping.ResourceModelProviderOperation#getDiffFilter()
67                  */

68                 protected FastDiffFilter getDiffFilter() {
69                     return new FastDiffFilter() {
70                         public boolean select(IDiff node) {
71                             if (node instanceof IThreeWayDiff) {
72                                 IThreeWayDiff twd = (IThreeWayDiff) node;
73                                 if ((twd.getDirection() == IThreeWayDiff.OUTGOING && overwrite) || twd.getDirection() == IThreeWayDiff.CONFLICTING || twd.getDirection() == IThreeWayDiff.INCOMING) {
74                                     return true;
75                                 }
76                                 return false;
77                             }
78                             // Overwrite should always be available for two-way diffs
79
return overwrite;
80                         }
81                     };
82                 }
83                 protected String JavaDoc getJobName() {
84                     IDiff[] diffs = getTargetDiffs();
85                     if (overwrite) {
86                         if (diffs.length == 1)
87                             return TeamUIMessages.ResourceMergeHandler_0;
88                         return NLS.bind(TeamUIMessages.ResourceMergeHandler_1, new Integer JavaDoc(diffs.length).toString());
89                         
90                     }
91                     if (diffs.length == 1)
92                         return TeamUIMessages.ResourceMergeHandler_2;
93                     return NLS.bind(TeamUIMessages.ResourceMergeHandler_3, new Integer JavaDoc(diffs.length).toString());
94                 }
95             };
96         }
97         return operation;
98     }
99     
100     /* (non-Javadoc)
101      * @see org.eclipse.team.ui.mapping.MergeActionHandler#updateEnablement(org.eclipse.jface.viewers.IStructuredSelection)
102      */

103     public void updateEnablement(IStructuredSelection selection) {
104         synchronized (this) {
105             operation = null;
106         }
107         super.updateEnablement(selection);
108         int mode = getConfiguration().getMode();
109         if (mode == ISynchronizePageConfiguration.OUTGOING_MODE && !overwrite) {
110             setEnabled(false);
111             return;
112         }
113     }
114     
115     public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
116         if (saveDirtyEditors() && (!overwrite || promptToConfirm()))
117             return super.execute(event);
118         return null;
119     }
120
121     protected boolean promptToConfirm() {
122         if (Display.getCurrent() != null)
123             return internalPromptToConfirm();
124         final boolean[] confirmed = new boolean[] { false };
125         Shell shell = getConfiguration().getSite().getShell();
126         if (!shell.isDisposed()) {
127             Utils.syncExec(new Runnable JavaDoc() {
128                 public void run() {
129                     confirmed[0] = promptToConfirm();
130                 }
131             
132             }, shell);
133         }
134         return confirmed[0];
135     }
136
137     private boolean internalPromptToConfirm() {
138         return MessageDialog.openQuestion(getConfiguration().getSite().getShell(), TeamUIMessages.ResourceMergeHandler_4, TeamUIMessages.ResourceMergeHandler_5);
139     }
140
141     protected void promptForNoChanges() {
142         Utils.syncExec(new Runnable JavaDoc() {
143             public void run() {
144                 MessageDialog.openInformation(getConfiguration().getSite().getShell(), TeamUIMessages.ResourceMergeHandler_6, TeamUIMessages.ResourceMergeHandler_7);
145             }
146         }, (StructuredViewer)getConfiguration().getPage().getViewer());
147     }
148
149     
150 }
151
Popular Tags