1 11 package org.eclipse.team.internal.ccvs.ui.mappings; 12 13 import java.util.*; 14 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.mapping.*; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.jface.viewers.IStructuredSelection; 19 import org.eclipse.team.internal.ccvs.ui.Policy; 20 import org.eclipse.team.internal.ui.TeamUIPlugin; 21 import org.eclipse.team.internal.ui.Utils; 22 import org.eclipse.team.internal.ui.mapping.ResourceModelParticipantAction; 23 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 24 import org.eclipse.ui.ide.IDE; 25 26 public abstract class CVSModelProviderAction extends ResourceModelParticipantAction { 27 28 public CVSModelProviderAction(ISynchronizePageConfiguration configuration) { 29 super(null, configuration); 30 Utils.initAction(this, getBundleKeyPrefix(), Policy.getActionBundle()); 31 } 32 33 38 protected String getBundleKeyPrefix() { 39 return getClass().getName() + "."; } 41 42 protected ResourceMapping[] getResourceMappings(IStructuredSelection selection) { 43 List mappings = new ArrayList(); 44 for (Iterator iter = selection.iterator(); iter.hasNext();) { 45 Object element = (Object ) iter.next(); 46 ResourceMapping mapping = Utils.getResourceMapping(element); 47 if (mapping != null) 48 mappings.add(mapping); 49 } 50 return (ResourceMapping[]) mappings.toArray(new ResourceMapping[mappings.size()]); 51 } 52 53 59 public final boolean saveDirtyEditors() { 60 if(needsToSaveDirtyEditors()) { 61 if(!saveAllEditors(getTargetResources(), confirmSaveOfDirtyEditor())) { 62 return false; 63 } 64 } 65 return true; 66 } 67 68 protected IResource[] getTargetResources() { 69 IStructuredSelection selection = getStructuredSelection(); 70 Object [] objects = selection.toArray(); 71 Set roots = new HashSet(); 72 for (int i = 0; i < objects.length; i++) { 73 Object object = objects[i]; 74 ResourceMapping mapping = Utils.getResourceMapping(object); 75 if (mapping != null) { 76 try { 77 ResourceTraversal[] traversals = mapping.getTraversals(ResourceMappingContext.LOCAL_CONTEXT, null); 78 for (int j = 0; j < traversals.length; j++) { 79 ResourceTraversal traversal = traversals[j]; 80 IResource[] resources = traversal.getResources(); 81 for (int k = 0; k < resources.length; k++) { 82 IResource resource = resources[k]; 83 roots.add(resource); 84 } 85 } 86 } catch (CoreException e) { 87 TeamUIPlugin.log(e); 88 } 89 } 90 } 91 return (IResource[]) roots.toArray(new IResource[roots.size()]); 92 } 93 94 103 public final boolean saveAllEditors(IResource[] resources, boolean confirm) { 104 return IDE.saveAllEditors(resources, confirm); 105 } 106 107 113 protected boolean needsToSaveDirtyEditors() { 114 return true; 115 } 116 117 123 protected boolean confirmSaveOfDirtyEditor() { 124 return true; 125 } 126 127 public void run() { 128 if (saveDirtyEditors()) 129 execute(); 130 } 131 132 protected abstract void execute(); 133 } 134 | Popular Tags |