1 /******************************************************************************* 2 * Copyright (c) 2006, 2015 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation 14 * - fix in bug: 174355,195908,198035,215069,227421 15 *******************************************************************************/ 16 17 package org.eclipse.jface.viewers; 18 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.TableItem; 25 import org.eclipse.swt.widgets.Widget; 26 27 /** 28 * TableViewerRow is the Table specific implementation of ViewerRow 29 * @since 3.3 30 * 31 */ 32 public class TableViewerRow extends ViewerRow { 33 private TableItem item; 34 35 /** 36 * Create a new instance of the receiver from item. 37 * @param item 38 */ TableViewerRow(TableItem item)39 TableViewerRow(TableItem item) { 40 this.item = item; 41 } 42 43 @Override getBounds(int columnIndex)44 public Rectangle getBounds(int columnIndex) { 45 return item.getBounds(columnIndex); 46 } 47 48 @Override getBounds()49 public Rectangle getBounds() { 50 return item.getBounds(); 51 } 52 53 @Override getItem()54 public Widget getItem() { 55 return item; 56 } 57 setItem(TableItem item)58 void setItem(TableItem item) { 59 this.item = item; 60 } 61 62 @Override getColumnCount()63 public int getColumnCount() { 64 return item.getParent().getColumnCount(); 65 } 66 67 @Override getBackground(int columnIndex)68 public Color getBackground(int columnIndex) { 69 return item.getBackground(columnIndex); 70 } 71 72 @Override getFont(int columnIndex)73 public Font getFont(int columnIndex) { 74 return item.getFont(columnIndex); 75 } 76 77 @Override getForeground(int columnIndex)78 public Color getForeground(int columnIndex) { 79 return item.getForeground(columnIndex); 80 } 81 82 @Override getImage(int columnIndex)83 public Image getImage(int columnIndex) { 84 return item.getImage(columnIndex); 85 } 86 87 @Override getText(int columnIndex)88 public String getText(int columnIndex) { 89 return item.getText(columnIndex); 90 } 91 92 @Override setBackground(int columnIndex, Color color)93 public void setBackground(int columnIndex, Color color) { 94 item.setBackground(columnIndex, color); 95 } 96 97 @Override setFont(int columnIndex, Font font)98 public void setFont(int columnIndex, Font font) { 99 item.setFont(columnIndex, font); 100 } 101 102 @Override setForeground(int columnIndex, Color color)103 public void setForeground(int columnIndex, Color color) { 104 item.setForeground(columnIndex, color); 105 } 106 107 @Override setImage(int columnIndex, Image image)108 public void setImage(int columnIndex, Image image) { 109 Image oldImage = item.getImage(columnIndex); 110 if (oldImage != image) { 111 item.setImage(columnIndex,image); 112 } 113 } 114 115 @Override setText(int columnIndex, String text)116 public void setText(int columnIndex, String text) { 117 item.setText(columnIndex, text == null ? "" : text); //$NON-NLS-1$ 118 } 119 120 @Override getControl()121 public Control getControl() { 122 return item.getParent(); 123 } 124 125 @Override getNeighbor(int direction, boolean sameLevel)126 public ViewerRow getNeighbor(int direction, boolean sameLevel) { 127 switch (direction) { 128 case ViewerRow.ABOVE: 129 return getRowAbove(); 130 case ViewerRow.BELOW: 131 return getRowBelow(); 132 default: 133 throw new IllegalArgumentException("Illegal value of direction argument."); //$NON-NLS-1$ 134 } 135 } 136 137 getRowAbove()138 private ViewerRow getRowAbove() { 139 int index = item.getParent().indexOf(item) - 1; 140 141 if( index >= 0 ) { 142 return new TableViewerRow(item.getParent().getItem(index)); 143 } 144 145 return null; 146 } 147 getRowBelow()148 private ViewerRow getRowBelow() { 149 int index = item.getParent().indexOf(item) + 1; 150 151 if( index < item.getParent().getItemCount() ) { 152 TableItem tmp = item.getParent().getItem(index); 153 //TODO NULL can happen in case of VIRTUAL => How do we deal with that 154 if( tmp != null ) { 155 return new TableViewerRow(tmp); 156 } 157 } 158 159 return null; 160 } 161 162 @Override getTreePath()163 public TreePath getTreePath() { 164 return new TreePath(new Object[] {item.getData()}); 165 } 166 167 @Override clone()168 public Object clone() { 169 return new TableViewerRow(item); 170 } 171 172 @Override getElement()173 public Object getElement() { 174 return item.getData(); 175 } 176 177 @Override getVisualIndex(int creationIndex)178 public int getVisualIndex(int creationIndex) { 179 int[] order = item.getParent().getColumnOrder(); 180 181 for (int i = 0; i < order.length; i++) { 182 if (order[i] == creationIndex) { 183 return i; 184 } 185 } 186 187 return super.getVisualIndex(creationIndex); 188 } 189 190 @Override getCreationIndex(int visualIndex)191 public int getCreationIndex(int visualIndex) { 192 if( item != null && ! item.isDisposed() && hasColumns() && isValidOrderIndex(visualIndex) ) { 193 return item.getParent().getColumnOrder()[visualIndex]; 194 } 195 return super.getCreationIndex(visualIndex); 196 } 197 198 @Override getTextBounds(int index)199 public Rectangle getTextBounds(int index) { 200 return item.getTextBounds(index); 201 } 202 203 @Override getImageBounds(int index)204 public Rectangle getImageBounds(int index) { 205 return item.getImageBounds(index); 206 } 207 hasColumns()208 private boolean hasColumns() { 209 return this.item.getParent().getColumnCount() != 0; 210 } 211 isValidOrderIndex(int currentIndex)212 private boolean isValidOrderIndex(int currentIndex) { 213 return currentIndex < this.item.getParent().getColumnOrder().length; 214 } 215 216 @Override getWidth(int columnIndex)217 int getWidth(int columnIndex) { 218 return item.getParent().getColumn(columnIndex).getWidth(); 219 } 220 221 @Override scrollCellIntoView(int columnIndex)222 protected boolean scrollCellIntoView(int columnIndex) { 223 item.getParent().showItem(item); 224 if( hasColumns() ) { 225 item.getParent().showColumn(item.getParent().getColumn(columnIndex)); 226 } 227 228 return true; 229 } 230 } 231