KickJava   Java API By Example, From Geeks To Geeks.

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


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 Shindl <tom.schindl@bestsolution.at> - initial API and implementation
11  *******************************************************************************/

12
13 package org.eclipse.jface.viewers;
14
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.graphics.Font;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.graphics.Rectangle;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Item;
21 import org.eclipse.swt.widgets.Widget;
22
23 /**
24  * The ViewerCell is the JFace representation of a cell entry in a ViewerRow.
25  *
26  * @since 3.3
27  *
28  */

29 public class ViewerCell {
30     private int columnIndex;
31
32     private ViewerRow row;
33
34     private Object JavaDoc element;
35
36     /**
37      * Constant denoting the cell above current one (value is 1).
38      */

39     public static int ABOVE = 1;
40
41     /**
42      * Constant denoting the cell below current one (value is 2).
43      */

44     public static int BELOW = 1 << 1;
45
46     /**
47      * Constant denoting the cell to the left of the current one (value is 4).
48      */

49     public static int LEFT = 1 << 2;
50
51     /**
52      * Constant denoting the cell to the right of the current one (value is 8).
53      */

54     public static int RIGHT = 1 << 3;
55
56     /**
57      * Create a new instance of the receiver on the row.
58      *
59      * @param row
60      * @param columnIndex
61      */

62     ViewerCell(ViewerRow row, int columnIndex, Object JavaDoc element) {
63         this.row = row;
64         this.columnIndex = columnIndex;
65         this.element = element;
66     }
67
68     /**
69      * Get the index of the cell.
70      *
71      * @return the index
72      */

73     public int getColumnIndex() {
74         return columnIndex;
75     }
76
77     /**
78      * Get the bounds of the cell.
79      *
80      * @return {@link Rectangle}
81      */

82     public Rectangle getBounds() {
83         return row.getBounds(columnIndex);
84     }
85
86     /**
87      * Get the element this row represents.
88      *
89      * @return {@link Object}
90      */

91     public Object JavaDoc getElement() {
92         if (element!=null) {
93             return element;
94         }
95         return row.getElement();
96     }
97
98     /**
99      * Return the text for the cell.
100      *
101      * @return {@link String}
102      */

103     public String JavaDoc getText() {
104         return row.getText(columnIndex);
105     }
106
107     /**
108      * Return the Image for the cell.
109      *
110      * @return {@link Image} or <code>null</code>
111      */

112     public Image getImage() {
113         return row.getImage(columnIndex);
114     }
115
116     /**
117      * Set the background color of the cell.
118      *
119      * @param background
120      */

121     public void setBackground(Color background) {
122         row.setBackground(columnIndex, background);
123
124     }
125
126     /**
127      * Set the foreground color of the cell.
128      *
129      * @param foreground
130      */

131     public void setForeground(Color foreground) {
132         row.setForeground(columnIndex, foreground);
133
134     }
135
136     /**
137      * Set the font of the cell.
138      *
139      * @param font
140      */

141     public void setFont(Font font) {
142         row.setFont(columnIndex, font);
143
144     }
145
146     /**
147      * Set the text for the cell.
148      *
149      * @param text
150      */

151     public void setText(String JavaDoc text) {
152         row.setText(columnIndex, text);
153
154     }
155
156     /**
157      * Set the Image for the cell.
158      *
159      * @param image
160      */

161     public void setImage(Image image) {
162         row.setImage(columnIndex, image);
163
164     }
165
166     /**
167      * Set the columnIndex.
168      *
169      * @param column
170      */

171     void setColumn(int column) {
172         columnIndex = column;
173
174     }
175
176     /**
177      * Set the row to rowItem and the columnIndex to column.
178      *
179      * @param rowItem
180      * @param column
181      */

182     void update(ViewerRow rowItem, int column, Object JavaDoc element) {
183         row = rowItem;
184         columnIndex = column;
185         this.element = element;
186     }
187
188     /**
189      * Return the item for the receiver.
190      *
191      * @return {@link Item}
192      */

193     public Widget getItem() {
194         return row.getItem();
195     }
196
197     /**
198      * Get the control for this cell.
199      *
200      * @return {@link Control}
201      */

202     public Control getControl() {
203         return row.getControl();
204     }
205
206     /**
207      * Returns the specified neighbor of this cell, or <code>null</code> if no
208      * neighbor exists in the given direction. Direction constants can be
209      * combined by bitwise OR; for example, this method will return the cell to
210      * the upper-left of the current cell by passing {@link #ABOVE} |
211      * {@link #LEFT}. If <code>sameLevel</code> is <code>true</code>, only
212      * cells in sibling rows (under the same parent) will be considered.
213      *
214      * @param directionMask
215      * the direction mask used to identify the requested neighbor
216      * cell
217      * @param sameLevel
218      * if <code>true</code>, only consider cells from sibling rows
219      * @return the requested neighbor cell, or <code>null</code> if not found
220      */

221     public ViewerCell getNeighbor(int directionMask, boolean sameLevel) {
222         ViewerRow row;
223         int columnIndex;
224
225         if ((directionMask & ABOVE) == ABOVE) {
226             row = this.row.getNeighbor(ViewerRow.ABOVE, sameLevel);
227         } else if ((directionMask & BELOW) == BELOW) {
228             row = this.row.getNeighbor(ViewerRow.BELOW, sameLevel);
229         } else {
230             row = this.row;
231         }
232
233         if (row != null) {
234             if ((directionMask & LEFT) == LEFT) {
235                 columnIndex = getColumnIndex() - 1;
236             } else if ((directionMask & RIGHT) == RIGHT) {
237                 columnIndex = getColumnIndex() + 1;
238             } else {
239                 columnIndex = getColumnIndex();
240             }
241
242             if (columnIndex >= 0 && columnIndex < row.getColumnCount()) {
243                 return row.getCell(columnIndex);
244             }
245         }
246
247         return null;
248     }
249     
250     /**
251      * @return the row
252      */

253     public ViewerRow getViewerRow() {
254         return row;
255     }
256
257     /* (non-Javadoc)
258      * @see java.lang.Object#hashCode()
259      */

260     public int hashCode() {
261         final int prime = 31;
262         int result = 1;
263         result = prime * result + columnIndex;
264         result = prime * result + ((row == null) ? 0 : row.hashCode());
265         return result;
266     }
267
268     /* (non-Javadoc)
269      * @see java.lang.Object#equals(java.lang.Object)
270      */

271     public boolean equals(Object JavaDoc obj) {
272         if (this == obj)
273             return true;
274         if (obj == null)
275             return false;
276         if (getClass() != obj.getClass())
277             return false;
278         final ViewerCell other = (ViewerCell) obj;
279         if (columnIndex != other.columnIndex)
280             return false;
281         if (row == null) {
282             if (other.row != null)
283                 return false;
284         } else if (!row.equals(other.row))
285             return false;
286         return true;
287     }
288 }
289
Popular Tags