Java Tutorial/SWT/TreeColumn TreeTable — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:21, 31 мая 2010

Create a TreeTable

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * Create a Tree with columns
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.1
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
public class TreeTableCreation {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Tree tree = new Tree(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    tree.setHeaderVisible(true);
    TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
    column1.setText("Column 1");
    column1.setWidth(200);
    TreeColumn column2 = new TreeColumn(tree, SWT.CENTER);
    column2.setText("Column 2");
    column2.setWidth(200);
    TreeColumn column3 = new TreeColumn(tree, SWT.RIGHT);
    column3.setText("Column 3");
    column3.setWidth(200);
    for (int i = 0; i < 4; i++) {
      TreeItem item = new TreeItem(tree, SWT.NONE);
      item.setText(new String[] { "item " + i, "abc", "defghi" });
      for (int j = 0; j < 4; j++) {
        TreeItem subItem = new TreeItem(item, SWT.NONE);
        subItem.setText(new String[] { "subitem " + j, "jklmnop", "qrs" });
        for (int k = 0; k < 4; k++) {
          TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
          subsubItem.setText(new String[] { "subsubitem " + k, "tuv", "wxyz" });
        }
      }
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Multi-line TreeTable

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/* 
 * Tree example snippet: Multiple lines in a TreeItem
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
public class TreeItemLines {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Multiple lines in a TreeItem");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
    tree.setHeaderVisible(true);
    tree.setLinesVisible(true);
    int columnCount = 4;
    for (int i = 0; i < columnCount; i++) {
      TreeColumn column = new TreeColumn(tree, SWT.NONE);
      column.setText("Column " + i);
      column.setWidth(100);
    }
    int itemCount = 3;
    for (int i = 0; i < itemCount; i++) {
      TreeItem item1 = new TreeItem(tree, SWT.NONE);
      item1.setText("item " + i);
      for (int c = 1; c < columnCount; c++) {
        item1.setText(c, "item [" + i + "-" + c + "]");
      }
      for (int j = 0; j < itemCount; j++) {
        TreeItem item2 = new TreeItem(item1, SWT.NONE);
        item2.setText("item [" + i + " " + j + "]");
        for (int c = 1; c < columnCount; c++) {
          item2.setText(c, "item [" + i + " " + j + "-" + c + "]");
        }
        for (int k = 0; k < itemCount; k++) {
          TreeItem item3 = new TreeItem(item2, SWT.NONE);
          item3.setText("item [" + i + " " + j + " " + k + "]");
          for (int c = 1; c < columnCount; c++) {
            item3.setText(c, "item [" + i + " " + j + " " + k + "-" + c + "]");
          }
        }
      }
    }
    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    Listener paintListener = new Listener() {
      public void handleEvent(Event event) {
        switch (event.type) {
        case SWT.MeasureItem: {
          TreeItem item = (TreeItem) event.item;
          String text = getText(item, event.index);
          Point size = event.gc.textExtent(text);
          event.width = size.x;
          event.height = Math.max(event.height, size.y);
          break;
        }
        case SWT.PaintItem: {
          TreeItem item = (TreeItem) event.item;
          String text = getText(item, event.index);
          Point size = event.gc.textExtent(text);
          int offset2 = event.index == 0 ? Math.max(0, (event.height - size.y) / 2) : 0;
          event.gc.drawText(text, event.x, event.y + offset2, true);
          break;
        }
        case SWT.EraseItem: {
          event.detail &= ~SWT.FOREGROUND;
          break;
        }
        }
      }
      String getText(TreeItem item, int column) {
        String text = item.getText(column);
        if (column != 0) {
          TreeItem parent = item.getParentItem();
          int index = parent == null ? tree.indexOf(item) : parent.indexOf(item);
          if ((index + column) % 3 == 1) {
            text += "\nnew line";
          }
          if ((index + column) % 3 == 2) {
            text += "\nnew line\nnew line";
          }
        }
        return text;
      }
    };
    tree.addListener(SWT.MeasureItem, paintListener);
    tree.addListener(SWT.PaintItem, paintListener);
    tree.addListener(SWT.EraseItem, paintListener);
    shell.setSize(600, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Show Barchar in TreeTable

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/* 
 * Tree example snippet: Draw a bar graph
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
public class TreeTableBarChart {
  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("Show results as a bar chart in Tree");
    final Tree tree = new Tree(shell, SWT.BORDER);
    tree.setHeaderVisible(true);
    tree.setLinesVisible(true);
    TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
    column1.setText("Bug Status");
    column1.setWidth(100);
    final TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
    column2.setText("Percent");
    column2.setWidth(200);
    String[] states = new String[] { "Resolved", "New", "Won"t Fix", "Invalid" };
    String[] teams = new String[] { "UI", "SWT", "OSGI" };
    for (int i = 0; i < teams.length; i++) {
      TreeItem item = new TreeItem(tree, SWT.NONE);
      item.setText(teams[i]);
      for (int j = 0; j < states.length; j++) {
        TreeItem subItem = new TreeItem(item, SWT.NONE);
        subItem.setText(states[j]);
      }
    }
    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    tree.addListener(SWT.PaintItem, new Listener() {
      int[] percents = new int[] { 50, 30, 5, 15 };
      public void handleEvent(Event event) {
        if (event.index == 1) {
          TreeItem item = (TreeItem) event.item;
          TreeItem parent = item.getParentItem();
          if (parent != null) {
            GC gc = event.gc;
            int index = parent.indexOf(item);
            int percent = percents[index];
            Color foreground = gc.getForeground();
            Color background = gc.getBackground();
            gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
            gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
            int width = (column2.getWidth() - 1) * percent / 100;
            gc.fillGradientRectangle(event.x, event.y, width, event.height, true);
            Rectangle rect2 = new Rectangle(event.x, event.y, width - 1, event.height - 1);
            gc.drawRectangle(rect2);
            gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
            String text = percent + "%";
            Point size = event.gc.textExtent(text);
            int offset = Math.max(0, (event.height - size.y) / 2);
            gc.drawText(text, event.x + 2, event.y + offset, true);
            gc.setForeground(background);
            gc.setBackground(foreground);
          }
        }
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





TreeTable: Allow user to reorder columns and reorder columns programmatically.

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * Allow user to reorder columns and reorder columns programmatically.
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
public class TreeTableReorderColumn {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout(SWT.HORIZONTAL));
    final Tree tree = new Tree(shell, SWT.BORDER | SWT.CHECK);
    tree.setLayoutData(new RowData(-1, 300));
    tree.setHeaderVisible(true);
    TreeColumn column = new TreeColumn(tree, SWT.LEFT);
    column.setText("Column 0");
    column = new TreeColumn(tree, SWT.CENTER);
    column.setText("Column 1");
    column = new TreeColumn(tree, SWT.LEFT);
    column.setText("Column 2");
    column = new TreeColumn(tree, SWT.RIGHT);
    column.setText("Column 3");
    column = new TreeColumn(tree, SWT.CENTER);
    column.setText("Column 4");
    for (int i = 0; i < 5; i++) {
      TreeItem item = new TreeItem(tree, SWT.NONE);
      String[] text = new String[] { i + ":0", i + ":1", i + ":2", i + ":3", i + ":4" };
      item.setText(text);
      for (int j = 0; j < 5; j++) {
        TreeItem subItem = new TreeItem(item, SWT.NONE);
        text = new String[] { i + "," + j + ":0", i + "," + j + ":1", i + "," + j + ":2",
            i + "," + j + ":3", i + "," + j + ":4" };
        subItem.setText(text);
        for (int k = 0; k < 5; k++) {
          TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
          text = new String[] { i + "," + j + "," + k + ":0", i + "," + j + "," + k + ":1",
              i + "," + j + "," + k + ":2", i + "," + j + "," + k + ":3",
              i + "," + j + "," + k + ":4" };
          subsubItem.setText(text);
        }
      }
    }
    Listener listener = new Listener() {
      public void handleEvent(Event e) {
        System.out.println("Move " + e.widget);
      }
    };
    TreeColumn[] columns = tree.getColumns();
    for (int i = 0; i < columns.length; i++) {
      columns[i].setWidth(100);
      columns[i].setMoveable(true);
      columns[i].addListener(SWT.Move, listener);
    }
    Button b = new Button(shell, SWT.PUSH);
    b.setText("invert column order");
    b.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event e) {
        int[] order = tree.getColumnOrder();
        for (int i = 0; i < order.length / 2; i++) {
          int temp = order[i];
          order[i] = order[order.length - i - 1];
          order[order.length - i - 1] = temp;
        }
        tree.setColumnOrder(order);
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Tree Table: Draw a custom gradient selection

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/* 
 * Tree example snippet: Draw a custom gradient selection
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
public class TreeTableGradient {
  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Custom gradient selection for Tree");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
    tree.setHeaderVisible(true);
    tree.setLinesVisible(true);
    int columnCount = 4;
    for (int i = 0; i < columnCount; i++) {
      TreeColumn column = new TreeColumn(tree, SWT.NONE);
      column.setText("Column " + i);
    }
    int itemCount = 3;
    for (int i = 0; i < itemCount; i++) {
      TreeItem item1 = new TreeItem(tree, SWT.NONE);
      item1.setText("item " + i);
      for (int c = 1; c < columnCount; c++) {
        item1.setText(c, "item [" + i + "-" + c + "]");
      }
      for (int j = 0; j < itemCount; j++) {
        TreeItem item2 = new TreeItem(item1, SWT.NONE);
        item2.setText("item [" + i + " " + j + "]");
        for (int c = 1; c < columnCount; c++) {
          item2.setText(c, "item [" + i + " " + j + "-" + c + "]");
        }
        for (int k = 0; k < itemCount; k++) {
          TreeItem item3 = new TreeItem(item2, SWT.NONE);
          item3.setText("item [" + i + " " + j + " " + k + "]");
          for (int c = 1; c < columnCount; c++) {
            item3.setText(c, "item [" + i + " " + j + " " + k + "-" + c + "]");
          }
        }
      }
    }
    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    tree.addListener(SWT.EraseItem, new Listener() {
      public void handleEvent(Event event) {
        if ((event.detail & SWT.SELECTED) != 0) {
          GC gc = event.gc;
          Rectangle area = tree.getClientArea();
          /*
           * If you wish to paint the selection beyond the end of last column,
           * you must change the clipping region.
           */
          int columnCount = tree.getColumnCount();
          if (event.index == columnCount - 1 || columnCount == 0) {
            int width = area.x + area.width - event.x;
            if (width > 0) {
              Region region = new Region();
              gc.getClipping(region);
              region.add(event.x, event.y, width, event.height);
              gc.setClipping(region);
              region.dispose();
            }
          }
          gc.setAdvanced(true);
          if (gc.getAdvanced())
            gc.setAlpha(127);
          Rectangle rect = event.getBounds();
          Color foreground = gc.getForeground();
          Color background = gc.getBackground();
          gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
          gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
          gc.fillGradientRectangle(0, rect.y, 500, rect.height, false);
          // restore colors for subsequent drawing
          gc.setForeground(foreground);
          gc.setBackground(background);
          event.detail &= ~SWT.SELECTED;
        }
      }
    });
    for (int i = 0; i < columnCount; i++) {
      tree.getColumn(i).pack();
    }
    tree.setSelection(tree.getItem(0));
    shell.setSize(500, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}