/**
  * Returns a rectangle describing the receiver's size and location relative to its parent at a
  * column in the table.
  *
  * @param index the index that specifies the column
  * @return the receiver's bounding column rectangle
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public Rectangle getBounds(final int index) {
   checkWidget();
   if (!parent.checkData(this, parent.indexOf(this))) {
     error(SWT.ERROR_WIDGET_DISPOSED);
   }
   Rectangle result;
   int columnCount = parent.getColumnCount();
   if (columnCount > 0 && (index < 0 || index >= columnCount)) {
     result = new Rectangle(0, 0, 0, 0);
   } else {
     Rectangle textBounds = getTextBounds(index);
     int left = getLeft(index);
     int itemIndex = parent.indexOf(this);
     int top = getTop(itemIndex);
     int width = 0;
     if (index == 0 && columnCount == 0) {
       Rectangle imageBounds = getImageBounds(index);
       int spacing = getSpacing(index);
       int paddingWidth = parent.getCellPadding().width;
       width = imageBounds.width + spacing + textBounds.width + paddingWidth;
     } else if (index >= 0 && index < columnCount) {
       width = parent.getColumn(index).getWidth() - getCheckWidth(index);
     }
     int height = getHeight(index);
     result = new Rectangle(left, top, width, height);
   }
   return result;
 }
 /**
  * Returns a rectangle describing the size and location relative to its parent of an image at a
  * column in the table. An empty rectangle is returned if index exceeds the index of the table's
  * last column.
  *
  * @param index the index that specifies the column
  * @return the receiver's bounding image rectangle
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public Rectangle getImageBounds(final int index) {
   checkWidget();
   if (!parent.checkData(this, parent.indexOf(this))) {
     error(SWT.ERROR_WIDGET_DISPOSED);
   }
   int itemIndex = parent.indexOf(this);
   Rectangle cellPadding = parent.getCellPadding();
   int left = getLeft(index) + cellPadding.x;
   int top = getTop(itemIndex);
   int width = getImageWidth(index);
   int height = getHeight(index);
   return new Rectangle(left, top, width, height);
 }
 /**
  * Returns the image stored at the given column index in the receiver, or null if the image has
  * not been set or if the column does not exist.
  *
  * @param index the column index
  * @return the image stored at the given column index in the receiver
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public Image getImage(final int index) {
   checkWidget();
   if (!parent.checkData(this, parent.indexOf(this))) {
     error(SWT.ERROR_WIDGET_DISPOSED);
   }
   return getImageInternal(index);
 }
 /**
  * Positions the TableCursor over the cell at the given row and column in the parent table.
  *
  * @param row the TableItem of the row for the cell to select
  * @param column the index of column for the cell to select
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public void setSelection(TableItem row, int column) {
   checkWidget();
   int columnCount = table.getColumnCount();
   int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1;
   if (row == null || row.isDisposed() || column < 0 || column > maxColumnIndex)
     SWT.error(SWT.ERROR_INVALID_ARGUMENT);
   setRowColumn(table.indexOf(row), column, false);
 }
 void _resize() {
   if (row == null) {
     setBounds(-200, -200, 0, 0);
   } else {
     int columnIndex = column == null ? 0 : table.indexOf(column);
     setBounds(row.getBounds(columnIndex));
   }
 }
 /**
  * Gets the image indent.
  *
  * @return the indent
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public int getImageIndent() {
   checkWidget();
   if (!parent.checkData(this, parent.indexOf(this))) {
     error(SWT.ERROR_WIDGET_DISPOSED);
   }
   // [rh] The only method to manipulate the image indent (setImageIndent) is
   // deprecated and thus not implemented, therefore we can safely return 0
   return 0;
 }
 /**
  * Returns <code>true</code> if the receiver is grayed, and false otherwise. When the parent does
  * not have the <code>CHECK</code> style, return false.
  *
  * @return the grayed state of the checkbox
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public boolean getGrayed() {
   checkWidget();
   if (!parent.checkData(this, parent.indexOf(this))) {
     error(SWT.ERROR_WIDGET_DISPOSED);
   }
   boolean result = false;
   if ((parent.style & SWT.CHECK) != 0) {
     result = grayed;
   }
   return result;
 }
 /**
  * Returns the text stored at the given column index in the receiver, or empty string if the text
  * has not been set.
  *
  * @param index the column index
  * @return the text stored at the given column index in the receiver
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public String getText(final int index) {
   checkWidget();
   if (!parent.checkData(this, parent.indexOf(this))) {
     error(SWT.ERROR_WIDGET_DISPOSED);
   }
   String result = "";
   if (data != null && index >= 0 && index < data.length && data[index] != null) {
     result = data[index].text;
   }
   return result;
 }
 /**
  * Returns the font that the receiver will use to paint textual information for this item.
  *
  * @return the receiver's font
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public Font getFont() {
   checkWidget();
   if (!parent.checkData(this, parent.indexOf(this))) {
     error(SWT.ERROR_WIDGET_DISPOSED);
   }
   Font result;
   if (font == null) {
     result = parent.getFont();
   } else {
     result = font;
   }
   return result;
 }
 /**
  * Returns the foreground color that the receiver will use to draw.
  *
  * @return the receiver's foreground color
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public Color getForeground() {
   checkWidget();
   if (!parent.checkData(this, parent.indexOf(this))) {
     error(SWT.ERROR_WIDGET_DISPOSED);
   }
   Color result;
   if (foreground == null) {
     result = parent.getForeground();
   } else {
     result = foreground;
   }
   return result;
 }
 void tableMouseDown(Event event) {
   if (isDisposed() || !isVisible()) return;
   Point pt = new Point(event.x, event.y);
   int lineWidth = table.getLinesVisible() ? table.getGridLineWidth() : 0;
   TableItem item = table.getItem(pt);
   if ((table.getStyle() & SWT.FULL_SELECTION) != 0) {
     if (item == null) return;
   } else {
     int start = item != null ? table.indexOf(item) : table.getTopIndex();
     int end = table.getItemCount();
     Rectangle clientRect = table.getClientArea();
     for (int i = start; i < end; i++) {
       TableItem nextItem = table.getItem(i);
       Rectangle rect = nextItem.getBounds(0);
       if (pt.y >= rect.y && pt.y < rect.y + rect.height + lineWidth) {
         item = nextItem;
         break;
       }
       if (rect.y > clientRect.y + clientRect.height) return;
     }
     if (item == null) return;
   }
   TableColumn newColumn = null;
   int columnCount = table.getColumnCount();
   if (columnCount == 0) {
     if ((table.getStyle() & SWT.FULL_SELECTION) == 0) {
       Rectangle rect = item.getBounds(0);
       rect.width += lineWidth;
       rect.height += lineWidth;
       if (!rect.contains(pt)) return;
     }
   } else {
     for (int i = 0; i < columnCount; i++) {
       Rectangle rect = item.getBounds(i);
       rect.width += lineWidth;
       rect.height += lineWidth;
       if (rect.contains(pt)) {
         newColumn = table.getColumn(i);
         break;
       }
     }
     if (newColumn == null) {
       if ((table.getStyle() & SWT.FULL_SELECTION) == 0) return;
       newColumn = table.getColumn(0);
     }
   }
   setRowColumn(item, newColumn, true);
   setFocus();
   return;
 }
 /**
  * Returns the foreground color at the given column index in the receiver.
  *
  * @param index the column index
  * @return the foreground color
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public Color getForeground(final int index) {
   checkWidget();
   if (!parent.checkData(this, parent.indexOf(this))) {
     error(SWT.ERROR_WIDGET_DISPOSED);
   }
   Color result = getForeground();
   if (data != null
       && index >= 0
       && index < data.length
       && data[index] != null
       && data[index].foreground != null) {
     result = data[index].foreground;
   }
   return result;
 }
 /**
  * Returns the font that the receiver will use to paint textual information for the specified cell
  * in this item.
  *
  * @param index the column index
  * @return the receiver's font
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public Font getFont(final int index) {
   checkWidget();
   if (!parent.checkData(this, parent.indexOf(this))) {
     error(SWT.ERROR_WIDGET_DISPOSED);
   }
   Font result = getFont();
   if (data != null
       && index >= 0
       && index < data.length
       && data[index] != null
       && data[index].font != null) {
     result = data[index].font;
   }
   return result;
 }
 /**
  * Returns a rectangle describing the size and location relative to its parent of the text at a
  * column in the table. An empty rectangle is returned if index exceeds the index of the table's
  * last column.
  *
  * @param index the index that specifies the column
  * @return the receiver's bounding text rectangle
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public Rectangle getTextBounds(final int index) {
   checkWidget();
   int itemIndex = parent.indexOf(this);
   if (!parent.checkData(this, itemIndex)) {
     error(SWT.ERROR_WIDGET_DISPOSED);
   }
   int left = 0;
   int top = 0;
   int width = 0;
   Rectangle cellPadding = parent.getCellPadding();
   if (index == 0 && parent.getColumnCount() == 0) {
     int imageWidth = 0;
     int spacing = 0;
     if (parent.hasColumnImages(0)) {
       imageWidth = parent.getItemImageSize().x;
       spacing = getSpacing(0);
     }
     left = getLeft(0) + cellPadding.x + imageWidth + spacing;
     top = getTop(itemIndex);
     Font font = getFont();
     width = Graphics.stringExtent(font, getText(0)).x;
   } else if (itemIndex != -1 && index < parent.getColumnCount()) {
     int imageWidth = 0;
     if (parent.hasColumnImages(index)) {
       imageWidth = parent.getItemImageSize().x;
     }
     int spacing = getSpacing(index);
     left = getLeft(index) + cellPadding.x + imageWidth + spacing;
     top = getTop(itemIndex);
     width = getColumnWidth(index) - cellPadding.width - imageWidth - spacing;
     if (width < 0) {
       width = 0;
     }
   }
   int height = getHeight(index);
   return new Rectangle(left, top, width, height);
 }
 void setRowColumn(TableItem row, TableColumn column, boolean notify) {
   if (this.row == row && this.column == column) {
     return;
   }
   if (this.row != null && this.row != row) {
     this.row.removeListener(SWT.Dispose, disposeItemListener);
     this.row = null;
   }
   if (this.column != null && this.column != column) {
     this.column.removeListener(SWT.Dispose, disposeColumnListener);
     this.column.removeListener(SWT.Move, resizeListener);
     this.column.removeListener(SWT.Resize, resizeListener);
     this.column = null;
   }
   if (row != null) {
     if (this.row != row) {
       this.row = row;
       row.addListener(SWT.Dispose, disposeItemListener);
       table.showItem(row);
     }
     if (this.column != column && column != null) {
       this.column = column;
       column.addListener(SWT.Dispose, disposeColumnListener);
       column.addListener(SWT.Move, resizeListener);
       column.addListener(SWT.Resize, resizeListener);
       table.showColumn(column);
     }
     int columnIndex = column == null ? 0 : table.indexOf(column);
     setBounds(row.getBounds(columnIndex));
     redraw();
     if (notify) {
       notifyListeners(SWT.Selection, new Event());
     }
   }
   getAccessible().setFocus(ACC.CHILDID_SELF);
 }
 void keyDown(Event event) {
   if (row == null) return;
   switch (event.character) {
     case SWT.CR:
       notifyListeners(SWT.DefaultSelection, new Event());
       return;
   }
   int rowIndex = table.indexOf(row);
   int columnIndex = column == null ? 0 : table.indexOf(column);
   switch (event.keyCode) {
     case SWT.ARROW_UP:
       setRowColumn(Math.max(0, rowIndex - 1), columnIndex, true);
       break;
     case SWT.ARROW_DOWN:
       setRowColumn(Math.min(rowIndex + 1, table.getItemCount() - 1), columnIndex, true);
       break;
     case SWT.ARROW_LEFT:
     case SWT.ARROW_RIGHT:
       {
         int columnCount = table.getColumnCount();
         if (columnCount == 0) break;
         int[] order = table.getColumnOrder();
         int index = 0;
         while (index < order.length) {
           if (order[index] == columnIndex) break;
           index++;
         }
         if (index == order.length) index = 0;
         int leadKey = (getStyle() & SWT.RIGHT_TO_LEFT) != 0 ? SWT.ARROW_RIGHT : SWT.ARROW_LEFT;
         if (event.keyCode == leadKey) {
           setRowColumn(rowIndex, order[Math.max(0, index - 1)], true);
         } else {
           setRowColumn(rowIndex, order[Math.min(columnCount - 1, index + 1)], true);
         }
         break;
       }
     case SWT.HOME:
       setRowColumn(0, columnIndex, true);
       break;
     case SWT.END:
       {
         int i = table.getItemCount() - 1;
         setRowColumn(i, columnIndex, true);
         break;
       }
     case SWT.PAGE_UP:
       {
         int index = table.getTopIndex();
         if (index == rowIndex) {
           Rectangle rect = table.getClientArea();
           TableItem item = table.getItem(index);
           Rectangle itemRect = item.getBounds(0);
           rect.height -= itemRect.y;
           int height = table.getItemHeight();
           int page = Math.max(1, rect.height / height);
           index = Math.max(0, index - page + 1);
         }
         setRowColumn(index, columnIndex, true);
         break;
       }
     case SWT.PAGE_DOWN:
       {
         int index = table.getTopIndex();
         Rectangle rect = table.getClientArea();
         TableItem item = table.getItem(index);
         Rectangle itemRect = item.getBounds(0);
         rect.height -= itemRect.y;
         int height = table.getItemHeight();
         int page = Math.max(1, rect.height / height);
         int end = table.getItemCount() - 1;
         index = Math.min(end, index + page - 1);
         if (index == rowIndex) {
           index = Math.min(end, index + page - 1);
         }
         setRowColumn(index, columnIndex, true);
         break;
       }
   }
 }
 void releaseParent() {
   parent.destroyItem(this, parent.indexOf(this));
 }
 /**
  * Returns the index of the column over which the TableCursor is positioned.
  *
  * @return the column index for the current position
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public int getColumn() {
   checkWidget();
   return column == null ? 0 : table.indexOf(column);
 }
 void paint(Event event) {
   if (row == null) return;
   int columnIndex = column == null ? 0 : table.indexOf(column);
   GC gc = event.gc;
   gc.setBackground(getBackground());
   gc.setForeground(getForeground());
   gc.fillRectangle(event.x, event.y, event.width, event.height);
   int x = 0;
   Point size = getSize();
   Image image = row.getImage(columnIndex);
   if (image != null) {
     Rectangle imageSize = image.getBounds();
     int imageY = (size.y - imageSize.height) / 2;
     gc.drawImage(image, x, imageY);
     x += imageSize.width;
   }
   String text = row.getText(columnIndex);
   if (text.length() > 0) {
     Rectangle bounds = row.getBounds(columnIndex);
     Point extent = gc.stringExtent(text);
     // Temporary code - need a better way to determine table trim
     String platform = SWT.getPlatform();
     if ("win32".equals(platform)) { // $NON-NLS-1$
       if (table.getColumnCount() == 0 || columnIndex == 0) {
         x += 2;
       } else {
         int alignmnent = column.getAlignment();
         switch (alignmnent) {
           case SWT.LEFT:
             x += 6;
             break;
           case SWT.RIGHT:
             x = bounds.width - extent.x - 6;
             break;
           case SWT.CENTER:
             x += (bounds.width - x - extent.x) / 2;
             break;
         }
       }
     } else {
       if (table.getColumnCount() == 0) {
         x += 5;
       } else {
         int alignmnent = column.getAlignment();
         switch (alignmnent) {
           case SWT.LEFT:
             x += 5;
             break;
           case SWT.RIGHT:
             x = bounds.width - extent.x - 2;
             break;
           case SWT.CENTER:
             x += (bounds.width - x - extent.x) / 2 + 2;
             break;
         }
       }
     }
     int textY = (size.y - extent.y) / 2;
     gc.drawString(text, x, textY);
   }
   if (isFocusControl()) {
     Display display = getDisplay();
     gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
     gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
     gc.drawFocus(0, 0, size.x, size.y);
   }
 }