KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ccvs.ui.mappings;
12
13 import org.eclipse.core.resources.ResourcesPlugin;
14 import org.eclipse.core.resources.mapping.ResourceMapping;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.team.core.diff.*;
20 import org.eclipse.team.core.mapping.IMergeContext;
21 import org.eclipse.team.core.mapping.ISynchronizationContext;
22 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
23 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
24 import org.eclipse.ui.IWorkbenchPart;
25
26 public class ModelReplaceOperation extends ModelUpdateOperation {
27     
28     boolean hasPrompted = false;
29
30     public ModelReplaceOperation(IWorkbenchPart part, ResourceMapping[] selectedMappings, boolean consultModels) {
31         super(part, selectedMappings, consultModels);
32     }
33     
34     /* (non-Javadoc)
35      * @see org.eclipse.team.ui.TeamOperation#getJobName()
36      */

37     protected String JavaDoc getJobName() {
38         return CVSUIMessages.ReplaceOperation_taskName;
39     }
40     
41     /* (non-Javadoc)
42      * @see org.eclipse.team.ui.operations.ResourceMappingMergeOperation#isAttemptHeadlessMerge()
43      */

44     protected boolean isAttemptHeadlessMerge() {
45         return true;
46     }
47     
48     /* (non-Javadoc)
49      * @see org.eclipse.team.ui.operations.ResourceMappingMergeOperation#hasChangesOfInterest()
50      */

51     protected boolean hasChangesOfInterest() {
52         IMergeContext context = (IMergeContext)getContext();
53         return !context.getDiffTree().isEmpty();
54     }
55     
56     /* (non-Javadoc)
57      * @see org.eclipse.team.internal.ccvs.ui.mappings.ModelUpdateOperation#getMergeType()
58      */

59     protected int getMergeType() {
60         return ISynchronizationContext.TWO_WAY;
61     }
62     
63     /* (non-Javadoc)
64      * @see org.eclipse.team.ui.operations.ResourceMappingMergeOperation#performMerge(org.eclipse.core.runtime.IProgressMonitor)
65      */

66     protected IStatus performMerge(IProgressMonitor monitor) throws CoreException {
67         if (!hasLocalChanges() || promptForOverwrite()) {
68             return super.performMerge(monitor);
69         }
70         return new Status(IStatus.ERROR, CVSUIPlugin.ID, REQUEST_PREVIEW, "", null); //$NON-NLS-1$
71
}
72
73     /*
74      * Mde porotected to be overriden by test cases.
75      */

76     protected boolean promptForOverwrite() {
77         if (hasPrompted)
78             return true;
79         final int[] result = new int[] { 1 };
80         Display.getDefault().syncExec(new Runnable JavaDoc() {
81             public void run() {
82                 MessageDialog dialog = new MessageDialog(getShell(), CVSUIMessages.ModelReplaceOperation_0, null, // accept
83
// the
84
// default
85
// window
86
// icon
87
CVSUIMessages.ModelReplaceOperation_1,
88                         MessageDialog.QUESTION, new String JavaDoc[] { CVSUIMessages.ModelReplaceOperation_2, CVSUIMessages.ModelReplaceOperation_3,
89                                 IDialogConstants.CANCEL_LABEL }, result[0]); // preview is the default
90

91                 result[0] = dialog.open();
92
93             };
94         });
95         if (result[0] == 2)
96             throw new OperationCanceledException();
97         hasPrompted = true;
98         return result[0] == 0;
99     }
100
101     private boolean hasLocalChanges() {
102         return getContext().getDiffTree().hasMatchingDiffs(ResourcesPlugin.getWorkspace().getRoot().getFullPath(), new FastDiffFilter() {
103             public boolean select(IDiff node) {
104                 if (node instanceof IThreeWayDiff) {
105                     IThreeWayDiff twd = (IThreeWayDiff) node;
106                     int direction = twd.getDirection();
107                     if (direction == IThreeWayDiff.OUTGOING || direction == IThreeWayDiff.CONFLICTING) {
108                         return true;
109                     }
110                 } else {
111                     // Return true for any two-way change
112
return true;
113                 }
114                 return false;
115             }
116         });
117     }
118 }
119
Popular Tags