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.ui.io.navigator.actions.NewFolderAction.java

License:Open Source License

protected boolean updateSelection(IStructuredSelection selection) {
    fSelectedElement = null;//from w  w w. j  a  v a 2s. c  om

    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.OpenActionProvider.java

License:Open Source License

private void addOpenWithMenu(IMenuManager menu) {
    IStructuredSelection selection = getSelection();
    if (selection == null || selection.size() != 1) {
        return;//from ww  w .j ava 2 s. c  o  m
    }

    Object element = selection.getFirstElement();
    if (!(element instanceof IAdaptable)) {
        return;
    }
    IMenuManager submenu = new MenuManager(Messages.OpenActionProvider_LBL_OpenWith,
            ICommonMenuConstants.GROUP_OPEN_WITH);
    submenu.add(new GroupMarker(ICommonMenuConstants.GROUP_TOP));
    submenu.add(new OpenWithMenu(fSite.getPage(), (IAdaptable) element, new Client() {

        public void openEditor(IFileStore file, IEditorDescriptor editorDescriptor) {
            EditorUtils.openFileInEditor(file, editorDescriptor);
        }
    }));
    submenu.add(new GroupMarker(ICommonMenuConstants.GROUP_ADDITIONS));

    // adds the submenu
    if (submenu.getItems().length > 2 && submenu.isEnabled()) {
        menu.appendToGroup(ICommonMenuConstants.GROUP_OPEN_WITH, submenu);
    }
}

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

License:Open Source License

public void activateEditor(IWorkbenchPage aPage, IStructuredSelection aSelection) {
    if (aSelection == null || aSelection.isEmpty()) {
        return;//  w w  w  . j  a  va 2 s .  c  o  m
    }
    Object element = aSelection.getFirstElement();
    if (element instanceof FileSystemObject) {
        FileSystemObject file = (FileSystemObject) element;
        IFileStore fileStore = file.getFileStore();
        try {
            IEditorPart editorPart = aPage.findEditor(UniformFileStoreEditorInputFactory
                    .getUniformEditorInput(fileStore, new NullProgressMonitor()));
            aPage.bringToTop(editorPart);
        } catch (CoreException e) {
            IdeLog.logError(IOUIPlugin.getDefault(), e);
        }
    }
}

From source file:com.aptana.ide.views.outline.UnifiedOutlinePage.java

License:Open Source License

/**
 * createTreeViewer//from w ww .ja v a2 s  . com
 * 
 * @param parent
 * @return TreeViewer
 */
private TreeViewer createTreeViewer(Composite parent, PatternFilter filter) {
    FilteredTree tree = null;
    try {
        // When available (3.5+) use new constructor
        Constructor<FilteredTree> cons = FilteredTree.class.getConstructor(Composite.class, int.class,
                PatternFilter.class, boolean.class);
        tree = cons.newInstance(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL, filter, true);
    } catch (Exception e) {
        // fallback to deprecated old constructor when new one not available
        tree = new FilteredTree(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL, filter);
    }

    TreeViewer result = tree.getViewer();

    outlineProvider = UnifiedOutlineProvider.getInstance();
    outlineProvider.setOutlinePage(this);

    result.setLabelProvider(outlineProvider);
    result.setContentProvider(outlineProvider);
    result.setInput(this._editor);

    // add selection changed listener
    result.addSelectionChangedListener(new ISelectionChangedListener() {
        /**
         * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
         */
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();

            if (selection.size() == 1) {
                Object item = selection.getFirstElement();

                if (item != null && item instanceof OutlineItem) {
                    OutlineItem outlineItem = (OutlineItem) selection.getFirstElement();

                    // Only select and reveal in editor if the tree viewer is focused meaning the selection
                    // originated from a user selection on the tree itself

                    // move cursor to start of this item's text
                    if (_treeViewer != null && _treeViewer.getTree() != null
                            && !_treeViewer.getTree().isDisposed() && _treeViewer.getTree().isFocusControl()) {
                        _editor.selectAndReveal(outlineItem.getStartingOffset(), 0);
                    }

                    // TODO: activate the editor window
                    // Note this code works, but should only be called on mouse clicks so users can
                    // navigate through outline items using the keyboard
                    // IWorkbenchWindow window = JSPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
                    //               
                    // if (window != null)
                    // {
                    // IWorkbenchPage page = window.getActivePage();
                    //                  
                    // page.activate(_editor);
                    // }
                }
            }
        }
    });

    result.setComparer(new IElementComparer() {
        /**
         * @see org.eclipse.jface.viewers.IElementComparer#equals(java.lang.Object, java.lang.Object)
         */
        public boolean equals(Object a, Object b) {
            boolean result = false;

            if (a instanceof OutlineItem && b instanceof OutlineItem) {
                OutlineItem item1 = (OutlineItem) a;
                OutlineItem item2 = (OutlineItem) b;

                result = item1.equals(item2);
            } else if (a instanceof IParseNode && b instanceof IParseNode) {
                if (a == b) {
                    result = true;
                } else {
                    IParseNode node1 = (IParseNode) a;
                    IParseNode node2 = (IParseNode) b;
                    String path1 = node1.getUniquePath();
                    String path2 = node2.getUniquePath();

                    result = path1.equals(path2);
                }
            } else {
                result = (a == b);
            }

            return result;
        }

        /**
         * @see org.eclipse.jface.viewers.IElementComparer#hashCode(java.lang.Object)
         */
        public int hashCode(Object element) {
            return 0;
        }
    });

    return result;
}

