Search in sources :

Example 86 with TreeItem

use of org.eclipse.swt.widgets.TreeItem in project eclipse.platform.swt by eclipse.

the class TreeTab method createExampleWidgets.

/**
 * Creates the "Example" widgets.
 */
@Override
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 (noScrollButton.getSelection())
        style |= SWT.NO_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 (String columnTitle : columnTitles) {
            TreeColumn treeColumn = new TreeColumn(tree1, SWT.NONE);
            treeColumn.setText(columnTitle);
            treeColumn.setToolTipText(ControlExample.getResourceString("Tooltip", columnTitle));
        }
        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", columnTitles[i]));
            if (headerImagesButton.getSelection())
                treeColumn.setImage(instance.images[i % 3]);
        }
    }
    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);
}
Also used : TreeItem(org.eclipse.swt.widgets.TreeItem) TreeColumn(org.eclipse.swt.widgets.TreeColumn) Tree(org.eclipse.swt.widgets.Tree) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.swt.graphics.Point)

Example 87 with TreeItem

use of org.eclipse.swt.widgets.TreeItem in project eclipse.platform.swt by eclipse.

the class TreeTab method parameterForType.

@Override
Object[] parameterForType(String typeName, String value, Widget widget) {
    if (typeName.equals("org.eclipse.swt.widgets.TreeItem")) {
        TreeItem item = findItem(value, ((Tree) widget).getItems());
        if (item != null)
            return new Object[] { item };
    }
    if (typeName.equals("[Lorg.eclipse.swt.widgets.TreeItem;")) {
        String[] values = split(value, ',');
        TreeItem[] items = new TreeItem[values.length];
        for (int i = 0; i < values.length; i++) {
            TreeItem item = findItem(values[i], ((Tree) widget).getItems());
            if (item == null)
                break;
            items[i] = item;
        }
        return new Object[] { items };
    }
    return super.parameterForType(typeName, value, widget);
}
Also used : TreeItem(org.eclipse.swt.widgets.TreeItem) Point(org.eclipse.swt.graphics.Point)

Example 88 with TreeItem

use of org.eclipse.swt.widgets.TreeItem in project eclipse.platform.swt by eclipse.

the class DNDExample method createDragSource.

