1 11 package org.eclipse.team.internal.ui.mapping; 12 13 import java.util.HashSet ; 14 import java.util.Set ; 15 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.mapping.*; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.jface.util.IPropertyChangeListener; 20 import org.eclipse.jface.util.PropertyChangeEvent; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.jface.viewers.StructuredViewer; 23 import org.eclipse.team.core.diff.*; 24 import org.eclipse.team.core.mapping.ISynchronizationContext; 25 import org.eclipse.team.internal.ui.TeamUIPlugin; 26 import org.eclipse.team.internal.ui.Utils; 27 import org.eclipse.team.ui.mapping.ITeamContentProviderManager; 28 import org.eclipse.team.ui.mapping.MergeActionHandler; 29 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 30 import org.eclipse.ui.ide.IDE; 31 32 public abstract class ResourceMergeActionHandler extends MergeActionHandler implements IDiffChangeListener, IPropertyChangeListener { 33 34 public ResourceMergeActionHandler(ISynchronizePageConfiguration configuration) { 35 super(configuration); 36 getSynchronizationContext().getDiffTree().addDiffChangeListener(this); 37 configuration.addPropertyChangeListener(this); 38 } 39 40 46 public final boolean saveDirtyEditors() { 47 if(needsToSaveDirtyEditors()) { 48 if(!saveAllEditors(getTargetResources(), confirmSaveOfDirtyEditor())) { 49 return false; 50 } 51 } 52 return true; 53 } 54 55 private IResource[] getTargetResources() { 56 IStructuredSelection selection = getStructuredSelection(); 57 Object [] objects = selection.toArray(); 58 Set roots = new HashSet (); 59 for (int i = 0; i < objects.length; i++) { 60 Object object = objects[i]; 61 ResourceMapping mapping = Utils.getResourceMapping(object); 62 if (mapping != null) { 63 try { 64 ResourceTraversal[] traversals = mapping.getTraversals(ResourceMappingContext.LOCAL_CONTEXT, null); 65 for (int j = 0; j < traversals.length; j++) { 66 ResourceTraversal traversal = traversals[j]; 67 IResource[] resources = traversal.getResources(); 68 for (int k = 0; k < resources.length; k++) { 69 IResource resource = resources[k]; 70 roots.add(resource); 71 } 72 } 73 } catch (CoreException e) { 74 TeamUIPlugin.log(e); 75 } 76 } 77 } 78 return (IResource[]) roots.toArray(new IResource[roots.size()]); 79 } 80 81 90 public final boolean saveAllEditors(IResource[] resources, boolean confirm) { 91 return IDE.saveAllEditors(resources, confirm); 92 } 93 94 100 protected boolean needsToSaveDirtyEditors() { 101 return true; 102 } 103 104 110 protected boolean confirmSaveOfDirtyEditor() { 111 return true; 112 } 113 114 protected ISynchronizationContext getSynchronizationContext() { 115 return (ISynchronizationContext)getConfiguration().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT); 116 } 117 118 public void propertyChanged(IDiffTree tree, int property, IPath[] paths) { 119 } 121 122 public void diffsChanged(IDiffChangeEvent event, IProgressMonitor monitor) { 123 Utils.syncExec(new Runnable () { 124 public void run() { 125 updateEnablement(getStructuredSelection()); 126 } 127 }, (StructuredViewer)getConfiguration().getPage().getViewer()); 128 129 } 130 131 public void propertyChange(PropertyChangeEvent event) { 132 if (event.getProperty() == ISynchronizePageConfiguration.P_MODE) { 133 Utils.syncExec(new Runnable () { 134 public void run() { 135 updateEnablement(getStructuredSelection()); 136 } 137 }, (StructuredViewer)getConfiguration().getPage().getViewer()); 138 } 139 } 140 141 } 142 | Popular Tags |