tabletreeitem.java

来自「SANCHO」· Java 代码 · 共 945 行 · 第 1/2 页

JAVA
945
字号
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.custom;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Widget;

import sancho.view.viewer.CustomTableTreeViewer;

/**
 * A TableTreeItem is a selectable user interface object
 * that represents an item in a heirarchy of items in a
 * TableTree.
 */
public class TableTreeItem extends Item {
  TableItem tableItem;
  TableTree parent;
  TableTreeItem parentItem;
  TableTreeItem[] items = TableTree.EMPTY_ITEMS;
  String[] texts = TableTree.EMPTY_TEXTS;
  Image[] images = TableTree.EMPTY_IMAGES;
  Color background;
  Color foreground;
  Font font;
  boolean expanded;
  boolean checked;
  boolean grayed;
  int sortCol = -1;

  /**
   * Constructs a new instance of this class given its parent
   * (which must be a <code>TableTree</code>)
   * and a style value describing its behavior and appearance.
   * The item is added to the end of the items maintained by its parent.
   * <p>
   * The style value is either one of the style constants defined in
   * class <code>SWT</code> which is applicable to instances of this
   * class, or must be built by <em>bitwise OR</em>'ing together 
   * (that is, using the <code>int</code> "|" operator) two or more
   * of those <code>SWT</code> style constants. The class description
   * lists the style constants that are applicable to the class.
   * Style bits are also inherited from superclasses.
   * </p>
   *
   * @param parent a composite control which will be the parent of the new instance (cannot be null)
   * @param style the style of control to construct
   *
   * @exception IllegalArgumentException <ul>
   *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
   * </ul>
   * @exception SWTException <ul>
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
   * </ul>
   *
   * @see SWT
   * @see Widget#getStyle()
   */
  public TableTreeItem(TableTree parent, int style) {
    this(parent, style, parent.getItemCount());
  }

  /**
   * Constructs a new instance of this class given its parent
   * (which must be a <code>TableTree</code>,
   * a style value describing its behavior and appearance, and the index
   * at which to place it in the items maintained by its parent.
   * <p>
   * The style value is either one of the style constants defined in
   * class <code>SWT</code> which is applicable to instances of this
   * class, or must be built by <em>bitwise OR</em>'ing together 
   * (that is, using the <code>int</code> "|" operator) two or more
   * of those <code>SWT</code> style constants. The class description
   * lists the style constants that are applicable to the class.
   * Style bits are also inherited from superclasses.
   * </p>
   *
   * @param parent a composite control which will be the parent of the new instance (cannot be null)
   * @param style the style of control to construct
   * @param index the index to store the receiver in its parent
   *
   * @exception IllegalArgumentException <ul>
   *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
   * </ul>
   * @exception SWTException <ul>
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
   * </ul>
   *
   * @see SWT
   * @see Widget#getStyle()
   */
  public TableTreeItem(TableTree parent, int style, int index) {
    this(parent, null, style, index);
  }

  /**
   * Constructs a new instance of this class given its parent
   * (which must be a <code>TableTreeItem</code>)
   * and a style value describing its behavior and appearance.
   * The item is added to the end of the items maintained by its parent.
   * <p>
   * The style value is either one of the style constants defined in
   * class <code>SWT</code> which is applicable to instances of this
   * class, or must be built by <em>bitwise OR</em>'ing together 
   * (that is, using the <code>int</code> "|" operator) two or more
   * of those <code>SWT</code> style constants. The class description
   * lists the style constants that are applicable to the class.
   * Style bits are also inherited from superclasses.
   * </p>
   *
   * @param parent a composite control which will be the parent of the new instance (cannot be null)
   * @param style the style of control to construct
   *
   * @exception IllegalArgumentException <ul>
   *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
   * </ul>
   * @exception SWTException <ul>
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
   * </ul>
   *
   * @see SWT
   * @see Widget#getStyle()
   */
  public TableTreeItem(TableTreeItem parent, int style) {
    this(parent, style, parent.getItemCount());
  }

