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

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

Introduction

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

Prototype

public Object getFirstElement();

Source Link

Document

Returns the first element in this selection, or null if the selection is empty.

Usage

From source file:ar.com.tadp.xml.rinzo.jdt.preferences.TableViewerSupport.java

License:Open Source License

private void initComponents(Composite parent) {
    this.control = new Composite(parent, SWT.NONE);
    this.control.setLayout(new GridLayout(2, false));

    viewer = new TableViewer(control,
            SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Table table = viewer.getTable();//  www .  j ava 2s. c  o  m
    table.setLayoutData(new GridData(GridData.FILL_BOTH));
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    table.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent evt) {
            TableItem[] items = viewer.getTable().getSelection();
            boolean enable = false;
            if (items.length > 0) {
                String path = items[0].getText(1);
                if (!path.equals("[Default]")) {
                    enable = true;
                }
            }
            buttonEdit.setEnabled(enable);
            buttonRemove.setEnabled(enable);
        }
    });

    initTableViewer(viewer);

    // create buttons
    Composite buttons = new Composite(control, SWT.NONE);
    buttons.setLayoutData(new GridData(GridData.FILL_VERTICAL));
    GridLayout layout = new GridLayout();
    layout.marginBottom = 0;
    layout.marginHeight = 0;
    layout.marginLeft = 0;
    layout.marginRight = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    buttons.setLayout(layout);
    buttonAdd = new Button(buttons, SWT.PUSH);
    buttonAdd.setText("Add");
    buttonAdd.setLayoutData(createButtonGridData());
    buttonAdd.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent evt) {
            T obj = doAdd();
            if (obj != null) {
                model.add(obj);
                viewer.refresh();
            }
        }
    });
    buttonEdit = new Button(buttons, SWT.PUSH);
    buttonEdit.setText("Edit");
    buttonEdit.setLayoutData(createButtonGridData());
    buttonEdit.setEnabled(false);
    buttonEdit.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent evt) {
            IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
            @SuppressWarnings("unchecked")
            T obj = (T) sel.getFirstElement();
            doEdit(obj);
            viewer.refresh();
        }
    });
    buttonRemove = new Button(buttons, SWT.PUSH);
    buttonRemove.setText("Remove");
    buttonRemove.setLayoutData(createButtonGridData());
    buttonRemove.setEnabled(false);
    buttonRemove.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent evt) {
            IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
            @SuppressWarnings("unchecked")
            List<T> list = (List<T>) sel.toList();
            doRemove(list);
            viewer.refresh();
        }
    });

    viewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
            if (sel == null || sel.getFirstElement() == null) {
                buttonEdit.setEnabled(false);
                buttonRemove.setEnabled(false);
            } else {
                buttonEdit.setEnabled(true);
                buttonRemove.setEnabled(true);
            }
        }
    });

    viewer.setContentProvider(new ListContentProvider());
    viewer.setLabelProvider(createLabelProvider());
    viewer.setInput(model);
}

From source file:ariba.ideplugin.eclipse.wizards.AWProjectDetailsPage.java

License:Apache License

private void initialize() {
    if (_selection != null && _selection.isEmpty() == false && _selection instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) _selection;
        if (ssel.size() > 1)
            return;
        Object obj = ssel.getFirstElement();
        if (obj instanceof IResource) {
            IContainer container;//from  w w  w  .  j a va 2s . co  m
            if (obj instanceof IContainer)
                container = (IContainer) obj;
            else
                container = ((IResource) obj).getParent();
        }
    }
}

From source file:ariba.ideplugin.eclipse.wizards.AWProjectTemplatePage.java

License:Apache License

/**
 * Tests if the current workbench selection is a suitable container to use.
 *///from w  w  w.  j  a v  a 2 s .com

private void initialize() {
    if (_selection != null && _selection.isEmpty() == false && _selection instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) _selection;
        if (ssel.size() > 1)
            return;
        Object obj = ssel.getFirstElement();
        if (obj instanceof IResource) {
            IContainer container;
            if (obj instanceof IContainer)
                container = (IContainer) obj;
            else
                container = ((IResource) obj).getParent();
        }
    }

}

