Example usage for org.eclipse.jface.viewers TreeViewer getTree

List of usage examples for org.eclipse.jface.viewers TreeViewer getTree

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers TreeViewer getTree.

Prototype

public Tree getTree() 

Source Link

Document

Returns this tree viewer's tree control.

Usage

From source file:Snippet048TreeViewerTabWithCheckboxFor3_3.java

License:Open Source License

public Snippet048TreeViewerTabWithCheckboxFor3_3(final Shell shell) {
    final TreeViewer v = new TreeViewer(shell, SWT.BORDER | SWT.FULL_SELECTION);
    v.getTree().setLinesVisible(true);
    v.getTree().setHeaderVisible(true);/*from w w w. j  a v a2  s.c  o  m*/

    final TreeViewerFocusCellManager mgr = new TreeViewerFocusCellManager(v,
            new FocusCellOwnerDrawHighlighter(v));
    ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(v) {
        protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
            return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
                    || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
                    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
                            && (event.keyCode == SWT.CR || event.character == ' '))
                    || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
        }
    };

    TreeViewerEditor.create(v, mgr, actSupport,
            ColumnViewerEditor.TABBING_HORIZONTAL | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
                    | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);

    final TextCellEditor textCellEditor = new TextCellEditor(v.getTree());
    final CheckboxCellEditor checkboxCellEditor = new CheckboxCellEditor(v.getTree());

    TreeViewerColumn column = new TreeViewerColumn(v, SWT.NONE);
    column.getColumn().setWidth(200);
    column.getColumn().setMoveable(true);
    column.getColumn().setText("Column 1");
    column.setLabelProvider(new ColumnLabelProvider() {

        public String getText(Object element) {
            return "Column 1 => " + element.toString();
        }

    });
    column.setEditingSupport(new EditingSupport(v) {
        protected boolean canEdit(Object element) {
            return false;
        }

        protected CellEditor getCellEditor(Object element) {
            return textCellEditor;
        }

        protected Object getValue(Object element) {
            return ((MyModel) element).counter + "";
        }

        protected void setValue(Object element, Object value) {
            ((MyModel) element).counter = Integer.parseInt(value.toString());
            v.update(element, null);
        }
    });

    column = new TreeViewerColumn(v, SWT.NONE);
    column.getColumn().setWidth(200);
    column.getColumn().setMoveable(true);
    column.getColumn().setText("Column 2");
    column.setLabelProvider(new ColumnLabelProvider() {

        public String getText(Object element) {
            return "Column 2 => " + element.toString();
        }

    });
    column.setEditingSupport(new EditingSupport(v) {
        protected boolean canEdit(Object element) {
            return true;
        }

        protected CellEditor getCellEditor(Object element) {
            return textCellEditor;
        }

        protected Object getValue(Object element) {
            return ((MyModel) element).counter + "";
        }

        protected void setValue(Object element, Object value) {
            ((MyModel) element).counter = Integer.parseInt(value.toString());
            v.update(element, null);
        }
    });

    column = new TreeViewerColumn(v, SWT.NONE);
    column.getColumn().setWidth(200);
    column.getColumn().setMoveable(true);
    column.getColumn().setText("Column 3");
    column.setLabelProvider(new ColumnLabelProvider() {

        public String getText(Object element) {
            return ((MyModel) element).bool + "";
        }

    });
    column.setEditingSupport(new EditingSupport(v) {
        protected boolean canEdit(Object element) {
            return true;
        }

        protected CellEditor getCellEditor(Object element) {
            return checkboxCellEditor;
        }

        protected Object getValue(Object element) {
            return new Boolean(((MyModel) element).bool);
        }

        protected void setValue(Object element, Object value) {
            ((MyModel) element).bool = ((Boolean) value).booleanValue();
            v.update(element, null);
        }
    });

    v.setContentProvider(new MyContentProvider());
    v.setInput(createModel());
    v.getControl().addTraverseListener(new TraverseListener() {

        public void keyTraversed(TraverseEvent e) {
            if ((e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS)
                    && mgr.getFocusCell().getColumnIndex() == 2) {
                ColumnViewerEditor editor = v.getColumnViewerEditor();
                ViewerCell cell = mgr.getFocusCell();

                try {
                    Method m = ColumnViewerEditor.class.getDeclaredMethod("processTraverseEvent",
                            new Class[] { int.class, ViewerRow.class, TraverseEvent.class });
                    m.setAccessible(true);
                    m.invoke(editor,
                            new Object[] { new Integer(cell.getColumnIndex()), cell.getViewerRow(), e });
                } catch (SecurityException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (NoSuchMethodException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (InvocationTargetException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }

    });
}

From source file:Snippet015CustomTooltipsForTree.java

License:Open Source License

/**
 * @param args//from  ww  w .  j a v  a  2s . co  m
 */
public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    TreeViewer v = new TreeViewer(shell, SWT.FULL_SELECTION);
    v.getTree().setLinesVisible(true);
    v.getTree().setHeaderVisible(true);
    ColumnViewerToolTipSupport.enableFor(v);

    v.setContentProvider(new MyContentProvider());

    CellLabelProvider labelProvider = new CellLabelProvider() {

        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.ViewerLabelProvider#getTooltipText(java.lang.Object)
         */
        public String getToolTipText(Object element) {
            return "Tooltip (" + element + ")";
        }

        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.ViewerLabelProvider#getTooltipShift(java.lang.Object)
         */
        public Point getToolTipShift(Object object) {
            return new Point(5, 5);
        }

        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.ViewerLabelProvider#getTooltipDisplayDelayTime(java.lang.Object)
         */
        public int getToolTipDisplayDelayTime(Object object) {
            return 1000;
        }

        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.ViewerLabelProvider#getTooltipTimeDisplayed(java.lang.Object)
         */
        public int getToolTipTimeDisplayed(Object object) {
            return 50000;
        }

        public void update(ViewerCell cell) {
            cell.setText(cell.getElement().toString());

        }
    };

    v.setLabelProvider(labelProvider);
    v.setInput("");

    shell.setSize(200, 200);
    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }

    display.dispose();
}

From source file:at.bitandart.zoubek.mervin.swt.diff.tree.TreeDiff.java

License:Open Source License

/**
 * draws an insertion line based on the {@link InsertionPoint} for the given
 * {@link TreeDiffItem} on the given {@link TreeDiffSide}, styled for the
 * given {@link TreeDiffType}./*w  ww. ja va2 s.c  o  m*/
 * 
 * @param item
 *            the item to draw the insertion line for.
 * @param side
 *            the side to draw the insertion line for.
 * @param diffType
 *            the {@link TreeDiffType} that should be used to derive the
 *            style of the insertion line.
 * @param gc
 *            the {@link GC} used to draw onto the tree widget.
 */
protected void drawInsertionLine(TreeDiffItem item, TreeDiffSide side, TreeDiffType diffType, GC gc) {

    TreeViewer treeViewer = null;

    if (side == TreeDiffSide.LEFT) {
        treeViewer = leftTreeViewer;
    } else {
        treeViewer = rightTreeViewer;
    }

    Tree tree = treeViewer.getTree();

    /* determine the tree items around the insertion point */
    InsertionPointTreeContext insertionPointTreeContext = getInsertionPointTreeContext(item, treeViewer);

    TreeItem ancestorTreeItem = insertionPointTreeContext.ancestor;
    TreeItem lastNotExpandedChild = ancestorTreeItem;
    if (insertionPointTreeContext.lastNotExpandedChild != null) {
        lastNotExpandedChild = insertionPointTreeContext.lastNotExpandedChild;
    }

    InsertionPoint insertionPoint = insertionPointTreeContext.insertionPoint;

    /* determine the point to attach to the center line */
    Point attachmentPoint = tree.toControl(getAttachmentPoint(lastNotExpandedChild, diffType, side));

    /*
     * determine the bounds of the first item that should be used for
     * insertions before the first item
     */
    Rectangle firstItemBounds = null;
    if (tree.getItemCount() > 0) {
        firstItemBounds = tree.getItem(0).getBounds();
    }

    /*
     * initialize the bounds that should be used for rendering with fallback
     * values
     */

    /*
     * the ancestor bounds are needed to determine the correct indentation
     * and width
     */
    Rectangle ancestorBounds = new Rectangle((firstItemBounds != null) ? firstItemBounds.x : 0,
            (firstItemBounds != null) ? firstItemBounds.y : 0,
            (firstItemBounds != null) ? firstItemBounds.width : 100, 0);
    /*
     * the bounds of the last expanded child are needed to determine the
     * correct y-value and width
     */
    Rectangle lastNotExpandedChildBounds = new Rectangle(ancestorBounds.x, ancestorBounds.y,
            ancestorBounds.width, ancestorBounds.height);

    /* apply the real tree item bounds if possible */
    if (ancestorTreeItem != null) {
        ancestorBounds = ancestorTreeItem.getBounds();
    }
    if (lastNotExpandedChild != null) {
        lastNotExpandedChildBounds = lastNotExpandedChild.getBounds();
    }

    int startX = ancestorBounds.x;
    int endX = Math.max(ancestorBounds.x + ancestorBounds.width,
            lastNotExpandedChildBounds.x + lastNotExpandedChildBounds.width);

    if (insertionPoint.descendant != null && insertionPoint.descendant.getParent() == insertionPoint.ancestor
            && ancestorTreeItem != null) {
        /*
         * the descendant is the child of the ancestor, indentation is
         * needed to indicate that this is an insertion of a child of the
         * ancestor and not an insertion of a sibling. Use a static
         * indentation value as fallback for now as this is platform
         * specific.
         */
        startX = ancestorTreeItem.getBounds().x + 10;
        if (ancestorTreeItem.getItemCount() > 0) {
            /*
             * the ancestor has child items, use them to derive the correct
             * indentation and width.
             */
            Rectangle firstChildBounds = ancestorTreeItem.getItem(0).getBounds();
            startX = firstChildBounds.x;
            endX = Math.max(ancestorBounds.x + ancestorBounds.width,
                    firstChildBounds.x + firstChildBounds.width);
        }
    }

    /*
     * derive the start and end point for the insertion line from the bounds
     */
    Point insertionLineStart = new Point(startX,
            lastNotExpandedChildBounds.y + lastNotExpandedChildBounds.height);
    Point insertionLineEnd = new Point(endX, insertionLineStart.y);

    /* draw the insertion line */
    gc.setLineWidth(4);
    gc.drawLine(insertionLineStart.x, insertionLineStart.y, insertionLineEnd.x, insertionLineEnd.y);
    gc.setLineWidth(1);

    /* draw the connection line to the center */
    if (side == TreeDiffSide.LEFT) {
        gc.drawLine(insertionLineEnd.x, insertionLineEnd.y, attachmentPoint.x, attachmentPoint.y);
    } else {
        gc.drawLine(insertionLineStart.x, insertionLineStart.y, attachmentPoint.x, attachmentPoint.y);
    }

}

From source file:at.bitandart.zoubek.mervin.swt.diff.tree.TreeDiff.java

License:Open Source License

/**
 * draws an outline around the context tree item on the given
 * {@link TreeDiffSide} for the given {@link TreeDiffItem}, styled for the
 * given {@link TreeDiffType}./*  w  w  w. j a v  a 2 s  .  c  o  m*/
 * 
 * @param item
 *            the {@link TreeDiffItem} to draw the outline for.
 * @param side
 *            the {@link TreeDiffSide} to draw the outline for.
 * @param diffType
 *            the {@link TreeDiffType} to draw the outline for.
 * @param gc
 *            the {@link GC} used to draw onto the tree widget.
 */
protected void drawOutline(TreeDiffItem item, TreeDiffSide side, TreeDiffType diffType, GC gc) {

    int horizontalMargin = -2;

    TreeViewer treeViewer = null;

    if (side == TreeDiffSide.LEFT) {
        treeViewer = leftTreeViewer;
    } else {
        treeViewer = rightTreeViewer;
    }

    Tree tree = treeViewer.getTree();

    /* find the item to outline and get its bounds */
    TreeItem treeItem = getContextTreeItem(item, side);
    Rectangle bounds = new Rectangle(0, 0, 0, 0);
    if (treeItem != null) {
        bounds = treeItem.getBounds();
    }

    /* determine the point to attach to at the bounds of the item */
    Point outlineAttachmentPoint = null;
    if (side == TreeDiffSide.LEFT) {
        outlineAttachmentPoint = new Point(bounds.x + bounds.width + horizontalMargin,
                bounds.y + (int) (bounds.height / 2.0));
    } else {
        outlineAttachmentPoint = new Point(bounds.x + horizontalMargin, bounds.y + (int) (bounds.height / 2.0));
    }

    /* determine the point to attach to at the center */
    Point sashAttachmentPoint = tree.toControl(getAttachmentPoint(treeItem, diffType, side));

    /* draw the connection line from the bounds to the center */
    gc.drawLine(sashAttachmentPoint.x, sashAttachmentPoint.y, outlineAttachmentPoint.x,
            outlineAttachmentPoint.y);

}

From source file:at.bitandart.zoubek.mervin.swt.diff.tree.TreeDiff.java

License:Open Source License

/**
 * /* w w w .j  a  v  a  2 s  .co m*/
 * determines the center attachment point for the given Bounds with respect
 * of the given {@link TreeDiffType} on the given {@link TreeDiffSide}.
 * 
 * @param bounds
 *            the bounds to get the attachment point for.
 * @param type
 *            the type to get the attachment point for.
 * @param side
 *            the side to get the attachment point for.
 * @return the {@link Display}-relative attachment point for the given
 *         bounds.
 */
protected Point getAttachmentPoint(Rectangle bounds, TreeDiffType type, TreeDiffSide side) {

    TreeViewer treeViewer = leftTreeViewer;

    if (side == TreeDiffSide.RIGHT) {
        treeViewer = rightTreeViewer;
    }

    Point fromPoint = new Point(0, 0);
    if ((type == TreeDiffType.ADD && side == changedSide.getOpposite())
            || (type == TreeDiffType.DELETE && side == changedSide)) {
        /*
         * "insertion line" (opposite side of an addition/right side of a
         * deletion): attach to the projected lower corners of the bounds,
         * but ignore the x coordinate for now.
         */
        fromPoint.y = bounds.y + bounds.height;
    } else {
        /*
         * default behavior: attach to the projected center of the
         * left/right sides of the bounds, but ignore the x coordinate for
         * now.
         */
        fromPoint.y = bounds.y + (int) (bounds.height / 2.0);
    }

    /*
     * the point is relative to the tree, so transform it to be relative to
     * the display
     */
    fromPoint = treeViewer.getTree().toDisplay(fromPoint);

    /* project onto the center sash control side */
    if (side == TreeDiffSide.LEFT) {
        fromPoint.x = 0;
    } else {
        fromPoint.x = centerSash.getBounds().width;
    }

    /*
     * the x-value is relative to the sash, so transform it to be relative
     * to the display
     */
    fromPoint.x = centerSash.toDisplay(fromPoint.x, 0).x;

    return fromPoint;

}

From source file:at.bitandart.zoubek.mervin.swt.diff.tree.TreeDiff.java

License:Open Source License

/**
 * finds the {@link TreeItem} in the given {@link TreeViewer} for the given
 * {@link TreeDiffItem}.//from  ww  w.  j av a 2s.  c o  m
 * 
 * @param treeViewer
 *            the {@link TreeViewer} to search.
 * @param item
 *            the {@link TreeDiffItem} to search for.
 * @return the {@link TreeItem} associated to the given {@link TreeDiffItem}
 */
protected TreeItem findTreeItem(TreeViewer treeViewer, TreeDiffItem item) {
    return findTreeItem(treeViewer.getTree().getItems(), item);
}

From source file:ca.uvic.cs.tagsea.editing.TreeItemWorker.java

License:Open Source License

/**
 * Initializes the worker to handle drag and drop, inline rename, and deletion.
 * Sets up the drag source and the drag target as the tree.
 * This constructor takes a tree viewer which is used to update selections.
 * @param treeViewer the tree viewer - its tree will be monitored for drags, renames, and deletions
 * @param childOrderImportant if the index of a Tree or a TreeItem's children matters.  
 * If this is false then moving a TreeItem to the same parent (different position) won't do anything.
 *///from ww  w. j a v  a 2s.  co m
public TreeItemWorker(TreeViewer treeViewer, boolean childOrderImportant) {
    this(treeViewer.getTree(), childOrderImportant);
    this.treeViewer = treeViewer;
}

From source file:cc.warlock.rcp.stormfront.ui.prefs.AccountsPreferencePage.java

License:Open Source License

protected TreeViewer createAccountViewer(Composite parent) {
    final TreeViewer viewer = new TreeViewer(parent, SWT.BORDER | SWT.SINGLE | SWT.H_SCROLL);
    viewer.getTree().setLayoutData(new GridData(GridData.BEGINNING, GridData.FILL, true, true));
    viewer.setContentProvider(new ITreeContentProvider() {
        public void dispose() {
        }// www . ja  v a  2  s. c  om

        public Object[] getChildren(Object parentElement) {
            if (parentElement instanceof Account) {
                return ((Account) parentElement).getProfiles().toArray();
            }
            return new Object[0];
        }

        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof Collection<?>) {
                return ((Collection<?>) inputElement).toArray();
            } else if (inputElement instanceof Object[]) {
                return (Object[]) inputElement;
            } else {
                return new Object[] { inputElement };
            }
        }

        public Object getParent(Object element) {
            if (element instanceof ProfileSetting) {
                return AccountProvider.getInstance().getAccountByProfile((ProfileSetting) element);
            }
            return null;
        }

        public boolean hasChildren(Object element) {
            if (element instanceof Account) {
                return ((Account) element).getProfiles().size() > 0;
            }
            return false;
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }
    });
    viewer.setLabelProvider(new ILabelProvider() {
        public void addListener(ILabelProviderListener listener) {
        }

        public void dispose() {
        }

        public Image getImage(Object element) {
            if (element instanceof ProfileSetting)
                return WarlockSharedImages.getImage(WarlockSharedImages.IMG_CHARACTER);
            return null;
        }

        public String getText(Object element) {
            if (element instanceof Account) {
                return ((Account) element).getAccountName();
            } else if (element instanceof ProfileSetting) {
                ProfileSetting profile = (ProfileSetting) element;
                return profile.getGameName() + " - " + profile.getCharacterName();
            }
            return "";
        }

        public boolean isLabelProperty(Object element, String property) {
            return true;
        }

        public void removeListener(ILabelProviderListener listener) {
        }
    });

    viewer.addFilter(new ViewerFilter() {
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            if (element instanceof Account) {
                Account account = (Account) element;
                if (removedAccounts.contains(account))
                    return false;
            } else if (element instanceof ProfileSetting) {
                ProfileSetting profile = (ProfileSetting) element;
                if (removedProfiles.containsKey(profile))
                    return false;
            }
            return true;
        }
    });

    viewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    viewer.getTree().setLinesVisible(true);
    viewer.getTree().setSize(300, viewer.getTree().getSize().y);
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            if (selection.getFirstElement() instanceof Account) {
                accountSelected((Account) selection.getFirstElement());
            } else if (selection.getFirstElement() instanceof ProfileSetting) {
                profileSelected((ProfileSetting) selection.getFirstElement());
            }
        }
    });
    return viewer;
}

