KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > actions > TagAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.NullProgressMonitor;
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.window.Window;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.team.internal.ccvs.core.CVSException;
25 import org.eclipse.team.internal.ccvs.core.CVSTag;
26 import org.eclipse.team.internal.ccvs.core.ICVSFolder;
27 import org.eclipse.team.internal.ccvs.core.ICVSResource;
28 import org.eclipse.team.internal.ccvs.ui.*;
29 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
30 import org.eclipse.team.internal.ccvs.ui.ICVSUIConstants;
31 import org.eclipse.team.internal.ccvs.ui.operations.ITagOperation;
32 import org.eclipse.team.internal.ccvs.ui.repo.RepositoryManager;
33 import org.eclipse.team.internal.ccvs.ui.tags.TagAsVersionDialog;
34
35 /**
36  * TagAction tags the selected resources with a version tag specified by the user.
37  */

38 public abstract class TagAction extends WorkspaceTraversalAction {
39     
40     // remember if the execute action was cancelled
41
private boolean wasCancelled = false;
42
43     /**
44      * @see CVSAction#execute(IAction)
45      */

46     public void execute(IAction action) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
47         setWasCancelled(false);
48         
49         // Prompt for the tag name
50
final ITagOperation[] result = new ITagOperation[1];
51         getShell().getDisplay().syncExec(new Runnable JavaDoc() {
52             public void run() {
53                 result[0] = configureOperation();
54                 if (result[0] == null) {
55                     return;
56                 }
57             }});
58         
59         if (result[0] == null) {
60             setWasCancelled(true);
61             return;
62         }
63         
64         result[0].run();
65     }
66     
67     protected boolean performPrompting(ITagOperation operation) {
68         return true;
69     }
70     
71     /**
72      * Prompts the user for a tag name.
73      * Note: This method is designed to be overridden by test cases.
74      * @return the operation, or null to cancel
75      */

76     protected ITagOperation configureOperation() {
77         IPreferenceStore store = CVSUIPlugin.getPlugin().getPreferenceStore();
78         ITagOperation operation = createTagOperation();
79         if (operation.isEmpty()) {
80             return null;
81         }
82         if (!performPrompting(operation)) {
83             return null;
84         }
85         TagAsVersionDialog dialog = new TagAsVersionDialog(getShell(),
86                                             CVSUIMessages.TagAction_tagResources,
87                                             operation);
88         if (dialog.open() != Window.OK) return null;
89
90         // The user has indicated they want to force a move. Make sure they really do.
91
if (dialog.shouldMoveTag() && store.getBoolean(ICVSUIConstants.PREF_CONFIRM_MOVE_TAG)) {
92             MessageDialogWithToggle confirmDialog = MessageDialogWithToggle.openYesNoQuestion(getShell(),
93                 CVSUIMessages.TagAction_moveTagConfirmTitle,
94                 NLS.bind(CVSUIMessages.TagAction_moveTagConfirmMessage, new String JavaDoc[] { dialog.getTagName() }),
95                 null,
96                 false,
97                 null,
98                 null);
99             
100             if (confirmDialog.getReturnCode() == IDialogConstants.YES_ID) {
101                 store.setValue(ICVSUIConstants.PREF_CONFIRM_MOVE_TAG, !confirmDialog.getToggleState());
102             } else {
103                 return null;
104             }
105         }
106         
107         // The user is a cowboy and wants to do it.
108
return dialog.getOperation();
109     }
110     
111     protected abstract ITagOperation createTagOperation();
112
113     protected String JavaDoc getErrorTitle() {
114         return CVSUIMessages.TagAction_tagErrorTitle;
115     }
116     
117     protected String JavaDoc getWarningTitle() {
118         return CVSUIMessages.TagAction_tagWarningTitle;
119     }
120     
121     /**
122      * @see org.eclipse.team.internal.ccvs.ui.actions.WorkspaceAction#isEnabledForAddedResources()
123      */

124     protected boolean isEnabledForAddedResources() {
125         return false;
126     }
127
128     public boolean wasCancelled() {
129         return wasCancelled;
130     }
131
132     public void setWasCancelled(boolean b) {
133         wasCancelled = b;
134     }
135
136     public static void broadcastTagChange(final ICVSResource[] resources, final CVSTag tag) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
137         final RepositoryManager manager = CVSUIPlugin.getPlugin().getRepositoryManager();
138         manager.run(new IRunnableWithProgress() {
139             public void run(IProgressMonitor monitor) {
140                 try {
141                     for (int i = 0; i < resources.length; i++) {
142                         ICVSResource resource = resources[i];
143                         // Cache the new tag creation even if the tag may have had warnings.
144
manager.addTags(getRootParent(resource), new CVSTag[] {tag});
145                     }
146                 } catch (CVSException e) {
147                     CVSUIPlugin.log(e);
148                 }
149             }
150             private ICVSResource getRootParent(ICVSResource resource) throws CVSException {
151                 if (!resource.isManaged()) return resource;
152                 ICVSFolder parent = resource.getParent();
153                 if (parent == null) return resource;
154                 // Special check for a parent which is the repository itself
155
if (parent.getName().length() == 0) return resource;
156                 return getRootParent(parent);
157             }
158         }, new NullProgressMonitor());
159     }
160 }
161
162
Popular Tags