From source file:at.bestsolution.code.swt.resources.WorkspaceViewer.java

License:Open Source License

@PostConstruct
void init(Composite parent) {
    viewer = new TreeViewer(parent);
    viewer.setLabelProvider(new ColumnLabelProvider() {
        @Override//w w  w. j  a  v  a2 s . co m
        public String getText(Object element) {
            IResource r = (IResource) element;
            return r.getName();
        }

        @Override
        public Image getImage(Object element) {
            if (element instanceof IProject) {
            }
            // TODO Auto-generated method stub
            return super.getImage(element);
        }
    });
    viewer.setContentProvider(new ContentProviderImpl());
    viewer.setInput(ResourcesPlugin.getWorkspace().getRoot());
    viewer.addOpenListener(s -> {
        IStructuredSelection ss = (IStructuredSelection) s.getSelection();
        @SuppressWarnings("unchecked")
        List<IResource> list = ss.toList();
        list.stream().filter(r -> r instanceof IFile).map(r -> ((IFile) r).getFullPath().toOSString())
                .forEach(editorOpener::openEditor);
    });
    viewer.addSelectionChangedListener(e -> {
        IStructuredSelection s = (IStructuredSelection) e.getSelection();
        primarySelection.publish((IResource) s.getFirstElement());
    });

    ResourcesPlugin.getWorkspace().addResourceChangeListener(this::handleResourceChanged);
}

