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.debug.internal.ui.launchConfigurations.HttpServerSettingsTab.java

License:Open Source License

private void removePaths() {
    IStructuredSelection selection = (IStructuredSelection) fListViewer.getSelection();
    Object first = selection.getFirstElement();
    int index = -1;
    for (int i = 0; i < elements.size(); i++) {
        Object object = elements.get(i);
        if (object.equals(first)) {
            index = i;/*from  w  ww . j av  a  2s. c  o  m*/
            break;
        }
    }
    elements.removeAll(selection.toList());
    if (index > elements.size() - 1) {
        index = elements.size() - 1;
    }
    if (index >= 0) {
        fListViewer.setSelection(new StructuredSelection(elements.get(index)));
    }
    setDirty(true);
    updateLaunchConfigurationDialog();
}

From source file:com.aptana.ide.debug.internal.ui.preferences.JSDetailFormattersPreferencePage.java

License:Open Source License

private void updatePage(IStructuredSelection selection) {
    removeFormatterButton.setEnabled(!selection.isEmpty());
    editFormatterButton.setEnabled(selection.size() == 1);
    sourceViewer.getDocument()//from  ww  w  .ja  v  a2 s.co m
            .set(selection.size() == 1 ? ((DetailFormatter) selection.getFirstElement()).getSnippet()
                    : StringUtils.EMPTY);
}

From source file:com.aptana.ide.debug.internal.ui.preferences.JSDetailFormattersPreferencePage.java

License:Open Source License

private void removeTypes() {
    Object[] list = formatters.toArray();
    IStructuredSelection selection = (IStructuredSelection) listViewer.getSelection();
    Object first = selection.getFirstElement();
    int index = -1;
    for (int i = 0; i < list.length; i++) {
        if (list[i].equals(first)) {
            index = i;/* w ww . j  a va  2  s  .c o  m*/
            break;
        }
    }

    removeDetailFormatters(selection.toArray());

    list = formatters.toArray();
    if (index > list.length - 1) {
        index = list.length - 1;
    }
    if (index >= 0) {
        listViewer.setSelection(new StructuredSelection(list[index]));
    }
}

From source file:com.aptana.ide.editor.erb.wizards.ERBNewFileWizard.java

License:Open Source License

private String getProperExtension(ISelection selection) {
    String extension = null;//from ww  w  .  j  a v  a  2s  .  co m
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structured = (IStructuredSelection) selection;
        Object first = structured.getFirstElement();
        if (!(first instanceof IResource) && (first instanceof IAdaptable)) {
            IAdaptable adaptable = (IAdaptable) first;
            Object resource = adaptable.getAdapter(IResource.class);
            if (resource != null) {
                first = resource;
            }
        }
        if (first instanceof IResource) {
            IResource res = (IResource) first;
            IProject proj = res.getProject();
            String version = RailsPlugin.getRailsVersion(proj);
            if (version == null)
                return HTML_ERB;
            Version yeah = new Version(version);
            if (yeah.getMajor() >= 2)
                extension = HTML_ERB;
            else
                extension = RHTML;
        }
    }
    return extension;
}

From source file:com.aptana.ide.editors.ui.views.problems.ProblemsPage.java

License:Open Source License

/**
 * viewerSelectionChanged//from  ww w. j  a  v  a2 s .  c o  m
 * 
 * @param selection
 */
protected void viewerSelectionChanged(IStructuredSelection selection) {

    IEditorPart ed = null;

    try {
        ed = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    } catch (Exception e) {
        return;
    }

    if (ed instanceof IUnifiedEditor) {
        ViewContentItem item = (ViewContentItem) selection.getFirstElement();
        if (item != null) {
            ((IUnifiedEditor) ed).selectAndReveal(item.offset, item.len);
        }
    }

}

From source file:com.aptana.ide.editors.views.actions.ActionsView.java

License:Open Source License

