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

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

Introduction

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

Prototype

@Override
    public Object getFirstElement() 

Source Link

Usage

From source file:com.mentor.nucleus.bp.core.ui.Selection.java

License:Open Source License

/**
 * Returns the domain associated with the first model element 
 * in the given selection, which is assumed to be non-empty.
 *//*from  ww  w  .ja  v a  2s  . c o  m*/
public static Ooaofooa getModelRoot(StructuredSelection selection) {
    NonRootModelElement element = (NonRootModelElement) selection.getFirstElement();
    return (Ooaofooa) (element.getModelRoot());
}

From source file:com.mentor.nucleus.bp.internal.tools.process.ChangeSetCreator.java

License:Open Source License

public void run() {
    ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
            .getSelection();//from ww  w .  ja  va 2  s  .  c  om
    if (selection instanceof StructuredSelection) {
        StructuredSelection structuredSelection = (StructuredSelection) selection;
        if (structuredSelection.size() == 1) {
            if (structuredSelection.getFirstElement() instanceof IFile) {
                IFile implementationNote = (IFile) structuredSelection.getFirstElement();
                if (implementationNote.getFileExtension().equals("int")) {
                    InputStream inStream;
                    try {
                        inStream = implementationNote.getContents();
                        long byteCount = implementationNote.getRawLocation().toFile().length();
                        byte[] byteArray = new byte[(int) byteCount];
                        inStream.read(byteArray);
                        IDocument document = new Document(new String(byteArray));
                        FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(document);
                        IRegion startRegion = frda.find(0, "8. Code Changes", true, true, true, false);
                        int startOffset = startRegion.getOffset() + 17;
                        IRegion endRegion = frda.find(startOffset, "End", true, true, true, false);
                        int endOffset = endRegion.getOffset();
                        String fileList = document.get(startOffset, endOffset - startOffset);
                        fileList = fileList.replaceAll("---------------\r\n", "");
                        fileList = fileList.replaceAll("Branch name:.*\r\n", "");
                        fileList = fileList.replaceAll("\r\n    ", "");
                        fileList = fileList.replaceAll("\r\n", ",");
                        fileList = fileList.replaceAll(">", "");
                        String[] filesString = fileList.split(",");
                        if (filesString.length == 0)
                            return;
                        ArrayList<IFile> list = new ArrayList<IFile>();
                        for (int i = 0; i < filesString.length; i++) {
                            if (!filesString[i].equals("")) {
                                IResource resource = ResourcesPlugin.getWorkspace().getRoot()
                                        .findMember(filesString[i]);
                                if (resource instanceof IFile) {
                                    list.add((IFile) resource);
                                }
                            }
                        }
                        if (list.size() == 0)
                            return;
                        IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
                        String workingSetName = "chgset-" + implementationNote.getName().replaceAll(".int", "");
                        IWorkingSet set = workingSetManager.createWorkingSet(workingSetName,
                                list.toArray(new IFile[list.size()]));
                        IWorkingSet existingSet = workingSetManager.getWorkingSet(workingSetName);
                        if (existingSet != null) {
                            workingSetManager.removeWorkingSet(existingSet);
                        }
                        workingSetManager.addWorkingSet(set);
                        inStream.close();
                    } catch (CoreException e) {
                    } catch (IOException e) {
                    } catch (BadLocationException e) {
                    }
                }
            }
        }
    }
}

From source file:com.mentor.nucleus.bp.io.mdl.wizards.ModelImportPage.java

License:Open Source License

/**
 * For the first version of this class the selection is valid
 * if it is not empty and is a SystemModel_c instance.
 * /*  w  ww  .j av  a  2s . c o m*/
 *  (non-Javadoc)
 * @see org.eclipse.ui.dialogs.WizardDataTransferPage#validateDestinationGroup()
 */
@Override
protected boolean validateDestinationGroup() {
    if (selection.getStructuredSelection().isEmpty()) {
        return noImportDestination();
    }
    if (m_treeviewer.getCheckedElements() == null || m_treeviewer.getCheckedElements().length == 0) {
        return noImportDestination();
    }
    StructuredSelection ss = (StructuredSelection) m_treeviewer.getSelection();
    if (!(ss.getFirstElement() instanceof SystemModel_c)) {
        setMessage("The selected destination is not valid for the given source model file.", DialogPage.ERROR);
        return false;
    }
    setMessage("Click Finish to import the selected model.", DialogPage.INFORMATION);
    return true;
}

From source file:com.mercatis.lighthouse3.ui.operations.ui.editors.pages.InstalledOperationsEditorPage.java

License:Apache License

