Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5dece06ba88c2fcd992663b2f7c6e3a63d401972 (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
158
159
160
161
162
163
164
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-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.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.InputDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSTag;
import org.eclipse.team.internal.ccvs.core.ICVSFolder;
import org.eclipse.team.internal.ccvs.core.ICVSResource;
import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
import org.eclipse.team.internal.ccvs.ui.ICVSUIConstants;
import org.eclipse.team.internal.ccvs.ui.MessageDialogWithToggle;
import org.eclipse.team.internal.ccvs.ui.Policy;
import org.eclipse.team.internal.ccvs.ui.TagAsVersionDialog;
import org.eclipse.team.internal.ccvs.ui.operations.ITagOperation;
import org.eclipse.team.internal.ccvs.ui.repo.RepositoryManager;

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

	/**
	 * @see CVSAction#execute(IAction)
	 */
	public void execute(IAction action) throws InvocationTargetException, InterruptedException {
		setWasCancelled(false);
		if (!performPrompting()) {
			setWasCancelled(true);
			return;
		}
		
		// Prompt for the tag name
		final ITagOperation[] result = new ITagOperation[1];
		getShell().getDisplay().syncExec(new Runnable() {
			public void run() {
				result[0] = configureOperation();
				if (result[0] == null)  {
					return;
				}
			}});
		
		if (result[0] == null)  {
			setWasCancelled(true);
			return;
		}
		
		try {
			result[0].run();
		} catch (CVSException e1) {
			throw new InvocationTargetException(e1);
		}
		
		broadcastTagChange(result[0]);
	}
	
	protected boolean performPrompting()  {
		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();
		TagAsVersionDialog dialog = new TagAsVersionDialog(getShell(),
											Policy.bind("TagAction.tagResources"), //$NON-NLS-1$
											operation);
		if (dialog.open() != InputDialog.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.openQuestion(getShell(), 
				Policy.bind("TagAction.moveTagConfirmTitle"),  //$NON-NLS-1$
				Policy.bind("TagAction.moveTagConfirmMessage", dialog.getTagName()), //$NON-NLS-1$
				null,
				false);
			
			if (confirmDialog.getReturnCode() == IDialogConstants.OK_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();

	protected String getErrorTitle() {
		return Policy.bind("TagAction.tagErrorTitle"); //$NON-NLS-1$
	}
	
	protected String getWarningTitle() {
		return Policy.bind("TagAction.tagWarningTitle"); //$NON-NLS-1$
	}
	
	/**
	 * @see org.eclipse.team.internal.ccvs.ui.actions.WorkspaceAction#isEnabledForAddedResources()
	 */
	protected boolean isEnabledForAddedResources() {
		return false;
	}

	public boolean wasCancelled() {
		return wasCancelled;
	}

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

	protected void broadcastTagChange(final ITagOperation operation) throws InvocationTargetException, InterruptedException {
		final RepositoryManager manager = CVSUIPlugin.getPlugin().getRepositoryManager();
		manager.run(new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
				ICVSResource[] resources = operation.getCVSResources();
				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.
					try {
						manager.addTags(getRootParent(resource), new CVSTag[] {operation.getTag()});
					} catch (CVSException e) {
						CVSUIPlugin.log(e);
					}
				}
			}
		}, new NullProgressMonitor());
	}

	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);
	}
}

Back to the top