KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > TreeSelection


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.jface.viewers;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18 /**
19  * A concrete implementation of the <code>ITreeSelection</code> interface,
20  * suitable for instantiating.
21  * <p>
22  * This class is not intended to be subclassed.
23  * </p>
24  *
25  * @since 3.2
26  */

27 public class TreeSelection extends StructuredSelection implements ITreeSelection {
28
29     /* Implementation note. This class extends StructuredSelection because many pre-existing
30      * JFace viewer clients assumed that the only implementation of IStructuredSelection
31      * was StructuredSelection. By extending StructuredSelection rather than implementing
32      * ITreeSelection directly, we avoid this problem.
33      * For more details, see Bug 121939 [Viewers] TreeSelection should subclass StructuredSelection.
34      */

35     
36     private TreePath[] paths = null;
37     private CustomHashtable element2TreePaths = null;
38
39     /**
40      * The canonical empty selection. This selection should be used instead of
41      * <code>null</code>.
42      */

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 JavaDoc 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 JavaDoc(size);
58             for (int i = 0; i < size; i++) {
59                 Object JavaDoc lastSegment= paths[i].getLastSegment();
60                 Object JavaDoc mapped= element2TreePaths.get(lastSegment);
61                 if (mapped == null) {
62                     selection.add(lastSegment);
63                     element2TreePaths.put(lastSegment, paths[i]);
64                 } else if (mapped instanceof List JavaDoc) {
65                     ((List JavaDoc)mapped).add(paths[i]);
66                 } else {
67                     List JavaDoc newMapped= new ArrayList JavaDoc(2);
68                     newMapped.add(mapped);
69                     newMapped.add(paths[i]);
70                     element2TreePaths.put(lastSegment, newMapped);
71                 }
72             }
73         }
74     }
75
76     /**
77      * Constructs a selection based on the elements identified by the given tree
78      * paths.
79      *
80      * @param paths
81      * tree paths
82      */

83     public TreeSelection(TreePath[] paths) {
84         this(new InitializeData(paths, null));
85     }
86
87     /**
88      * Constructs a selection based on the elements identified by the given tree
89      * paths.
90      *
91      * @param paths
92      * tree paths
93      * @param comparer
94      * the comparer, or <code>null</code> if default equals is to be used
95      */

96     public TreeSelection(TreePath[] paths, IElementComparer comparer) {
97         this(new InitializeData(paths, comparer));
98     }
99
100     /**
101      * Constructs a selection based on the elements identified by the given tree
102      * path.
103      *
104      * @param treePath
105      * tree path, or <code>null</code> for an empty selection
106      */

107     public TreeSelection(TreePath treePath) {
108         this(treePath != null ? new TreePath[] { treePath } : EMPTY_TREE_PATHS, null);
109     }
110
111     /**
112      * Constructs a selection based on the elements identified by the given tree
113      * path.
114      *
115      * @param treePath
116      * tree path, or <code>null</code> for an empty selection
117      * @param comparer
118      * the comparer, or <code>null</code> if default equals is to be used
119      */

120     public TreeSelection(TreePath treePath, IElementComparer comparer) {
121         this(treePath != null ? new TreePath[] { treePath } : EMPTY_TREE_PATHS, comparer);
122     }
123     
124     /**
125      * Creates a new tree selection based on the initialization data.
126      *
127      * @param data the data
128      */

129     private TreeSelection(InitializeData data) {
130         super(data.selection);
131         paths= data.paths;
132         element2TreePaths= data.element2TreePaths;
133     }
134
135     /**
136      * Creates a new empty selection. See also the static field
137      * <code>EMPTY</code> which contains an empty selection singleton.
138      * <p>
139      * Note that TreeSelection.EMPTY is not equals() to StructuredViewer.EMPTY.
140      * </p>
141      *
142      * @see #EMPTY
143      */

144     public TreeSelection() {
145         super();
146     }
147     
148     /**
149      * Returns the element comparer passed in when the tree selection
150      * has been created or <code>null</code> if no comparer has been
151      * provided.
152      *
153      * @return the element comparer or <code>null</code>
154      *
155      * @since 3.2
156      */

157     public IElementComparer getElementComparer() {
158         if (element2TreePaths == null)
159             return null;
160         return element2TreePaths.getComparer();
161     }
162     
163     public boolean equals(Object JavaDoc obj) {
164         if (!(obj instanceof TreeSelection)) {
165             // Fall back to super implementation, see bug 135837.
166
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 JavaDoc element) {
200         Object JavaDoc 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 JavaDoc) {
206             List JavaDoc l= (List JavaDoc)value;
207             return (TreePath[])l.toArray(new TreePath[l.size()]);
208         } else {
209             // should not happen:
210
Assert.isTrue(false, "Unhandled case"); //$NON-NLS-1$
211
return null;
212         }
213     }
214 }
215
Popular Tags