From source file:ccw.editors.outline.ClojureOutlinePage.java

License:Open Source License

protected void setInputInUiThread(List<List> forms) {
    if (sort) {//from w  w w . ja  v a 2s .c  o  m
        List<List> sorted = new ArrayList(forms);
        Collections.sort(sorted, new Comparator<List>() {
            public int compare(List o1, List o2) {
                Symbol s1 = symbol(RT.first(o1)), s2 = symbol(RT.first(o2));

                if (s1 == null && s2 == null)
                    return 0; // mandatory for Comparator contract
                if (s1 == null)
                    return 1;
                if (s2 == null)
                    return -1;

                if (s1.getName().equals("ns") && s2.getName().equals("ns")) {
                    // fall through to order ns decls correctly
                } else {
                    if (s1.getName().equals("ns"))
                        return -1;
                    if (s2.getName().equals("ns"))
                        return 1;
                }

                if (isPrivate(o1) != isPrivate(o2)) {
                    return isPrivate(o1) ? -1 : 1;
                }

                s1 = symbol(RT.second(o1));
                s2 = symbol(RT.second(o2));

                if (s1 == null && s2 == null)
                    return 0; // mandatory for Comparator contract
                if (s1 == null)
                    return 1;
                if (s2 == null)
                    return -1;

                return s1.getName().compareToIgnoreCase(s2.getName());
            }
        });
        forms = sorted;
    }
    final List<List> theForms = forms;

    if (getControl() != null) {
        getControl().getDisplay().asyncExec(new Runnable() {
            public void run() {
                if (getControl().isDisposed())
                    return;

                TreeViewer treeViewer = getTreeViewer();
                if (treeViewer != null) {
                    treeViewer.getTree().setRedraw(false);
                    treeViewer.setInput(theForms);
                    ISelection treeSelection = treeViewer.getSelection();
                    if (treeSelection == null || treeSelection.isEmpty()) {
                        selectInOutline(lastSelection);
                    }
                    treeViewer.getTree().setRedraw(true);
                }
            }
        });
    }
}

From source file:ch.hsr.ifs.cdt.metriculator.views.MetricColumn.java

License:Open Source License

public static TreeColumn createFor(AbstractMetric metric, TreeViewer treeViewer) {

    TreeColumn column = new TreeColumn(treeViewer.getTree(), SWT.RIGHT);
    column.setText(metric.getName());//from   w ww .j a va2s  .co m
    column.setToolTipText(metric.getDescription());

    setMetric(metric, column);
    MetricColumnViewerSorter.registerFor(column, treeViewer);

    return column;
}