private void createRightSection(ScrolledForm form, FormToolkit toolkit) {
    Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
    section.clientVerticalSpacing = 5;//from w  w  w.ja  v  a  2  s . c o  m
    section.marginHeight = 3;
    section.marginWidth = 3;
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    section.setLayoutData(gd);
    section.setText("Automated Jobs");

    Composite client = toolkit.createComposite(section);
    toolkit.paintBordersFor(client);
    client.setLayout(new GridLayout(1, false));

    jobTable = new TableViewer(client);
    jobTable.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    jobTable.setContentProvider(new JobTableContentProvider());
    jobTable.setLabelProvider(new JobTableLabelProvider());
    jobTable.getControl().addFocusListener(new FocusListener() {

        public void focusLost(FocusEvent e) {
        }

        public void focusGained(FocusEvent e) {
            ((AbstractExtendableFormEditor) getEditor()).setSelectionProvider(jobTable);
        }
    });
    jobTable.addDoubleClickListener(new IDoubleClickListener() {

        public void doubleClick(DoubleClickEvent event) {
            StructuredSelection selection = (StructuredSelection) event.getSelection();
            if (selection.getFirstElement() instanceof Job) {
                GenericEditorInput<Job> input = new GenericEditorInput<Job>(lighthouseDomain,
                        (Job) selection.getFirstElement());
                IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                try {
                    IDE.openEditor(page, input, JobEditor.ID);
                } catch (PartInitException ex) {
                    UIBase.getDefault().getLog()
                            .log(new Status(IStatus.ERROR, UIBase.PLUGIN_ID, ex.getMessage(), ex));
                }
            }
        }
    });
    fillJobTable();
    createJobContextMenu();
    section.setClient(client);
}

From source file:com.mindquarry.desktop.client.widget.task.DoubleClickListener.java

License:Open Source License

public void doubleClick(DoubleClickEvent event) {
    Profile prof = Profile.getSelectedProfile(client.getPreferenceStore());

    ISelection selection = event.getSelection();

    if (selection instanceof StructuredSelection) {
        StructuredSelection structsel = (StructuredSelection) selection;
        Object element = structsel.getFirstElement();

        if (element instanceof Task) {
            Task task = (Task) element;/*from w ww  .  j av a 2s . co  m*/

            try {
                // use a clone of the task so cancel works:
                TaskSettingsDialog dlg = new TaskSettingsDialog(client.getShell(), task.clone(), false);

                if (dlg.open() == Window.OK) {
                    Task newTask = dlg.getChangedTask();
                    HttpUtilities.putAsXML(prof.getLogin(), prof.getPassword(), newTask.getId(),
                            newTask.getContentAsXML().asXML().getBytes("utf-8"));
                    updateTask(viewer, task, newTask);
                }
            } catch (Exception e) {
                MessageDialog.openError(new Shell(SWT.ON_TOP), I18N.getString("Network error"), //$NON-NLS-1$
                        I18N.getString("Could not update the task")//$NON-NLS-1$
                                + ": " + e.toString());
                log.error("Could not update task with id " //$NON-NLS-1$
                        + task.getId(), e);
            }
        }
        if (viewer != null) {
            // avoid crash if last bug got closed but the task dialog
            // was still open and then OK was pressed:
            // (TODO: task is submitted but not visible until manual
            // refresh)
            viewer.refresh();
        }
    }
}

From source file:com.mindquarry.desktop.client.widget.workspace.WorkspaceBrowserWidget.java

License:Open Source License

public void onEvent(com.mindquarry.desktop.event.Event event) {
    if (event instanceof OpenSelectedFileEvent && viewer != null) {
        ISelection selection = viewer.getSelection();
        if (selection instanceof StructuredSelection) {
            StructuredSelection structsel = (StructuredSelection) selection;
            Object element = structsel.getFirstElement();
            if (element instanceof File) {
                if (!selection.isEmpty()) {
                    File file = (File) element;
                    // TODO: open remotely added files from repository
                    if (file.exists()) {
                        log.debug("Launching " + file.getAbsolutePath());
                        // TODO: doesn't work on Linux
                        Program.launch(file.getAbsolutePath());
                    } else {
                        log.warn("onEvent: cannot open remote files: " + file);
                    }/* w  w  w.j  a  v  a2 s  .c o m*/
                }
            } else {
                log.warn("onEvent: unexpected type: " + element.getClass());
            }
        }
    }
}

From source file:com.mindquarry.desktop.client.widget.workspace.WorkspaceUpdateContainerRunnable.java

License:Open Source License

/**
 * Return true if the user selected an item which can be opened.
 */// w  w w .  j  a  v a 2 s  .co  m
