1 11 package org.eclipse.team.internal.ui.synchronize.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Iterator ; 17 import java.util.List ; 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 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."); } else { 50 Utils.initAction(this, "action.removePage."); } 52 } 53 54 public void run() { 55 try { 56 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() { 57 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 58 if (removeAll) { 59 removeAll(); 60 } else { 61 removeCurrent(); 62 } 63 } 64 }); 65 } catch (InvocationTargetException e) { 66 Utils.handle(e); 67 } catch (InterruptedException e) { 68 } 70 } 71 72 private void removeCurrent() { 73 final ISynchronizeParticipant participant = view.getParticipant(); 74 if (participant != null) { 75 final List dirtyModels = getDirtyModels(new ISynchronizeParticipant[] { participant }); 76 if (participant.isPinned() || !dirtyModels.isEmpty()) { 77 final boolean[] keepGoing = new boolean[] { false }; 78 Display.getDefault().syncExec(new Runnable () { 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 removals = new ArrayList (); 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 } 113 } 114 ISynchronizeParticipant[] toRemove = (ISynchronizeParticipant[]) removals.toArray(new ISynchronizeParticipant[removals.size()]); 115 final List dirtyModels = getDirtyModels(toRemove); 116 if (!dirtyModels.isEmpty()) { 117 final boolean[] keepGoing = new boolean[] { false }; 118 Display.getDefault().syncExec(new Runnable () { 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 dirtyModels) { 133 if (dirtyModels.size() == 1) { 134 Saveable model = (Saveable) dirtyModels.get(0); 135 String message = NLS.bind(TeamUIMessages.RemoveSynchronizeParticipantAction_2, model.getName()); 136 String [] buttons = new String [] { 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 switch (choice) { 148 case 0: break; 150 case 1: return true; 152 default: 153 case 2: 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 if (result == IDialogConstants.CANCEL_ID) 167 return false; 168 169 dirtyModels = Arrays.asList(dlg.getResult()); 170 } 171 172 if (dirtyModels.isEmpty()) 174 return true; 175 176 final List finalModels = dirtyModels; 178 IRunnableWithProgress progressOp = new IRunnableWithProgress() { 179 public void run(IProgressMonitor monitor) { 180 monitor.beginTask(null, finalModels.size()); 181 for (Iterator 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 e) { 199 Utils.handleError(view.getSite().getShell(), e, null, null); 200 return false; 201 } catch (InterruptedException e) { 202 } 204 return true; 206 } 207 208 private List getDirtyModels(ISynchronizeParticipant[] participants) { 209 List result = new ArrayList (); 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 |