KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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  * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
11  * - Fix for bug 174355, 171126
12  *******************************************************************************/

13
14 package org.eclipse.jface.viewers;
15
16 import java.util.LinkedList JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.swt.graphics.Color;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Tree;
25 import org.eclipse.swt.widgets.TreeItem;
26 import org.eclipse.swt.widgets.Widget;
27
28 /**
29  * TreeViewerRow is the Tree implementation of ViewerRow.
30  * @since 3.3
31  *
32  */

33 public class TreeViewerRow extends ViewerRow {
34     private TreeItem item;
35     
36     /**
37      * Create a new instance of the receiver.
38      * @param item
39      */

40     TreeViewerRow(TreeItem item) {
41         this.item = item;
42     }
43     
44     /* (non-Javadoc)
45      * @see org.eclipse.jface.viewers.ViewerRow#getBounds(int)
46      */

47     public Rectangle getBounds(int columnIndex) {
48         return item.getBounds(columnIndex);
49     }
50
51     /* (non-Javadoc)
52      * @see org.eclipse.jface.viewers.ViewerRow#getBounds()
53      */

54     public Rectangle getBounds() {
55         return item.getBounds();
56     }
57
58     /* (non-Javadoc)
59      * @see org.eclipse.jface.viewers.ViewerRow#getColumnCount()
60      */

61     public int getColumnCount() {
62         return item.getParent().getColumnCount();
63     }
64
65     /* (non-Javadoc)
66      * @see org.eclipse.jface.viewers.ViewerRow#getItem()
67      */

68     public Widget getItem() {
69         return item;
70     }
71
72     /* (non-Javadoc)
73      * @see org.eclipse.jface.viewers.ViewerRow#getBackground(int)
74      */

75     public Color getBackground(int columnIndex) {
76         return item.getBackground(columnIndex);
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.jface.viewers.ViewerRow#getFont(int)
81      */

82     public Font getFont(int columnIndex) {
83         return item.getFont(columnIndex);
84     }
85
86     /* (non-Javadoc)
87      * @see org.eclipse.jface.viewers.ViewerRow#getForeground(int)
88      */

89     public Color getForeground(int columnIndex) {
90         return item.getForeground(columnIndex);
91     }
92
93     /* (non-Javadoc)
94      * @see org.eclipse.jface.viewers.ViewerRow#getImage(int)
95      */

96     public Image getImage(int columnIndex) {
97         return item.getImage(columnIndex);
98     }
99
100     /* (non-Javadoc)
101      * @see org.eclipse.jface.viewers.ViewerRow#getText(int)
102      */

103     public String JavaDoc getText(int columnIndex) {
104         return item.getText(columnIndex);
105     }
106
107     /* (non-Javadoc)
108      * @see org.eclipse.jface.viewers.ViewerRow#setBackground(int, org.eclipse.swt.graphics.Color)
109      */

110     public void setBackground(int columnIndex, Color color) {
111         item.setBackground(columnIndex, color);
112     }
113
114     /* (non-Javadoc)
115      * @see org.eclipse.jface.viewers.ViewerRow#setFont(int, org.eclipse.swt.graphics.Font)
116      */

117     public void setFont(int columnIndex, Font font) {
118         item.setFont(columnIndex, font);
119     }
120
121     /* (non-Javadoc)
122      * @see org.eclipse.jface.viewers.ViewerRow#setForeground(int, org.eclipse.swt.graphics.Color)
123      */

124     public void setForeground(int columnIndex, Color color) {
125         item.setForeground(columnIndex, color);
126     }
127
128     /* (non-Javadoc)
129      * @see org.eclipse.jface.viewers.ViewerRow#setImage(int, org.eclipse.swt.graphics.Image)
130      */

131     public void setImage(int columnIndex, Image image) {
132         Image oldImage = item.getImage(columnIndex);
133         if (image != oldImage) {
134             item.setImage(columnIndex, image);
135         }
136     }
137
138     /* (non-Javadoc)
139      * @see org.eclipse.jface.viewers.ViewerRow#setText(int, java.lang.String)
140      */

141     public void setText(int columnIndex, String JavaDoc text) {
142         item.setText(columnIndex, text == null ? "" : text); //$NON-NLS-1$
143
}
144     
145     /* (non-Javadoc)
146      * @see org.eclipse.jface.viewers.ViewerRow#getControl()
147      */

148     public Control getControl() {
149         return item.getParent();
150     }
151
152     
153     public ViewerRow getNeighbor(int direction, boolean sameLevel) {
154         if( direction == ViewerRow.ABOVE ) {
155             return getRowAbove(sameLevel);
156         } else if( direction == ViewerRow.BELOW ) {
157             return getRowBelow(sameLevel);
158         } else {
159             throw new IllegalArgumentException JavaDoc("Illegal value of direction argument."); //$NON-NLS-1$
160
}
161     }
162     
163     private ViewerRow getRowBelow(boolean sameLevel) {
164         Tree tree = item.getParent();
165         
166         // This means we have top-level item
167
if( item.getParentItem() == null ) {
168             if( sameLevel || ! item.getExpanded() ) {
169                 int index = tree.indexOf(item) + 1;
170                 
171                 if( index < tree.getItemCount() ) {
172                     return new TreeViewerRow(tree.getItem(index));
173                 }
174             } else if( item.getExpanded() && item.getItemCount() > 0 ) {
175                 return new TreeViewerRow(item.getItem(0));
176             }
177         } else {
178             if( sameLevel || ! item.getExpanded() ) {
179                 TreeItem parentItem = item.getParentItem();
180                 
181                 int nextIndex = parentItem.indexOf(item) + 1;
182                 int totalIndex = parentItem.getItemCount();
183                 
184                 TreeItem itemAfter;
185                 
186                 // This would mean that it was the last item
187
if( nextIndex == totalIndex ) {
188                     itemAfter = findNextItem( parentItem );
189                 } else {
190                     itemAfter = parentItem.getItem(nextIndex);
191                 }
192                 
193                 if( itemAfter != null ) {
194                     return new TreeViewerRow(itemAfter);
195                 }
196                 
197             } else if( item.getExpanded() && item.getItemCount() > 0 ) {
198                 return new TreeViewerRow(item.getItem(0));
199             }
200         }
201
202         return null;
203     }
204     
205     private ViewerRow getRowAbove(boolean sameLevel) {
206         Tree tree = item.getParent();
207         
208         // This means we have top-level item
209
if( item.getParentItem() == null ) {
210             int index = tree.indexOf(item) - 1;
211             TreeItem nextTopItem = null;
212             
213             if( index >= 0 ) {
214                 nextTopItem = tree.getItem(index);
215             }
216             
217             if( nextTopItem != null ) {
218                 if( sameLevel ) {
219                     return new TreeViewerRow(nextTopItem);
220                 }
221                 
222                 return new TreeViewerRow(findLastVisibleItem(nextTopItem));
223             }
224         } else {
225             TreeItem parentItem = item.getParentItem();
226             int previousIndex = parentItem.indexOf(item) - 1;
227             
228             TreeItem itemBefore;
229             if( previousIndex >= 0 ) {
230                 if( sameLevel ) {
231                     itemBefore = parentItem.getItem(previousIndex);
232                 } else {
233                     itemBefore = findLastVisibleItem(parentItem.getItem(previousIndex));
234                 }
235             } else {
236                 itemBefore = parentItem;
237             }
238             
239             if( itemBefore != null ) {
240                 return new TreeViewerRow(itemBefore);
241             }
242         }
243         
244         return null;
245     }
246
247     private TreeItem findLastVisibleItem(TreeItem parentItem) {
248         TreeItem rv = parentItem;
249         
250         while( rv.getExpanded() && rv.getItemCount() > 0 ) {
251             rv = rv.getItem(rv.getItemCount()-1);
252         }
253         
254         return rv;
255     }
256         
257     private TreeItem findNextItem(TreeItem item) {
258         TreeItem rv = null;
259         Tree tree = item.getParent();
260         TreeItem parentItem = item.getParentItem();
261         
262         int nextIndex;
263         int totalItems;
264         
265         if( parentItem == null ) {
266             nextIndex = tree.indexOf(item) + 1;
267             totalItems = tree.getItemCount();
268         } else {
269             nextIndex = parentItem.indexOf(item) + 1;
270             totalItems = parentItem.getItemCount();
271         }
272         
273         // This is once more the last item in the tree
274
// Search on
275
if( nextIndex == totalItems ) {
276             if( item.getParentItem() != null ) {
277                 rv = findNextItem(item.getParentItem());
278             }
279         } else {
280             if( parentItem == null ) {
281                 rv = tree.getItem(nextIndex);
282             } else {
283                 rv = parentItem.getItem(nextIndex);
284             }
285         }
286         
287         return rv;
288     }
289
290     public TreePath getTreePath() {
291         TreeItem tItem = item;
292         LinkedList JavaDoc segments = new LinkedList JavaDoc();
293         while (tItem != null) {
294             Object JavaDoc segment = tItem.getData();
295             Assert.isNotNull(segment);
296             segments.addFirst(segment);
297             tItem = tItem.getParentItem();
298         }
299         
300         return new TreePath(segments.toArray());
301     }
302     
303     void setItem(TreeItem item) {
304         this.item = item;
305     }
306             
307     public Object JavaDoc clone() {
308         return new TreeViewerRow(item);
309     }
310     
311     public Object JavaDoc getElement() {
312         return item.getData();
313     }
314 }
315
Popular Tags