Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 22bf5c49b183cc86ff6a4bb6cdea243d477b685a (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.actions;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.ui.*;
import org.eclipse.team.internal.ccvs.ui.operations.ITagOperation;
import org.eclipse.team.internal.ccvs.ui.repo.RepositoryManager;
import org.eclipse.team.internal.ccvs.ui.tags.TagAsVersionDialog;

/**
 * TagAction tags the selected resources with a version tag specified by the user.
 */
public abstract class TagAction extends WorkspaceTraversalAction {
	
	// remember if the execute action was cancelled
	private boolean wasCancelled = false;

	@Override
	public void execute(IAction action) throws InvocationTargetException, InterruptedException {
		setWasCancelled(false);
		
		// Prompt for the tag name
		final ITagOperation[] result = new ITagOperation[1];
		getShell().getDisplay().syncExec(() -> {
			result[0] = configureOperation();
			if (result[0] == null) {
				return;
			}
		});
		
		if (result[0] == null)  {
			setWasCancelled(true);
			return;
		}
		
		result[0].run();
	}
	
	protected boolean performPrompting(ITagOperation operation)  {
		return true;
	}
	
	/**
	 * Prompts the user for a tag name.
	 * Note: This method is designed to be overridden by test cases.
	 * @return the operation, or null to cancel
	 */
	protected ITagOperation configureOperation() {
		IPreferenceStore store = CVSUIPlugin.getPlugin().getPreferenceStore();
		ITagOperation operation = createTagOperation();
		if (operation.isEmpty()) {
		    return null;
		}
		if (!performPrompting(operation)) {
			return null;
		}
		TagAsVersionDialog dialog = new TagAsVersionDialog(getShell(),
											CVSUIMessages.TagAction_tagResources, 
											operation);
		if (dialog.open() != Window.OK) return null;

		// The user has indicated they want to force a move.  Make sure they really do.		
		if (dialog.shouldMoveTag() && store.getBoolean(ICVSUIConstants.PREF_CONFIRM_MOVE_TAG))  {
			MessageDialogWithToggle confirmDialog = MessageDialogWithToggle.openYesNoQuestion(getShell(), 
				CVSUIMessages.TagAction_moveTagConfirmTitle,  
				NLS.bind(CVSUIMessages.TagAction_moveTagConfirmMessage, new String[] { dialog.getTagName() }), 
				null,
				false,
				null,
				null);
			
			if (confirmDialog.getReturnCode() == IDialogConstants.YES_ID)  {
				store.setValue(ICVSUIConstants.PREF_CONFIRM_MOVE_TAG, !confirmDialog.getToggleState());
			} else  {
				return null;
			}
		}
		
		// The user is a cowboy and wants to do it.
		return dialog.getOperation();
	}
	
	protected abstract ITagOperation createTagOperation();

	@Override
	protected String getErrorTitle() {
		return CVSUIMessages.TagAction_tagErrorTitle; 
	}
	
	@Override
	protected String getWarningTitle() {
		return CVSUIMessages.TagAction_tagWarningTitle; 
	}
	
	@Override
	protected boolean isEnabledForAddedResources() {
		return false;
	}

	public boolean wasCancelled() {
		return wasCancelled;
	}

	public void setWasCancelled(boolean b) {
		wasCancelled = b;
	}

	public static void broadcastTagChange(final ICVSResource[] resources, final CVSTag tag) throws InvocationTargetException, InterruptedException {
		final RepositoryManager manager = CVSUIPlugin.getPlugin().getRepositoryManager();
		manager.run(new IRunnableWithProgress() {
			@Override
			public void run(IProgressMonitor monitor) {
				try {
					for (int i = 0; i < resources.length; i++) {
						ICVSResource resource = resources[i];
						// Cache the new tag creation even if the tag may have had warnings.
						manager.addTags(getRootParent(resource), new CVSTag[] {tag});
					}
				} catch (CVSException e) {
					CVSUIPlugin.log(e);
				}
			}
			private ICVSResource getRootParent(ICVSResource resource) throws CVSException {
				if (!resource.isManaged()) return resource;
				ICVSFolder parent = resource.getParent();
				if (parent == null) return resource;
				// Special check for a parent which is the repository itself
				if (parent.getName().length() == 0) return resource;
				return getRootParent(parent);
			}
		}, new NullProgressMonitor());
	}
}

Back to the top