From source file:at.bestsolution.efxclipse.robovm.RobovmSetupHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveMenuSelection(event);
    ICompilationUnit unit = (ICompilationUnit) selection.getFirstElement();

    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    Set<IPath> sourcePaths = new HashSet<>();
    Set<IPath> refProjectSourcePaths = new HashSet<>();
    Set<IPath> libsPaths = new HashSet<>();

    resolveDataProject(unit.getJavaProject(), sourcePaths, refProjectSourcePaths, libsPaths);

    BuildConfiguration config = new BuildConfiguration();
    config.buildDirectory = "robovm-build";

    {/*from  ww  w.  j a  v a 2s.c o m*/
        Set<String> set = new HashSet<String>();
        Set<File> set2 = new HashSet<File>();
        for (IPath p : libsPaths) {
            set.add(p.lastSegment());
            IFile file = root.getFile(p);
            if (file != null && file.exists()) {
                p = file.getLocation();
            }
            set2.add(p.toFile());
        }
        config.externalLibs = set;
        config.origExternalLibs = set2;
    }

    {
        Set<String> set = new HashSet<String>();
        Set<SetupDirectory> set2 = new HashSet<SetupDirectory>();
        for (IPath p : sourcePaths) {
            IFolder t = root.getFolder(p);
            set.add(t.getProjectRelativePath().toString());

            if (t.isLinked()) {
                set2.add(new SetupDirectory(t.getLocation().toFile().getParentFile(),
                        new File(t.getProjectRelativePath().toString())));
            } else {
                set2.add(new SetupDirectory(t.getProject().getLocation().toFile(),
                        new File(t.getProjectRelativePath().toString())));
            }
        }
        config.projectSourceDirs = set;
        config.origProjectSourceDirs = set2;
    }

    {
        Set<String> set = new HashSet<String>();
        Set<SetupDirectory> set2 = new HashSet<SetupDirectory>();
        for (IPath p : refProjectSourcePaths) {
            IFolder t = root.getFolder(p);
            set.add(t.getProject().getName() + "/" + t.getProjectRelativePath());
            set2.add(new SetupDirectory(t.getProject().getLocation().toFile().getParentFile(),
                    new File(t.getProject().getName() + "/" + t.getProjectRelativePath().toString())));

        }
        config.projectRefSourceDirs = set;
        config.origProjectRefSourceDirs = set2;
    }

    IProject p = unit.getJavaProject().getProject();
    config.projectName = getApplicationName(p);

    try {
        config.projectEncoding = p.getDefaultCharset();
        config.sourceCompliance = "1.7";//unit.getJavaProject().getOption( JavaCore.COMPILER_SOURCE, true );
        config.targetCompliance = "1.7";//unit.getJavaProject().getOption( JavaCore.COMPILER_COMPLIANCE, true );
    } catch (CoreException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    IFolder buildFolder = p.getFolder(new Path(config.buildDirectory));
    if (!buildFolder.exists()) {
        try {
            buildFolder.create(true, true, null);
        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    IFolder templateFolder = buildFolder.getFolder(new Path("robovm-template"));
    if (!templateFolder.exists()) {
        try {
            templateFolder.create(true, true, null);
        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    if (!buildFolder.getFolder(new Path("robovm")).exists()) {
        try {
            File dir = buildFolder.getLocation().toFile();
            export("binaries/robovm-" + ROBOVM_VERSION + ".tar.gz", dir.getAbsolutePath(),
                    "robovm-" + ROBOVM_VERSION + ".tar.gz");
            Process exec = Runtime.getRuntime().exec(
                    new String[] { "tar", "xzvf", "robovm-" + ROBOVM_VERSION + ".tar.gz" }, new String[0], dir);
            exec.waitFor();
            Files.move(new File(dir, "robovm-" + ROBOVM_VERSION).toPath(), new File(dir, "robovm").toPath(),
                    StandardCopyOption.ATOMIC_MOVE);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    if (!buildFolder.getFolder(new Path("jfx78")).exists()) {
        try {
            File dir = new File(buildFolder.getLocation().toFile(), "jfx78");
            export("binaries/jfxrt.jar", dir.getAbsolutePath(), "jfxrt.jar");
            export("binaries/libdecora_sse_armv7.a", dir.getAbsolutePath(), "libdecora_sse_armv7.a");
            export("binaries/libglass.a", dir.getAbsolutePath(), "libglass.a");
            export("binaries/libjavafx_font.a", dir.getAbsolutePath(), "libjavafx_font.a");
            export("binaries/libjavafx_iio.a", dir.getAbsolutePath(), "libjavafx_iio.a");
            export("binaries/libprism_common.a", dir.getAbsolutePath(), "libprism_common.a");
            export("binaries/libprism_es2.a", dir.getAbsolutePath(), "libprism_es2.a");
            export("binaries/openjfx-78-backport-compat-" + OPENJFX_BACKPORT + ".jar", dir.getAbsolutePath(),
                    "openjfx-78-backport-compat.jar");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    try {
        File dir = buildFolder.getLocation().toFile();
        export("binaries/robovm-run.sh", dir.getAbsolutePath(), "robovm-run.sh");
        export("binaries/org.eclipse.fx.fxml.compiler_0.9.0-SNAPSHOT.jar", dir.getAbsolutePath(),
                "org.eclipse.fx.fxml.compiler.jar");
        new File(dir, "robovm-run.sh").setExecutable(true);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    RobovmBuild b = new RobovmBuild();

    {
        IFile f = buildFolder.getFile("robovm.properties");
        fillFile(b.generateBuildProperties(config.projectName), f);
    }

    {
        IFile f = buildFolder.getFile("robovm.xml");
        fillFile(b.generateConfigXML(config), f);
    }

    {
        IFile f = buildFolder.getFile("Info.plist.xml");
        fillFile(b.generatePlistContent(), f);
    }

    {
        IFile f = templateFolder.getFile(config.projectName + ".java");
        fillFile(b.generateBootstrapFile(config.projectName), f);
    }

    {
        IFile f = templateFolder.getFile(config.projectName + "Main.java");
        try {
            fillFile(b.generateFXMainFile(config.projectName, unit.getTypes()[0].getFullyQualifiedName()), f);
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    {
        IFile f = buildFolder.getFile("robovm-ant.xml");
        fillFile(b.generate(config), f);
    }

    //      System.err.println(sourcePaths);
    //      System.err.println(refProjectSourcePaths);
    //      System.err.println(libsPaths);

    // TODO Auto-generated method stub
    return null;
}

From source file:at.bestsolution.efxclipse.tooling.ui.wizards.AbstractNewJDTElementWizard.java

License:Open Source License

protected IJavaElement getInitialJavaElement(IStructuredSelection selection) {
    IJavaElement jelem = null;// www  .j av a2s  .c  o m
    if (selection != null && !selection.isEmpty()) {
        Object selectedElement = selection.getFirstElement();
        if (selectedElement instanceof IAdaptable) {
            IAdaptable adaptable = (IAdaptable) selectedElement;

            jelem = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
            if (jelem == null || !jelem.exists()) {
                jelem = null;
                IResource resource = (IResource) adaptable.getAdapter(IResource.class);
                if (resource != null && resource.getType() != IResource.ROOT) {
                    while (jelem == null && resource.getType() != IResource.PROJECT) {
                        resource = resource.getParent();
                        jelem = (IJavaElement) resource.getAdapter(IJavaElement.class);
                    }
                    if (jelem == null) {
                        jelem = JavaCore.create(resource); // java project
                    }
                }
            }
        }
    }

    return jelem;
}

From source file:at.bestsolution.translate.view.TranslatorComponent.java

License:Open Source License

private void createUI(Composite parent) {
    GridLayout layout = new GridLayout(2, false);
    parent.setLayout(layout);/*www.  ja  v a 2s .  co  m*/

    Label l = new Label(parent, SWT.NONE);
    l.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false, 2, 1));
    l.setText("Translator");

    l = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
    l.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false, 2, 1));

    l = new Label(parent, SWT.NONE);
    l.setText("Term");

    term = new Text(parent, SWT.BORDER);
    term.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    l = new Label(parent, SWT.NONE);
    l.setText("Translator");

    translator = new ComboViewer(parent);
    translator.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    translator.setContentProvider(new ObservableListContentProvider());
    translator.setLabelProvider(new LabelProvider() {
        @Override
        public String getText(Object element) {
            return ((ITranslator) element).getName();
        }
    });

    l = new Label(parent, SWT.NONE);
    l.setText("Source-Language");

    final ComboViewer sourceLanguage = new ComboViewer(parent);
    sourceLanguage.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    sourceLanguage.setContentProvider(new ObservableListContentProvider());
    sourceLanguage.setLabelProvider(new LabelProvider() {
        @Override
        public String getText(Object element) {
            return ((TranslationLanguage) element).name;
        }
    });

    IListProperty fromProp = PojoProperties.list("languages");
    IValueProperty selectionProp = ViewerProperties.singleSelection();

    IObservableList input = fromProp.observeDetail(selectionProp.observe(translator));
    sourceLanguage.setInput(input);

    l = new Label(parent, SWT.NONE);
    l.setText("Target-Language");

    final ComboViewer targetLanguage = new ComboViewer(parent);
    targetLanguage.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    targetLanguage.setContentProvider(new ObservableListContentProvider());
    targetLanguage.setLabelProvider(new LabelProvider());

    IListProperty targetsProp = PojoProperties.list("targets");

    input = targetsProp.observeDetail(selectionProp.observe(sourceLanguage));
    targetLanguage.setInput(input);

    Button b = new Button(parent, SWT.PUSH);
    b.setText("Translate");
    b.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false, 2, 1));

    l = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
    l.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));

    l = new Label(parent, SWT.NONE);
    l.setText("Translation");

    final Label translation = new Label(parent, SWT.NONE);
    translation.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    b.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection transSelection = (IStructuredSelection) translator.getSelection();
            IStructuredSelection sourceSelection = (IStructuredSelection) sourceLanguage.getSelection();
            IStructuredSelection targetSelection = (IStructuredSelection) targetLanguage.getSelection();

            if (!transSelection.isEmpty() && !sourceSelection.isEmpty()) {
                try {
                    String trans = ((ITranslator) transSelection.getFirstElement()).translate(
                            ((TranslationLanguage) sourceSelection.getFirstElement()).name,
                            (String) targetSelection.getFirstElement(), term.getText());
                    translation.setText(trans);
                } catch (InvocationTargetException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    });
}

From source file:at.bitandart.zoubek.mervin.review.ReviewOptionsView.java

License:Open Source License

/**
 * creates the compare section/*  w ww .  j a va2  s.c o m*/
 * 
 * @param toolkit
 */
private void createCompareSection(FormToolkit toolkit) {

    Section compareSection = toolkit.createSection(mainPanel, Section.TITLE_BAR);
    compareSection.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    compareSection.setText("Compare Patch Sets");

    comparePanel = toolkit.createComposite(compareSection);
    comparePanel.setLayout(new GridLayout(2, true));
    compareSection.setClient(comparePanel);

    CCombo leftSidePatchSetCombo = new CCombo(comparePanel, SWT.DROP_DOWN | SWT.READ_ONLY);
    leftSidePatchSetCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    toolkit.adapt(leftSidePatchSetCombo);
    leftSidePatchSetViewer = new ComboViewer(leftSidePatchSetCombo);
    leftSidePatchSetViewer.setContentProvider(ArrayContentProvider.getInstance());
    leftSidePatchSetViewer.setLabelProvider(new PatchSetLabelProvider());
    leftSidePatchSetViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {

            ISelection selection = event.getSelection();

            if (!selection.isEmpty() && selection instanceof IStructuredSelection) {

                IStructuredSelection structuredSelection = (IStructuredSelection) selection;
                Object element = structuredSelection.getFirstElement();
                ModelReview currentModelReview = getCurrentModelReview();

                if (COMPARE_BASE.equals(element)) {
                    // null represents the base version
                    currentModelReview.setLeftPatchSet(null);
                    udpateSelectedComparison();

                } else if (element instanceof PatchSet) {
                    currentModelReview.setLeftPatchSet((PatchSet) element);
                    udpateSelectedComparison();
                }
            }
        }
    });

    CCombo rightSidePatchSetCombo = new CCombo(comparePanel, SWT.DROP_DOWN | SWT.READ_ONLY);
    rightSidePatchSetCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    toolkit.adapt(rightSidePatchSetCombo);
    rightSidePatchSetViewer = new ComboViewer(rightSidePatchSetCombo);
    rightSidePatchSetViewer.setContentProvider(ArrayContentProvider.getInstance());
    rightSidePatchSetViewer.setLabelProvider(new PatchSetLabelProvider());
    rightSidePatchSetViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {

            ISelection selection = event.getSelection();

            if (!selection.isEmpty() && selection instanceof IStructuredSelection) {

                IStructuredSelection structuredSelection = (IStructuredSelection) selection;
                Object element = structuredSelection.getFirstElement();
                ModelReview currentModelReview = getCurrentModelReview();

                if (COMPARE_BASE.equals(element)) {
                    // null represents the base version
                    currentModelReview.setRightPatchSet(null);
                    udpateSelectedComparison();

                } else if (element instanceof PatchSet) {
                    currentModelReview.setRightPatchSet((PatchSet) element);
                    udpateSelectedComparison();
                }
            }
        }
    });

}