From source file:com.aptana.ide.views.outline.UnifiedOutlinePage.java

License:Open Source License

/**
 * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
 *///from   w  w  w . j a  va  2  s  .co  m
public void selectionChanged(SelectionChangedEvent event) {
    IStructuredSelection selection = (IStructuredSelection) event.getSelection();

    // If a node in the outline view is selected
    if (selection.size() == 1) {
        Object element = selection.getFirstElement();

        if (element instanceof IResolvableItem) {
            IResolvableItem item = (IResolvableItem) element;
            // if item is resolvable and is targeting on external content
            if (item.isResolvable()) {
                // removing all item if needed
                if (openAction != null) {
                    _actionBars.getToolBarManager().remove(openAction);
                }
                openAction = new ActionContributionItem(new OpenExternalAction(item));
                _actionBars.getToolBarManager().add(openAction);
                _actionBars.getToolBarManager().update(false);
                // while item is not able to be resolved in current file
                // getting parent item
                while (!item.stillHighlight()) {
                    item = item.getParentItem();
                    if (item == null) {
                        return;
                    }
                }
                // selecting it in editor
                if (item instanceof IParseNode) {
                    int position = ((IParseNode) item).getStartingOffset();
                    this._editor.selectAndReveal(position, 0);
                    return;
                }
            } else {
                // removing item from toolbar
                removeOpenActionIfNeeded();
            }
        }
        removeOpenActionIfNeeded();
        if (element instanceof IRange) {
            int position = ((IRange) element).getStartingOffset();

            this._editor.selectAndReveal(position, 0);
        }

    } else {
        removeOpenActionIfNeeded();
        this._editor.getViewer().removeRangeIndication();
    }
}

From source file:com.aptana.ide.views.outline.UnifiedQuickOutlinePage.java

License:Open Source License

/**
  * {@inheritDoc}//  w ww . ja  v a  2s.c  o  m
  */
