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:com.amalto.workbench.widgets.composites.SelectXPathComposite.java

License:Open Source License

private void initListenerForXPathTree() {

    ISelectionChangedListener listener = new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            txtXPath.setText(getXpath());
            IStructuredSelection sel = (IStructuredSelection) event.getSelection();
            boolean enable = true;
            if (xpathSelectionFilter != null) {
                enable = xpathSelectionFilter.check(sel.getFirstElement()) == FilterResult.ENABLE;
            }//from  w  ww.  j  av a2  s.  co  m
            if (propertyListener != null) {
                propertyListener
                        .propertyChange(new PropertyChangeEvent(event.getSource(), "selection", null, enable)); //$NON-NLS-1$
            }
        }
    };
    addSelectionChangedListener(listener);

}

From source file:com.amalto.workbench.widgets.RepositoryCheckTreeViewer.java

License:Open Source License

public RepositoryCheckTreeViewer(IStructuredSelection selection) {
    this.selection = selection;
    Object firstElement = selection.getFirstElement();
    if (firstElement != null && firstElement instanceof TreeObject) {
        serverRoot = ((TreeObject) firstElement).getServerRoot();
    }//from w w  w . j  av a2 s.  c  o m
    checkItems = selection.toList();
}

From source file:com.amazonaws.eclipse.codedeploy.deploy.handler.DeployProjectToCodeDeployHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {

    ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();

    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structurredSelection = (IStructuredSelection) selection;

        Object firstSeleciton = structurredSelection.getFirstElement();
        if (firstSeleciton instanceof IProject) {
            IProject selectedProject = (IProject) firstSeleciton;

            try {
                WizardDialog wizardDialog = new WizardDialog(Display.getCurrent().getActiveShell(),
                        new DeployProjectToCodeDeployWizard(selectedProject));

                wizardDialog.open();/*ww  w.j  av  a2s  . c  om*/

            } catch (Exception e) {
                CodeDeployPlugin.getDefault().reportException("Failed to launch deployment wizard.", e);
            }

        } else {
            CodeDeployPlugin.getDefault()
                    .logInfo("Invalid selection: " + firstSeleciton + " is not a project.");
        }
    }

    return null;
}

From source file:com.amazonaws.eclipse.core.ui.preferences.AwsAccountPreferencePageTab.java

License:Apache License

/**
 * Creates the account selection section.
 *///from   w  w w  .ja v  a  2s. co  m
