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.aptana.ide.server.ui.views.GenericServersView.java

License:Open Source License

/**
 * Creates and registers the context menu
 *//*from   w  ww.  jav a 2s. co  m*/
private void createPopupMenu() {
    deleteAction = ActionFactory.DELETE.create(getViewSite().getWorkbenchWindow());
    MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
    menuMgr.setRemoveAllWhenShown(true);
    getViewSite().getActionBars().setGlobalActionHandler(ActionFactory.DELETE.getId(), new Action() {

        public void run() {
            doDelete();
        }
    });
    menuMgr.addMenuListener(new IMenuListener() {
        public void menuAboutToShow(IMenuManager manager) {
            IContributionItem[] items = getViewSite().getActionBars().getToolBarManager().getItems();
            for (int i = 0; i < items.length; i++) {

                if (items[i] instanceof ActionContributionItem) {
                    ActionContributionItem aci = (ActionContributionItem) items[i];
                    IAction action = aci.getAction();
                    if (action == openLog) {
                        // adds the Open Log action to the context menu as a push button instead of 
                        // drop-down
                        boolean enabled = action.isEnabled();
                        action = new OpenLogAction(serverViewer, Action.AS_PUSH_BUTTON);
                        action.setEnabled(enabled);
                    }
                    if (action.isEnabled() && action.getStyle() != Action.AS_DROP_DOWN_MENU) {
                        if (action.getText() == null || action.getText().length() == 0) {
                            action.setText(action.getToolTipText());
                        }
                        manager.add(action);
                    }
                } else {
                    if (items[i] instanceof Separator) {
                        manager.add(new Separator());
                    }
                }
            }
            manager.add(new Separator());
            IStructuredSelection selection = (IStructuredSelection) serverViewer.getSelection();
            final IServer server = (IServer) selection.getFirstElement();
            if (server != null) {
                deleteAction.setText(StringUtils.format(Messages.ServersView_DELETE, getShortenName(server)));
                // deleteAction.setEnabled(server.getServerState() == IServer.STATE_STOPPED);
                deleteAction.setEnabled(server.canDelete().isOK());
                manager.add(deleteAction);

                Action action = new Action() {
                    public void run() {
                        doEdit(server);
                    }
                };
                action.setText(StringUtils.format(Messages.ServersView_EDIT, getShortenName(server)));
                IStatus canModify = server.canModify();
                IStatus canModifyInStoppedStateOnly = server.canModifyInStoppedStateOnly();
                action.setEnabled(((canModifyInStoppedStateOnly == null
                        || canModifyInStoppedStateOnly.getCode() == IStatus.OK)
                                ? server.getServerState() == IServer.STATE_STOPPED
                                : true)
                        && (canModify == null || canModify.getCode() == IStatus.OK));
                manager.add(action);
            }
            // deleteAction.setEnabled(!selection.isEmpty());
            manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); // Allow
        }

        private String getShortenName(final IServer server) {
            String name = server.getName();
            int length = name.length();
            if (length > MAX_SHOWN_SERVER_NAME) {
                int delta = (length - 15) / 2;
                int pivot = length / 2;
                int start = pivot - delta;
                int end = pivot + delta;
                String s1 = name.substring(0, start);
                String s2 = name.substring(end, length);
                String s = s1 + ELLIPSIS + s2;
                return s;
            }
            return name;
        }
    });
    Menu menu = menuMgr.createContextMenu(serverViewer.getControl());
    serverViewer.getControl().setMenu(menu);
    getSite().registerContextMenu(menuMgr, serverViewer);
}

From source file:com.aptana.ide.server.ui.views.GenericServersView.java

License:Open Source License