public void open(OpenEvent event) {
    IStructuredSelection selection = (IStructuredSelection) event.getSelection();

    // If a node in the outline view is selected
    if (selection.size() == 1) {
        Object element = selection.getFirstElement();

        if (element instanceof IResolvableItem) {
            IResolvableItem item = (IResolvableItem) element;
            // if item is resolvable and is targeting on external content
            if (item.isResolvable()) {
                // selecting it in editor
                if (item instanceof IParseNode) {
                    int position = ((IParseNode) item).getStartingOffset();
                    this._editor.selectAndReveal(position, 0);
                    return;
                }
            } else {
                if (item instanceof OutlineItem) {
                    OutlineItem outlineItem = (OutlineItem) selection.getFirstElement();

                    // Only select and reveal in editor if the tree viewer is focused meaning the selection
                    // originated from a user selection on the tree itself

                    // move cursor to start of this item's text
                    if (_treeViewer != null && _treeViewer.getTree() != null
                            && !_treeViewer.getTree().isDisposed() && _treeViewer.getTree().isFocusControl()) {
                        _editor.selectAndReveal(outlineItem.getStartingOffset(), 0);
                    }
                }
            }
        }
        if (element instanceof IRange) {
            int position = ((IRange) element).getStartingOffset();

            this._editor.selectAndReveal(position, 0);
        }

        notifyCloseListeners();
    } else {
        removeOpenActionIfNeeded();
        this._editor.getViewer().removeRangeIndication();
    }
}

From source file:com.aptana.ide.views.outline.UnifiedQuickOutlinePage.java

License:Open Source License

/**
 * {@inheritDoc}/*from   www  .  ja  v  a2s. c  om*/
 */
public void selectionChanged(SelectionChangedEvent event) {
    IStructuredSelection selection = (IStructuredSelection) event.getSelection();

    if (openAction != null) {
        _toolbarManager.remove(openAction);
        _toolbarManager.update(true);
        _toolbarManager.getControl().getParent().layout(true, true);
    }

    // If a node in the outline view is selected
    if (selection.size() == 1) {
        Object element = selection.getFirstElement();

        if (element instanceof IResolvableItem) {
            IResolvableItem item = (IResolvableItem) element;
            // if item is resolvable and is targeting on external content
            // removing all item if needed

            if (item.isResolvable()) {

                openAction = new ActionContributionItem(new OpenExternalAction(item));
                _toolbarManager.add(openAction);
                _toolbarManager.update(true);
                _toolbarManager.getControl().getParent().layout(true, true);

                // while item is not able to be resolved in current file
                // getting parent item
                while (!item.stillHighlight()) {
                    item = item.getParentItem();
                    if (item == null) {
                        return;
                    }
                }
                // selecting it in editor
                if (item instanceof IParseNode) {
                    int position = ((IParseNode) item).getStartingOffset();
                    this._editor.selectAndReveal(position, 0);
                    return;
                }
            } else {
                // removing item from toolbar
                removeOpenActionIfNeeded();
            }
        }
        removeOpenActionIfNeeded();
    } else {
        removeOpenActionIfNeeded();
        this._editor.getViewer().removeRangeIndication();
    }
}

From source file:com.aptana.ide.wizards.LibraryImportWizard.java

License:Open Source License

/**
 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
 *      org.eclipse.jface.viewers.IStructuredSelection)
 *///from   www .jav  a2s .  com
public void init(IWorkbench workbench, IStructuredSelection s) {
    Object firstItem = s.getFirstElement();

    if (firstItem instanceof IProject) {
        this._project = (IProject) firstItem;
    } else {
        _project = null;
        MessageDialog.openInformation(workbench.getActiveWorkbenchWindow().getShell(),
                Messages.LibraryImportWizard_ImportJavaScriptLibrary,
                Messages.LibraryImportWizard_CanOnlyImportIntoTopLevel);
    }
}

From source file:com.aptana.ide.xul.FirefoxOutline.java

License:Open Source License

/**
 * @see com.aptana.ide.editors.unified.ContributedOutline#createControl(org.eclipse.swt.widgets.Composite)
 *///from  w ww .java2  s. com
