1 /******************************************************************************* 2 * Copyright (c) 2006, 2019 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: 166346,167325,174355,195908,198035,215069,227421 15 *******************************************************************************/ 16 17 package org.eclipse.jface.viewers; 18 19 import java.util.Objects; 20 21 import org.eclipse.jface.util.Policy; 22 import org.eclipse.swt.custom.StyleRange; 23 import org.eclipse.swt.graphics.Color; 24 import org.eclipse.swt.graphics.Font; 25 import org.eclipse.swt.graphics.Image; 26 import org.eclipse.swt.graphics.Point; 27 import org.eclipse.swt.graphics.Rectangle; 28 import org.eclipse.swt.widgets.Control; 29 import org.eclipse.swt.widgets.Widget; 30 31 /** 32 * ViewerRow is the abstract superclass of the part that represents items in a 33 * Table or Tree. Implementors of {@link ColumnViewer} have to provide a 34 * concrete implementation for the underlying widget 35 * 36 * @since 3.3 37 * 38 */ 39 public abstract class ViewerRow implements Cloneable { 40 41 /** 42 * Constant denoting the row above the current one (value is 1). 43 * 44 * @see #getNeighbor(int, boolean) 45 */ 46 public static final int ABOVE = 1; 47 48 /** 49 * Constant denoting the row below the current one (value is 2). 50 * 51 * @see #getNeighbor(int, boolean) 52 */ 53 public static final int BELOW = 2; 54 55 private static final String KEY_TEXT_LAYOUT = Policy.JFACE + "styled_label_key_"; //$NON-NLS-1$ 56 57 private static final String KEY_TEXT_LAYOUT_0 = Policy.JFACE + "styled_label_key_0"; //$NON-NLS-1$ 58 59 private static String[] cachedDataKeys; 60 61 /** 62 * Get the bounds of the entry at the columnIndex, 63 * 64 * @param columnIndex column index of interest 65 * @return {@link Rectangle} 66 */ getBounds(int columnIndex)67 public abstract Rectangle getBounds(int columnIndex); 68 69 /** 70 * Return the bounds for the whole item. 71 * 72 * @return {@link Rectangle} 73 */ getBounds()74 public abstract Rectangle getBounds(); 75 76 /** 77 * Return the item for the receiver. 78 * 79 * @return {@link Widget} 80 */ getItem()81 public abstract Widget getItem(); 82 83 /** 84 * Return the number of columns for the receiver. 85 * 86 * @return the number of columns 87 */ getColumnCount()88 public abstract int getColumnCount(); 89 90 /** 91 * Return the image at the columnIndex. 92 * 93 * @param columnIndex column index of interest 94 * @return {@link Image} or <code>null</code> 95 */ getImage(int columnIndex)96 public abstract Image getImage(int columnIndex); 97 98 /** 99 * Set the image at the columnIndex 100 * 101 * @param columnIndex column index to set image for 102 * @param image image to set 103 */ setImage(int columnIndex, Image image)104 public abstract void setImage(int columnIndex, Image image); 105 106 /** 107 * Get the text at the columnIndex. 108 * 109 * @param columnIndex column index of interest 110 * @return {@link String} 111 */ getText(int columnIndex)112 public abstract String getText(int columnIndex); 113 114 /** 115 * Set the text at the columnIndex 116 * 117 * @param columnIndex column index to set text for 118 * @param text text to set 119 */ setText(int columnIndex, String text)120 public abstract void setText(int columnIndex, String text); 121 122 /** 123 * Get the background at the columnIndex, 124 * 125 * @param columnIndex column index of interest 126 * @return {@link Color} or <code>null</code> 127 */ getBackground(int columnIndex)128 public abstract Color getBackground(int columnIndex); 129 130 /** 131 * Set the background at the columnIndex. 132 * 133 * @param columnIndex column index to set color for 134 * @param color color to set 135 */ setBackground(int columnIndex, Color color)136 public abstract void setBackground(int columnIndex, Color color); 137 138 /** 139 * Get the foreground at the columnIndex. 140 * 141 * @param columnIndex column index of interest 142 * @return {@link Color} or <code>null</code> 143 */ getForeground(int columnIndex)144 public abstract Color getForeground(int columnIndex); 145 146 /** 147 * Set the foreground at the columnIndex. 148 * 149 * @param columnIndex column index to set color for 150 * @param color color to set 151 */ setForeground(int columnIndex, Color color)152 public abstract void setForeground(int columnIndex, Color color); 153 154 /** 155 * Get the font at the columnIndex. 156 * 157 * @param columnIndex column index of interest 158 * @return {@link Font} or <code>null</code> 159 */ getFont(int columnIndex)160 public abstract Font getFont(int columnIndex); 161 162 /** 163 * Set the {@link Font} at the columnIndex. 164 * 165 * @param columnIndex column index to set font for 166 * @param font font to set 167 */ setFont(int columnIndex, Font font)168 public abstract void setFont(int columnIndex, Font font); 169 170 /** 171 * Get the ViewerCell at point. 172 * 173 * @param point position to get cell for 174 * @return {@link ViewerCell} or <code>null</code> if the point is not in the 175 * bounds of a cell 176 */ getCell(Point point)177 public ViewerCell getCell(Point point) { 178 int index = getColumnIndex(point); 179 return getCell(index); 180 } 181 182 /** 183 * Get the columnIndex of the point. 184 * 185 * @param point position to get column index for 186 * @return int or -1 if it cannot be found. 187 */ getColumnIndex(Point point)188 public int getColumnIndex(Point point) { 189 int count = getColumnCount(); 190 191 // If there are no columns the column-index is 0 192 if (count == 0) { 193 return 0; 194 } 195 196 for (int i = 0; i < count; i++) { 197 if (getBounds(i).contains(point)) { 198 return i; 199 } 200 } 201 202 return -1; 203 } 204 205 /** 206 * Get a ViewerCell for the column at index. 207 * 208 * @param column column index of interest 209 * @return {@link ViewerCell} or <code>null</code> if the index is negative. 210 */ getCell(int column)211 public ViewerCell getCell(int column) { 212 if (column >= 0) 213 return new ViewerCell((ViewerRow) clone(), column, getElement()); 214 215 return null; 216 } 217 218 /** 219 * Get the Control for the receiver. 220 * 221 * @return {@link Control} 222 */ getControl()223 public abstract Control getControl(); 224 225 /** 226 * Returns a neighboring row, or <code>null</code> if no neighbor exists in 227 * the given direction. If <code>sameLevel</code> is <code>true</code>, only 228 * sibling rows (under the same parent) will be considered. 229 * 230 * @param direction 231 * the direction {@link #BELOW} or {@link #ABOVE} 232 * 233 * @param sameLevel 234 * if <code>true</code>, search only within sibling rows 235 * @return the row above/below, or <code>null</code> if not found 236 */ getNeighbor(int direction, boolean sameLevel)237 public abstract ViewerRow getNeighbor(int direction, boolean sameLevel); 238 239 /** 240 * The tree path used to identify an element by the unique path 241 * 242 * @return the path 243 */ getTreePath()244 public abstract TreePath getTreePath(); 245 246 @Override clone()247 public abstract Object clone(); 248 249 /** 250 * @return the model element 251 */ getElement()252 public abstract Object getElement(); 253 254 @Override hashCode()255 public int hashCode() { 256 return Objects.hashCode(getItem()); 257 } 258 259 @Override equals(Object obj)260 public boolean equals(Object obj) { 261 if (this == obj) 262 return true; 263 if (obj == null) 264 return false; 265 if (getClass() != obj.getClass()) 266 return false; 267 final ViewerRow other = (ViewerRow) obj; 268 return Objects.equals(getItem(), other.getItem()); 269 } 270 271 /** 272 * The cell at the current index (as shown in the UI). This can be different 273 * to the original index when columns are reordered. 274 * 275 * @param visualIndex 276 * the current index (as shown in the UI) 277 * @return the cell at the currently visible index 278 */ getCellAtVisualIndex(int visualIndex)279 ViewerCell getCellAtVisualIndex(int visualIndex) { 280 return getCell(getCreationIndex(visualIndex)); 281 } 282 283 /** 284 * Translate the original column index to the actual one. 285 * <p> 286 * <b>Because of backwards API compatibility the default implementation 287 * returns the original index. Implementators of {@link ColumnViewer} should 288 * overwrite this method if their widget supports reordered columns</b> 289 * </p> 290 * 291 * @param creationIndex 292 * the original index 293 * @return the current index (as shown in the UI) 294 * @since 3.4 295 */ getVisualIndex(int creationIndex)296 protected int getVisualIndex(int creationIndex) { 297 return creationIndex; 298 } 299 300 /** 301 * Translate the current column index (as shown in the UI) to the original 302 * one. 303 * <p> 304 * <b>Because of backwards API compatibility the default implementation 305 * returns the original index. Implementators of {@link ColumnViewer} should 306 * overwrite this method if their widget supports reordered columns</b> 307 * </p> 308 * 309 * @param visualIndex 310 * the current index (as shown in the UI) 311 * @return the original index 312 * @since 3.4 313 */ getCreationIndex(int visualIndex)314 protected int getCreationIndex(int visualIndex) { 315 return visualIndex; 316 } 317 318 /** 319 * The location and bounds of the area where the text is drawn depends on 320 * various things (image displayed, control with SWT.CHECK) 321 * 322 * @param index 323 * the column index 324 * @return the bounds of the of the text area. May return <code>null</code> 325 * if the underlying widget implementation doesn't provide this 326 * information 327 * @since 3.4 328 */ getTextBounds(int index)329 public Rectangle getTextBounds(int index) { 330 return null; 331 } 332 333 /** 334 * Returns the location and bounds of the area where the image is drawn. 335 * 336 * @param index 337 * the column index 338 * @return the bounds of the of the image area. May return <code>null</code> 339 * if the underlying widget implementation doesn't provide this 340 * information 341 * @since 3.4 342 */ getImageBounds(int index)343 public Rectangle getImageBounds(int index) { 344 return null; 345 } 346 347 /** 348 * Set the style ranges to be applied on the text label at the column index 349 * Note: Requires {@link StyledCellLabelProvider} with owner draw enabled. 350 * 351 * @param columnIndex 352 * the index of the column 353 * @param styleRanges 354 * the styled ranges 355 * 356 * @since 3.4 357 */ setStyleRanges(int columnIndex, StyleRange[] styleRanges)358 public void setStyleRanges(int columnIndex, StyleRange[] styleRanges) { 359 getItem().setData(getStyleRangesDataKey(columnIndex), styleRanges); 360 } 361 getStyleRangesDataKey(int columnIndex)362 private String getStyleRangesDataKey(int columnIndex) { 363 if (columnIndex == 0) 364 return KEY_TEXT_LAYOUT_0; 365 366 if (cachedDataKeys == null) { 367 int size = Math.max(10, columnIndex + 1); 368 cachedDataKeys= new String[size]; 369 for (int i = 1; i < cachedDataKeys.length; i++) { 370 cachedDataKeys[i] = KEY_TEXT_LAYOUT + i; 371 } 372 } else if (columnIndex >= cachedDataKeys.length) { 373 String[] newCachedDataKeys = new String[columnIndex + 1]; 374 System.arraycopy(cachedDataKeys, 0, newCachedDataKeys, 0, cachedDataKeys.length); 375 for (int i = cachedDataKeys.length; i < newCachedDataKeys.length; i++) { 376 newCachedDataKeys[i] = KEY_TEXT_LAYOUT + i; 377 } 378 cachedDataKeys = newCachedDataKeys; 379 } 380 return cachedDataKeys[columnIndex]; 381 } 382 383 /** 384 * Returns the style ranges to be applied on the text label at the column 385 * index or <code>null</code> if no style ranges have been set. 386 * 387 * @param columnIndex 388 * the index of the column 389 * @return styleRanges the styled ranges 390 * 391 * @since 3.4 392 */ getStyleRanges(int columnIndex)393 public StyleRange[] getStyleRanges(int columnIndex) { 394 return (StyleRange[]) getItem().getData(getStyleRangesDataKey(columnIndex)); 395 } 396 getWidth(int columnIndex)397 int getWidth(int columnIndex) { 398 return getBounds(columnIndex).width; 399 } 400 401 /** 402 * Scrolls the cell at this index into view 403 * <p> 404 * <b>Because of backwards API compatibility the default implementation is a 405 * no-op. Implementators of {@link ColumnViewer} should overwrite this 406 * method if their widget supports reordered columns</b> 407 * </p> 408 * 409 * @param columnIndex 410 * the column index 411 * @return return <code>true</code> when the cell is scrolled into view 412 * @since 3.5 413 */ scrollCellIntoView(int columnIndex)414 protected boolean scrollCellIntoView(int columnIndex) { 415 return false; 416 } 417 418 /** 419 * Returns <code>true</code> if the column with the given index is visible 420 * 421 * @param columnIndex 422 * the column index 423 * 424 * @return <code>true</code> if the column is visible 425 * @since 3.5 426 */ isColumnVisible(int columnIndex)427 protected boolean isColumnVisible(int columnIndex) { 428 return getWidth(columnIndex) > 0; 429 } 430 } 431