private void doDelete() {

    IStructuredSelection selection = (IStructuredSelection) serverViewer.getSelection();
    if (selection.isEmpty()) {
        return;/* ww  w  .  j a  v  a 2 s .  co m*/
    }
    IServer server = (IServer) selection.getFirstElement();
    if (!server.canDelete().isOK()) {
        return;
    }
    boolean mayStop = (server.getServerState() != IServer.STATE_STOPPED
            && server.getServerState() != IServer.STATE_UNKNOWN);
    boolean askStopBeforeDelete = (server.askStopBeforeDelete().getCode() == IStatus.OK);
    DeleteServerConfirmDialog dlg = new DeleteServerConfirmDialog(getViewSite().getShell(),
            Messages.ServersView_CONFIRM_DELETE, null,
            StringUtils.format(Messages.ServersView_CONFIRM_DELETE_TEXT, server.getName()),
            MessageDialog.QUESTION,
            new String[] { Messages.GenericServersView_YES, Messages.GenericServersView_NO }, 0, mayStop,
            askStopBeforeDelete);

    int openConfirm = dlg.open();
    if (openConfirm != 0) {
        return;
    }
    boolean doStop = dlg.shouldStop;
    if (doStop) {
        server.stop(true, null, null);
    }

    try {
        ServerCore.getServerManager().removeServer(server);
    } catch (CoreException e) {
        MessageDialog.openError(getViewSite().getShell(), Messages.ServersView_CONFIRM_DELETE,
                StringUtils.format(Messages.ServersView_CONFIRM_DELETE_TEXT, server.getName()));
    }
}

From source file:com.aptana.ide.syncing.ui.navigator.actions.DoubleClickAction.java

License:Open Source License

public void run() {
    IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection();
    Object element = selection.getFirstElement();
    if (element instanceof ISiteConnection) {
        // double-clicked on a site; opens it in the connection editor
        EditorUtils.openConnectionEditor((ISiteConnection) element);
    } else if (element instanceof ProjectSiteConnection) {
        // double-clicked on a site inside a project; both expands the node
        // and opens the connection editor
        super.run();
        EditorUtils.openConnectionEditor(((ProjectSiteConnection) element).getSiteConnection());
    } else {/*from w  ww.  j a  va 2  s  .  c om*/
        if (selectionHasChildren()) {
            super.run();
        } else {
            // no connection point has been defined against the project;
            // opens the new site connection dialog
            IAdaptable source = null;
            if (element instanceof IAdaptable) {
                source = (IAdaptable) element;
            }
            openNewSiteConnectionDialog(source);
        }
    }
}

From source file:com.aptana.ide.syncing.ui.navigator.actions.SyncingActionProvider.java

License:Open Source License

@Override
public void fillActionBars(IActionBars actionBars) {
    boolean hasSyncConnection = false;
    IStructuredSelection selection = getSelection();
    if (!selection.isEmpty()) {
        Object element = selection.getFirstElement();
        if (element instanceof IAdaptable) {
            hasSyncConnection = hasSyncConnection((IAdaptable) element);
        }/*from  w w  w  . jav  a 2  s . co m*/
    }
    // fillActionBars() is called each time the selection changes, so adds a check to only add the toolbar items
    // once
    IToolBarManager toolbar = actionBars.getToolBarManager();
    if (!isToolbarFilled) {
        if (hasSyncConnection) {
            fillToolBar(toolbar);
            actionBars.updateActionBars();
            isToolbarFilled = true;
        }
    } else if (updateToolbar(toolbar, hasSyncConnection)) {
        actionBars.updateActionBars();
    }
    updateSelection();
}

From source file:com.aptana.ide.syncing.ui.wizards.RemoteConnectionSelectionPage.java

License:Open Source License

/**
 * @param selection/*from   w  w  w .j av  a 2  s .  co  m*/
 *            the initial selection
 */
public RemoteConnectionSelectionPage(IStructuredSelection selection) {
    super("connectionPage"); //$NON-NLS-1$
    if (selection != null) {
        Object possibleSite = selection.getFirstElement();
        if (possibleSite != null && possibleSite instanceof IConnectionPoint) {
            site = (IConnectionPoint) possibleSite;
        }
    }
}

From source file:com.aptana.ide.ui.io.IOUIPlugin.java

License:Open Source License

private void handleProjectExplorerListeners(final IWorkbenchPart part) {
    CommonViewer viewer = ((ProjectExplorer) part).getCommonViewer();
    viewer.setComparer(new FileSystemElementComparer());
    final Tree tree = viewer.getTree();
    viewer.addDoubleClickListener(new IDoubleClickListener() {
        public void doubleClick(DoubleClickEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            Object element = selection.getFirstElement();

            if (selection.size() == 1 && (element instanceof IResource)
                    && ((IResource) element).getType() == IResource.PROJECT) {
                OpenResourceAction openResourceAction = new OpenResourceAction(
                        PlatformUI.getWorkbench().getActiveWorkbenchWindow());
                openResourceAction.selectionChanged((IStructuredSelection) event.getViewer().getSelection());
                if (openResourceAction.isEnabled()) {
                    openResourceAction.run();
                }/*from w  w  w.j a v  a 2s .  co m*/
            }

        }
    });
    tree.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDown(MouseEvent e) {
            if (tree.getItem(new Point(e.x, e.y)) == null) {
                tree.deselectAll();
                tree.notifyListeners(SWT.Selection, new Event());
            }
        }
    });
}

