KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > repo > RemoveRootAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.repo;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.jobs.ISchedulingRule;
21 import org.eclipse.core.runtime.jobs.Job;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.operation.IRunnableWithProgress;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.osgi.util.NLS;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.team.core.RepositoryProvider;
29 import org.eclipse.team.internal.ccvs.core.*;
30 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
31 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
32 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
33 import org.eclipse.team.internal.ccvs.ui.model.RepositoryLocationSchedulingRule;
34 import org.eclipse.team.internal.ui.actions.TeamAction;
35 import org.eclipse.team.internal.ui.dialogs.DetailsDialogWithProjects;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.actions.SelectionListenerAction;
38 import org.eclipse.ui.progress.IProgressService;
39
40
41 /**
42  * RemoveRootAction removes a repository
43  */

44 public class RemoveRootAction extends SelectionListenerAction {
45     private IStructuredSelection selection;
46     /* internal use only */ Shell shell;
47     private RepositoriesView view;
48     
49     public RemoveRootAction(Shell shell, RepositoriesView view) {
50         super(CVSUIMessages.RemoteRootAction_label);
51         this.view = view;
52         this.shell = shell;
53     }
54     
55     /**
56      * Returns the selected remote files
57      */

58     protected ICVSRepositoryLocation[] getSelectedRemoteRoots() {
59         ArrayList JavaDoc resources = null;
60         if (selection!=null && !selection.isEmpty()) {
61             resources = new ArrayList JavaDoc();
62             Iterator JavaDoc elements = selection.iterator();
63             while (elements.hasNext()) {
64                 Object JavaDoc next = TeamAction.getAdapter(elements.next(), RepositoryRoot.class);
65                 if (next instanceof RepositoryRoot) {
66                     resources.add(((RepositoryRoot)next).getRoot());
67                 }
68             }
69         }
70         if (resources != null && !resources.isEmpty()) {
71             ICVSRepositoryLocation[] result = new ICVSRepositoryLocation[resources.size()];
72             resources.toArray(result);
73             return result;
74         }
75         return new ICVSRepositoryLocation[0];
76     }
77
78     public void run() {
79         final ICVSRepositoryLocation[] roots = getSelectedRemoteRoots();
80         if (roots.length == 0) return;
81         final boolean[] proceed = new boolean[1];
82         shell.getDisplay().syncExec(new Runnable JavaDoc(){
83             public void run() {
84                 String JavaDoc message;
85                 if(roots.length == 1){
86                     message = NLS.bind(CVSUIMessages.RemoveRootAction_RepositoryRemovalDialogMessageSingle, roots[0].getLocation(true));
87                 } else {
88                     message = NLS.bind(CVSUIMessages.RemoveRootAction_RepositoryRemovalDialogMessageMultiple, new Integer JavaDoc(roots.length));
89                 }
90                 proceed[0] = MessageDialog.openQuestion(shell,
91                         CVSUIMessages.RemoveRootAction_RepositoryRemovalDialogTitle,
92                         message);
93             }
94         });
95         if(!proceed[0]){
96             return;
97         }
98         for (int i = 0; i < roots.length; i++) {
99             final ICVSRepositoryLocation root = roots[i];
100             try {
101                 // Check if any projects are shared with the repository
102
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
103                 final ArrayList JavaDoc shared = new ArrayList JavaDoc();
104                 for (int j = 0; j < projects.length; j++) {
105                     RepositoryProvider teamProvider = RepositoryProvider.getProvider(projects[j], CVSProviderPlugin.getTypeId());
106                     if (teamProvider!=null) {
107                         CVSTeamProvider cvsProvider = (CVSTeamProvider)teamProvider;
108                         if (cvsProvider.getCVSWorkspaceRoot().getRemoteLocation().equals(roots[i])) {
109                             shared.add(projects[j]);
110                         }
111                     }
112                 }
113             
114                 // This will notify the RepositoryManager of the removal
115
if (!shared.isEmpty()) {
116                     final String JavaDoc location = roots[i].getLocation(true);
117                     shell.getDisplay().syncExec(new Runnable JavaDoc() {
118                         public void run() {
119                             DetailsDialogWithProjects dialog = new DetailsDialogWithProjects(
120                                 shell,
121                                 CVSUIMessages.RemoteRootAction_Unable_to_Discard_Location_1,
122                                 NLS.bind(CVSUIMessages.RemoteRootAction_Projects_in_the_local_workspace_are_shared_with__2, new String JavaDoc[] { location }),
123                                 CVSUIMessages.RemoteRootAction_The_projects_that_are_shared_with_the_above_repository_are__4,
124                                 (IProject[]) shared.toArray(new IProject[shared.size()]),
125                                 false,
126                                 Dialog.DLG_IMG_ERROR);
127                             dialog.open();
128                         }
129                     });
130                 } else {
131                     IProgressService manager = PlatformUI.getWorkbench().getProgressService();
132                     try {
133                         manager.busyCursorWhile(new IRunnableWithProgress() {
134                             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
135                                 final ISchedulingRule rule = new RepositoryLocationSchedulingRule(root);
136                                 try {
137                                     Job.getJobManager().beginRule(rule, monitor);
138                                     view.getContentProvider().cancelJobs(root);
139                                     KnownRepositories.getInstance().disposeRepository(root);
140                                 } finally {
141                                     Job.getJobManager().endRule(rule);
142                                 }
143
144                             }
145                         });
146                     } catch (InvocationTargetException JavaDoc e) {
147                         throw CVSException.wrapException(e);
148                     } catch (InterruptedException JavaDoc e) {
149                         // Canceled
150
return;
151                     }
152                 }
153             } catch (CVSException e) {
154                 CVSUIPlugin.openError(view.getShell(), null, null, e, CVSUIPlugin.PERFORM_SYNC_EXEC | CVSUIPlugin.LOG_TEAM_EXCEPTIONS | CVSUIPlugin.LOG_NONTEAM_EXCEPTIONS);
155             }
156         }
157     }
158
159     protected boolean updateSelection(IStructuredSelection selection) {
160         this.selection = selection;
161         ICVSRepositoryLocation[] roots = getSelectedRemoteRoots();
162         boolean b = roots.length > 0;
163         setEnabled(b);
164         return b;
165     }
166
167 }
168
169
Popular Tags