private boolean enableOpenButton() {
    ISelection iSelection = containerWidget.getViewer().getSelection();
    if (iSelection instanceof StructuredSelection) {
        StructuredSelection structsel = (StructuredSelection) iSelection;
        Object element = structsel.getFirstElement();
        if (element instanceof File) {
            File file = (File) element;
            boolean enableButton = false;
            if (containerWidget.getViewer().getSelection().isEmpty()) {
                enableButton = false;
            } else {
                if (file.exists() && (SVNFileUtil.isWindows || SVNFileUtil.isOSX)) {
                    // on windows and Mac we can open both files and
                    // directories with Program.launch()
                    enableButton = true;
                } else if (file.exists() && file.isFile()) {
                    // TODO: we cannot open directories on Unix yet,
                    // it depends on the desktop (gnome-open, kfmclient)
                    enableButton = true;
                } else {
                    // a remotely added file, we cannot view that yet:
                    enableButton = false;
                }
            }
            return enableButton;
        }
    }
    return false;
}

From source file:com.nextep.designer.sqlgen.ui.editors.sql.SQLContentOutlinePage.java

License:Open Source License

/**
 * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
 *///from   w w w.j av a2 s  . c om
@Override
public void createControl(Composite parent) {
    outlineTree = new Tree(parent, SWT.NONE);
    selProvider = TreeSelectionProvider.handle(outlineTree, false);
    addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            StructuredSelection sel = (StructuredSelection) selProvider.getSelection();
            if (!sel.isEmpty() && packageConnector.getModel() instanceof IPackage) {
                IParseable pkg = (IParseable) packageConnector.getModel();
                setInternalSelection(pkg, sel.getFirstElement());
            }
        }
    });
    Object model = ((ISQLEditorInput) editor.getEditorInput()).getModel();
    if (model != null) {
        packageConnector = UIControllerFactory.getController(model).initializeNavigator(model);
        packageConnector.setTree(outlineTree);
        packageConnector.create(null, -1);
        //        if(packageConnector instanceof PackageNavigator) {
        //           ((PackageNavigator)packageConnector).dedicatedChildInit();
        //        }
        packageConnector.initialize();
        packageConnector.refreshConnector();
        packageConnector.getSWTConnector().setExpanded(true);
    }
}

From source file:com.nextep.designer.sqlgen.ui.editors.sql.SQLContentOutlinePage.java

License:Open Source License

/**
 * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
 */// w  w  w. j av  a  2 s  . c  o  m
@Override
public ISelection getSelection() {
    StructuredSelection sel = (StructuredSelection) selProvider.getSelection();
    if (!sel.isEmpty() && packageConnector.getModel() instanceof IPackage) {
        IParseable pkg = (IParseable) packageConnector.getModel();
        setInternalSelection(pkg, sel.getFirstElement());
    }
    return sel;
}

From source file:com.nokia.carbide.cpp.internal.api.sdk.ui.SBSv2PlatformFilterComposite.java

License:Open Source License

