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: 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.Rectangle;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.swt.widgets.TableItem;
020: import org.eclipse.swt.widgets.Widget;
021:
022: /**
023: * TableViewerRow is the Table specific implementation of ViewerRow
024: * @since 3.3
025: *
026: */
027: public class TableViewerRow extends ViewerRow {
028: private TableItem item;
029:
030: /**
031: * Create a new instance of the receiver from item.
032: * @param item
033: */
034: TableViewerRow(TableItem item) {
035: this .item = item;
036: }
037:
038: /* (non-Javadoc)
039: * @see org.eclipse.jface.viewers.ViewerRow#getBounds(int)
040: */
041: public Rectangle getBounds(int columnIndex) {
042: return item.getBounds(columnIndex);
043: }
044:
045: /* (non-Javadoc)
046: * @see org.eclipse.jface.viewers.ViewerRow#getBounds()
047: */
048: public Rectangle getBounds() {
049: return item.getBounds();
050: }
051:
052: /* (non-Javadoc)
053: * @see org.eclipse.jface.viewers.ViewerRow#getItem()
054: */
055: public Widget getItem() {
056: return item;
057: }
058:
059: void setItem(TableItem item) {
060: this .item = item;
061: }
062:
063: /* (non-Javadoc)
064: * @see org.eclipse.jface.viewers.ViewerRow#getColumnCount()
065: */
066: public int getColumnCount() {
067: return item.getParent().getColumnCount();
068: }
069:
070: /* (non-Javadoc)
071: * @see org.eclipse.jface.viewers.ViewerRow#getBackground(int)
072: */
073: public Color getBackground(int columnIndex) {
074: return item.getBackground(columnIndex);
075: }
076:
077: /* (non-Javadoc)
078: * @see org.eclipse.jface.viewers.ViewerRow#getFont(int)
079: */
080: public Font getFont(int columnIndex) {
081: return item.getFont(columnIndex);
082: }
083:
084: /* (non-Javadoc)
085: * @see org.eclipse.jface.viewers.ViewerRow#getForeground(int)
086: */
087: public Color getForeground(int columnIndex) {
088: return item.getForeground(columnIndex);
089: }
090:
091: /* (non-Javadoc)
092: * @see org.eclipse.jface.viewers.ViewerRow#getImage(int)
093: */
094: public Image getImage(int columnIndex) {
095: return item.getImage(columnIndex);
096: }
097:
098: /* (non-Javadoc)
099: * @see org.eclipse.jface.viewers.ViewerRow#getText(int)
100: */
101: public String getText(int columnIndex) {
102: return item.getText(columnIndex);
103: }
104:
105: /* (non-Javadoc)
106: * @see org.eclipse.jface.viewers.ViewerRow#setBackground(int, org.eclipse.swt.graphics.Color)
107: */
108: public void setBackground(int columnIndex, Color color) {
109: item.setBackground(columnIndex, color);
110: }
111:
112: /* (non-Javadoc)
113: * @see org.eclipse.jface.viewers.ViewerRow#setFont(int, org.eclipse.swt.graphics.Font)
114: */
115: public void setFont(int columnIndex, Font font) {
116: item.setFont(columnIndex, font);
117: }
118:
119: /* (non-Javadoc)
120: * @see org.eclipse.jface.viewers.ViewerRow#setForeground(int, org.eclipse.swt.graphics.Color)
121: */
122: public void setForeground(int columnIndex, Color color) {
123: item.setForeground(columnIndex, color);
124: }
125:
126: /* (non-Javadoc)
127: * @see org.eclipse.jface.viewers.ViewerRow#setImage(int, org.eclipse.swt.graphics.Image)
128: */
129: public void setImage(int columnIndex, Image image) {
130: Image oldImage = item.getImage(columnIndex);
131: if (oldImage != image) {
132: item.setImage(columnIndex, image);
133: }
134: }
135:
136: /* (non-Javadoc)
137: * @see org.eclipse.jface.viewers.ViewerRow#setText(int, java.lang.String)
138: */
139: public void setText(int columnIndex, String text) {
140: item.setText(columnIndex, text == null ? "" : text); //$NON-NLS-1$
141: }
142:
143: /* (non-Javadoc)
144: * @see org.eclipse.jface.viewers.ViewerRow#getControl()
145: */
146: public Control getControl() {
147: return item.getParent();
148: }
149:
150: public ViewerRow getNeighbor(int direction, boolean sameLevel) {
151: if (direction == ViewerRow.ABOVE) {
152: return getRowAbove();
153: } else if (direction == ViewerRow.BELOW) {
154: return getRowBelow();
155: } else {
156: throw new IllegalArgumentException(
157: "Illegal value of direction argument."); //$NON-NLS-1$
158: }
159: }
160:
161: private ViewerRow getRowAbove() {
162: int index = item.getParent().indexOf(item) - 1;
163:
164: if (index >= 0) {
165: return new TableViewerRow(item.getParent().getItem(index));
166: }
167:
168: return null;
169: }
170:
171: private ViewerRow getRowBelow() {
172: int index = item.getParent().indexOf(item) + 1;
173:
174: if (index < item.getParent().getItemCount()) {
175: TableItem tmp = item.getParent().getItem(index);
176: //TODO NULL can happen in case of VIRTUAL => How do we deal with that
177: if (tmp != null) {
178: return new TableViewerRow(tmp);
179: }
180: }
181:
182: return null;
183: }
184:
185: public TreePath getTreePath() {
186: return new TreePath(new Object[] { item.getData() });
187: }
188:
189: public Object clone() {
190: return new TableViewerRow(item);
191: }
192:
193: public Object getElement() {
194: return item.getData();
195: }
196:
197: public int getVisualIndex(int creationIndex) {
198: int[] order = item.getParent().getColumnOrder();
199:
200: for (int i = 0; i < order.length; i++) {
201: if (order[i] == creationIndex) {
202: return i;
203: }
204: }
205:
206: return super .getVisualIndex(creationIndex);
207: }
208:
209: public int getCreationIndex(int visualIndex) {
210: if (item != null && !item.isDisposed() && hasColumns()
211: && isValidOrderIndex(visualIndex)) {
212: return item.getParent().getColumnOrder()[visualIndex];
213: }
214: return super .getCreationIndex(visualIndex);
215: }
216:
217: private boolean hasColumns() {
218: return this .item.getParent().getColumnCount() != 0;
219: }
220:
221: private boolean isValidOrderIndex(int currentIndex) {
222: return currentIndex < this.item.getParent().getColumnOrder().length;
223: }
224: }
|