Example usage for org.eclipse.jface.viewers IStructuredSelection size

List of usage examples for org.eclipse.jface.viewers IStructuredSelection size

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers IStructuredSelection size.

Prototype

public int size();

Source Link

Document

Returns the number of elements selected in this selection.

Usage

From source file:bndtools.MakeBundleWithRefreshAction.java

License:Open Source License

@Override
public void run(IAction action) {
    // What should we refresh??
    IStructuredSelection selection = StructuredSelection.EMPTY;
    if (targetPart instanceof IEditorPart) {
        IEditorInput input = ((IEditorPart) targetPart).getEditorInput();
        if (input instanceof IFileEditorInput) {
            IFile file = ((IFileEditorInput) input).getFile();
            selection = new StructuredSelection(file);
        }/* w ww .  j  a v  a2 s.com*/
    } else {
        ISelection sel = targetPart.getSite().getSelectionProvider().getSelection();
        if (sel instanceof IStructuredSelection) {
            selection = (IStructuredSelection) sel;
        }
    }
    selectionChanged(action, selection);
    super.run(action);

    final List<IResource> resources = new ArrayList<IResource>(selection.size());
    for (Iterator<?> iter = selection.iterator(); iter.hasNext();) {
        Object next = iter.next();
        if (next instanceof IResource)
            resources.add((IResource) next);
    }
    if (!resources.isEmpty()) {
        final IWorkspace workspace = resources.get(0).getWorkspace();
        final IWorkspaceRunnable operation = new IWorkspaceRunnable() {
            public void run(IProgressMonitor monitor) throws CoreException {
                SubMonitor progress = SubMonitor.convert(monitor, resources.size());
                MultiStatus status = new MultiStatus(Plugin.PLUGIN_ID, 0,
                        "One or more errors occurred while refreshing resources", null);
                for (IResource resource : resources) {
                    try {
                        resource.getParent().refreshLocal(1, progress.newChild(1));
                    } catch (CoreException e) {
                        status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, e.getMessage(), e));
                    }
                }
                if (!status.isOK()) {
                    ErrorDialog.openError(targetPart.getSite().getShell(), "Error", null, status);
                }
            }
        };
        IRunnableWithProgress task = new IRunnableWithProgress() {
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    workspace.run(operation, monitor);
                } catch (CoreException e) {
                    throw new InvocationTargetException(e);
                }
            }
        };
        try {
            targetPart.getSite().getWorkbenchWindow().run(false, false, task);
        } catch (InvocationTargetException e) {
            CoreException ce = (CoreException) e.getCause();
            ErrorDialog.openError(targetPart.getSite().getShell(), "Error", null,
                    new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error refreshing resources", ce));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    // Process it
    IRunnableContext context = targetPart.getSite().getWorkbenchWindow();
    try {
        context.run(false, false, new IRunnableWithProgress() {
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            }
        });
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:bndtools.utils.SelectionUtils.java

License:Open Source License

public static <T> Collection<T> getSelectionMembers(ISelection selection, Class<T> clazz,
        Predicate<? super T> filter) throws Exception {
    if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
        return Collections.emptyList();
    }//from   ww  w .j  a va  2 s.  c  om

    IStructuredSelection structSel = (IStructuredSelection) selection;
    List<T> result = new ArrayList<T>(structSel.size());
    Iterator<?> iter = structSel.iterator();
    while (iter.hasNext()) {
        Object element = iter.next();
        if (clazz.isInstance(element)) {
            @SuppressWarnings("unchecked")
            T casted = (T) element;
            if (filter == null || filter.test(casted)) {
                result.add(casted);
            }
        } else if (element instanceof IAdaptable) {
            @SuppressWarnings("unchecked")
            T adapted = (T) ((IAdaptable) element).getAdapter(clazz);
            if (adapted != null) {
                if (filter == null || filter.test(adapted)) {
                    result.add(adapted);
                }
            }
        }
    }
    return result;
}

From source file:ca.edchipman.silverstripepdt.wizards.NewSilverStripeTemplatesWizardPage.java

License:Open Source License

/**
 * Get the currently selected template.//from   w  ww  .jav  a 2 s . c om
 * 
 * @return
 */
private Template getSelectedTemplate() {
    Template template = null;
    IStructuredSelection selection = (IStructuredSelection) fTableViewer.getSelection();

    if (selection.size() == 1) {
        template = (Template) selection.getFirstElement();
    }
    return template;
}

From source file:ca.usask.cs.srlab.simclipse.clone.comparison.CloneCompareAction.java

License:Open Source License

protected boolean isEnabled(ISelection selection) {

    if (!(selection instanceof StructuredSelection)) {
        return false;
    }/* ww w  . ja  va  2  s.  c om*/

    IStructuredSelection ssel = (IStructuredSelection) selection;
    if (ssel.size() < 1) {
        return false;
    }

    if ((((StructuredSelection) selection).getFirstElement() instanceof CloneSetDisplayModel)) {
        List<IFile> fileList = new ArrayList<IFile>(
                ((CloneSetDisplayModel) ((StructuredSelection) selection).getFirstElement()).size());

        List<? extends ICloneViewItem> cloneFragmentModels = ((CloneSetDisplayModel) ((StructuredSelection) selection)
                .getFirstElement()).getCloneFragmentModels();
        if (cloneFragmentModels.size() > 2) {

            //            MessageDialog
            //                  .openWarning(
            //                        SimClipsePlugin.getActiveWorkbenchShell(),
            //                        "Warning!",
            //                        "Fragments more than two cannot be compared. Please select any two fragments and click compare");
            return false;
        }

        for (ICloneViewItem cvi : cloneFragmentModels) {
            CloneFragmentDisplayModel tempObj = (CloneFragmentDisplayModel) cvi;
            IFile ifile = (IFile) tempObj.getResource();
            fileList.add(ifile);
        }
        selection = new StructuredSelection(fileList);
    }

    if (comparatorInput == null) {
        CompareConfiguration cc = new CompareConfiguration();
        // buffered merge mode: don't ask for confirmation
        // when switching between modified resources

        //cc.setProperty(CompareEditor.CONFIRM_SAVE_PROPERTY, new Boolean(false));

        // uncomment following line to have separate outline view
        //cc.setProperty(CompareConfiguration.USE_OUTLINE_VIEW, new Boolean(true));

        comparatorInput = new ResourceCompareInput(cc);
    }
    return comparatorInput.isEnabled(selection);
}

From source file:ca.uvic.cs.tagsea.ui.views.RoutesComposite.java

License:Open Source License

protected void selectionChanged(SelectionChangedEvent event) {
    if (event.getSelection().isEmpty()) {
        enableAllActions(false);/* ww w .j  av a2 s. c  om*/
    } else {
        enableAllActions(true);
        IStructuredSelection selections = (IStructuredSelection) event.getSelection();

        if (selections.size() == 1 && selections.getFirstElement() instanceof Route) {
            Route route = (Route) selections.getFirstElement();

            if (route.getWaypoints().size() == 0) {
                backAction.setEnabled(false);
                forwardAction.setEnabled(false);
            }
        }
    }

    boolean routesSelected = false;

    TreeItem[] selections = routesTreeViewer.getTree().getSelection();

    for (TreeItem item : selections) {
        if (item.getData() instanceof Route)
            routesSelected = true;
    }

    if (routesSelected)
        removeStaleWaypointsAction.setEnabled(true);
    else
        removeStaleWaypointsAction.setEnabled(false);
}

From source file:ca.uwaterloo.gp.fmp.system.drillDown.FmpDrillDownAdapter.java

License:Open Source License

/**
 * Returns whether "go into" is possible for child tree.  This is only possible 
 * if the current selection in the client has one item and it has children.
 *
 * @return <code>true</code> if "go into" is possible; <code>false</code> otherwise
 *///w w  w.  j av  a 2 s.  c om
public boolean canGoInto() {
    IStructuredSelection oSelection = (IStructuredSelection) fChildTree.getSelection();
    if (oSelection == null || oSelection.size() != 1)
        return false;
    Object anElement = oSelection.getFirstElement();
    return (RoleQuery.INSTANCE.getNodeType((Node) anElement) == RoleQuery.ROOT_FEATURE);
}

From source file:carisma.ui.eclipse.editors.AdfEditorCheckDetailsPage.java

License:Open Source License

@Override
public final void selectionChanged(final IFormPart part, final ISelection selection) {
    if (selection != null && !selection.isEmpty()) {
        IStructuredSelection ssel = (IStructuredSelection) selection;
        if (ssel.size() == 1) {
            this.input = (CheckReference) ssel.getFirstElement();
            if (this.descriptionSection != null) {
                this.descriptionSection.dispose();
            }//from  w  w w.j  a va2  s.c o  m
            if (this.parameterSection != null) {
                this.parameterSection.dispose();
            }
            createDescriptionSection(this.input);
            createParameterSection(this.input);
            this.parentComposite.layout(); // refresh
        }
    } else {
        createDescriptionSection(null);
    }
}

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

License:Open Source License

private void selectInEditor(ISelection selection) {
    IStructuredSelection sel = (IStructuredSelection) selection;
    if (sel.size() == 0)
        return;/*from  w ww.  j  a v  a2s .  c om*/

    Obj obj = (Obj) sel.getFirstElement();
    int lineNr = getLineNr(obj);
    if (lineNr >= 0) {
        ClojureCore.gotoEditorLine(editor, lineNr);
    }
}

From source file:ch.elexis.core.ui.dialogs.KontaktSelektor.java

License:Open Source License

@Override
protected Control createDialogArea(final Composite parent) {
    // SashForm ret=new SashForm(parent,SWT.NONE);
    Composite ret = new Composite(parent, SWT.NONE);
    ret.setLayout(new GridLayout());
    ret.setLayoutData(SWTHelper.getFillGridData(1, true, 1, true));

    if (showBezugsKontakt) {
        new Label(ret, SWT.NONE).setText("Bezugskontakte");
        bezugsKontaktViewer = new ListViewer(ret, SWT.SINGLE);
        bezugsKontaktViewer.getControl().setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));

        bezugsKontaktViewer.setContentProvider(new IStructuredContentProvider() {
            public Object[] getElements(Object inputElement) {
                Patient patient = ElexisEventDispatcher.getSelectedPatient();
                if (patient != null) {
                    ArrayList<PersistentObject> elements = new ArrayList<PersistentObject>();
                    ArrayList<String> addedKontakte = new ArrayList<String>();

                    // add the patient itself
                    elements.add(patient);
                    addedKontakte.add(patient.getId());

                    List<BezugsKontakt> bezugsKontakte = patient.getBezugsKontakte();
                    if (bezugsKontakte != null) {
                        for (BezugsKontakt bezugsKontakt : bezugsKontakte) {
                            elements.add(bezugsKontakt);
                            addedKontakte.add(bezugsKontakt.get("otherID"));
                        }//  ww w.j a v  a  2  s . co m
                    }

                    // required contacts of biling system
                    Fall[] faelle = patient.getFaelle();
                    for (Fall fall : faelle) {
                        String reqs = fall.getRequirements();
                        if (reqs != null) {
                            for (String req : reqs.split(";")) {
                                final String[] r = req.split(":");

                                // no valid entry
                                if (r.length < 2) {
                                    continue;
                                }

                                // only consider contacts
                                if (r[1].equals("K")) {
                                    String kontaktID = fall.getInfoString(r[0]);
                                    if (!kontaktID.startsWith("**ERROR")) {
                                        Kontakt kontakt = Kontakt.load(kontaktID);
                                        if (kontakt.isValid()) {
                                            elements.add(kontakt);
                                            addedKontakte.add(kontakt.getId());
                                        }
                                    }
                                }
                            }
                        }
                    }

                    return elements.toArray();
                }

                return new Object[] {};
            }

            public void dispose() {
                // nothing to do
            }

            public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
                // nothing to do
            }
        });
        bezugsKontaktViewer.setLabelProvider(new KontaktSelektorLabelProvider());
        bezugsKontaktViewer.setInput(this);
        bezugsKontaktViewer.addSelectionChangedListener(new ISelectionChangedListener() {
            public void selectionChanged(SelectionChangedEvent event) {
                if (isSelecting) {
                    return;
                }

                IStructuredSelection sel = (IStructuredSelection) cv.getViewerWidget().getSelection();
                if (sel.size() > 0) {
                    isSelecting = true;
                    cv.getViewerWidget().setSelection(new StructuredSelection(), false);
                    isSelecting = false;
                }
            }
        });
    } else {
        bezugsKontaktViewer = null;
    }

    if (showBezugsKontakt) {
        new Label(ret, SWT.NONE).setText("Andere Kontakte");
    }
    if (extraText != null) {
        new Label(ret, SWT.WRAP).setText(extraText);
    }
    vc = new ViewerConfigurer(
            // new LazyContentProvider(cv,dataloader, null),
            kl, new KontaktSelektorLabelProvider(),
            new DefaultControlFieldProvider(cv, new String[] { Messages.KontaktSelector_abbreviation,
                    Messages.KontaktSelector_expression1, Messages.KontaktSelector_birthDate }),
            new ViewerConfigurer.ButtonProvider() {

                public Button createButton(final Composite parent) {
                    Button ret = new Button(parent, SWT.PUSH);
                    ret.setText("Neu erstellen...");
                    ret.addSelectionListener(new SelectionAdapter() {

                        @Override
                        public void widgetSelected(SelectionEvent e) {
                            if (hints == null) {
                                hints = new String[3];
                                hints[0] = vc.getControlFieldProvider().getValues()[1];
                            }
                            KontaktErfassenDialog ked = new KontaktErfassenDialog(parent.getShell(), hints);
                            ked.open();
                            selection = ked.getResult();
                            okPressed();
                            // cv.getViewerWidget().refresh();
                            // cv.getViewerWidget().setSelection(new
                            // StructuredSelection(kr), true);
                        }

                    });
                    return ret;
                }

                public boolean isAlwaysEnabled() {
                    return false;
                }
            }, new SimpleWidgetProvider(SimpleWidgetProvider.TYPE_LAZYLIST, SWT.NONE, cv));
    Composite types = new Composite(ret, SWT.BORDER);
    types.setLayout(new FillLayout());
    types.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    bAll = new Button(types, SWT.RADIO);
    bPersons = new Button(types, SWT.RADIO);
    bOrgs = new Button(types, SWT.RADIO);
    bAll.setText("Alle");
    bPersons.setText("Personen");
    bOrgs.setText("Organisationen");
    bAll.addSelectionListener(fba);
    bPersons.addSelectionListener(fba);
    bOrgs.addSelectionListener(fba);
    initContactTypeSelection();

    cv.create(vc, ret, SWT.NONE, "1");
    GridData gd = SWTHelper.getFillGridData(1, true, 1, true);
    gd.heightHint = 100;
    cv.getViewerWidget().getControl().setLayoutData(gd);
    setTitle(title);
    setMessage(message);
    vc.getContentProvider().startListening();
    cv.addDoubleClickListener(this);
    // cv.getViewerWidget().addFilter(filter);
    kl.addQueryFilter(fp);

    if (showBezugsKontakt) {
        cv.getViewerWidget().addSelectionChangedListener(new ISelectionChangedListener() {
            public void selectionChanged(SelectionChangedEvent event) {
                if (isSelecting) {
                    return;
                }

                if (bezugsKontaktViewer != null) {
                    IStructuredSelection sel = (IStructuredSelection) bezugsKontaktViewer.getSelection();
                    if (sel.size() > 0) {
                        isSelecting = true;
                        bezugsKontaktViewer.setSelection(new StructuredSelection(), false);
                        isSelecting = false;
                    }
                }
            }
        });
    }
    return ret;
}

From source file:ch.elexis.core.ui.dialogs.KontaktSelektor.java

License:Open Source License

private Object getBezugsKontaktSelection() {
    Object bezugsKontakt = null;/*  w  w w  . j a  v a 2 s  . c  o  m*/

    if (bezugsKontaktViewer != null) {
        IStructuredSelection sel = (IStructuredSelection) bezugsKontaktViewer.getSelection();
        if (sel.size() > 0) {
            bezugsKontakt = sel.getFirstElement();
        }
    }

    return bezugsKontakt;
}