private Composite createAccountSelector(final Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    composite.setLayout(new GridLayout(4, false));

    new Label(composite, SWT.READ_ONLY).setText("Default Profile: ");

    accountSelector = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
    accountSelector.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    // Use a List of AccountInfo objects as the data input for the combo
    // viewer
    accountSelector.setContentProvider(ArrayContentProvider.getInstance());
    accountSelector.setLabelProvider(new LabelProvider() {
        @Override
        public String getText(Object element) {
            if (element instanceof AccountInfo) {
                AccountInfo account = (AccountInfo) element;
                if (account.isDirty()) {
                    return "*" + account.getAccountName();
                } else {
                    return account.getAccountName();
                }
            }
            return super.getText(element);
        }
    });

    AccountInfo currentRegionAccount = accountInfoByIdentifier.get(currentRegionAccountId);
    // In some of the edge-cases, currentRegionAccount could be null.
    // e.g. a specific credential profile account is removed externally, but
    // the data in the preference store is not yet updated.
    if (currentRegionAccount == null) {
        currentRegionAccount = accountInfoByIdentifier.values().iterator().next();
        currentRegionAccountId = currentRegionAccount.getInternalAccountId();
    }

    final List<AccountInfo> allAccounts = new LinkedList<AccountInfo>(accountInfoByIdentifier.values());

    setUpAccountSelectorItems(allAccounts, currentRegionAccount);

    // Add selection listener to the account selector, so that all the
    // account info editors are notified of the newly selected AccountInfo
    // object.
    accountSelector.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            Object selectedObject = selection.getFirstElement();

            if (selectedObject instanceof AccountInfo) {
                AccountInfo accountInfo = (AccountInfo) selectedObject;
                accountChanged(accountInfo);
            }
        }

    });

    final Button addNewAccount = new Button(composite, SWT.PUSH);
    addNewAccount.setText("Add profile");
    deleteAccount = new Button(composite, SWT.PUSH);
    deleteAccount.setText("Remove profile");
    deleteAccount.setEnabled(allAccounts.size() > 1);

    defaultAccountExplanationLabel = new Label(composite, SWT.WRAP);
    defaultAccountExplanationLabel.setText(getDefaultAccountExplanationText());

    parentPrefPage.setItalicFont(defaultAccountExplanationLabel);

    GridData layoutData = new GridData(SWT.FILL, SWT.TOP, true, false);
    layoutData.horizontalSpan = 4;
    layoutData.widthHint = 200;
    defaultAccountExplanationLabel.setLayoutData(layoutData);

    addNewAccount.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            String newAccountId = UUID.randomUUID().toString();
            AccountInfo newAccountInfo = createNewProfileAccountInfo(newAccountId);

            String newAccountName = region == null ? "New Profile" : "New " + region.getName() + " Profile";
            newAccountInfo.setAccountName(newAccountName); // this will mark the AccountInfo object dirty

            accountInfoByIdentifier.put(newAccountId, newAccountInfo);

            setUpAccountSelectorItems(accountInfoByIdentifier.values(), newAccountInfo);

            for (AwsAccountPreferencePageTab tab : parentPrefPage.getAllAccountPreferencePageTabs()) {
                if (tab != AwsAccountPreferencePageTab.this) {
                    tab.refreshAccountSelectorItems();
                }
            }

            parentPrefPage.updatePageValidationOfAllTabs();
        }
    });

    deleteAccount.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            accountInfoToBeDeleted.add(accountInfoByIdentifier.get(currentRegionAccountId));
            accountInfoByIdentifier.remove(currentRegionAccountId);

            // If all the accounts are deleted, create a temporary
            // AccountInfo object
            if (accountInfoByIdentifier.isEmpty()) {
                String newAccountId = UUID.randomUUID().toString();
                AccountInfo newAccountInfo = createNewProfileAccountInfo(newAccountId);

                // Account name : default-region-id
                newAccountInfo.setAccountName(getRegionAccountDefaultName());
                accountInfoByIdentifier.put(newAccountId, newAccountInfo);
            }

            // Use the first AccountInfo as the next selected account
            AccountInfo nextDefaultAccount = accountInfoByIdentifier.values().iterator().next();

            setUpAccountSelectorItems(accountInfoByIdentifier.values(), nextDefaultAccount);

            for (AwsAccountPreferencePageTab tab : parentPrefPage.getAllAccountPreferencePageTabs()) {
                if (tab != AwsAccountPreferencePageTab.this) {
                    tab.refreshAccountSelectorItems();
                }
            }

            parentPrefPage.updatePageValidationOfAllTabs();
        }
    });

    accountSelector.getCombo().addModifyListener(new ModifyListener() {

        public void modifyText(ModifyEvent arg0) {
            if (accountSelector.getCombo().getItemCount() > 1) {
                deleteAccount.setEnabled(true);
            } else {
                deleteAccount.setEnabled(false);
            }

        }
    });

    return composite;
}

From source file:com.amazonaws.eclipse.elasticbeanstalk.ui.menu.DeployToElasticBeanstalkHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
    if (selection != null & selection instanceof IStructuredSelection) {
        IStructuredSelection structurredSelection = (IStructuredSelection) selection;
        new RunOnServerAction(structurredSelection.getFirstElement()).run();
    }/*ww  w  .j a va  2s.c  o m*/
    return null;
}

From source file:com.amazonaws.eclipse.explorer.DragAdapterAssistant.java

License:Apache License

