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: 166346,167325,174355,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.Point;
018: import org.eclipse.swt.graphics.Rectangle;
019: import org.eclipse.swt.widgets.Control;
020: import org.eclipse.swt.widgets.Widget;
021:
022: /**
023: * ViewerRow is the abstract superclass of the part that represents items in a
024: * Table or Tree. Implementors of {@link ColumnViewer} have to provide a
025: * concrete implementation for the underlying widget
026: *
027: * @since 3.3
028: *
029: */
030: public abstract class ViewerRow implements Cloneable {
031:
032: /**
033: * Constant denoting the row above the current one (value is 1).
034: *
035: * @see #getNeighbor(int, boolean)
036: */
037: public static final int ABOVE = 1;
038:
039: /**
040: * Constant denoting the row below the current one (value is 2).
041: *
042: * @see #getNeighbor(int, boolean)
043: */
044: public static final int BELOW = 2;
045:
046: /**
047: * Get the bounds of the entry at the columnIndex,
048: *
049: * @param columnIndex
050: * @return {@link Rectangle}
051: */
052: public abstract Rectangle getBounds(int columnIndex);
053:
054: /**
055: * Return the bounds for the whole item.
056: *
057: * @return {@link Rectangle}
058: */
059: public abstract Rectangle getBounds();
060:
061: /**
062: * Return the item for the receiver.
063: *
064: * @return {@link Widget}
065: */
066: public abstract Widget getItem();
067:
068: /**
069: * Return the number of columns for the receiver.
070: *
071: * @return the number of columns
072: */
073: public abstract int getColumnCount();
074:
075: /**
076: * Return the image at the columnIndex.
077: *
078: * @param columnIndex
079: * @return {@link Image} or <code>null</code>
080: */
081: public abstract Image getImage(int columnIndex);
082:
083: /**
084: * Set the image at the columnIndex
085: *
086: * @param columnIndex
087: * @param image
088: */
089: public abstract void setImage(int columnIndex, Image image);
090:
091: /**
092: * Get the text at the columnIndex.
093: *
094: * @param columnIndex
095: * @return {@link String}
096: */
097: public abstract String getText(int columnIndex);
098:
099: /**
100: * Set the text at the columnIndex
101: *
102: * @param columnIndex
103: * @param text
104: */
105: public abstract void setText(int columnIndex, String text);
106:
107: /**
108: * Get the background at the columnIndex,
109: *
110: * @param columnIndex
111: * @return {@link Color} or <code>null</code>
112: */
113: public abstract Color getBackground(int columnIndex);
114:
115: /**
116: * Set the background at the columnIndex.
117: *
118: * @param columnIndex
119: * @param color
120: */
121: public abstract void setBackground(int columnIndex, Color color);
122:
123: /**
124: * Get the foreground at the columnIndex.
125: *
126: * @param columnIndex
127: * @return {@link Color} or <code>null</code>
128: */
129: public abstract Color getForeground(int columnIndex);
130:
131: /**
132: * Set the foreground at the columnIndex.
133: *
134: * @param columnIndex
135: * @param color
136: */
137: public abstract void setForeground(int columnIndex, Color color);
138:
139: /**
140: * Get the font at the columnIndex.
141: *
142: * @param columnIndex
143: * @return {@link Font} or <code>null</code>
144: */
145: public abstract Font getFont(int columnIndex);
146:
147: /**
148: * Set the {@link Font} at the columnIndex.
149: *
150: * @param columnIndex
151: * @param font
152: */
153: public abstract void setFont(int columnIndex, Font font);
154:
155: /**
156: * Get the ViewerCell at point.
157: *
158: * @param point
159: * @return {@link ViewerCell}
160: */
161: public ViewerCell getCell(Point point) {
162: int index = getColumnIndex(point);
163: return getCell(index);
164: }
165:
166: /**
167: * Get the columnIndex of the point.
168: *
169: * @param point
170: * @return int or -1 if it cannot be found.
171: */
172: public int getColumnIndex(Point point) {
173: int count = getColumnCount();
174:
175: // If there are no columns the column-index is 0
176: if (count == 0) {
177: return 0;
178: }
179:
180: for (int i = 0; i < count; i++) {
181: if (getBounds(i).contains(point)) {
182: return i;
183: }
184: }
185:
186: return -1;
187: }
188:
189: /**
190: * Get a ViewerCell for the column at index.
191: *
192: * @param column
193: * @return {@link ViewerCell} or <code>null</code> if the index is
194: * negative.
195: */
196: public ViewerCell getCell(int column) {
197: if (column >= 0)
198: return new ViewerCell((ViewerRow) clone(), column,
199: getElement());
200:
201: return null;
202: }
203:
204: /**
205: * Get the Control for the receiver.
206: *
207: * @return {@link Control}
208: */
209: public abstract Control getControl();
210:
211: /**
212: * Returns a neighboring row, or <code>null</code> if no neighbor exists
213: * in the given direction. If <code>sameLevel</code> is <code>true</code>,
214: * only sibling rows (under the same parent) will be considered.
215: *
216: * @param direction
217: * the direction {@link #BELOW} or {@link #ABOVE}
218: *
219: * @param sameLevel
220: * if <code>true</code>, search only within sibling rows
221: * @return the row above/below, or <code>null</code> if not found
222: */
223: public abstract ViewerRow getNeighbor(int direction,
224: boolean sameLevel);
225:
226: /**
227: * The tree path used to identify an element by the unique path
228: *
229: * @return the path
230: */
231: public abstract TreePath getTreePath();
232:
233: public abstract Object clone();
234:
235: /**
236: * @return the model element
237: */
238: public abstract Object getElement();
239:
240: public int hashCode() {
241: final int prime = 31;
242: int result = 1;
243: result = prime * result
244: + ((getItem() == null) ? 0 : getItem().hashCode());
245: return result;
246: }
247:
248: public boolean equals(Object obj) {
249: if (this == obj)
250: return true;
251: if (obj == null)
252: return false;
253: if (getClass() != obj.getClass())
254: return false;
255: final ViewerRow other = (ViewerRow) obj;
256: if (getItem() == null) {
257: if (other.getItem() != null)
258: return false;
259: } else if (!getItem().equals(other.getItem()))
260: return false;
261: return true;
262: }
263:
264: /**
265: * The cell at the current index (as shown in the UI). This can be different
266: * to the original index when columns are reordered.
267: *
268: * @param visualIndex
269: * the current index (as shown in the UI)
270: * @return the cell at the currently visible index
271: */
272: ViewerCell getCellAtVisualIndex(int visualIndex) {
273: return getCell(getCreationIndex(visualIndex));
274: }
275:
276: /**
277: * Translate the original column index to the actual one.
278: * <p>
279: * <b>Because of backwards API compatibility the default implementation
280: * returns the original index. Implementators of {@link ColumnViewer} should
281: * overwrite this method if their widget supports reordered columns</b>
282: * </p>
283: *
284: * @param creationIndex
285: * the original index
286: * @return the current index (as shown in the UI)
287: * @since 3.4
288: */
289: protected int getVisualIndex(int creationIndex) {
290: return creationIndex;
291: }
292:
293: /**
294: * Translate the current column index (as shown in the UI) to the original
295: * one.
296: * <p>
297: * <b>Because of backwards API compatibility the default implementation
298: * returns the original index. Implementators of {@link ColumnViewer} should
299: * overwrite this method if their widget supports reordered columns</b>
300: * </p>
301: *
302: * @param visualIndex
303: * the current index (as shown in the UI)
304: * @return the original index
305: * @since 3.4
306: */
307: protected int getCreationIndex(int visualIndex) {
308: return visualIndex;
309: }
310: }
|