private void makeActions() {

    actionMakeExecutable = new org.eclipse.jface.action.Action() {
        public void run() {
            ISelection selection = viewer.getSelection();

            if (selection != null && selection instanceof IStructuredSelection) {
                IStructuredSelection structuredSelection = (IStructuredSelection) selection;
                Object element = structuredSelection.getFirstElement();
                if (element instanceof ActionSet) {
                    if (((ActionSet) element).isExecutable()) {
                        ((ActionSet) element).setExecutable(false);
                    } else {
                        ((ActionSet) element).setExecutable(true);
                    }/*ww  w.  j av  a  2s.  co  m*/

                    viewer.refresh();
                }
            }

        }
    };
    actionMakeExecutable.setText(Messages.ActionsView_ToggleExecutable);
    actionMakeExecutable.setToolTipText(Messages.ActionsView_ToggleToolTip);

    actionExecute = new org.eclipse.jface.action.Action() {
        public void run() {
            ActionsViewEvent e = new ActionsViewEvent(ActionsViewEventTypes.EXECUTE);
            ISelection selection = viewer.getSelection();

            if (selection != null && selection instanceof IStructuredSelection) {
                IStructuredSelection structuredSelection = (IStructuredSelection) selection;
                Object element = structuredSelection.getFirstElement();
                IAction[] actions;

                if (element instanceof IAction) {
                    actions = new IAction[] { (IAction) element };
                } else {
                    actions = new IAction[0];
                }

                e.setActions(actions);
            }

            fireActionsViewEvent(e);
        }
    };
    actionExecute.setText(Messages.ActionsView_Execute);
    actionExecute.setToolTipText(Messages.ActionsView_ExecuteToolTip);

    actionAddCurrentFile = new org.eclipse.jface.action.Action() {
        public void run() {
            ActionsViewEvent e = new ActionsViewEvent(ActionsViewEventTypes.ADD_CURRENT_FILE);
            ISelection selection = viewer.getSelection();
            if (selection != null) {
                Object firstElement = ((IStructuredSelection) selection).getFirstElement();
                if (firstElement instanceof ActionSet) {
                    e.setName(((ActionSet) firstElement).getName());
                }
            }

            fireActionsViewEvent(e);
        }
    };
    actionAddCurrentFile.setText(Messages.ActionsView_AddCurrentFile);
    actionAddCurrentFile.setToolTipText(Messages.ActionsView_AddCurrentFileToolTip);

    actionNewActionSet = new org.eclipse.jface.action.Action() {
        public void run() {
            InputDialog input = new InputDialog(getSite().getShell(), Messages.ActionsView_NewActionSetName,
                    Messages.ActionsView_PleaseEnterActionSetName, StringUtils.EMPTY, null);

            if (input.open() == Window.OK && input.getValue().length() > 0) {
                ActionsViewEvent e = new ActionsViewEvent(ActionsViewEventTypes.CREATE_ACTION_SET);
                e.setName(input.getValue());
                fireActionsViewEvent(e);
            }
        }
    };
    actionNewActionSet.setText(Messages.ActionsView_NewActionSet);
    actionNewActionSet.setToolTipText(Messages.ActionsView_NewActionSet);
    actionNewActionSet.setImageDescriptor(fAddFileIconDescriptor);

    actionEdit = new org.eclipse.jface.action.Action() {
        public void run() {
            ISelection selection = viewer.getSelection();

            if (selection != null) {
                Object firstElement = ((IStructuredSelection) selection).getFirstElement();
                if (firstElement instanceof Action) {
                    Action a = (Action) firstElement;
                    editAction(a);
                }
            }
        }
    };
    actionEdit.setText(Messages.ActionsView_EditAction);
    actionEdit.setToolTipText(Messages.ActionsView_EditAction);

    actionReload = new org.eclipse.jface.action.Action() {
        public void run() {
            ISelection selection = viewer.getSelection();

            if (selection != null) {
                Object firstElement = ((IStructuredSelection) selection).getFirstElement();
                if (firstElement instanceof Action) {
                    Action a = (Action) firstElement;
                    reloadAction(a);
                }
            }
        }
    };
    actionReload.setText(Messages.ActionsView_ReloadAction);
    actionReload.setToolTipText(Messages.ActionsView_ReloadAction);

    actionAdd = new org.eclipse.jface.action.Action() {
        public void run() {
            FileDialog fileDialog = new FileDialog(viewer.getControl().getShell(), SWT.MULTI);
            fileDialog.setFilterExtensions(FILTER_EXTENSIONS);
            fileDialog.setFilterNames(FILTER_NAMES);
            String text = fileDialog.open();
            if (text != null) {
                IPath basePath = new Path(fileDialog.getFilterPath());
                String[] fileNames = fileDialog.getFileNames();
                IPath[] paths = new IPath[fileNames.length];

                for (int i = 0; i < paths.length; i++) {
                    paths[i] = basePath.append(fileNames[i]);
                }

                ActionsViewEvent e = new ActionsViewEvent(ActionsViewEventTypes.ADD);
                e.setPaths(paths);
                fireActionsViewEvent(e);
            }
        }
    };

    actionAdd.setText(Messages.ActionsView_AddFile);
    actionAdd.setToolTipText(Messages.ActionsView_AddFile);
    actionAdd.setImageDescriptor(fAddFileIconDescriptor);

    actionDelete = new org.eclipse.jface.action.Action() {
        public void run() {
            removeFiles(viewer.getSelection());
        }
    };
    actionDelete.setText(Messages.ActionsView_RemoveFile);
    actionDelete.setToolTipText(Messages.ActionsView_RemoveFile);
    actionDelete.setImageDescriptor(
            PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));

    actionDoubleClick = new org.eclipse.jface.action.Action() {
        public void run() {
            ISelection selection = viewer.getSelection();
            Object firstElement = ((IStructuredSelection) selection).getFirstElement();

            if (firstElement instanceof ActionSet) {
                toggleElementState(firstElement);
            } else if (firstElement instanceof IAction) {
                ActionsViewEvent e = new ActionsViewEvent(ActionsViewEventTypes.EXECUTE);
                IAction[] actions = new IAction[] { (IAction) firstElement };

                e.setActions(actions);

                fireActionsViewEvent(e);
            }
        }
    };
}

