tabletreeitem.java

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

JAVA
945
字号
    checkWidget();
    return getText(0);
  }

  /**
   * Gets the item text at the specified index.
   * <p>
   * Indexing is zero based.
   *
   * This operation will fail when the index is out
   * of range or an item could not be queried from
   * the OS.
   *
   * @param index the index of the item
   * @return the item text at the specified index, which can be null
   */
  public String getText(int index) {
    //checkWidget();
    if (0 <= index && index < texts.length)
      return texts[index];
    return null;
  }

  boolean getVisible() {
    return tableItem != null;
  }

  /**
   * Gets the index of the specified item.
   * 
   * <p>The widget is searched starting at 0 until an
   * item is found that is equal to the search item.
   * If no item is found, -1 is returned.  Indexing
   * is zero based.  This index is relative to the parent only.
   *
   * @param item the search item
   * @return the index of the item or -1 if the item is not found
   *
   */
  public int indexOf(TableTreeItem item) {
    //checkWidget();  
    for (int i = 0; i < items.length; i++) {
      if (items[i] == item)
        return i;
    }
    return -1;
  }

  void expandAll(boolean notify) {
    if (items.length == 0)
      return;
    if (!expanded) {
      setExpanded(true);
      if (notify) {
        Event event = new Event();
        event.item = this;
        parent.notifyListeners(SWT.Expand, event);
      }
    }
    for (int i = 0; i < items.length; i++) {
      items[i].expandAll(notify);
    }
  }

  int expandedIndexOf(TableTreeItem item) {
    int index = 0;
    for (int i = 0; i < items.length; i++) {
      if (items[i] == item)
        return index;
      if (items[i].expanded)
        index += items[i].visibleChildrenCount();
      index++;
    }
    return -1;
  }

  int visibleChildrenCount() {
    int count = 0;
    for (int i = 0; i < items.length; i++) {
      if (items[i].getVisible()) {
        count += 1 + items[i].visibleChildrenCount();
      }
    }
    return count;
  }

  public void dispose() {
    if (isDisposed())
      return;
    for (int i = items.length - 1; i >= 0; i--) {
      items[i].dispose();
    }
    super.dispose();
    if (!parent.inDispose) {
      if (parentItem != null) {
        parentItem.removeItem(this);
      } else {
        parent.removeItem(this);
      }
      if (tableItem != null)
        tableItem.dispose();
    }
    items = null;
    parentItem = null;
    parent = null;
    images = null;
    texts = null;
    tableItem = null;
    foreground = null;
    background = null;
    font = null;
  }

  void removeItem(TableTreeItem item) {
    int index = 0;
    while (index < items.length && items[index] != item)
      index++;
    if (index == items.length)
      return;
    TableTreeItem[] newItems = new TableTreeItem[items.length - 1];
    System.arraycopy(items, 0, newItems, 0, index);
    System.arraycopy(items, index + 1, newItems, index, items.length - index - 1);
    items = newItems;
    if (items.length == 0) {
      if (tableItem != null)
        tableItem.setImage(0, null);
    }
  }

  /**
   * Sets the receiver's background color to the color specified
   * by the argument, or to the default system color for the item
   * if the argument is null.
   *
   * @param color the new color (or null)
   * 
   * @exception IllegalArgumentException <ul>
   *    <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li> 
   * </ul>
   * @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 void setBackground(Color color) {
    checkWidget();
    if (color != null && color.isDisposed()) {
      SWT.error(SWT.ERROR_INVALID_ARGUMENT);
    }
    if (tableItem != null) {
      tableItem.setBackground(color);
    }
    background = color;
  }

  public void setBackground(int index, Color color) {
    checkWidget();
    if (color != null && color.isDisposed()) {
      SWT.error(SWT.ERROR_INVALID_ARGUMENT);
    }
    if (tableItem != null) {
      tableItem.setBackground(index, color);
    }

    if (CustomTableTreeViewer.hilightSorted) {
      if (color != null && color.getRGB().equals(CustomTableTreeViewer.sortedBGColor.getRGB())) {
        sortCol = index;
      } else {
      if (index == sortCol)
          sortCol = -1;
      }
    } else
      sortCol = -1;
    

  }

  public Color getBackground(int index) {
    if (tableItem != null) {
      return tableItem.getBackground(index);
    }
    return parent.getBackground();
  }

  /**
   * Sets the checked state of the checkbox for this item.  This state change 
   * only applies if the Table was created with the SWT.CHECK style.
   *
   * @param checked the new 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 void setChecked(boolean checked) {
    checkWidget();
    Table table = parent.getTable();
    if ((table.getStyle() & SWT.CHECK) == 0)
      return;
    if (tableItem != null) {
      tableItem.setChecked(checked);
    }
    this.checked = checked;
  }

  /**
   * Sets the grayed state of the checkbox for this item.  This state change 
   * only applies if the Table was created with the SWT.CHECK style.
   *
   * @param grayed the new 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 void setGrayed(boolean grayed) {
    checkWidget();
    Table table = parent.getTable();
    if ((table.getStyle() & SWT.CHECK) == 0)
      return;
    if (tableItem != null) {
      tableItem.setGrayed(grayed);
    }
    this.grayed = grayed;
  }

  /**
   * Sets the expanded state.
   * <p>
   * @param expanded the new expanded state.
   *
   * @exception SWTException <ul>
   *    <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
   *    <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
   *  </ul>
   */
  public void setExpanded(boolean expanded) {
    checkWidget();
    if (items.length == 0)
      return;
    if (this.expanded == expanded)
      return;
    this.expanded = expanded;
    if (tableItem == null)
      return;
    parent.setRedraw(false);
    for (int i = 0; i < items.length; i++) {
      items[i].setVisible(expanded);
      if (expanded && sortCol != -1 && CustomTableTreeViewer.hilightSorted)
        items[i].setBackground(sortCol, CustomTableTreeViewer.sortedBGColor);
    }
    Image image = expanded ? parent.getMinusImage() : parent.getPlusImage();
    tableItem.setImage(0, image);
    parent.setRedraw(true);
  }

  /**
   * Sets the font that the receiver will use to paint textual information
   * for this item to the font specified by the argument, or to the default font
   * for that kind of control if the argument is null.
   *
   * @param font the new font (or null)
   *
   * @exception IllegalArgumentException <ul>
   *    <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li> 
   * </ul>
   * @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 void setFont(Font font) {
    checkWidget();
    if (font != null && font.isDisposed()) {
      SWT.error(SWT.ERROR_INVALID_ARGUMENT);
    }
    if (tableItem != null) {
      tableItem.setFont(font);
    }
    this.font = font;
  }

  /**
   * Sets the receiver's foreground color to the color specified
   * by the argument, or to the default system color for the item
   * if the argument is null.
   *
   * @param color the new color (or null)
   *
   * @since 2.0
   * 
   * @exception IllegalArgumentException <ul>
   *    <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li> 
   * </ul>
   * @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 void setForeground(Color color) {
    checkWidget();
    if (color != null && color.isDisposed()) {
      SWT.error(SWT.ERROR_INVALID_ARGUMENT);
    }
    if (tableItem != null) {
      tableItem.setForeground(color);
    }
    foreground = color;
  }

  /**
   * Sets the image at an index.
   * <p>
   * The image can be null.
   * The image in column 0 is reserved for the [+] and [-]
   * images of the tree, therefore do nothing if index is 0.
   *
   * @param image the new image or null
   *
   * @exception SWTException <ul>
   *    <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
   *    <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
   *  </ul>
   */
  public void setImage(int index, Image image) {
    checkWidget();
    int columnCount = Math.max(parent.getTable().getColumnCount(), 1);
    if (index <= 0 || index >= columnCount)
      return;
    if (images.length < columnCount) {
      Image[] newImages = new Image[columnCount];
      System.arraycopy(images, 0, newImages, 0, images.length);
      images = newImages;
    }
    images[index] = image;
    if (tableItem != null)
      tableItem.setImage(index, image);
  }

  /**
   * Sets the first image.
   * <p>
   * The image can be null.
   * The image in column 0 is reserved for the [+] and [-]
   * images of the tree, therefore do nothing.
   *
   * @param image the new image or null
   * 
   * @exception SWTException <ul>
   *    <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
   *    <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
   *  </ul>
   */
  public void setImage(Image image) {
    setImage(0, image);
  }

  /**
   * Sets the widget text.
   * <p>
   *
   * The widget text for an item is the label of the
   * item or the label of the text specified by a column
   * number.
   *
   * @param index the column number
   * @param text the new text
   *
   * @exception IllegalArgumentException <ul>
   *    <li>ERROR_NULL_ARGUMENT - if the text is null</li>
   * </ul>
   * @exception SWTException <ul>
   *    <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
   *    <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
   *  </ul>
   */
  public void setText(int index, String text) {
    checkWidget();
    if (text == null)
      SWT.error(SWT.ERROR_NULL_ARGUMENT);
    int columnCount = Math.max(parent.getTable().getColumnCount(), 1);
    if (index < 0 || index >= columnCount)
      return;
    if (texts.length < columnCount) {
      String[] newTexts = new String[columnCount];
      System.arraycopy(texts, 0, newTexts, 0, texts.length);
      texts = newTexts;
    }
    texts[index] = text;
    if (tableItem != null)
      tableItem.setText(index, text);
  }

  public void setText(String string) {
    setText(0, string);
  }

  void setVisible(boolean show) {
    if (parentItem == null)
      return; // this is a root and can not be toggled between visible and hidden
    if (getVisible() == show)
      return;

    if (show) {
      if (!parentItem.getVisible())
        return; // parentItem must already be visible
      // create underlying table item and set data in table item to stored data
      Table table = parent.getTable();
      int parentIndex = table.indexOf(parentItem.tableItem);
      int index = parentItem.expandedIndexOf(this) + parentIndex + 1;
      if (index < 0)
        return;
      tableItem = new TableItem(table, getStyle(), index);
      tableItem.setData(TableTree.ITEMID, this);
      tableItem.setImageIndent(getIndent());
      if (background != null)
        tableItem.setBackground(background);
      if (foreground != null)
        tableItem.setForeground(foreground);
      if (sortCol != -1 && CustomTableTreeViewer.hilightSorted)
        tableItem.setBackground(sortCol,CustomTableTreeViewer.sortedBGColor );
      
      if (font != null)
        tableItem.setFont(font);
      addCheck();

      // restore fields to item
      // ignore any images in the first column
      int columnCount = Math.max(table.getColumnCount(), 1);
      for (int i = 0; i < columnCount; i++) {
        if (i < texts.length && texts[i] != null)
          setText(i, texts[i]);
        if (i < images.length && images[i] != null)
          setImage(i, images[i]);
      }

      // display the children and the appropriate [+]/[-] symbol as required
      if (items.length != 0) {
        if (expanded) {
          tableItem.setImage(0, parent.getMinusImage());
          for (int i = 0, length = items.length; i < length; i++) {
            items[i].setVisible(true);
          }
        } else {
          tableItem.setImage(0, parent.getPlusImage());
        }
      }

    } else {

      for (int i = 0, length = items.length; i < length; i++) {
        items[i].setVisible(false);
      }
      // remove row from table
      tableItem.dispose();
      tableItem = null;
    }
  }
}

⌨️ 快捷键说明

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