@Override
public void dragStart(DragSourceEvent anEvent, IStructuredSelection aSelection) {

    anEvent.doit = aSelection.size() == 1 && aSelection.getFirstElement() instanceof S3ObjectSummary;

    /*//from  w  w  w .j a va  2  s  . c o  m
     * We need to make sure that our drag is treated *only* as a plugin
     * transfer, whereas the superclass defaults to treating all such events
     * as either LocalSelectionTransfer or PluginTransfer. In the case of
     * the former, the drag adapter for other views won't recognize the
     * object being dropped and so disallows it.
     */
    DragSource source = ((DragSource) anEvent.getSource());
    source.setTransfer(getSupportedTransferTypes());
    //printEvent(anEvent);
}

From source file:com.amazonaws.eclipse.explorer.rds.RDSExplorerActionProvider.java

License:Apache License

@Override
public void fillContextMenu(IMenuManager menu) {
    IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
    if (selection.getFirstElement() == RDSExplorerNodes.RDS_ROOT_NODE) {
        menu.add(new OpenRdsConsoleAction());
    }//from   ww  w  .j ava 2  s.  c om
}

From source file:com.amazonaws.eclipse.explorer.s3.dnd.DownloadDropAssistant.java

License:Apache License

protected IStatus doDrop(Object aTarget, IStructuredSelection s3ObjectSelection) {
    if (!(aTarget instanceof IResource)) {
        return Status.CANCEL_STATUS;
    }/*  w  ww  .  j  av a2s  .  c  om*/

    // Drop targets can be folders, projects, or files. In the case of
    // files, we just want to identify the parent directory.
    IResource resource = (IResource) aTarget;
    if (resource instanceof IFile) {
        resource = resource.getParent();
    }

    final IResource dropFolder = resource;

    final S3ObjectSummary s3object = (S3ObjectSummary) s3ObjectSelection.getFirstElement();

    final File f = dropFolder.getLocation().toFile();
    if (!f.exists())
        return Status.CANCEL_STATUS;

    String fileName = getOutputFileName(s3object, f);

    if (fileName == null || fileName.length() == 0) {
        return Status.CANCEL_STATUS;
    }

    final File outputFile = new File(fileName);

    new DownloadObjectJob("Downloading " + s3object.getKey(), s3object, dropFolder, outputFile).schedule();

    return Status.OK_STATUS;
}

From source file:com.amazonaws.eclipse.explorer.s3.dnd.UploadDropAssistant.java

License:Apache License

public static File getFileToDrop(TransferData transfer) {
    File f = null;/*from  w w  w. j  a v  a2s .c  o m*/
    if (LocalSelectionTransfer.getTransfer().isSupportedType(transfer)) {
        IStructuredSelection selection = (IStructuredSelection) LocalSelectionTransfer.getTransfer()
                .nativeToJava(transfer);
        IResource resource = (IResource) selection.getFirstElement();
        f = resource.getLocation().toFile();
    } else if (FileTransfer.getInstance().isSupportedType(transfer)) {
        String[] files = (String[]) FileTransfer.getInstance().nativeToJava(transfer);
        f = new File(files[0]);
    }
    return f;
}

From source file:com.amazonaws.eclipse.explorer.simpledb.SimpleDBNavigatorActionProvider.java

License:Apache License

@Override
public void fillContextMenu(final IMenuManager menu) {
    IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();

    if (selection.getFirstElement() == SimpleDBRootElement.ROOT_ELEMENT) {
        menu.add(new CreateDomainAction());
    } else if (selection.getFirstElement() instanceof DomainNode) {

        List<String> domainNames = new ArrayList<String>();
        Iterator iterator = selection.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            if (next instanceof DomainNode) {
                DomainNode domainNode = (DomainNode) next;
                domainNames.add(domainNode.getName());
            }//from   www  . j av a 2  s  . c  om
        }

        menu.add(new CreateDomainAction());
        menu.add(new DeleteDomainAction(domainNames));
        menu.add(new Separator());

        menu.add(new OpenSQLScrapbookAction());
        DomainNode domainNode = (DomainNode) selection.getFirstElement();
        menu.add(new OpenDataTableEditorAction(domainNode.getName()));
    }
}