  /**
   * Constructs a new instance of this class given its parent
   * (which must be a <code>TableTreeItem</code>),
   * a style value describing its behavior and appearance, and the index
   * at which to place it in the items maintained by its parent.
   * <p>
   * The style value is either one of the style constants defined in
   * class <code>SWT</code> which is applicable to instances of this
   * class, or must be built by <em>bitwise OR</em>'ing together 
   * (that is, using the <code>int</code> "|" operator) two or more
   * of those <code>SWT</code> style constants. The class description
   * lists the style constants that are applicable to the class.
   * Style bits are also inherited from superclasses.
   * </p>
   *
   * @param parent a composite control which will be the parent of the new instance (cannot be null)
   * @param style the style of control to construct
   * @param index the index to store the receiver in its parent
   *
   * @exception IllegalArgumentException <ul>
   *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
   * </ul>
   * @exception SWTException <ul>
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
   * </ul>
   *
   * @see SWT
   * @see Widget#getStyle()
   */
  public TableTreeItem(TableTreeItem parent, int style, int index) {
    this(parent.getParent(), parent, style, index);
  }

  TableTreeItem(TableTree parent, TableTreeItem parentItem, int style, int index) {
    super(parent, style);
    this.parent = parent;
    this.parentItem = parentItem;
    if (parentItem == null) {

      /* Root items are visible immediately */
      int tableIndex = parent.addItem(this, index);
      tableItem = new TableItem(parent.getTable(), style, tableIndex);
      tableItem.setData(TableTree.ITEMID, this);
      addCheck();
      /*
       * Feature in the Table.  The table uses the first image that
       * is inserted into the table to size the table rows.  If the
       * user is allowed to insert the first image, this will cause
       * the +/- images to be scaled.  The fix is to insert a dummy
       * image to force the size.
       */
      if (parent.sizeImage == null) {
        int itemHeight = parent.getItemHeight();
        parent.sizeImage = new Image(null, itemHeight, itemHeight);
        GC gc = new GC(parent.sizeImage);
        gc.setBackground(parent.getBackground());
        gc.fillRectangle(0, 0, itemHeight, itemHeight);
        gc.dispose();
        tableItem.setImage(0, parent.sizeImage);
      }
    } else {
      parentItem.addItem(this, index);
    }
  }

  void addCheck() {
    Table table = parent.getTable();
    if ((table.getStyle() & SWT.CHECK) == 0)
      return;
    tableItem.setChecked(checked);
    tableItem.setGrayed(grayed);
  }

  void addItem(TableTreeItem item, int index) {
    if (item == null)
      SWT.error(SWT.ERROR_NULL_ARGUMENT);
    if (index < 0 || index > items.length)
      SWT.error(SWT.ERROR_INVALID_ARGUMENT);

    /* Now that item has a sub-node it must indicate that it can be expanded */
    if (items.length == 0 && index == 0) {
      if (tableItem != null) {
        Image image = expanded ? parent.getMinusImage() : parent.getPlusImage();
        tableItem.setImage(0, image);
      }
    }

    /* Put the item in the items list */
    TableTreeItem[] newItems = new TableTreeItem[items.length + 1];
    System.arraycopy(items, 0, newItems, 0, index);
    newItems[index] = item;
    System.arraycopy(items, index, newItems, index + 1, items.length - index);
    items = newItems;
    if (expanded)
      item.setVisible(true);
  }

  /**
   * Returns the receiver's background color.
   *
   * @return the background color
   * 
   * @exception SWTException <ul>
   *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
   * </ul>
   * 
   * @since 2.0
   * 
   */
  public Color getBackground() {
    checkWidget();
    return (background == null) ? parent.getBackground() : background;
  }

  /**
   * Returns a rectangle describing the receiver's size and location
   * relative to its parent.
   *
   * @return the receiver's bounding rectangle
   *
   * @exception SWTException <ul>
   *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
   * </ul>
   */
  public Rectangle getBounds(int index) {
    checkWidget();
    if (tableItem != null) {
      return tableItem.getBounds(index);
    } else {
      return new Rectangle(0, 0, 0, 0);
    }
  }