From source file:com.aptana.ide.ui.io.navigator.actions.BaseDoubleClickAction.java

License:Open Source License

public void run() {
    if (selectionHasChildren()) {
        // performs the usual double-click action
        IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection();
        TreeItem item = fTreeViewer.getTree().getSelection()[0];
        if (item.getExpanded()) {
            fTreeViewer.collapseToLevel(selection.getFirstElement(), AbstractTreeViewer.ALL_LEVELS);
        } else {// w w  w.  ja  va  2 s.co  m
            fTreeViewer.expandToLevel(selection.getFirstElement(), 1);
        }
    }
}

From source file:com.aptana.ide.ui.io.navigator.actions.NewFileAction.java

License:Open Source License

@Override
protected boolean updateSelection(IStructuredSelection selection) {
    fSelectedElement = null;//from   www .ja va  2 s.co  m

    if (selection != null && !selection.isEmpty()) {
        Object element = selection.getFirstElement();
        if (element instanceof IAdaptable) {
            fSelectedElement = (IAdaptable) element;
        }
    }

    return super.updateSelection(selection) && fSelectedElement != null;
}

From source file:com.aptana.ide.ui.io.navigator.actions.NewFileTemplateMenuContributor.java

License:Open Source License

protected void createNewFileFromTemplate(final TemplateElement template) {
    IStructuredSelection selection = getActiveSelection();
    if (!selection.isEmpty()) {
        Object element = selection.getFirstElement();
        if (element instanceof IAdaptable) {
            IFileStore fileStore = (IFileStore) ((IAdaptable) element).getAdapter(IFileStore.class);
            if (fileStore != null) {
                // this is a non-workspace selection
                String filetype = template.getFiletype();
                // strips the leading * before . if there is one
                int index = filetype.lastIndexOf('.');
                if (index > -1) {
                    filetype = filetype.substring(index);
                }//from  w  w  w. j a v a  2 s. co  m
                NewFileAction action = new NewFileAction("new_file" + filetype, template); //$NON-NLS-1$
                action.updateSelection(selection);
                action.run();
                return;
            }
        }
    }

    NewTemplateFileWizard wizard = new NewTemplateFileWizard(template);
    wizard.init(PlatformUI.getWorkbench(), selection);
    WizardDialog dialog = new WizardDialog(UIUtils.getActiveShell(), wizard);
    dialog.open();
}

From source file:com.aptana.ide.ui.io.navigator.actions.NewFileTemplateMenuContributor.java

License:Open Source License

protected void createNewBlankFile(String editorType, String fileExtension) {
    final String initialFileName = "new_file." + fileExtension; //$NON-NLS-1$

    IStructuredSelection selection = getActiveSelection();
    if (!selection.isEmpty()) {
        Object element = selection.getFirstElement();
        if (element instanceof IAdaptable) {
            IFileStore fileStore = (IFileStore) ((IAdaptable) element).getAdapter(IFileStore.class);
            if (fileStore != null) {
                // this is a non-workspace selection
                NewFileAction action = new NewFileAction(initialFileName) {

                    @Override/*from  ww  w.  j av  a 2 s.com*/
                    protected InputStream getInitialContents() {
                        // empty content
                        return new ByteArrayInputStream(ArrayUtil.NO_BYTES);
                    }
                };
                action.updateSelection(selection);
                action.run();
                return;
            }
        }
    }

    BasicNewFileResourceWizard wizard = new BasicNewFileResourceWizard() {

        @Override
        public void addPages() {
            super.addPages();
            ((WizardNewFileCreationPage) getPages()[0]).setFileName(initialFileName);
        }
    };
    wizard.init(PlatformUI.getWorkbench(), selection);
    WizardDialog dialog = new WizardDialog(UIUtils.getActiveShell(), wizard);
    dialog.open();
}