📄 treetab.java
字号:
/******************************************************************************* * Copyright (c) 2000, 2007 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.examples.controlexample;import org.eclipse.swt.*;import org.eclipse.swt.layout.*;import org.eclipse.swt.widgets.*;import org.eclipse.swt.graphics.*;import org.eclipse.swt.events.*;class TreeTab extends ScrollableTab { /* Example widgets and groups that contain them */ Tree tree1, tree2; TreeItem textNode1, imageNode1; Group treeGroup, imageTreeGroup, itemGroup; /* Size widgets added to the "Size" group */ Button packColumnsButton; /* Style widgets added to the "Style" group */ Button checkButton, fullSelectionButton; /* Other widgets added to the "Other" group */ Button multipleColumns, moveableColumns, resizableColumns, headerVisibleButton, sortIndicatorButton, headerImagesButton, subImagesButton, linesVisibleButton; /* Controls and resources added to the "Colors and Fonts" group */ static final int ITEM_FOREGROUND_COLOR = 3; static final int ITEM_BACKGROUND_COLOR = 4; static final int ITEM_FONT = 5; static final int CELL_FOREGROUND_COLOR = 6; static final int CELL_BACKGROUND_COLOR = 7; static final int CELL_FONT = 8; Color itemForegroundColor, itemBackgroundColor, cellForegroundColor, cellBackgroundColor; Font itemFont, cellFont; static String [] columnTitles = {ControlExample.getResourceString("TableTitle_0"), ControlExample.getResourceString("TableTitle_1"), ControlExample.getResourceString("TableTitle_2"), ControlExample.getResourceString("TableTitle_3")}; static String[][] tableData = { { ControlExample.getResourceString("TableLine0_0"), ControlExample.getResourceString("TableLine0_1"), ControlExample.getResourceString("TableLine0_2"), ControlExample.getResourceString("TableLine0_3") }, { ControlExample.getResourceString("TableLine1_0"), ControlExample.getResourceString("TableLine1_1"), ControlExample.getResourceString("TableLine1_2"), ControlExample.getResourceString("TableLine1_3") }, { ControlExample.getResourceString("TableLine2_0"), ControlExample.getResourceString("TableLine2_1"), ControlExample.getResourceString("TableLine2_2"), ControlExample.getResourceString("TableLine2_3") } }; Point menuMouseCoords; /** * Creates the Tab within a given instance of ControlExample. */ TreeTab(ControlExample instance) { super(instance); } /** * Creates the "Colors and Fonts" group. */ void createColorAndFontGroup () { super.createColorAndFontGroup(); TableItem item = new TableItem(colorAndFontTable, SWT.None); item.setText(ControlExample.getResourceString ("Item_Foreground_Color")); item = new TableItem(colorAndFontTable, SWT.None); item.setText(ControlExample.getResourceString ("Item_Background_Color")); item = new TableItem(colorAndFontTable, SWT.None); item.setText(ControlExample.getResourceString ("Item_Font")); item = new TableItem(colorAndFontTable, SWT.None); item.setText(ControlExample.getResourceString ("Cell_Foreground_Color")); item = new TableItem(colorAndFontTable, SWT.None); item.setText(ControlExample.getResourceString ("Cell_Background_Color")); item = new TableItem(colorAndFontTable, SWT.None); item.setText(ControlExample.getResourceString ("Cell_Font")); shell.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent event) { if (itemBackgroundColor != null) itemBackgroundColor.dispose(); if (itemForegroundColor != null) itemForegroundColor.dispose(); if (itemFont != null) itemFont.dispose(); if (cellBackgroundColor != null) cellBackgroundColor.dispose(); if (cellForegroundColor != null) cellForegroundColor.dispose(); if (cellFont != null) cellFont.dispose(); itemBackgroundColor = null; itemForegroundColor = null; itemFont = null; cellBackgroundColor = null; cellForegroundColor = null; cellFont = null; } }); } void changeFontOrColor(int index) { switch (index) { case ITEM_FOREGROUND_COLOR: { Color oldColor = itemForegroundColor; if (oldColor == null) oldColor = textNode1.getForeground (); colorDialog.setRGB(oldColor.getRGB()); RGB rgb = colorDialog.open(); if (rgb == null) return; oldColor = itemForegroundColor; itemForegroundColor = new Color (display, rgb); setItemForeground (); if (oldColor != null) oldColor.dispose (); } break; case ITEM_BACKGROUND_COLOR: { Color oldColor = itemBackgroundColor; if (oldColor == null) oldColor = textNode1.getBackground (); colorDialog.setRGB(oldColor.getRGB()); RGB rgb = colorDialog.open(); if (rgb == null) return; oldColor = itemBackgroundColor; itemBackgroundColor = new Color (display, rgb); setItemBackground (); if (oldColor != null) oldColor.dispose (); } break; case ITEM_FONT: { Font oldFont = itemFont; if (oldFont == null) oldFont = textNode1.getFont (); fontDialog.setFontList(oldFont.getFontData()); FontData fontData = fontDialog.open (); if (fontData == null) return; oldFont = itemFont; itemFont = new Font (display, fontData); setItemFont (); setExampleWidgetSize (); if (oldFont != null) oldFont.dispose (); } break; case CELL_FOREGROUND_COLOR: { Color oldColor = cellForegroundColor; if (oldColor == null) oldColor = textNode1.getForeground (1); colorDialog.setRGB(oldColor.getRGB()); RGB rgb = colorDialog.open(); if (rgb == null) return; oldColor = cellForegroundColor; cellForegroundColor = new Color (display, rgb); setCellForeground (); if (oldColor != null) oldColor.dispose (); } break; case CELL_BACKGROUND_COLOR: { Color oldColor = cellBackgroundColor; if (oldColor == null) oldColor = textNode1.getBackground (1); colorDialog.setRGB(oldColor.getRGB()); RGB rgb = colorDialog.open(); if (rgb == null) return; oldColor = cellBackgroundColor; cellBackgroundColor = new Color (display, rgb); setCellBackground (); if (oldColor != null) oldColor.dispose (); } break; case CELL_FONT: { Font oldFont = cellFont; if (oldFont == null) oldFont = textNode1.getFont (1); fontDialog.setFontList(oldFont.getFontData()); FontData fontData = fontDialog.open (); if (fontData == null) return; oldFont = cellFont; cellFont = new Font (display, fontData); setCellFont (); setExampleWidgetSize (); if (oldFont != null) oldFont.dispose (); } break; default: super.changeFontOrColor(index); } } /** * Creates the "Other" group. */ void createOtherGroup () { super.createOtherGroup (); /* Create display controls specific to this example */ linesVisibleButton = new Button (otherGroup, SWT.CHECK); linesVisibleButton.setText (ControlExample.getResourceString("Lines_Visible")); multipleColumns = new Button (otherGroup, SWT.CHECK); multipleColumns.setText (ControlExample.getResourceString("Multiple_Columns")); headerVisibleButton = new Button (otherGroup, SWT.CHECK); headerVisibleButton.setText (ControlExample.getResourceString("Header_Visible")); sortIndicatorButton = new Button (otherGroup, SWT.CHECK); sortIndicatorButton.setText (ControlExample.getResourceString("Sort_Indicator")); moveableColumns = new Button (otherGroup, SWT.CHECK); moveableColumns.setText (ControlExample.getResourceString("Moveable_Columns")); resizableColumns = new Button (otherGroup, SWT.CHECK); resizableColumns.setText (ControlExample.getResourceString("Resizable_Columns")); headerImagesButton = new Button (otherGroup, SWT.CHECK); headerImagesButton.setText (ControlExample.getResourceString("Header_Images")); subImagesButton = new Button (otherGroup, SWT.CHECK); subImagesButton.setText (ControlExample.getResourceString("Sub_Images")); /* Add the listeners */ linesVisibleButton.addSelectionListener (new SelectionAdapter () { public void widgetSelected (SelectionEvent event) { setWidgetLinesVisible (); } }); multipleColumns.addSelectionListener (new SelectionAdapter () { public void widgetSelected (SelectionEvent event) { recreateExampleWidgets (); } }); headerVisibleButton.addSelectionListener (new SelectionAdapter () { public void widgetSelected (SelectionEvent event) { setWidgetHeaderVisible (); } }); sortIndicatorButton.addSelectionListener (new SelectionAdapter () { public void widgetSelected (SelectionEvent event) { setWidgetSortIndicator (); } }); moveableColumns.addSelectionListener (new SelectionAdapter () { public void widgetSelected (SelectionEvent event) { setColumnsMoveable (); } }); resizableColumns.addSelectionListener (new SelectionAdapter () { public void widgetSelected (SelectionEvent event) { setColumnsResizable (); } }); headerImagesButton.addSelectionListener (new SelectionAdapter () { public void widgetSelected (SelectionEvent event) { recreateExampleWidgets (); } }); subImagesButton.addSelectionListener (new SelectionAdapter () { public void widgetSelected (SelectionEvent event) { recreateExampleWidgets (); } }); } /** * Creates the "Example" group. */ void createExampleGroup () { super.createExampleGroup (); /* Create a group for the text tree */ treeGroup = new Group (exampleGroup, SWT.NONE); treeGroup.setLayout (new GridLayout ()); treeGroup.setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, true)); treeGroup.setText ("Tree"); /* Create a group for the image tree */ imageTreeGroup = new Group (exampleGroup, SWT.NONE); imageTreeGroup.setLayout (new GridLayout ()); imageTreeGroup.setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, true)); imageTreeGroup.setText (ControlExample.getResourceString("Tree_With_Images")); } /** * Creates the "Example" widgets. */ void createExampleWidgets () { /* Compute the widget style */ int style = getDefaultStyle(); if (singleButton.getSelection()) style |= SWT.SINGLE; if (multiButton.getSelection()) style |= SWT.MULTI; if (horizontalButton.getSelection ()) style |= SWT.H_SCROLL; if (verticalButton.getSelection ()) style |= SWT.V_SCROLL; if (checkButton.getSelection()) style |= SWT.CHECK; if (fullSelectionButton.getSelection ()) style |= SWT.FULL_SELECTION; if (borderButton.getSelection()) style |= SWT.BORDER; /* Create the text tree */ tree1 = new Tree (treeGroup, style); boolean multiColumn = multipleColumns.getSelection(); if (multiColumn) { for (int i = 0; i < columnTitles.length; i++) { TreeColumn treeColumn = new TreeColumn(tree1, SWT.NONE); treeColumn.setText(columnTitles[i]); treeColumn.setToolTipText(ControlExample.getResourceString("Tooltip", new String [] {columnTitles[i]})); } tree1.setSortColumn(tree1.getColumn(0)); } for (int i = 0; i < 4; i++) { TreeItem item = new TreeItem (tree1, SWT.NONE); setItemText(item, i, ControlExample.getResourceString("Node_" + (i + 1))); if (i < 3) { TreeItem subitem = new TreeItem (item, SWT.NONE); setItemText(subitem, i, ControlExample.getResourceString("Node_" + (i + 1) + "_1")); } } TreeItem treeRoots[] = tree1.getItems (); TreeItem item = new TreeItem (treeRoots[1], SWT.NONE); setItemText(item, 1, ControlExample.getResourceString("Node_2_2")); item = new TreeItem (item, SWT.NONE); setItemText(item, 1, ControlExample.getResourceString("Node_2_2_1")); textNode1 = treeRoots[0]; packColumns(tree1); try { TreeColumn column = tree1.getColumn(0); resizableColumns.setSelection (column.getResizable()); } catch (IllegalArgumentException ex) {} /* Create the image tree */ tree2 = new Tree (imageTreeGroup, style); Image image = instance.images[ControlExample.ciClosedFolder]; if (multiColumn) { for (int i = 0; i < columnTitles.length; i++) { TreeColumn treeColumn = new TreeColumn(tree2, SWT.NONE); treeColumn.setText(columnTitles[i]); treeColumn.setToolTipText(ControlExample.getResourceString("Tooltip", new String [] {columnTitles[i]})); if (headerImagesButton.getSelection()) treeColumn.setImage(image); } } for (int i = 0; i < 4; i++) { item = new TreeItem (tree2, SWT.NONE); setItemText(item, i, ControlExample.getResourceString("Node_" + (i + 1))); if (multiColumn && subImagesButton.getSelection()) { for (int j = 0; j < columnTitles.length; j++) { item.setImage(j, image); } } else { item.setImage(image); } if (i < 3) { TreeItem subitem = new TreeItem (item, SWT.NONE); setItemText(subitem, i, ControlExample.getResourceString("Node_" + (i + 1) + "_1")); if (multiColumn && subImagesButton.getSelection()) { for (int j = 0; j < columnTitles.length; j++) { subitem.setImage(j, image); } } else { subitem.setImage(image); } } } treeRoots = tree2.getItems (); item = new TreeItem (treeRoots[1], SWT.NONE); setItemText(item, 1, ControlExample.getResourceString("Node_2_2")); if (multiColumn && subImagesButton.getSelection()) { for (int j = 0; j < columnTitles.length; j++) { item.setImage(j, image); } } else { item.setImage(image); } item = new TreeItem (item, SWT.NONE); setItemText(item, 1, ControlExample.getResourceString("Node_2_2_1")); if (multiColumn && subImagesButton.getSelection()) { for (int j = 0; j < columnTitles.length; j++) { item.setImage(j, image); } } else { item.setImage(image); } imageNode1 = treeRoots[0]; packColumns(tree2); } void setItemText(TreeItem item, int i, String node) { int index = i % 3; if (multipleColumns.getSelection()) { tableData [index][0] = node; item.setText (tableData [index]); } else { item.setText (node); } } /** * Creates the "Size" group. The "Size" group contains * controls that allow the user to change the size of * the example widgets.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -