1 11 package org.eclipse.jface.viewers; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.Assert; 17 18 27 public class TreeSelection extends StructuredSelection implements ITreeSelection { 28 29 35 36 private TreePath[] paths = null; 37 private CustomHashtable element2TreePaths = null; 38 39 43 public static final TreeSelection EMPTY = new TreeSelection(); 44 45 private static final TreePath[] EMPTY_TREE_PATHS= new TreePath[0]; 46 47 private static class InitializeData { 48 List selection; 49 TreePath[] paths; 50 CustomHashtable element2TreePaths; 51 52 private InitializeData(TreePath[] paths, IElementComparer comparer) { 53 this.paths= new TreePath[paths.length]; 54 System.arraycopy(paths, 0, this.paths, 0, paths.length); 55 element2TreePaths = new CustomHashtable(comparer); 56 int size = paths.length; 57 selection = new ArrayList (size); 58 for (int i = 0; i < size; i++) { 59 Object lastSegment= paths[i].getLastSegment(); 60 Object mapped= element2TreePaths.get(lastSegment); 61 if (mapped == null) { 62 selection.add(lastSegment); 63 element2TreePaths.put(lastSegment, paths[i]); 64 } else if (mapped instanceof List ) { 65 ((List )mapped).add(paths[i]); 66 } else { 67 List newMapped= new ArrayList (2); 68 newMapped.add(mapped); 69 newMapped.add(paths[i]); 70 element2TreePaths.put(lastSegment, newMapped); 71 } 72 } 73 } 74 } 75 76 83 public TreeSelection(TreePath[] paths) { 84 this(new InitializeData(paths, null)); 85 } 86 87 96 public TreeSelection(TreePath[] paths, IElementComparer comparer) { 97 this(new InitializeData(paths, comparer)); 98 } 99 100 107 public TreeSelection(TreePath treePath) { 108 this(treePath != null ? new TreePath[] { treePath } : EMPTY_TREE_PATHS, null); 109 } 110 111 120 public TreeSelection(TreePath treePath, IElementComparer comparer) { 121 this(treePath != null ? new TreePath[] { treePath } : EMPTY_TREE_PATHS, comparer); 122 } 123 124 129 private TreeSelection(InitializeData data) { 130 super(data.selection); 131 paths= data.paths; 132 element2TreePaths= data.element2TreePaths; 133 } 134 135 144 public TreeSelection() { 145 super(); 146 } 147 148 157 public IElementComparer getElementComparer() { 158 if (element2TreePaths == null) 159 return null; 160 return element2TreePaths.getComparer(); 161 } 162 163 public boolean equals(Object obj) { 164 if (!(obj instanceof TreeSelection)) { 165 return super.equals(obj); 167 } 168 TreeSelection selection = (TreeSelection) obj; 169 int size = getPaths().length; 170 if (selection.getPaths().length == size) { 171 IElementComparer comparerOrNull = (getElementComparer() == selection 172 .getElementComparer()) ? getElementComparer() : null; 173 if (size > 0) { 174 for (int i = 0; i < paths.length; i++) { 175 if (!paths[i].equals(selection.paths[i], comparerOrNull)) { 176 return false; 177 } 178 } 179 } 180 return true; 181 } 182 return false; 183 } 184 185 public int hashCode() { 186 int code = getClass().hashCode(); 187 if (paths != null) { 188 for (int i = 0; i < paths.length; i++) { 189 code = code * 17 + paths[i].hashCode(getElementComparer()); 190 } 191 } 192 return code; 193 } 194 195 public TreePath[] getPaths() { 196 return paths==null ? EMPTY_TREE_PATHS : (TreePath[]) paths.clone(); 197 } 198 199 public TreePath[] getPathsFor(Object element) { 200 Object value= element2TreePaths==null ? null : element2TreePaths.get(element); 201 if (value == null) { 202 return EMPTY_TREE_PATHS; 203 } else if (value instanceof TreePath) { 204 return new TreePath[] { (TreePath)value }; 205 } else if (value instanceof List ) { 206 List l= (List )value; 207 return (TreePath[])l.toArray(new TreePath[l.size()]); 208 } else { 209 Assert.isTrue(false, "Unhandled case"); return null; 212 } 213 } 214 } 215 | Popular Tags |