  /**
   * Returns <code>true</code> if the receiver is checked,
   * and false otherwise.  When the parent does not have
   * the <code>CHECK style, return false.
   *
   * @return the checked state of the checkbox
   *
   * @exception SWTException <ul>
   *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
   * </ul>
   */
  public boolean getChecked() {
    checkWidget();
    if (tableItem == null)
      return checked;
    return tableItem.getChecked();
  }

  /**
   * 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>
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
   * </ul>
   * 
   * @since 2.1
   */
  public boolean getGrayed() {
    checkWidget();
    if (tableItem == null)
      return grayed;
    return tableItem.getGrayed();
  }

  /**
   * Returns <code>true</code> if the receiver is expanded,
   * and false otherwise.
   * <p>
   *
   * @return the expanded state
   */
  public boolean getExpanded() {
    //checkWidget();
    return expanded;
  }

  /**
   * 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>
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
   * </ul>
   *
   * @since 3.0
   */
  public Font getFont() {
    checkWidget();
    return (font == null) ? parent.getFont() : font;
  }

  /**
   * 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>
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
   * </ul>
   * 
   * @since 2.0
   * 
   */
  public Color getForeground() {
    checkWidget();
    return (foreground == null) ? parent.getForeground() : foreground;
  }

  /**
   * Gets the first image.
   * <p>
   * The image in column 0 is reserved for the [+] and [-]
   * images of the tree, therefore getImage(0) will return null.
   *
   * @return the image at index 0
   * 
   * @exception SWTException <ul>
   *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
   * </ul>
   */
  public Image getImage() {
    checkWidget();
    return getImage(0);
  }

  /**
   * Gets the image at the specified index.
   * <p>
   * Indexing is zero based. The image can be null.
   * The image in column 0 is reserved for the [+] and [-]
   * images of the tree, therefore getImage(0) will return null.
   * Return null if the index is out of range.
   *
   * @param index the index of the image
   * @return the image at the specified index or null
   */
  public Image getImage(int index) {
    //checkWidget();
    if (0 < index && index < images.length)
      return images[index];
    return null;
  }

  int getIndent() {
    if (parentItem == null)
      return 0;
    return parentItem.getIndent() + 1;
  }

  /**
   * Returns the number of items contained in the receiver
   * that are direct item children of the receiver.
   *
   * @return the number of items
   */
  public int getItemCount() {
    //checkWidget();
    return items.length;
  }

  /**
   * Returns an array of <code>TableTreeItem</code>s which are the
   * direct item children of the receiver.
   * <p>
   * Note: This is not the actual structure used by the receiver
   * to maintain its list of items, so modifying the array will
   * not affect the receiver. 
   * </p>
   *
   * @return the receiver's items
   */
  public TableTreeItem[] getItems() {
    //checkWidget();
    TableTreeItem[] newItems = new TableTreeItem[items.length];
    System.arraycopy(items, 0, newItems, 0, items.length);
    return newItems;
  }

  TableTreeItem getItem(TableItem tableItem) {
    if (tableItem == null)
      return null;
    if (this.tableItem == tableItem)
      return this;
    for (int i = 0; i < items.length; i++) {
      TableTreeItem item = items[i].getItem(tableItem);
      if (item != null)
        return item;
    }
    return null;
  }

  /**
   * Returns the receiver's parent, which must be a <code>TableTree</code>.
   *
   * @return the receiver's parent
   */
  public TableTree getParent() {
    //checkWidget();
    return parent;
  }

  /**
   * Returns the receiver's parent item, which must be a
   * <code>TableTreeItem</code> or null when the receiver is a
   * root.
   *
   * @return the receiver's parent item
   */
  public TableTreeItem getParentItem() {
    //checkWidget();
    return parentItem;
  }

  public String getText() {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?