Skip to main content
summaryrefslogtreecommitdiffstats
blob: 08a9dfb038a513c2c22fabc4090971138f6dfcc8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.actions;
 
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.mapping.ResourceMapping;
import org.eclipse.core.resources.mapping.ResourceMappingContext;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.internal.ccvs.core.CVSTag;
import org.eclipse.team.internal.ccvs.ui.*;
import org.eclipse.team.internal.ccvs.ui.operations.ReplaceOperation;
import org.eclipse.team.internal.ccvs.ui.tags.TagSelectionDialog;
import org.eclipse.team.internal.ccvs.ui.tags.TagSource;
import org.eclipse.team.internal.core.InfiniteSubProgressMonitor;
import org.eclipse.team.internal.ui.dialogs.ResourceMappingResourceDisplayArea;
import org.eclipse.ui.PlatformUI;

/**
 * Action for replace with tag.
 */
public class ReplaceWithTagAction extends WorkspaceTraversalAction {
    
    /* package*/ static UncommittedChangesDialog getPromptingDialog(final Shell shell, final ResourceMapping[] mappings) {
        final UncommittedChangesDialog[] dialog = new UncommittedChangesDialog[] { null };
        try {
            PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    dialog[0] = new UncommittedChangesDialog(shell, Policy.bind("ReplaceWithTagAction.4"), mappings, monitor) { //$NON-NLS-1$
                        protected String getSingleMappingMessage(ResourceMapping mapping) {
                            String label = ResourceMappingResourceDisplayArea.getLabel(mapping);
                            if (getAllMappings().length == 1) {
                                return Policy.bind("ReplaceWithTagAction.2", label); //$NON-NLS-1$
                            }
                            return Policy.bind("ReplaceWithTagAction.0", label); //$NON-NLS-1$
                        }
            
                        protected String getMultipleMappingsMessage() {
                            return Policy.bind("ReplaceWithTagAction.1"); //$NON-NLS-1$
                        }
                    };
                }
            });
        } catch (InvocationTargetException e) {
            CVSUIPlugin.openError(shell, null, null, e);
            return null;
        } catch (InterruptedException e) {
            return null;
        }
        return dialog[0];
    }
    
    protected static ResourceMapping[] checkOverwriteOfDirtyResources(Shell shell, ResourceMapping[] mappings, IProgressMonitor monitor)  {
        // Prompt for any uncommitted changes
        UncommittedChangesDialog dialog = getPromptingDialog(shell, mappings);
        if (dialog == null) return null;
        mappings = dialog.promptToSelectMappings();
        if(mappings.length == 0) {
            // nothing to do
            return null;                       
        }
        
        return mappings;
    }

	/*
	 * Method declared on IActionDelegate.
	 */
	public void execute(IAction action) throws InterruptedException, InvocationTargetException {
		
		// Setup the holders
		final ResourceMapping[][] resourceMappings = new ResourceMapping[][] {null};
		final CVSTag[] tag = new CVSTag[] {null};
		
		// Show a busy cursor while display the tag selection dialog
		run(new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InterruptedException, InvocationTargetException {
                monitor = Policy.monitorFor(monitor);
				resourceMappings[0] = checkOverwriteOfDirtyResources(getShell(), getCVSResourceMappings(), new InfiniteSubProgressMonitor(monitor, 100));
				if(resourceMappings[0] == null || resourceMappings[0].length == 0) {
					// nothing to do
					return;
				}
				TagSelectionDialog dialog = new TagSelectionDialog(getShell(), TagSource.create(resourceMappings[0]), 
					Policy.bind("ReplaceWithTagAction.message"), //$NON-NLS-1$
					Policy.bind("TagSelectionDialog.Select_a_Tag_1"), //$NON-NLS-1$
					TagSelectionDialog.INCLUDE_ALL_TAGS, 
					false, /*show recurse*/
					IHelpContextIds.REPLACE_TAG_SELECTION_DIALOG); //$NON-NLS-1$
				dialog.setBlockOnOpen(true);
				if (dialog.open() == Dialog.CANCEL) {
					return;
				}
				tag[0] = dialog.getResult();
				
				// For non-projects determine if the tag being loaded is the same as the resource's parent
				// If it's not, warn the user that they will have strange sync behavior
				try {
					if(!CVSAction.checkForMixingTags(getShell(), getRootTraversalResources(resourceMappings[0], ResourceMappingContext.LOCAL_CONTEXT, null), tag[0])) {
						tag[0] = null;
						return;
					}
				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				}
			}
		}, false /* cancelable */, PROGRESS_BUSYCURSOR);			 //$NON-NLS-1$
		
		if (resourceMappings[0] == null || resourceMappings[0].length == 0 || tag[0] == null) return;
		
		// Peform the replace in the background
		new ReplaceOperation(getTargetPart(), resourceMappings[0], tag[0]).run();
	}
	
	/**
	 * @see org.eclipse.team.internal.ccvs.ui.actions.CVSAction#getErrorTitle()
	 */
	protected String getErrorTitle() {
		return Policy.bind("ReplaceWithTagAction.replace"); //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ccvs.ui.actions.WorkspaceAction#isEnabledForNonExistantResources()
	 */
	protected boolean isEnabledForNonExistantResources() {
		return true;
	}
	
}

Back to the top