private void createDragSource() {
    if (dragSource != null)
        dragSource.dispose();
    dragSource = new DragSource(dragControl, dragOperation);
    dragSource.setTransfer(dragTypes);
    dragSource.addDragListener(new DragSourceListener() {

        @Override
        public void dragFinished(org.eclipse.swt.dnd.DragSourceEvent event) {
            dragConsole.append(">>dragFinished\n");
            printEvent(event);
            dragDataText = dragDataRTF = dragDataHTML = dragDataURL = null;
            dragDataFiles = null;
            if (event.detail == DND.DROP_MOVE) {
                switch(dragControlType) {
                    case BUTTON_CHECK:
                    case BUTTON_TOGGLE:
                    case BUTTON_RADIO:
                        {
                            Button b = (Button) dragControl;
                            b.setText("");
                            break;
                        }
                    case STYLED_TEXT:
                        {
                            StyledText text = (StyledText) dragControl;
                            text.insert("");
                            break;
                        }
                    case TABLE:
                        {
                            Table table = (Table) dragControl;
                            TableItem[] items = table.getSelection();
                            for (TableItem item : items) {
                                item.dispose();
                            }
                            break;
                        }
                    case TEXT:
                        {
                            Text text = (Text) dragControl;
                            text.insert("");
                            break;
                        }
                    case TREE:
                        {
                            Tree tree = (Tree) dragControl;
                            TreeItem[] items = tree.getSelection();
                            for (TreeItem item : items) {
                                item.dispose();
                            }
                            break;
                        }
                    case CANVAS:
                        {
                            dragControl.setData("STRINGS", null);
                            dragControl.redraw();
                            break;
                        }
                    case LABEL:
                        {
                            Label label = (Label) dragControl;
                            label.setText("");
                            break;
                        }
                    case LIST:
                        {
                            List list = (List) dragControl;
                            int[] indices = list.getSelectionIndices();
                            list.remove(indices);
                            break;
                        }
                    case COMBO:
                        {
                            Combo combo = (Combo) dragControl;
                            combo.setText("");
                            break;
                        }
                }
            }
        }

        @Override
        public void dragSetData(org.eclipse.swt.dnd.DragSourceEvent event) {
            dragConsole.append(">>dragSetData\n");
            printEvent(event);
            if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataText;
            }
            if (RTFTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataRTF;
            }
            if (HTMLTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataHTML;
            }
            if (URLTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataURL;
            }
            if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataFiles;
            }
        }

        @Override
        public void dragStart(org.eclipse.swt.dnd.DragSourceEvent event) {
            dragConsole.append(">>dragStart\n");
            printEvent(event);
            dragDataFiles = fileList.getItems();
            switch(dragControlType) {
                case BUTTON_CHECK:
                case BUTTON_TOGGLE:
                case BUTTON_RADIO:
                    {
                        Button b = (Button) dragControl;
                        dragDataText = b.getSelection() ? "true" : "false";
                        break;
                    }
                case STYLED_TEXT:
                    {
                        StyledText text = (StyledText) dragControl;
                        String s = text.getSelectionText();
                        if (s.length() == 0) {
                            event.doit = false;
                        } else {
                            dragDataText = s;
                        }
                        break;
                    }
                case TABLE:
                    {
                        Table table = (Table) dragControl;
                        TableItem[] items = table.getSelection();
                        if (items.length == 0) {
                            event.doit = false;
                        } else {
                            StringBuilder buffer = new StringBuilder();
                            for (int i = 0; i < items.length; i++) {
                                buffer.append(items[i].getText());
                                if (items.length > 1 && i < items.length - 1) {
                                    buffer.append("\n");
                                }
                            }
                            dragDataText = buffer.toString();
                        }
                        break;
                    }
                case TEXT:
                    {
                        Text text = (Text) dragControl;
                        String s = text.getSelectionText();
                        if (s.length() == 0) {
                            event.doit = false;
                        } else {
                            dragDataText = s;
                        }
                        break;
                    }
                case TREE:
                    {
                        Tree tree = (Tree) dragControl;
                        TreeItem[] items = tree.getSelection();
                        if (items.length == 0) {
                            event.doit = false;
                        } else {
                            StringBuilder buffer = new StringBuilder();
                            for (int i = 0; i < items.length; i++) {
                                buffer.append(items[i].getText());
                                if (items.length > 1 && i < items.length - 1) {
                                    buffer.append("\n");
                                }
                            }
                            dragDataText = buffer.toString();
                        }
                        break;
                    }
                case CANVAS:
                    {
                        String[] strings = (String[]) dragControl.getData("STRINGS");
                        if (strings == null || strings.length == 0) {
                            event.doit = false;
                        } else {
                            StringBuilder buffer = new StringBuilder();
                            for (int i = 0; i < strings.length; i++) {
                                buffer.append(strings[i]);
                                if (strings.length > 1 && i < strings.length - 1) {
                                    buffer.append("\n");
                                }
                            }
                            dragDataText = buffer.toString();
                        }
                        break;
                    }
                case LABEL:
                    {
                        Label label = (Label) dragControl;
                        String string = label.getText();
                        if (string.length() == 0) {
                            event.doit = false;
                        } else {
                            dragDataText = string;
                        }
                        break;
                    }
                case LIST:
                    {
                        List list = (List) dragControl;
                        String[] selection = list.getSelection();
                        if (selection.length == 0) {
                            event.doit = false;
                        } else {
                            StringBuilder buffer = new StringBuilder();
                            for (int i = 0; i < selection.length; i++) {
                                buffer.append(selection[i]);
                                if (selection.length > 1 && i < selection.length - 1) {
                                    buffer.append("\n");
                                }
                            }
                            dragDataText = buffer.toString();
                        }
                        break;
                    }
                case COMBO:
                    {
                        Combo combo = (Combo) dragControl;
                        String string = combo.getText();
                        Point selection = combo.getSelection();
                        if (selection.x == selection.y) {
                            event.doit = false;
                        } else {
                            dragDataText = string.substring(selection.x, selection.y);
                        }
                        break;
                    }
                default:
                    throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED);
            }
            if (dragDataText != null) {
                dragDataRTF = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\cf1\\b " + dragDataText + "}";
                dragDataHTML = "<b>" + dragDataText + "</b>";
                dragDataURL = "http://" + dragDataText.replace(' ', '.');
                try {
                    new URL(dragDataURL);
                } catch (MalformedURLException e) {
                    dragDataURL = null;
                }
            }
            for (Transfer dragType : dragTypes) {
                if (dragType instanceof TextTransfer && dragDataText == null) {
                    event.doit = false;
                }
                if (dragType instanceof RTFTransfer && dragDataRTF == null) {
                    event.doit = false;
                }
                if (dragType instanceof HTMLTransfer && dragDataHTML == null) {
                    event.doit = false;
                }
                if (dragType instanceof URLTransfer && dragDataURL == null) {
                    event.doit = false;
                }
                if (dragType instanceof FileTransfer && (dragDataFiles == null || dragDataFiles.length == 0)) {
                    event.doit = false;
                }
            }
        }
    });
}
Also used : MalformedURLException(java.net.MalformedURLException) TreeItem(org.eclipse.swt.widgets.TreeItem) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) Combo(org.eclipse.swt.widgets.Combo) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) URL(java.net.URL) Button(org.eclipse.swt.widgets.Button) Tree(org.eclipse.swt.widgets.Tree) List(org.eclipse.swt.widgets.List) SWTError(org.eclipse.swt.SWTError) StyledText(org.eclipse.swt.custom.StyledText) Table(org.eclipse.swt.widgets.Table) URLTransfer(org.eclipse.swt.dnd.URLTransfer) FileTransfer(org.eclipse.swt.dnd.FileTransfer) StyledText(org.eclipse.swt.custom.StyledText) Text(org.eclipse.swt.widgets.Text) HTMLTransfer(org.eclipse.swt.dnd.HTMLTransfer) DragSource(org.eclipse.swt.dnd.DragSource) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) URLTransfer(org.eclipse.swt.dnd.URLTransfer) Transfer(org.eclipse.swt.dnd.Transfer) HTMLTransfer(org.eclipse.swt.dnd.HTMLTransfer) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) FileTransfer(org.eclipse.swt.dnd.FileTransfer) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) TextTransfer(org.eclipse.swt.dnd.TextTransfer)

