Example usage for org.eclipse.jface.viewers StructuredViewer getStructuredSelection

List of usage examples for org.eclipse.jface.viewers StructuredViewer getStructuredSelection

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers StructuredViewer getStructuredSelection.

Prototype

public IStructuredSelection getStructuredSelection() throws ClassCastException 

Source Link

Document

Returns the IStructuredSelection of this viewer.

Usage

From source file:org.eclipse.oomph.internal.ui.FindAndReplaceTarget.java

License:Open Source License

public void initialize(IWorkbenchPart workbenchPart) {
    // This method is called by the find and replace action before it opens the find and replace dialog.
    // It has the opportunity to see the state before the find and replace dialog takes focus away.
    // In particular, it can see the selected text in an active cell editor in the properties view.
    StructuredViewer viewer = getViewer();
    Tree propertySheetTree = getActivePropertySheetTree();
    if (propertySheetTree != null) {
        // If there is an active property sheet with a tree, iterate over the selected tree items.
        for (TreeItem treeItem : propertySheetTree.getSelection()) {
            // Determine if there is an EMF property descriptor associated with it.
            PropertyDescriptor propertyDescriptor = getPropertyDescriptor(treeItem);
            if (propertyDescriptor != null) {
                // If so, extract the object and look for it in the data.
                Object object = getObject(propertyDescriptor);
                for (FindAndReplaceTarget.Data data : new TextData(viewer)) {
                    if (data.object == object) {
                        // Look for the feature in the data items.
                        Object feature = getFeature(propertyDescriptor);
                        for (Data.Item item : data.items) {
                            Object itemFeature = item.getFeature();
                            if (itemFeature == feature) {
                                // If there is a focus text control, it must be the cell editor of this property.
                                Control control = workbenchPart.getSite().getShell().getDisplay()
                                        .getFocusControl();
                                if (control instanceof Text) {
                                    // Extract the selected text, if any...
                                    Text text = (Text) control;
                                    selectionText = text.getSelectionText();
                                    if (selectionText.length() > 0) {
                                        // Use this item's selected text as our initial selection.
                                        Point selection = text.getSelection();
                                        setSelection(true, viewer, item, selection.x,
                                                Pattern.compile(Pattern.quote(selectionText)));

                                        // Then we're done.
                                        return;
                                    }/*from   w ww. ja va 2 s  . co m*/
                                }

                                // Use this overall item as our initial selection.
                                setSelection(true, viewer, item, 0, Pattern.compile(Pattern.quote(item.value)));
                                return;
                            }
                        }

                        // Once we've passed the object of interest, there is nothing left to do.
                        break;
                    }
                }
            }
        }
    } else {
        // Otherwise, use the first item selected in the viewer as our initial selection.
        List<?> list = viewer.getStructuredSelection().toList();
        for (FindAndReplaceTarget.Data data : new TextData(viewer)) {
            if (list.contains(data.object)) {
                Data.Item item = data.items.get(0);
                setSelection(true, viewer, item, 0, Pattern.compile(Pattern.quote(item.value)));
                return;
            }
        }
    }
}

From source file:org.eclipse.oomph.internal.ui.FindAndReplaceTarget.java

License:Open Source License

public Point getLineSelection() {
    // This method is used only to compute region to pass to setScope.
    // So instead of computing something useless we use this opportunity to remember the viewer's selection.
    StructuredViewer viewer = getViewer();
    selectionScope = viewer.getStructuredSelection().toList();
    return new Point(0, 0);
}

From source file:org.eclipse.oomph.internal.ui.FindAndReplaceTarget.java

License:Open Source License

public Point getSelection() {
    if (pendingReplacements >= 0) {
        return new Point(pendingReplacements, 0);
    }/*from  w  w  w.ja v  a 2  s  . c  o m*/

    // This method is super important for determining the point from which the next find processing will proceed.
    // If there is a selected item, we should proceed from the end of the current match.
    if (selectedItem != null) {
        Matcher matcher = selectedItemPattern.matcher(selectedItem.value);
        int size = 0;
        if (selectedItem.value.length() >= selectedItemStart && matcher.find(selectedItemStart)) {
            size = matcher.group().length();
        }

        Point point = new Point(selectedItem.index + selectedItemStart, size);
        return point;
    }

    // If there is an active property sheet tree.
    StructuredViewer viewer = getViewer();
    Tree propertySheetTree = getActivePropertySheetTree();
    if (propertySheetTree != null) {
        // Look through the selection.
        for (final TreeItem treeItem : propertySheetTree.getSelection()) {
            // If it has and EMF property descriptor
            PropertyDescriptor propertyDescriptor = getPropertyDescriptor(treeItem);
            if (propertyDescriptor != null) {
                // Determine it's wrapped object and look for it in the induced text data.
                Object object = getObject(propertyDescriptor);
                for (FindAndReplaceTarget.Data data : new TextData(viewer)) {
                    // If we find it...
                    if (data.object == object) {
                        // Determine which feature is the selected feature.
                        Object feature = getFeature(propertyDescriptor);

                        // Collection all features before the selected feature.
                        List<Object> features = new ArrayList<Object>();
                        for (final TreeItem otherTreeIem : propertySheetTree.getItems()) {
                            PropertyDescriptor otherPropertyDescriptor = getPropertyDescriptor(otherTreeIem);
                            if (otherPropertyDescriptor != null) {
                                Object otherFeature = getFeature(otherPropertyDescriptor);
                                features.add(otherFeature);
                                if (otherFeature == feature) {
                                    break;
                                }
                            }
                        }

                        // Consider all the items.
                        Data.Item candidate = null;
                        for (Data.Item item : data.items) {
                            // If we find an exact match, return the information for it immediately, otherwise consider it a candidate.
                            Object itemFeature = item.getFeature();
                            if (itemFeature == feature) {
                                return new Point(item.index, 0);
                            } else if (features.contains(itemFeature)) {
                                candidate = item;
                            }
                        }

                        // If there is a candidate, return the information for it.
                        if (candidate != null) {
                            return new Point(candidate.index, 0);
                        }

                        // If we find nothing, there's no point in looking anywhere else.
                        break;
                    }
                }
            }
        }
    }

    // Otherwise find the first item of an object in the selection.
    List<?> list = viewer.getStructuredSelection().toList();
    for (FindAndReplaceTarget.Data data : new TextData(viewer)) {
        if (list.contains(data.object)) {
            return new Point(data.items.get(0).index, 0);
        }
    }

    // Start at the beginning.
    return new Point(0, 0);
}