1 11 package org.eclipse.team.internal.ccvs.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collections ; 16 import java.util.Comparator ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.WeakHashMap ; 22 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.team.internal.ccvs.core.ICVSResource; 27 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 28 import org.eclipse.team.internal.ui.Utils; 29 30 33 public class CVSActionSelectionProperties { 34 35 private static Map selectionProperties = new WeakHashMap (); 37 38 private Object [] selection; 39 private Map properties = new HashMap (); 40 41 private static final String SELECTED_RESOURCES = "selectedResources"; private static final String NONOVERLAPPING_SELECTED_RESOURCES = "nonoverlappingSelectedResources"; private static final String CVS_RESOURCE_MAP = "cvsResourceMap"; 45 public static CVSActionSelectionProperties getProperties(IStructuredSelection selection) { 46 if (selection == null) return null; 47 CVSActionSelectionProperties props = (CVSActionSelectionProperties)selectionProperties.get(selection); 48 if (props == null) { 49 props = new CVSActionSelectionProperties(selection); 50 selectionProperties.put(selection, props); 51 } 52 return props; 53 } 54 55 public CVSActionSelectionProperties(IStructuredSelection selection) { 56 this.selection = selection.toArray(); 58 } 59 60 public void put(String key, Object value) { 61 properties.put(key, value); 62 } 63 64 public Object get(String key) { 65 return properties.get(key); 66 } 67 68 public IResource[] getAllSelectedResources() { 69 IResource[] resources = (IResource[])get(SELECTED_RESOURCES); 70 if (resources == null) { 71 resources = getResources(selection); 72 put(SELECTED_RESOURCES, resources); 73 } 74 return resources; 75 } 76 77 83 private IResource[] getResources(Object [] objects) { 84 return Utils.getContributedResources(objects); 85 } 86 87 public IResource[] getNonoverlappingSelectedResources() { 88 IResource[] resources = (IResource[])get(NONOVERLAPPING_SELECTED_RESOURCES); 89 if (resources == null) { 90 resources = getNonOverlapping(getAllSelectedResources()); 91 put (NONOVERLAPPING_SELECTED_RESOURCES, resources); 92 } 93 return resources; 94 } 95 96 public ICVSResource getCVSResourceFor(IResource resource) { 97 Map map = (Map )get(CVS_RESOURCE_MAP); 98 if (map == null) { 99 map = new HashMap (); 100 put(CVS_RESOURCE_MAP, map); 101 } 102 ICVSResource cvsResource = (ICVSResource)map.get(resource); 103 if (cvsResource == null) { 104 cvsResource = CVSWorkspaceRoot.getCVSResourceFor(resource); 105 map.put(resource, cvsResource); 106 } 107 return cvsResource; 108 } 109 110 115 public static IResource[] getNonOverlapping(IResource[] resources) { 116 if (resources == null || resources.length == 0 || resources.length == 1) { 117 return resources; 118 } 119 List sorted = new ArrayList (); 121 sorted.addAll(Arrays.asList(resources)); 122 Collections.sort(sorted, new Comparator () { 123 public int compare(Object arg0, Object arg1) { 124 IResource resource0 = (IResource) arg0; 125 IResource resource1 = (IResource) arg1; 126 return resource0.getFullPath().segmentCount() - resource1.getFullPath().segmentCount(); 127 } 128 public boolean equals(Object arg0) { 129 return false; 130 } 131 }); 132 List coveredPaths = new ArrayList (); 134 for (Iterator iter = sorted.iterator(); iter.hasNext();) { 135 IResource resource = (IResource) iter.next(); 136 IPath resourceFullPath = resource.getFullPath(); 137 boolean covered = false; 138 for (Iterator it = coveredPaths.iterator(); it.hasNext();) { 139 IPath path = (IPath) it.next(); 140 if(path.isPrefixOf(resourceFullPath)) { 141 covered = true; 142 } 143 } 144 if (covered) { 145 iter.remove(); 147 } else { 148 if (resource.getType() == IResource.FOLDER) { 150 coveredPaths.add(resource.getFullPath()); 151 } 152 } 153 } 154 return (IResource[]) sorted.toArray(new IResource[sorted.size()]); 155 } 156 157 } 158 | Popular Tags |