KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > actions > RemoveSynchronizeParticipantAction


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.ui.synchronize.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.dialogs.*;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.eclipse.jface.viewers.ArrayContentProvider;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.team.core.TeamException;
27 import org.eclipse.team.internal.ui.TeamUIMessages;
28 import org.eclipse.team.internal.ui.Utils;
29 import org.eclipse.team.ui.TeamUI;
30 import org.eclipse.team.ui.synchronize.*;
31 import org.eclipse.ui.*;
32 import org.eclipse.ui.dialogs.ListSelectionDialog;
33 import org.eclipse.ui.model.WorkbenchPartLabelProvider;
34
35 /**
36  * Action to remove the given participant from the synchronize manager.
37  * @since 3.0
38  */

39 public class RemoveSynchronizeParticipantAction extends Action {
40
41     private final ISynchronizeView view;
42     private boolean removeAll;
43
44     public RemoveSynchronizeParticipantAction(ISynchronizeView view, boolean removeAll) {
45         this.view = view;
46         this.removeAll = removeAll;
47         if (removeAll) {
48             Utils.initAction(this, "action.removeAllPage."); //$NON-NLS-1$
49
} else {
50             Utils.initAction(this, "action.removePage."); //$NON-NLS-1$
51
}
52     }
53
54     public void run() {
55         try {
56             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
57                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
58                     if (removeAll) {
59                         removeAll();
60                     } else {
61                         removeCurrent();
62                     }
63                 }
64             });
65         } catch (InvocationTargetException JavaDoc e) {
66             Utils.handle(e);
67         } catch (InterruptedException JavaDoc e) {
68             // Cancelled. Just ignore
69
}
70     }
71
72     private void removeCurrent() {
73         final ISynchronizeParticipant participant = view.getParticipant();
74         if (participant != null) {
75             final List JavaDoc dirtyModels = getDirtyModels(new ISynchronizeParticipant[] { participant });
76             if (participant.isPinned() || !dirtyModels.isEmpty()) {
77                 final boolean[] keepGoing = new boolean[] { false };
78                 Display.getDefault().syncExec(new Runnable JavaDoc() {
79                     public void run() {
80                         if (!dirtyModels.isEmpty()) {
81                             keepGoing[0] = promptToSave(dirtyModels);
82                         } else {
83                             keepGoing[0] = MessageDialog.openQuestion(
84                                     view.getSite().getShell(),
85                                     TeamUIMessages.RemoveSynchronizeParticipantAction_0,
86                                     TeamUIMessages.RemoveSynchronizeParticipantAction_1);
87                         }
88
89                     }
90                 });
91                 if (!keepGoing[0]) {
92                     return;
93                 }
94             }
95             TeamUI.getSynchronizeManager().removeSynchronizeParticipants(new ISynchronizeParticipant[]{participant});
96         }
97     }
98
99     private void removeAll() {
100         ISynchronizeManager manager = TeamUI.getSynchronizeManager();
101         ISynchronizeParticipantReference[] refs = manager.getSynchronizeParticipants();
102         ArrayList JavaDoc removals = new ArrayList JavaDoc();
103         for (int i = 0; i < refs.length; i++) {
104             ISynchronizeParticipantReference reference = refs[i];
105             ISynchronizeParticipant p;
106             try {
107                 p = reference.getParticipant();
108                 if (! p.isPinned())
109                     removals.add(p);
110             } catch (TeamException e) {
111                 // keep going
112
}
113         }
114         ISynchronizeParticipant[] toRemove = (ISynchronizeParticipant[]) removals.toArray(new ISynchronizeParticipant[removals.size()]);
115         final List JavaDoc dirtyModels = getDirtyModels(toRemove);
116         if (!dirtyModels.isEmpty()) {
117             final boolean[] keepGoing = new boolean[] { false };
118             Display.getDefault().syncExec(new Runnable JavaDoc() {
119                 public void run() {
120                     if (!dirtyModels.isEmpty()) {
121                         keepGoing[0] = promptToSave(dirtyModels);
122                     }
123                 }
124             });
125             if (!keepGoing[0]) {
126                 return;
127             }
128         }
129         manager.removeSynchronizeParticipants(toRemove);
130     }
131
132     private boolean promptToSave(List JavaDoc dirtyModels) {
133         if (dirtyModels.size() == 1) {
134             Saveable model = (Saveable) dirtyModels.get(0);
135             String JavaDoc message = NLS.bind(TeamUIMessages.RemoveSynchronizeParticipantAction_2, model.getName());
136             // Show a dialog.
137
String JavaDoc[] buttons = new String JavaDoc[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL };
138             MessageDialog d = new MessageDialog(
139                     view.getSite().getShell(), TeamUIMessages.RemoveSynchronizeParticipantAction_3,
140                 null, message, MessageDialog.QUESTION, buttons, 0);
141             
142             int choice = d.open();
143
144             // Branch on the user choice.
145
// The choice id is based on the order of button labels
146
// above.
147
switch (choice) {
148             case 0: // yes
149
break;
150             case 1: // no
151
return true;
152             default:
153             case 2: // cancel
154
return false;
155             }
156         } else {
157             ListSelectionDialog dlg = new ListSelectionDialog(
158                     view.getSite().getShell(), dirtyModels,
159                     new ArrayContentProvider(),
160                     new WorkbenchPartLabelProvider(), TeamUIMessages.RemoveSynchronizeParticipantAction_4);
161             dlg.setInitialSelections(dirtyModels.toArray());
162             dlg.setTitle(TeamUIMessages.RemoveSynchronizeParticipantAction_5);
163
164             int result = dlg.open();
165             //Just return false to prevent the operation continuing
166
if (result == IDialogConstants.CANCEL_ID)
167                 return false;
168
169             dirtyModels = Arrays.asList(dlg.getResult());
170         }
171
172         // If the editor list is empty return.
173
if (dirtyModels.isEmpty())
174             return true;
175         
176         // Create save block.
177
final List JavaDoc finalModels = dirtyModels;
178         IRunnableWithProgress progressOp = new IRunnableWithProgress() {
179             public void run(IProgressMonitor monitor) {
180                 monitor.beginTask(null, finalModels.size());
181                 for (Iterator JavaDoc i = finalModels.iterator(); i.hasNext();) {
182                     Saveable model = (Saveable) i.next();
183                     if (model.isDirty()) {
184                         try {
185                             model.doSave(new SubProgressMonitor(monitor, 1));
186                         } catch (CoreException e) {
187                             ErrorDialog.openError(view.getSite().getShell(), null, e.getMessage(), e.getStatus());
188                         }
189                     }
190                     if (monitor.isCanceled())
191                         break;
192                 }
193                 monitor.done();
194             }
195         };
196         try {
197             PlatformUI.getWorkbench().getProgressService().run(true, true, progressOp);
198         } catch (InvocationTargetException JavaDoc e) {
199             Utils.handleError(view.getSite().getShell(), e, null, null);
200             return false;
201         } catch (InterruptedException JavaDoc e) {
202             // Ignore
203
}
204         // TODO: How do we handle a cancel during save?
205
return true;
206     }
207     
208     private List JavaDoc getDirtyModels(ISynchronizeParticipant[] participants) {
209         List JavaDoc result = new ArrayList JavaDoc();
210         for (int i = 0; i < participants.length; i++) {
211             ISynchronizeParticipant participant = participants[i];
212             if (participant instanceof ModelSynchronizeParticipant) {
213                 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant;
214                 Saveable s = msp.getActiveSaveable();
215                 if (s != null && s.isDirty())
216                     result.add(s);
217             }
218         }
219         return result;
220     }
221 }
222
Popular Tags