Example 89 with TreeItem

use of org.eclipse.swt.widgets.TreeItem in project eclipse.platform.swt by eclipse.

the class FileViewer method notifySelectedDirectory.

/**
 * Notifies the application components that a new current directory has been selected
 *
 * @param dir the directory that was selected, null is ignored
 */
void notifySelectedDirectory(File dir) {
    if (dir == null)
        return;
    if (currentDirectory != null && dir.equals(currentDirectory))
        return;
    currentDirectory = dir;
    notifySelectedFiles(null);
    /* Shell:
		 * Sets the title to indicate the selected directory
		 */
    shell.setText(getResourceString("Title", new Object[] { currentDirectory.getPath() }));
    /* Table view:
		 * Displays the contents of the selected directory.
		 */
    workerUpdate(dir, false);
    /* Combo view:
		 * Sets the combo box to point to the selected directory.
		 */
    final File[] comboRoots = (File[]) combo.getData(COMBODATA_ROOTS);
    int comboEntry = -1;
    if (comboRoots != null) {
        for (int i = 0; i < comboRoots.length; ++i) {
            if (dir.equals(comboRoots[i])) {
                comboEntry = i;
                break;
            }
        }
    }
    if (comboEntry == -1)
        combo.setText(dir.getPath());
    else
        combo.select(comboEntry);
    /* Tree view:
		 * If not already expanded, recursively expands the parents of the specified
		 * directory until it is visible.
		 */
    List<File> path = new ArrayList<>();
    // Build a stack of paths from the root of the tree
    while (dir != null) {
        path.add(dir);
        dir = dir.getParentFile();
    }
    // Recursively expand the tree to get to the specified directory
    TreeItem[] items = tree.getItems();
    TreeItem lastItem = null;
    for (int i = path.size() - 1; i >= 0; --i) {
        final File pathElement = path.get(i);
        // Search for a particular File in the array of tree items
        // No guarantee that the items are sorted in any recognizable fashion, so we'll
        // just sequential scan.  There shouldn't be more than a few thousand entries.
        TreeItem item = null;
        for (TreeItem currentItem : items) {
            item = currentItem;
            if (item.isDisposed())
                continue;
            final File itemFile = (File) item.getData(TREEITEMDATA_FILE);
            if (itemFile != null && itemFile.equals(pathElement))
                break;
        }
        if (item == null)
            break;
        lastItem = item;
        if (i != 0 && !item.getExpanded()) {
            treeExpandItem(item);
            item.setExpanded(true);
        }
        items = item.getItems();
    }
    tree.setSelection((lastItem != null) ? new TreeItem[] { lastItem } : new TreeItem[0]);
}
Also used : TreeItem(org.eclipse.swt.widgets.TreeItem) ArrayList(java.util.ArrayList) File(java.io.File) Point(org.eclipse.swt.graphics.Point)

