Example usage for org.eclipse.jface.viewers ITreeSelection isEmpty

List of usage examples for org.eclipse.jface.viewers ITreeSelection isEmpty

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers ITreeSelection isEmpty.

Prototype

public boolean isEmpty();

Source Link

Document

Returns whether this selection is empty.

Usage

From source file:com.android.ide.eclipse.adt.internal.editors.layout.gle2.SelectionManager.java

License:Open Source License

/**
 * Sets the selection. It must be an {@link ITreeSelection} where each segment
 * of the tree path is a {@link CanvasViewInfo}. A null selection is considered
 * as an empty selection./*from  w w  w  . j a  va 2 s. co  m*/
 * <p/>
 * This method is invoked by {@link LayoutCanvasViewer#setSelection(ISelection)}
 * in response to an <em>outside</em> selection (compatible with ours) that has
 * changed. Typically it means the outline selection has changed and we're
 * synchronizing ours to match.
 */
@Override
public void setSelection(ISelection selection) {
    if (mInsideUpdateSelection) {
        return;
    }

    boolean changed = false;
    try {
        mInsideUpdateSelection = true;

        if (selection == null) {
            selection = TreeSelection.EMPTY;
        }

        if (selection instanceof ITreeSelection) {
            ITreeSelection treeSel = (ITreeSelection) selection;

            if (treeSel.isEmpty()) {
                // Clear existing selection, if any
                if (!mSelections.isEmpty()) {
                    mSelections.clear();
                    mAltSelection = null;
                    updateActionsFromSelection();
                    redraw();
                }
                return;
            }

            boolean redoLayout = false;

            // Create a list of all currently selected view infos
            Set<CanvasViewInfo> oldSelected = new HashSet<CanvasViewInfo>();
            for (SelectionItem cs : mSelections) {
                oldSelected.add(cs.getViewInfo());
            }

            // Go thru new selection and take care of selecting new items
            // or marking those which are the same as in the current selection
            for (TreePath path : treeSel.getPaths()) {
                Object seg = path.getLastSegment();
                if (seg instanceof CanvasViewInfo) {
                    CanvasViewInfo newVi = (CanvasViewInfo) seg;
                    if (oldSelected.contains(newVi)) {
                        // This view info is already selected. Remove it from the
                        // oldSelected list so that we don't deselect it later.
                        oldSelected.remove(newVi);
                    } else {
                        // This view info is not already selected. Select it now.

                        // reset alternate selection if any
                        mAltSelection = null;
                        // otherwise add it.
                        mSelections.add(createSelection(newVi));
                        changed = true;
                    }
                    if (newVi.isInvisible()) {
                        redoLayout = true;
                    }
                } else {
                    // Unrelated selection (e.g. user clicked in the Project Explorer
                    // or something) -- just ignore these
                    return;
                }
            }

            // Deselect old selected items that are not in the new one
            for (CanvasViewInfo vi : oldSelected) {
                if (vi.isExploded()) {
                    redoLayout = true;
                }
                deselect(vi);
                changed = true;
            }

            if (redoLayout) {
                mCanvas.getEditorDelegate().recomputeLayout();
            }
        }
    } finally {
        mInsideUpdateSelection = false;
    }

    if (changed) {
        redraw();
        fireSelectionChanged();
        updateActionsFromSelection();
    }
}

From source file:com.android.ide.eclipse.adt.internal.wizards.newproject.WorkingSetHelper.java

License:Open Source License

private static IWorkingSet[] getSelectedWorkingSet(IStructuredSelection selection) {
    if (!(selection instanceof ITreeSelection))
        return EMPTY_WORKING_SET_ARRAY;

    ITreeSelection treeSelection = (ITreeSelection) selection;
    if (treeSelection.isEmpty())
        return EMPTY_WORKING_SET_ARRAY;

    List<?> elements = treeSelection.toList();
    if (elements.size() == 1) {
        Object element = elements.get(0);
        TreePath[] paths = treeSelection.getPathsFor(element);
        if (paths.length != 1)
            return EMPTY_WORKING_SET_ARRAY;

        TreePath path = paths[0];
        if (path.getSegmentCount() == 0)
            return EMPTY_WORKING_SET_ARRAY;

        Object candidate = path.getSegment(0);
        if (!(candidate instanceof IWorkingSet))
            return EMPTY_WORKING_SET_ARRAY;

        IWorkingSet workingSetCandidate = (IWorkingSet) candidate;
        if (isValidWorkingSet(workingSetCandidate))
            return new IWorkingSet[] { workingSetCandidate };

        return EMPTY_WORKING_SET_ARRAY;
    }/*w w w  .  j  ava2 s .c  om*/

    ArrayList<Object> result = new ArrayList<Object>();
    for (Iterator<?> iterator = elements.iterator(); iterator.hasNext();) {
        Object element = iterator.next();
        if (element instanceof IWorkingSet && isValidWorkingSet((IWorkingSet) element)) {
            result.add(element);
        }
    }
    return result.toArray(new IWorkingSet[result.size()]);
}

From source file:com.nokia.s60tools.stif.scripteditor.editors.ScriptEditor.java

License:Open Source License

public void selectionChanged(SelectionChangedEvent sce) {
    ITreeSelection selection = (ITreeSelection) sce.getSelection();
    if (selection.isEmpty())
        return;//from  ww w. j a v a2  s .c  om

    TreePath[] selections = selection.getPaths();
    TestCase testCase = (TestCase) selections[0].getFirstSegment();

    IDocument doc = getDocumentProvider().getDocument(getEditorInput());

    String contentOfDoc = doc.get();
    String name = testCase.getName();
    name = name.replaceAll("\\(", "\\\\(").replaceAll("\\)", "\\\\)");
    String patternString = "\\s+title\\s+(" + name + ")\\s*$";

    Pattern pattern = Pattern.compile(patternString, Pattern.MULTILINE);
    Matcher regExMatcher = pattern.matcher(contentOfDoc);
    ArrayList<Integer> indexesList = new ArrayList<Integer>();
    while (regExMatcher.find()) {
        indexesList.add(regExMatcher.start(1));
        indexesList.add(regExMatcher.end(1));
    }

    int oneFromCasesWithSameName = 0;
    if (indexesList.size() > 2) {
        TreeViewer tv = (TreeViewer) sce.getSource();
        Tree tree = tv.getTree();
        TreeItem[] treeItems = tree.getItems();

        for (int i = 0; i < treeItems.length; i++) {
            if (treeItems[i].getText().equals(tree.getSelection()[0].getText())) {
                if (treeItems[i] == tree.getSelection()[0]) {
                    break;
                }
                oneFromCasesWithSameName++;
            }
        }
    }
    if (indexesList.size() > 0) {
        selectAndReveal(indexesList.get(oneFromCasesWithSameName * 2),
                indexesList.get(oneFromCasesWithSameName * 2 + 1)
                        - indexesList.get(oneFromCasesWithSameName * 2));
    }
}

From source file:com.redhat.ceylon.eclipse.code.outline.CeylonOutlinePage.java

License:Open Source License

@Override
public void selectionChanged(SelectionChangedEvent event) {
    super.selectionChanged(event);
    if (!suspend) {
        ITreeSelection sel = (ITreeSelection) event.getSelection();
        if (!sel.isEmpty()) {
            Node node = ((CeylonOutlineNode) sel.getFirstElement()).getTreeNode();
            int startOffset = getStartOffset(node);
            int endOffset = getEndOffset(node);
            int length = endOffset - startOffset + 1;
            ((ITextEditor) getCurrentEditor()).selectAndReveal(startOffset, length);
        }//from www  .  ja  va 2 s. com
    }
}

From source file:com.redhat.ceylon.eclipse.code.wizard.NewCeylonProjectWizardPageOne.java

License:Open Source License

private IWorkingSet[] getSelectedWorkingSet(IStructuredSelection selection) {
    if (!(selection instanceof ITreeSelection))
        return EMPTY_WORKING_SET_ARRAY;

    ITreeSelection treeSelection = (ITreeSelection) selection;
    if (treeSelection.isEmpty())
        return EMPTY_WORKING_SET_ARRAY;

    List<?> elements = treeSelection.toList();
    if (elements.size() == 1) {
        Object element = elements.get(0);
        TreePath[] paths = treeSelection.getPathsFor(element);
        if (paths.length != 1)
            return EMPTY_WORKING_SET_ARRAY;

        TreePath path = paths[0];
        if (path.getSegmentCount() == 0)
            return EMPTY_WORKING_SET_ARRAY;

        Object candidate = path.getSegment(0);
        if (!(candidate instanceof IWorkingSet))
            return EMPTY_WORKING_SET_ARRAY;

        IWorkingSet workingSetCandidate = (IWorkingSet) candidate;
        if (isValidWorkingSet(workingSetCandidate))
            return new IWorkingSet[] { workingSetCandidate };

        return EMPTY_WORKING_SET_ARRAY;
    }// w  ww.  j a  va 2s.com

    ArrayList<IWorkingSet> result = new ArrayList<IWorkingSet>();
    for (Iterator<?> iterator = elements.iterator(); iterator.hasNext();) {
        Object element = iterator.next();
        if (element instanceof IWorkingSet && isValidWorkingSet((IWorkingSet) element)) {
            result.add((IWorkingSet) element);
        }
    }
    return result.toArray(new IWorkingSet[result.size()]);
}

From source file:com.siteview.mde.internal.runtime.registry.RegistryBrowser.java

License:Open Source License

private void makeActions() {
    fRefreshAction = new Action("refresh") { //$NON-NLS-1$
        public void run() {
            BusyIndicator.showWhile(fTreeViewer.getTree().getDisplay(), new Runnable() {
                public void run() {
                    refresh(fTreeViewer.getInput());
                }//from   w w w.j av  a 2s.  c o  m
            });
        }
    };
    fRefreshAction.setText(MDERuntimeMessages.RegistryView_refresh_label);
    fRefreshAction.setToolTipText(MDERuntimeMessages.RegistryView_refresh_tooltip);
    fRefreshAction.setImageDescriptor(PDERuntimePluginImages.DESC_REFRESH);
    fRefreshAction.setDisabledImageDescriptor(PDERuntimePluginImages.DESC_REFRESH_DISABLED);

    fShowPluginsAction = new Action(MDERuntimeMessages.RegistryView_showRunning_label) {
        public void run() {
            if (fShowPluginsAction.isChecked()) {
                fTreeViewer.addFilter(fActiveFilter);
            } else {
                fTreeViewer.removeFilter(fActiveFilter);
            }
            updateTitle();
        }
    };
    fShowPluginsAction.setChecked(fMemento.getString(SHOW_RUNNING_PLUGINS).equals("true")); //$NON-NLS-1$

    fShowDisabledAction = new Action(MDERuntimeMessages.RegistryView_showDisabled_label) {
        public void run() {
            if (fShowDisabledAction.isChecked()) {
                fTreeViewer.addFilter(fDisabledFilter);
            } else {
                fTreeViewer.removeFilter(fDisabledFilter);
            }
            updateTitle();
        }
    };
    fShowDisabledAction.setChecked(fMemento.getString(SHOW_DISABLED_MODE).equals("true")); //$NON-NLS-1$

    fCopyAction = new Action(MDERuntimeMessages.RegistryBrowser_copy_label) {
        public void run() {
            ITreeSelection selection = (ITreeSelection) fFilteredTree.getViewer().getSelection();
            if (selection.isEmpty()) {
                return;
            }

            String textVersion = ((ILabelProvider) fTreeViewer.getLabelProvider())
                    .getText(selection.getFirstElement());
            if ((textVersion != null) && (textVersion.trim().length() > 0)) {
                // set the clipboard contents
                fClipboard.setContents(new Object[] { textVersion },
                        new Transfer[] { TextTransfer.getInstance() });
            }
        }
    };
    fCopyAction.setImageDescriptor(PDERuntimePluginImages.COPY_QNAME);

    fGroupByBundlesAction = new GroupByAction(MDERuntimeMessages.RegistryBrowser_Bundle, BUNDLES);
    int groupBy = getGroupBy();
    fGroupByBundlesAction.setChecked(groupBy == BUNDLES);
    fGroupByExtensionPointsAction = new GroupByAction(MDERuntimeMessages.RegistryBrowser_ExtensionPoint,
            EXTENSION_REGISTRY);
    fGroupByExtensionPointsAction.setChecked(groupBy == EXTENSION_REGISTRY);
    fGroupByServicesAction = new GroupByAction(MDERuntimeMessages.RegistryBrowser_Service, SERVICES);
    fGroupByServicesAction.setChecked(groupBy == SERVICES);

    fShowAdvancedOperationsAction = new Action(MDERuntimeMessages.RegistryView_showAdvanced_label) {
        public void run() { // do nothing
        }
    };
    fShowAdvancedOperationsAction.setChecked(fMemento.getString(SHOW_ADVANCED_MODE).equals("true")); //$NON-NLS-1$

    fStartAction = new Action(MDERuntimeMessages.RegistryView_startAction_label) {
        public void run() {
            try {
                List bundles = getSelectedBundles();
                for (Iterator it = bundles.iterator(); it.hasNext();) {
                    Bundle bundle = (Bundle) it.next();
                    bundle.start();
                }
            } catch (BundleException e) {
                PDERuntimePlugin.log(e);
            }
        }
    };

    fStopAction = new Action(MDERuntimeMessages.RegistryView_stopAction_label) {
        public void run() {
            try {
                List bundles = getSelectedBundles();
                for (Iterator it = bundles.iterator(); it.hasNext();) {
                    Bundle bundle = (Bundle) it.next();
                    bundle.stop();
                }
            } catch (BundleException e) {
                PDERuntimePlugin.log(e);
            }
        }
    };

    fEnableAction = new Action(MDERuntimeMessages.RegistryView_enableAction_label) {
        public void run() {
            List bundles = getSelectedBundles();
            for (Iterator it = bundles.iterator(); it.hasNext();) {
                Bundle bundle = (Bundle) it.next();
                bundle.enable();
            }
        }
    };

    fDisableAction = new Action(MDERuntimeMessages.RegistryView_disableAction_label) {
        public void run() {
            List bundles = getSelectedBundles();
            for (Iterator it = bundles.iterator(); it.hasNext();) {
                Bundle bundle = (Bundle) it.next();
                bundle.disable();
            }
        }
    };

    fDiagnoseAction = new Action(MDERuntimeMessages.RegistryView_diagnoseAction_label) {
        public void run() {
            List bundles = getSelectedBundles();
            for (Iterator it = bundles.iterator(); it.hasNext();) {
                Bundle bundle = (Bundle) it.next();
                MultiStatus problems = bundle.diagnose();

                Dialog dialog;
                if ((problems != null) && (problems.getChildren().length > 0)) {
                    dialog = new DiagnosticsDialog(getSite().getShell(),
                            MDERuntimeMessages.RegistryView_diag_dialog_title, null, problems, IStatus.WARNING);
                    dialog.open();
                } else {
                    MessageDialog.openInformation(getSite().getShell(),
                            MDERuntimeMessages.RegistryView_diag_dialog_title,
                            MDERuntimeMessages.RegistryView_no_unresolved_constraints);
                }

            }
        }
    };

    fCollapseAllAction = new Action("collapseAll") { //$NON-NLS-1$
        public void run() {
            fTreeViewer.collapseAll();
        }
    };
    fCollapseAllAction.setText(MDERuntimeMessages.RegistryView_collapseAll_label);
    fCollapseAllAction.setImageDescriptor(PDERuntimePluginImages.DESC_COLLAPSE_ALL);
    fCollapseAllAction.setToolTipText(MDERuntimeMessages.RegistryView_collapseAll_tooltip);
}

From source file:de.bmw.yamaica.ide.ui.internal.editor.OverviewPage.java

License:Mozilla Public License

private ITreeSelection createSingleSelection(ISelection selection, Object element) {
    ITreeSelection treeSelection = (ITreeSelection) selection;

    if (treeSelection.isEmpty() || treeSelection.size() > 1) {
        Object[] segments = new Object[] { element };
        treeSelection = new TreeSelection(new TreePath(segments));
    }//  w w  w .  j  a  v a 2 s . c  o  m

    return treeSelection;
}

From source file:de.walware.statet.r.internal.objectbrowser.CopyElementNameHandler.java

License:Open Source License

private boolean isValidSelection(ITreeSelection selection) {
    if (selection == null || selection.isEmpty()) {
        return false;
    }//w  ww.  j a  v  a 2 s  .  c o m
    for (final Iterator<?> iter = selection.iterator(); iter.hasNext();) {
        Object element = iter.next();
        if (element instanceof IElementPartition) {
            return false;
        }
    }
    return true;
}

From source file:edu.utexas.cs.orc.orceclipse.edit.OrcContentOutlinePage.java

License:Open Source License

/**
 * Notifies that the selection has changed.
 *
 * @param event event object describing the change
 *//*from w w  w  .  java2 s . com*/
@Override
public void selectionChanged(final SelectionChangedEvent event) {
    super.selectionChanged(event);

    final ITreeSelection selection = (ITreeSelection) event.getSelection();
    if (selection.isEmpty()) {
        return;
    }
    final OutlineTreeNode selection1 = (OutlineTreeNode) selection.getFirstElement();
    if (selection1 instanceof IRegion) {
        final IRegion region = (IRegion) selection1;
        pairedEditor.selectAndReveal(region.getOffset(), region.getLength());
    } else {
        pairedEditor.resetHighlightRange();
    }
}

From source file:eu.modelwriter.marker.internal.MarkerFactory.java

License:Open Source License

/**
 * @param selections// ww w.  ja  va2s. c om
 * @return
 */
public static String getQualifiedName(final ITreeSelection selections) {
    final TreePath[] paths = selections.getPaths();

    // Consider only not empty and single selection
    if (selections.isEmpty() || selections.size() > 1) {
        return null;
    }

    final TreePath path = paths[0];
    IElementComparer comparer = null;
    if (selections instanceof TreeSelection) {
        comparer = ((TreeSelection) selections).getElementComparer();
    }
    System.out.println(path.hashCode(comparer));
    for (int i = 1; i < path.getSegmentCount(); i++) {
        if (path.getSegment(i) instanceof ResourceFactoryImpl) {
            final EcoreResourceFactoryImpl eResourceFactory = (EcoreResourceFactoryImpl) path.getSegment(i);
            System.out.println(eResourceFactory.getClass().getName() + ": " + eResourceFactory.toString());
        } else if (path.getSegment(i) instanceof ENamedElement) {
            final ENamedElement namedElement = (ENamedElement) path.getSegment(i);
            System.out.println(namedElement.getClass().getName() + ": " + namedElement.getName());
        } else {
            System.out.println("?");
        }
    }
    return null;
}