1 11 12 package org.eclipse.ui.actions; 13 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.resources.IResource; 20 import org.eclipse.core.resources.mapping.ResourceMapping; 21 import org.eclipse.core.resources.mapping.ResourceMappingContext; 22 import org.eclipse.core.resources.mapping.ResourceTraversal; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IAdaptable; 25 import org.eclipse.core.runtime.IAdapterManager; 26 import org.eclipse.core.runtime.NullProgressMonitor; 27 import org.eclipse.core.runtime.Platform; 28 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 29 30 57 public abstract class SelectionListenerAction extends 58 BaseSelectionListenerAction { 59 62 private static final List EMPTY_LIST = Arrays.asList(new Object [0]); 63 64 68 private boolean selectionDirty = true; 69 70 75 private List resources; 76 77 82 private List nonResources; 83 84 91 protected SelectionListenerAction(String text) { 92 super(text); 93 } 94 95 100 protected void clearCache() { 101 selectionDirty = true; 102 resources = null; 105 nonResources = null; 106 } 107 108 112 private final void computeResources() { 113 resources = null; 114 nonResources = null; 115 116 for (Iterator e = getStructuredSelection().iterator(); e.hasNext();) { 117 Object next = e.next(); 118 if (next instanceof IResource) { 119 if (resources == null) { 120 resources = new ArrayList (getStructuredSelection().size()); 122 } 123 resources.add(next); 124 continue; 125 } else if (next instanceof IAdaptable) { 126 Object resource = ((IAdaptable) next) 127 .getAdapter(IResource.class); 128 if (resource != null) { 129 if (resources == null) { 130 resources = new ArrayList (getStructuredSelection() 132 .size()); 133 } 134 resources.add(resource); 135 continue; 136 } 137 } else { 138 139 boolean resourcesFoundForThisSelection = false; 140 141 IAdapterManager adapterManager = Platform.getAdapterManager(); 142 ResourceMapping mapping = (ResourceMapping) adapterManager 143 .getAdapter(next, ResourceMapping.class); 144 145 if (mapping != null) { 146 147 ResourceTraversal[] traversals = null; 148 try { 149 traversals = mapping.getTraversals( 150 ResourceMappingContext.LOCAL_CONTEXT, 151 new NullProgressMonitor()); 152 } catch (CoreException exception) { 153 IDEWorkbenchPlugin.log(exception.getLocalizedMessage(), 154 exception.getStatus()); 155 } 156 157 if (traversals != null) { 158 159 for (int i = 0; i < traversals.length; i++) { 160 161 IResource[] traversalResources = traversals[i] 162 .getResources(); 163 164 if (traversalResources != null) { 165 166 resourcesFoundForThisSelection = true; 167 168 if (resources == null) { 169 resources = new ArrayList ( 170 getStructuredSelection().size()); 171 } 172 173 for (int j = 0; j < traversalResources.length; j++) { 174 resources.add(traversalResources[j]); 175 } 177 } 179 } 181 } 183 } 185 if (resourcesFoundForThisSelection) { 186 continue; 187 } 188 } 189 190 if (nonResources == null) { 191 nonResources = new ArrayList (1); 193 } 194 nonResources.add(next); 195 } 196 } 197 198 204 protected List getSelectedNonResources() { 205 if (selectionDirty) { 207 computeResources(); 208 selectionDirty = false; 209 } 210 211 if (nonResources == null) { 212 return EMPTY_LIST; 213 } 214 215 return nonResources; 216 } 217 218 224 protected List getSelectedResources() { 225 if (selectionDirty) { 227 computeResources(); 228 selectionDirty = false; 229 } 230 231 if (resources == null) { 232 return EMPTY_LIST; 233 } 234 return resources; 235 } 236 237 251 protected boolean resourceIsType(IResource resource, int resourceMask) { 252 return (resource.getType() & resourceMask) != 0; 253 } 254 255 269 protected boolean selectionIsOfType(int resourceMask) { 270 if (getSelectedNonResources().size() > 0) { 271 return false; 272 } 273 274 for (Iterator e = getSelectedResources().iterator(); e.hasNext();) { 275 IResource next = (IResource) e.next(); 276 if (!resourceIsType(next, resourceMask)) { 277 return false; 278 } 279 } 280 return true; 281 } 282 283 } 284 | Popular Tags |