public void createControl(Composite parent) {
    treeViewer = new TreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE);
    treeViewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    treeViewer.getTree().setLayout(new GridLayout(1, true));
    treeViewer.setLabelProvider(new DOMLabelProvider());
    treeViewer.setAutoExpandLevel(3);
    treeViewer.setContentProvider(new DOMContentProvider());
    treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            if (selection.size() == 1) {
                nsIDOMNode node = (nsIDOMNode) selection.getFirstElement();
                browser.highlightElement(node);
            }
        }

    });
    treeViewer.addFilter(new InternalNodeFilter());
    filter = new PatternFilter() {

        protected boolean isLeafMatch(Viewer viewer, Object element) {
            if (element instanceof nsIDOMNode) {
                if (((nsIDOMNode) element).getNodeType() == nsIDOMNode.ELEMENT_NODE) {
                    nsIDOMElement e = (nsIDOMElement) ((nsIDOMNode) element)
                            .queryInterface(nsIDOMElement.NS_IDOMELEMENT_IID);
                    if (Activator.INTERNAL_ID.equals(e.getAttribute("class"))) //$NON-NLS-1$
                    {
                        return false;
                    }
                }
                DOMLabelProvider prov = (DOMLabelProvider) treeViewer.getLabelProvider();
                return this.wordMatches(prov.getText(element));
            }
            return true;
        }

    };
    treeViewer.addFilter(filter);
    refreshJob = new WorkbenchJob("Refresh Filter") {//$NON-NLS-1$
        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
         */
        public IStatus runInUIThread(IProgressMonitor monitor) {
            if (treeViewer.getControl().isDisposed()) {
                return Status.CANCEL_STATUS;
            }

            if (pattern == null) {
                return Status.OK_STATUS;
            }

            filter.setPattern(pattern);

            try {
                // don't want the user to see updates that will be made to the tree
                treeViewer.getControl().setRedraw(false);
                treeViewer.refresh(true);

                if (pattern.length() > 0) {
                    /*
                     * Expand elements one at a time. After each is expanded, check to see if the filter text has
                     * been modified. If it has, then cancel the refresh job so the user doesn't have to endure
                     * expansion of all the nodes.
                     */
                    IStructuredContentProvider provider = (IStructuredContentProvider) treeViewer
                            .getContentProvider();
                    Object[] elements = provider.getElements(treeViewer.getInput());
                    for (int i = 0; i < elements.length; i++) {
                        if (monitor.isCanceled()) {
                            return Status.CANCEL_STATUS;
                        }
                        treeViewer.expandToLevel(elements[i], AbstractTreeViewer.ALL_LEVELS);
                    }

                    TreeItem[] items = treeViewer.getTree().getItems();
                    if (items.length > 0) {
                        // to prevent scrolling
                        treeViewer.getTree().showItem(items[0]);
                    }

                }
            } finally {
                // done updating the tree - set redraw back to true
                treeViewer.getControl().setRedraw(true);
            }
            return Status.OK_STATUS;
        }

    };
    TreeItem item = new TreeItem(treeViewer.getTree(), SWT.NONE);
    item.setText(Messages.getString("FirefoxOutline.Select_Tab_To_Load_Outline")); //$NON-NLS-1$
    refreshJob.setSystem(true);
    refresh();
}

From source file:com.aptana.js.debug.ui.internal.actions.EditDetailFormatterAction.java

License:Open Source License

/**
 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
 *//*  www.  ja va  2 s  .  c om*/
public void run(IAction action) {
    IStructuredSelection selection = getCurrentSelection();
    if (selection == null || selection.size() != 1) {
        return;
    }
    Object element = selection.getFirstElement();
    String typeName;
    try {
        if (element instanceof IJSVariable) {
            typeName = ((IJSVariable) element).getReferenceTypeName();
        } else {
            return;
        }
    } catch (DebugException e) {
        IdeLog.logError(JSDebugUIPlugin.getDefault(), e);
        return;
    }
    DebugOptionsManager detailFormattersManager = JSDebugPlugin.getDefault().getDebugOptionsManager();
    DetailFormatter detailFormatter = detailFormattersManager.getAssociatedDetailFormatter(typeName);
    if (new DetailFormatterDialog(UIUtils.getActiveShell(), detailFormatter, null, false, true)
            .open() == Window.OK) {
        detailFormattersManager.setAssociatedDetailFormatter(detailFormatter);
        refreshCurrentSelection();
    }
}