From source file:com.aptana.ide.editors.wizards.SimpleNewWizardPage.java

License:Open Source License

/**
 * Tests if the current workbench selection is a suitable container to use.
 *//*from   w w w . ja  v  a  2s . c o  m*/

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) && (obj instanceof IAdaptable)) {
            IAdaptable adaptable = (IAdaptable) obj;
            Object resource = adaptable.getAdapter(IResource.class);
            if (resource != null) {
                obj = resource;
            }
        }
        if (obj instanceof IResource) {
            IContainer container;
            if (obj instanceof IContainer) {
                container = (IContainer) obj;
            } else {
                container = ((IResource) obj).getParent();
            }
            containerText.setText(container.getFullPath().toString());
        }
    }
    if (defaultFileName != null) {
        fileText.setText(defaultFileName);
    }
}

From source file:com.aptana.ide.installer.actions.RemoveFeatureAction.java

License:Open Source License

/**
 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
 *      org.eclipse.jface.viewers.ISelection)
 *///from   w w  w  .ja  v a 2s.c  o  m
public void selectionChanged(IAction action, ISelection selection) {
    this.selectedFeature = null;
    if (selection != null && !selection.isEmpty()) {
        if (selection instanceof IStructuredSelection) {
            IStructuredSelection sel = (IStructuredSelection) selection;
            Object element = sel.getFirstElement();
            if (element instanceof IPlugin) {
                IPlugin ref = (IPlugin) element;
                IPlugin ref2 = FeatureUtil.findInstalledPlugin(ref.getId());

                if (ref2 != null) {
                    URL url = ref2.getURL();
                    String protocol = url.getProtocol();
                    if (protocol.equals("file")) //$NON-NLS-1$
                    {
                        this.selectedFeature = ref2;
                    }
                }
            }
        }
    }
    action.setEnabled(selectedFeature != null);
}

From source file:com.aptana.ide.search.AptanaFileSearchPage.java

License:Open Source License

/**
 * hack for obtaining correct match/* w  w w .j  av a  2 s .c  o m*/
 * uses stack trace to ovverride behaviour if is called from magic super class methods
 * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#getCurrentMatch()
 */
public Match getCurrentMatch() {
    if (this.layout == AptanaFileSearchPage.LAYOUT_MATCHES) {
        // hack for returning correct results for next and previous matches
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        String methodName = stackTrace[1].getMethodName();
        if (methodName.equals("gotoNextMatch")) //$NON-NLS-1$
        {
            return null;
        }
        if (methodName.equals("gotoPreviousMatch")) //$NON-NLS-1$
        {
            return null;
        }

        IStructuredSelection selection = (IStructuredSelection) this.getViewer().getSelection();
        Object element = selection.getFirstElement();
        return (Match) element;
    }
    return super.getCurrentMatch();
}

From source file:com.aptana.ide.search.ui.filesystem.AptanaFileSystemSearchPage.java

License:Open Source License

/**
 * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#getCurrentMatch()
 *///from www . ja v  a 2s . com
public Match getCurrentMatch() {
    if (this.layout == LAYOUT_MATCHES) {
        // hack for returning correct results for next and previous matches
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        String methodName = stackTrace[1].getMethodName();
        if (methodName.equals("gotoNextMatch")) //$NON-NLS-1$
        {
            return null;
        }
        if (methodName.equals("gotoPreviousMatch")) //$NON-NLS-1$
        {
            return null;
        }

        IStructuredSelection selection = (IStructuredSelection) this.getViewer().getSelection();
        Object element = selection.getFirstElement();
        return (Match) element;
    }
    return super.getCurrentMatch();
}