From source file:at.bitandart.zoubek.mervin.review.wizards.ReviewSelectionPage.java

License:Open Source License

/**
 * // w w w  .j a v a 2  s . c o m
 * @return the selected review id or null if none is selected
 */
public String getReviewId() {
    IStructuredSelection structuredSelection = reviewListViewer.getStructuredSelection();
    if (structuredSelection != null && !structuredSelection.isEmpty()) {
        Object firstElement = structuredSelection.getFirstElement();
        if (firstElement instanceof IReviewDescriptor) {
            return ((IReviewDescriptor) firstElement).getId();
        }
    }
    return null;
}

From source file:at.medevit.elexis.ehc.ui.example.wizard.ExportPatientWizardPage1.java

License:Open Source License

public boolean finish() {
    IStructuredSelection contentSelection = (IStructuredSelection) contentViewer.getSelection();

    if (!contentSelection.isEmpty()) {
        Patient selectedPatient = (Patient) contentSelection.getFirstElement();
        AbstractCdaCh<?> document = ServiceComponent.getService().createCdaChDocument(selectedPatient,
                (Mandant) ElexisEventDispatcher.getSelected(Mandant.class));
        try {/*w ww.ja  v  a  2s . com*/
            String outputDir = CoreHub.userCfg.get(PreferencePage.EHC_OUTPUTDIR,
                    PreferencePage.getDefaultOutputDir());
            document.saveToFile(
                    outputDir + File.separator + selectedPatient.get(Patient.FLD_PATID) + "_patientdata.xml");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return true;
}