KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > packageview > MultiElementSelection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.packageview;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
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 /**
22  * A special structured selection that carries additional information
23  * about parent information since the package explorer show elements
24  * multiple times
25  */

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 JavaDoc 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 JavaDoc element) {
44         Object JavaDoc 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 JavaDoc) {
50             List JavaDoc l= (List JavaDoc)value;
51             return (TreePath[])l.toArray(new TreePath[l.size()]);
52         } else {
53             Assert.isTrue(false, "Should not happen"); //$NON-NLS-1$
54
return null;
55         }
56     }
57
58     public boolean equals(Object JavaDoc 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 JavaDoc key= path.getLastSegment();
78             if (key != null) {
79                 Object JavaDoc value= result.get(key);
80                 if (value == null) {
81                     result.put(key, path);
82                 } else if (value instanceof TreePath) {
83                     List JavaDoc l= new ArrayList JavaDoc();
84                     l.add(value);
85                     l.add(path);
86                     result.put(key, l);
87                 } else if (value instanceof List JavaDoc) {
88                     ((List JavaDoc)value).add(path);
89                 } else {
90                     Assert.isTrue(false, "Should not happen"); //$NON-NLS-1$
91
}
92             }
93         }
94         return result;
95     }
96 }
97
Popular Tags