001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
011: * - fix in bug: 195908,198035
012: *******************************************************************************/package org.eclipse.jface.viewers;
013:
014: import org.eclipse.swt.graphics.Color;
015: import org.eclipse.swt.graphics.Font;
016: import org.eclipse.swt.graphics.Image;
017: import org.eclipse.swt.graphics.Rectangle;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.swt.widgets.Item;
020: import org.eclipse.swt.widgets.Widget;
021:
022: /**
023: * The ViewerCell is the JFace representation of a cell entry in a ViewerRow.
024: *
025: * @since 3.3
026: *
027: */
028: public class ViewerCell {
029: private int columnIndex;
030:
031: private ViewerRow row;
032:
033: private Object element;
034:
035: /**
036: * Constant denoting the cell above current one (value is 1).
037: */
038: public static int ABOVE = 1;
039:
040: /**
041: * Constant denoting the cell below current one (value is 2).
042: */
043: public static int BELOW = 1 << 1;
044:
045: /**
046: * Constant denoting the cell to the left of the current one (value is 4).
047: */
048: public static int LEFT = 1 << 2;
049:
050: /**
051: * Constant denoting the cell to the right of the current one (value is 8).
052: */
053: public static int RIGHT = 1 << 3;
054:
055: /**
056: * Create a new instance of the receiver on the row.
057: *
058: * @param row
059: * @param columnIndex
060: */
061: ViewerCell(ViewerRow row, int columnIndex, Object element) {
062: this .row = row;
063: this .columnIndex = columnIndex;
064: this .element = element;
065: }
066:
067: /**
068: * Get the index of the cell.
069: *
070: * @return the index
071: */
072: public int getColumnIndex() {
073: return columnIndex;
074: }
075:
076: /**
077: * Get the bounds of the cell.
078: *
079: * @return {@link Rectangle}
080: */
081: public Rectangle getBounds() {
082: return row.getBounds(columnIndex);
083: }
084:
085: /**
086: * Get the element this row represents.
087: *
088: * @return {@link Object}
089: */
090: public Object getElement() {
091: if (element != null) {
092: return element;
093: }
094: return row.getElement();
095: }
096:
097: /**
098: * Return the text for the cell.
099: *
100: * @return {@link String}
101: */
102: public String getText() {
103: return row.getText(columnIndex);
104: }
105:
106: /**
107: * Return the Image for the cell.
108: *
109: * @return {@link Image} or <code>null</code>
110: */
111: public Image getImage() {
112: return row.getImage(columnIndex);
113: }
114:
115: /**
116: * Set the background color of the cell.
117: *
118: * @param background
119: */
120: public void setBackground(Color background) {
121: row.setBackground(columnIndex, background);
122:
123: }
124:
125: /**
126: * Set the foreground color of the cell.
127: *
128: * @param foreground
129: */
130: public void setForeground(Color foreground) {
131: row.setForeground(columnIndex, foreground);
132:
133: }
134:
135: /**
136: * Set the font of the cell.
137: *
138: * @param font
139: */
140: public void setFont(Font font) {
141: row.setFont(columnIndex, font);
142:
143: }
144:
145: /**
146: * Set the text for the cell.
147: *
148: * @param text
149: */
150: public void setText(String text) {
151: row.setText(columnIndex, text);
152:
153: }
154:
155: /**
156: * Set the Image for the cell.
157: *
158: * @param image
159: */
160: public void setImage(Image image) {
161: row.setImage(columnIndex, image);
162:
163: }
164:
165: /**
166: * Set the columnIndex.
167: *
168: * @param column
169: */
170: void setColumn(int column) {
171: columnIndex = column;
172:
173: }
174:
175: /**
176: * Set the row to rowItem and the columnIndex to column.
177: *
178: * @param rowItem
179: * @param column
180: */
181: void update(ViewerRow rowItem, int column, Object element) {
182: row = rowItem;
183: columnIndex = column;
184: this .element = element;
185: }
186:
187: /**
188: * Return the item for the receiver.
189: *
190: * @return {@link Item}
191: */
192: public Widget getItem() {
193: return row.getItem();
194: }
195:
196: /**
197: * Get the control for this cell.
198: *
199: * @return {@link Control}
200: */
201: public Control getControl() {
202: return row.getControl();
203: }
204:
205: /**
206: * Get the current index. This can be different from the original index when
207: * columns are reordered
208: *
209: * @return the current index (as shown in the UI)
210: * @since 3.4
211: */
212: public int getVisualIndex() {
213: return row.getVisualIndex(getColumnIndex());
214: }
215:
216: /**
217: * Returns the specified neighbor of this cell, or <code>null</code> if no
218: * neighbor exists in the given direction. Direction constants can be
219: * combined by bitwise OR; for example, this method will return the cell to
220: * the upper-left of the current cell by passing {@link #ABOVE} |
221: * {@link #LEFT}. If <code>sameLevel</code> is <code>true</code>, only
222: * cells in sibling rows (under the same parent) will be considered.
223: *
224: * @param directionMask
225: * the direction mask used to identify the requested neighbor
226: * cell
227: * @param sameLevel
228: * if <code>true</code>, only consider cells from sibling rows
229: * @return the requested neighbor cell, or <code>null</code> if not found
230: */
231: public ViewerCell getNeighbor(int directionMask, boolean sameLevel) {
232: ViewerRow row;
233:
234: if ((directionMask & ABOVE) == ABOVE) {
235: row = this .row.getNeighbor(ViewerRow.ABOVE, sameLevel);
236: } else if ((directionMask & BELOW) == BELOW) {
237: row = this .row.getNeighbor(ViewerRow.BELOW, sameLevel);
238: } else {
239: row = this .row;
240: }
241:
242: if (row != null) {
243: int columnIndex;
244: columnIndex = getVisualIndex();
245:
246: if ((directionMask & LEFT) == LEFT) {
247: columnIndex -= 1;
248: } else if ((directionMask & RIGHT) == RIGHT) {
249: columnIndex += 1;
250: }
251:
252: if (columnIndex >= 0 && columnIndex < row.getColumnCount()) {
253: return row.getCellAtVisualIndex(columnIndex);
254: }
255: }
256:
257: return null;
258: }
259:
260: /**
261: * @return the row
262: */
263: public ViewerRow getViewerRow() {
264: return row;
265: }
266:
267: /*
268: * (non-Javadoc)
269: *
270: * @see java.lang.Object#hashCode()
271: */
272: public int hashCode() {
273: final int prime = 31;
274: int result = 1;
275: result = prime * result + columnIndex;
276: result = prime * result + ((row == null) ? 0 : row.hashCode());
277: return result;
278: }
279:
280: /*
281: * (non-Javadoc)
282: *
283: * @see java.lang.Object#equals(java.lang.Object)
284: */
285: public boolean equals(Object obj) {
286: if (this == obj)
287: return true;
288: if (obj == null)
289: return false;
290: if (getClass() != obj.getClass())
291: return false;
292: final ViewerCell other = (ViewerCell) obj;
293: if (columnIndex != other.columnIndex)
294: return false;
295: if (row == null) {
296: if (other.row != null)
297: return false;
298: } else if (!row.equals(other.row))
299: return false;
300: return true;
301: }
302: }
|