1 11 package org.eclipse.team.ui.synchronize; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.compare.structuremergeviewer.IDiffElement; 18 import org.eclipse.jface.viewers.*; 19 import org.eclipse.swt.events.DisposeEvent; 20 import org.eclipse.swt.events.DisposeListener; 21 import org.eclipse.team.core.synchronize.*; 22 import org.eclipse.team.internal.ui.Utils; 23 import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement; 24 import org.eclipse.ui.actions.BaseSelectionListenerAction; 25 import org.eclipse.ui.ide.IDE; 26 27 39 public abstract class SynchronizeModelAction extends BaseSelectionListenerAction { 40 41 private ISynchronizePageConfiguration configuration; 42 43 51 protected SynchronizeModelAction(String text, ISynchronizePageConfiguration configuration) { 52 this(text, configuration, configuration.getSite().getSelectionProvider()); 53 } 54 55 63 protected SynchronizeModelAction(String text, ISynchronizePageConfiguration configuration, ISelectionProvider selectionProvider) { 64 super(text); 65 this.configuration = configuration; 66 initialize(configuration, selectionProvider); 67 } 68 69 77 protected void initialize(final ISynchronizePageConfiguration configuration, final ISelectionProvider selectionProvider) { 78 selectionProvider.addSelectionChangedListener(this); 79 configuration.getPage().getViewer().getControl().addDisposeListener(new DisposeListener() { 80 public void widgetDisposed(DisposeEvent e) { 81 selectionProvider.removeSelectionChangedListener(SynchronizeModelAction.this); 82 } 83 }); 84 } 85 86 89 public void run() { 90 if(needsToSaveDirtyEditors()) { 91 if(!saveAllEditors(confirmSaveOfDirtyEditor())) { 92 return; 93 } 94 } 95 try { 96 runOperation(); 97 } catch (InvocationTargetException e) { 98 handle(e); 99 } catch (InterruptedException e) { 100 handle(e); 101 } 102 } 103 104 113 protected void runOperation() throws InvocationTargetException , InterruptedException { 114 getSubscriberOperation(configuration, getFilteredDiffElements()).run(); 115 } 116 117 123 protected boolean needsToSaveDirtyEditors() { 124 return true; 125 } 126 127 133 protected boolean confirmSaveOfDirtyEditor() { 134 return true; 135 } 136 137 150 protected abstract SynchronizeModelOperation getSubscriberOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements); 151 152 158 protected void handle(Exception e) { 159 Utils.handle(e); 160 } 161 162 165 protected boolean updateSelection(IStructuredSelection selection) { 166 super.updateSelection(selection); 167 return isEnabledForSelection(selection); 168 } 169 170 private boolean isEnabledForSelection(IStructuredSelection selection) { 171 return Utils.hasMatchingDescendant(selection, getSyncInfoFilter()); 172 } 173 174 180 protected final IDiffElement[] getSelectedDiffElements() { 181 return Utils.getDiffNodes(getStructuredSelection().toArray()); 182 } 183 184 191 protected FastSyncInfoFilter getSyncInfoFilter() { 192 return new FastSyncInfoFilter(); 193 } 194 195 200 protected final IDiffElement[] getFilteredDiffElements() { 201 IDiffElement[] elements = getSelectedDiffElements(); 202 List filtered = new ArrayList (); 203 for (int i = 0; i < elements.length; i++) { 204 IDiffElement e = elements[i]; 205 if (e instanceof SyncInfoModelElement) { 206 SyncInfo info = ((SyncInfoModelElement) e).getSyncInfo(); 207 if (info != null && getSyncInfoFilter().select(info)) { 208 filtered.add(e); 209 } 210 } 211 } 212 return (IDiffElement[]) filtered.toArray(new IDiffElement[filtered.size()]); 213 } 214 215 220 public void selectionChanged(ISelection selection) { 221 if (selection instanceof IStructuredSelection) { 222 super.selectionChanged((IStructuredSelection)selection); 223 } else { 224 super.selectionChanged(StructuredSelection.EMPTY); 225 } 226 227 } 228 229 234 public ISynchronizePageConfiguration getConfiguration() { 235 return configuration; 236 } 237 238 247 public final boolean saveAllEditors(boolean confirm) { 248 return IDE.saveAllEditors(Utils.getResources(getFilteredDiffElements()), confirm); 249 } 250 } 251 | Popular Tags |