Example 90 with TreeItem

use of org.eclipse.swt.widgets.TreeItem in project eclipse.platform.swt by eclipse.

the class FileViewer method createTreeView.

/**
 * Creates the file tree view.
 *
 * @param parent the parent control
 */
private void createTreeView(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 1;
    gridLayout.marginHeight = gridLayout.marginWidth = 2;
    gridLayout.horizontalSpacing = gridLayout.verticalSpacing = 0;
    composite.setLayout(gridLayout);
    treeScopeLabel = new Label(composite, SWT.BORDER);
    treeScopeLabel.setText(FileViewer.getResourceString("details.AllFolders.text"));
    treeScopeLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL));
    tree = new Tree(composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE);
    tree.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
    tree.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            final TreeItem[] selection = tree.getSelection();
            if (selection != null && selection.length != 0) {
                TreeItem item = selection[0];
                File file = (File) item.getData(TREEITEMDATA_FILE);
                notifySelectedDirectory(file);
            }
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent event) {
            final TreeItem[] selection = tree.getSelection();
            if (selection != null && selection.length != 0) {
                TreeItem item = selection[0];
                item.setExpanded(true);
                treeExpandItem(item);
            }
        }
    });
    tree.addTreeListener(new TreeAdapter() {

        @Override
        public void treeExpanded(TreeEvent event) {
            final TreeItem item = (TreeItem) event.item;
            final Image image = (Image) item.getData(TREEITEMDATA_IMAGEEXPANDED);
            if (image != null)
                item.setImage(image);
            treeExpandItem(item);
        }

        @Override
        public void treeCollapsed(TreeEvent event) {
            final TreeItem item = (TreeItem) event.item;
            final Image image = (Image) item.getData(TREEITEMDATA_IMAGECOLLAPSED);
            if (image != null)
                item.setImage(image);
        }
    });
    createTreeDragSource(tree);
    createTreeDropTarget(tree);
}
Also used : Composite(org.eclipse.swt.widgets.Composite) TreeItem(org.eclipse.swt.widgets.TreeItem) Label(org.eclipse.swt.widgets.Label) TreeAdapter(org.eclipse.swt.events.TreeAdapter) Image(org.eclipse.swt.graphics.Image) TreeEvent(org.eclipse.swt.events.TreeEvent) GridLayout(org.eclipse.swt.layout.GridLayout) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Tree(org.eclipse.swt.widgets.Tree) File(java.io.File) SelectionListener(org.eclipse.swt.events.SelectionListener)

Aggregations

TreeItem (org.eclipse.swt.widgets.TreeItem)226 Tree (org.eclipse.swt.widgets.Tree)71 Point (org.eclipse.swt.graphics.Point)56 TreeColumn (org.eclipse.swt.widgets.TreeColumn)30 GridData (org.eclipse.swt.layout.GridData)29 Test (org.junit.Test)28 SelectionEvent (org.eclipse.swt.events.SelectionEvent)27 ArrayList (java.util.ArrayList)24 GridLayout (org.eclipse.swt.layout.GridLayout)23 Composite (org.eclipse.swt.widgets.Composite)22 TableItem (org.eclipse.swt.widgets.TableItem)21 Button (org.eclipse.swt.widgets.Button)20 Table (org.eclipse.swt.widgets.Table)20 TreeViewer (org.eclipse.jface.viewers.TreeViewer)19 FillLayout (org.eclipse.swt.layout.FillLayout)19 Label (org.eclipse.swt.widgets.Label)18 Group (org.eclipse.swt.widgets.Group)17 Text (org.eclipse.swt.widgets.Text)17 SelectionListener (org.eclipse.swt.events.SelectionListener)16 Rectangle (org.eclipse.swt.graphics.Rectangle)16