public void createControls() {
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;//from   w ww  .j  ava2  s  .com
    setLayout(gridLayout);

    GridData gd = new GridData(SWT.LEFT, SWT.LEFT, true, false);
    gd.widthHint = 200;
    gd.heightHint = 350;

    Label aliasBoxLabel = new Label(this, SWT.NONE);
    aliasBoxLabel.setText(Messages.getString("SBSv2PlatformFilterComposite.PlatformsGroupText"));
    aliasBoxLabel.setToolTipText(Messages.getString("SBSv2PlatformFilterComposite.PlatformsGroupToolTip"));

    Label variantBoxLabel = new Label(this, SWT.NONE);
    variantBoxLabel.setText(Messages.getString("SBSv2PlatformFilterComposite.ProductsGroupText"));
    variantBoxLabel.setToolTipText(Messages.getString("SBSv2PlatformFilterComposite.ProductsGroupToolTip"));

    buildAliasTableViewer = CheckboxTableViewer.newCheckList(this, SWT.BORDER);
    buildAliasTableViewer.getTable().setLayoutData(gd);
    buildAliasTableViewer.setContentProvider(new ArrayContentProvider());
    buildAliasTableViewer.setLabelProvider(new LabelProvider());

    customVariantListViewer = new ListViewer(this);
    customVariantListViewer.getList().setLayoutData(gd);
    customVariantListViewer.setContentProvider(new ArrayContentProvider());
    customVariantListViewer.setLabelProvider(new LabelProvider());
    customVariantListViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            if (customVariantListViewer.getSelection() != null
                    && !customVariantListViewer.getSelection().isEmpty()) {
                removeVariantButton.setEnabled(true);
            } else {
                removeVariantButton.setEnabled(false);
            }
        }
    });

    refreshButton = new Button(this, SWT.NONE);
    refreshButton.setText(Messages.getString("SBSv2PlatformFilterComposite.RefreshButtonText")); //$NON-NLS-1$
    refreshButton.setToolTipText(Messages.getString("SBSv2PlatformFilterComposite.RefreshButtonToolTip")); //$NON-NLS-1$

    refreshButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
        }

        public void widgetSelected(SelectionEvent e) {
            refreshButton.setEnabled(false);
            refreshButton.setText(Messages.getString("SBSv2PlatformFilterComposite.RefreshButtonScanningText")); //$NON-NLS-1$

            //            for (ISymbianSDK sdk : SDKCorePlugin.getSDKManager().getSDKList()){
            //               ((SBSv2BuildInfo)sdk.getBuildInfo(ISymbianBuilderID.SBSV2_BUILDER)).clearDataFromBuildCache();
            //            }
            SBSv2QueryUtils.removeAllCachedQueries();

            refreshLocalSBSCacheData();

            SBSv2QueryUtils.flushAllSBSv2Caches();
            refreshButton.setText(Messages.getString("SBSv2PlatformFilterComposite.RefreshButtonText")); //$NON-NLS-1$
            refreshButton.setEnabled(true);
        }

    });

    Composite variantButtonsComposite = new Composite(this, SWT.NONE);
    gridLayout = new GridLayout();
    gridLayout.makeColumnsEqualWidth = true;
    gridLayout.numColumns = 2;
    variantButtonsComposite.setLayout(gridLayout);
    GridData gridData = new GridData(SWT.LEFT, SWT.TOP, true, false);
    variantButtonsComposite.setLayoutData(gridData);

    addVariantButton = new Button(variantButtonsComposite, SWT.NONE);
    addVariantButton.setText(Messages.getString("SBSv2PlatformFilterComposite.AddProductButtonText")); //$NON-NLS-1$
    addVariantButton.setToolTipText(Messages.getString("SBSv2PlatformFilterComposite.AddProductButtonToolTip")); //$NON-NLS-1$
    addVariantButton.setLayoutData(gridData);
    addVariantButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
        }

        public void widgetSelected(SelectionEvent e) {
            if (aliasMap.size() == 0) {
                MessageDialog.openError(getShell(), "No build configurations found.",
                        "No build configurations (aliases) were found from any SDKs. Attempted 'sbs --query=aliases' but found no results.");
            } else if (productVariantList.size() == 0) {
                MessageDialog.openError(getShell(), "No products found.",
                        "No products were found from any SDKs. Attempted 'sbs --query=products' but found no results.");
            } else {
                String selectedAlias = "";
                ISelection selectedItem = buildAliasTableViewer.getSelection();

                StructuredSelection selection = (StructuredSelection) selectedItem;
                String stringSelection = (String) selection.getFirstElement();
                if (stringSelection != null) {
                    TableItem[] tableItems = buildAliasTableViewer.getTable().getItems();
                    for (TableItem item : tableItems) {
                        if (stringSelection.equals(item.getText())) {
                            selectedAlias = item.getText();
                            break;
                        }
                    }
                }
                AddSBSv2ProductVariant addVariantDlg = new AddSBSv2ProductVariant(getShell(), selectedAlias,
                        aliasMap, productVariantList);
                if (addVariantDlg.open() == TrayDialog.OK) {
                    if (customVariantListViewer.testFindItem(addVariantDlg.getUserCreatedVariant()) == null) {
                        // doesn't exist, add it. if it does exist just ignore it
                        List<String> variantList = (List<String>) customVariantListViewer.getInput();
                        variantList.add(addVariantDlg.getUserCreatedVariant());
                        customVariantListViewer.setInput(variantList);
                        customVariantListViewer.refresh();
                    }
                }
            }
        }

    });

    removeVariantButton = new Button(variantButtonsComposite, SWT.NONE);
    removeVariantButton.setText(Messages.getString("SBSv2PlatformFilterComposite.RemoveProductButtonText")); //$NON-NLS-1$
    removeVariantButton
            .setToolTipText(Messages.getString("SBSv2PlatformFilterComposite.RemoveProductButtonToolTip")); //$NON-NLS-1$
    removeVariantButton.setLayoutData(gridData);
    removeVariantButton.setEnabled(false);
    removeVariantButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
        }

        public void widgetSelected(SelectionEvent e) {
            ISelection selectedVariant = customVariantListViewer.getSelection();
            if (selectedVariant != null) {
                StructuredSelection selection = (StructuredSelection) selectedVariant;
                String stringSelection = (String) selection.getFirstElement();
                List<String> data = (List<String>) customVariantListViewer.getInput();
                data.remove(stringSelection);
                customVariantListViewer.setInput(data);
                customVariantListViewer.refresh(true);
                removeVariantButton.setEnabled(false);
            }
        }

    });

    initTable();
    addListeners();
}