1 11 package org.eclipse.jdt.internal.ui.packageview; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.jface.util.Assert; 17 import org.eclipse.jface.viewers.IElementComparer; 18 import org.eclipse.jface.viewers.StructuredSelection; 19 import org.eclipse.jface.viewers.StructuredViewer; 20 21 26 public class MultiElementSelection extends StructuredSelection { 27 28 private static final TreePath[] EMPTY_TREE_PATHS= new TreePath[0]; 29 30 private TreePath[] fAllTreePaths; 31 private CustomHashtable fElement2TreePaths; 32 33 public MultiElementSelection(StructuredViewer viewer, List elements, TreePath[] treePaths) { 34 super(elements); 35 fAllTreePaths= treePaths; 36 fElement2TreePaths= createTreePathMap(viewer.getComparer()); 37 } 38 39 public TreePath[] getAllTreePaths() { 40 return fAllTreePaths; 41 } 42 43 public TreePath[] getTreePaths(Object element) { 44 Object value= fElement2TreePaths.get(element); 45 if (value == null) { 46 return EMPTY_TREE_PATHS; 47 } else if (value instanceof TreePath) { 48 return new TreePath[] { (TreePath)value }; 49 } else if (value instanceof List ) { 50 List l= (List )value; 51 return (TreePath[])l.toArray(new TreePath[l.size()]); 52 } else { 53 Assert.isTrue(false, "Should not happen"); return null; 55 } 56 } 57 58 public boolean equals(Object o) { 59 if (!super.equals(o)) 60 return false; 61 if (!getClass().getName().equals(o.getClass().getName())) 62 return false; 63 MultiElementSelection otherSelection= (MultiElementSelection)o; 64 if (fAllTreePaths.length != otherSelection.fAllTreePaths.length) 65 return false; 66 for (int i= 0; i < fAllTreePaths.length; i++) { 67 if (!fAllTreePaths[i].equals(otherSelection.fAllTreePaths[i])) 68 return false; 69 } 70 return true; 71 } 72 73 private CustomHashtable createTreePathMap(IElementComparer comparer) { 74 CustomHashtable result= new CustomHashtable(comparer); 75 for (int i= 0; i < fAllTreePaths.length; i++) { 76 TreePath path= fAllTreePaths[i]; 77 Object key= path.getLastSegment(); 78 if (key != null) { 79 Object value= result.get(key); 80 if (value == null) { 81 result.put(key, path); 82 } else if (value instanceof TreePath) { 83 List l= new ArrayList (); 84 l.add(value); 85 l.add(path); 86 result.put(key, l); 87 } else if (value instanceof List ) { 88 ((List )value).add(path); 89 } else { 90 Assert.isTrue(false, "Should not happen"); } 92 } 93 } 94 return result; 95 } 96 } 97 | Popular Tags |