Example usage for org.eclipse.jface.viewers IStructuredSelection size

List of usage examples for org.eclipse.jface.viewers IStructuredSelection size

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers IStructuredSelection size.

Prototype

public int size();

Source Link

Document

Returns the number of elements selected in this selection.

Usage

From source file:com.android.ide.eclipse.adt.project.ExportAction.java

License:Open Source License

public void run(IAction action) {
    if (mSelection instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) mSelection;
        // get the unique selected item.
        if (selection.size() == 1) {
            Object element = selection.getFirstElement();

            // get the project object from it.
            IProject project = null;/*from  w w w  .  j a v a  2s  .  c o  m*/
            if (element instanceof IProject) {
                project = (IProject) element;
            } else if (element instanceof IAdaptable) {
                project = (IProject) ((IAdaptable) element).getAdapter(IProject.class);
            }

            // and finally do the action
            if (project != null) {
                ExportHelper.exportProject(project);
            }
        }
    }
}

From source file:com.android.ide.eclipse.editors.resources.explorer.ResourceExplorerView.java

License:Open Source License

@Override
public void createPartControl(Composite parent) {
    mTree = new Tree(parent, SWT.SINGLE | SWT.VIRTUAL);
    mTree.setLayoutData(new GridData(GridData.FILL_BOTH));
    mTree.setHeaderVisible(true);// w  w  w.ja v a  2 s .c o  m
    mTree.setLinesVisible(true);

    final IPreferenceStore store = AdtPlugin.getDefault().getPreferenceStore();

    // create 2 columns. The main one with the resources, and an "info" column.
    createTreeColumn(mTree, "Resources", SWT.LEFT, "abcdefghijklmnopqrstuvwxz", -1, PREFS_COLUMN_RES, store); //$NON-NLS-2$
    createTreeColumn(mTree, "Info", SWT.LEFT, "0123456789", -1, PREFS_COLUMN_2, store); //$NON-NLS-2$

    // create the jface wrapper
    mTreeViewer = new TreeViewer(mTree);

    mTreeViewer.setContentProvider(new ResourceContentProvider(true /* fullLevels */));
    mTreeViewer.setLabelProvider(new ResourceLabelProvider());

    // listen to selection change in the workbench.
    IWorkbenchPage page = getSite().getPage();

    page.addSelectionListener(this);

    // init with current selection
    selectionChanged(getSite().getPart(), page.getSelection());

    // add support for double click.
    mTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
        public void doubleClick(DoubleClickEvent event) {
            ISelection sel = event.getSelection();

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

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

                    // if it's a resourceFile, we directly open it.
                    if (element instanceof ResourceFile) {
                        try {
                            IDE.openEditor(getSite().getWorkbenchWindow().getActivePage(),
                                    ((ResourceFile) element).getFile().getIFile());
                        } catch (PartInitException e) {
                        }
                    } else if (element instanceof ProjectResourceItem) {
                        // if it's a ResourceItem, we open the first file, but only if
                        // there's no alternate files.
                        ProjectResourceItem item = (ProjectResourceItem) element;

                        if (item.isEditableDirectly()) {
                            ResourceFile[] files = item.getSourceFileArray();
                            if (files[0] != null) {
                                try {
                                    IDE.openEditor(getSite().getWorkbenchWindow().getActivePage(),
                                            files[0].getFile().getIFile());
                                } catch (PartInitException e) {
                                }
                            }
                        }
                    }
                }
            }
        }
    });

    // set up the resource manager to send us resource change notification
    AdtPlugin.getDefault().getResourceMonitor().addResourceEventListener(this);
}

From source file:com.android.ide.eclipse.ndk.internal.actions.AddNativeAction.java

License:Open Source License

@Override
public void run(IAction action) {
    IProject project = null;/*from www . ja  va 2s  .  com*/
    if (mSelection instanceof IStructuredSelection) {
        IStructuredSelection ss = (IStructuredSelection) mSelection;
        if (ss.size() == 1) {
            Object obj = ss.getFirstElement();
            if (obj instanceof IProject) {
                project = (IProject) obj;
            } else if (obj instanceof PlatformObject) {
                project = (IProject) ((PlatformObject) obj).getAdapter(IProject.class);
            }
        }
    }

    if (project != null) {
        AddNativeWizard wizard = new AddNativeWizard(project, mPart.getSite().getWorkbenchWindow());
        WizardDialog dialog = new WizardDialog(mPart.getSite().getShell(), wizard);
        dialog.open();
    }

}

From source file:com.apicloud.navigator.ui.wizards.NewFileWizardPage.java

License:Open Source License

private void initialize() {
    if ((this.selection != null) && (!(this.selection.isEmpty()))
            && (this.selection instanceof IStructuredSelection)) {
        IStructuredSelection ssel = (IStructuredSelection) this.selection;
        if (ssel.size() > 1)
            return;
        Object obj = ssel.getFirstElement();
        if (obj instanceof IResource) {
            IContainer container;/*from   w  w w.  j a  v  a 2s.  c o  m*/
            if (obj instanceof IContainer)
                container = (IContainer) obj;
            else
                container = ((IResource) obj).getParent();
            this.containerText.setText(container.getFullPath().toString());
        }
    }
    if ((this.fileExtends != null) && (this.fileExtends.length > 0))
        this.fileText.setText("new_file." + this.fileExtends[0]);
    else {
        this.fileText.setText("new_file");
    }
    this.fileText.setFocus();
    this.fileText.setSelection(0, "new_file".length());
}

From source file:com.apicloud.navigator.ui.wizards.TempleteFarmeWizardPage.java

License:Open Source License

private void updateDate() {
    if ((this.selection != null) && (!(this.selection.isEmpty()))
            && (this.selection instanceof IStructuredSelection)) {
        IStructuredSelection ssel = (IStructuredSelection) this.selection;
        if (ssel.size() > 1)
            return;
        Object obj = ssel.getFirstElement();
        if (obj instanceof IResource) {
            IResource res = (IResource) obj;
            project = res.getProject();/*from   ww  w . jav  a 2  s.  c om*/
        }
    }
    if (project != null) {
        this.list.setSelection(new StructuredSelection(project));
    }
}

From source file:com.appnativa.studio.dialogs.SequenceArrayEditor.java

License:Open Source License

protected void handleSelection(IStructuredSelection sel) {
    if ((sel.size() > 0) && (sel.getFirstElement() instanceof PropertySheetEntryEx)) {
        final PropertySheetEntryEx e = (PropertySheetEntryEx) sel.getFirstElement();

        showEditorForEntry(e);//w w  w  .j a v a2s  . c  om
    } else {
        if (editorComposite != null) {
            editorComposite.getParent().layout(true, true);
            propertyEditors.disposeOfLastEditor();
        }
    }
}

From source file:com.appnativa.studio.views.PropertiesView.java

License:Open Source License

protected void handleSelection(IStructuredSelection sel) {
    if ((sel.size() > 0) && (sel.getFirstElement() instanceof PropertySheetEntryEx)) {
        final PropertySheetEntryEx e = (PropertySheetEntryEx) sel.getFirstElement();

        if (propertySheetViewer.getControl().isFocusControl() || editorFocusedLocked) {
            if ((e != editorEntry) || (propertyEditors.getActiveEditor() == null)) {
                showEditorForEntry(e);//from  w  w  w  .ja v  a 2 s. com
            } else if (editorFocusedLocked) {
                propertyEditors.focusActiveEditor();
            }
        }
    } else {
        editorEntry = null;

        if (editorComposite != null) {
            hideEditor();
        }
    }
}

From source file:com.aptana.editor.common.outline.CommonOutlinePage.java

License:Open Source License

private void setEditorSelection(IStructuredSelection selection, boolean checkIfActive) {
    if (selection.size() == 1) {
        Object element = selection.getFirstElement();
        if (element instanceof IRange) {
            // selects the range in the editor
            fEditor.select((IRange) element, checkIfActive);
        }// w  w  w.j a  v a 2s .c  o  m
    }
}

From source file:com.aptana.editor.common.outline.CommonQuickOutlinePage.java

License:Open Source License

private void gotoSelectedElement() {
    ISelection selection = getSelection();
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structured = (IStructuredSelection) selection;
        // If a node in the outline view is selected
        if (structured.size() == 1) {
            Object element = structured.getFirstElement();

            if (element instanceof CommonOutlineItem) {
                CommonOutlineItem item = (CommonOutlineItem) element;
                this._editor.selectAndReveal(item.getStartingOffset(), item.getLength());
                closeDialog();//from   ww  w .java 2 s  . c o m
            } else if (element instanceof IParseNode) {
                int position = ((IParseNode) element).getStartingOffset();
                this._editor.selectAndReveal(position, 0);
                closeDialog();
            }
            return;
        }
    }

    this._editor.getISourceViewer().removeRangeIndication();
}

From source file:com.aptana.git.ui.internal.actions.CommitDialog.java

License:Open Source License

private Table createTable(Composite composite, final boolean staged) {
    Table table = new Table(composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION);
    table.setLinesVisible(true);//w  w  w  . j  a  va 2  s.  c o  m
    table.setHeaderVisible(true);
    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
    data.heightHint = 200;
    data.widthHint = 250;
    table.setLayoutData(data);
    String[] titles = { " ", Messages.CommitDialog_PathColumnLabel }; //$NON-NLS-1$
    int[] widths = new int[] { 20, 250 };
    for (int i = 0; i < titles.length; i++) {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText(titles[i]);
        column.setWidth(widths[i]);
    }
    List<ChangedFile> changedFiles = gitRepository.index().changedFiles();
    Collections.sort(changedFiles);
    for (ChangedFile file : changedFiles) {
        boolean match = false;
        if (staged && file.hasStagedChanges()) {
            match = true;
        } else if (!staged && file.hasUnstagedChanges()) {
            match = true;
        }

        if (match) {
            createTableItem(table, file, false);
        }
    }

    // Drag and Drop
    // FIXME If user drags and drops while we're still crunching on last drag/drop then we end up hanging
    // Seems to be related to manipulating the table here before we receive the index changed callback
    Transfer[] types = new Transfer[] { LocalSelectionTransfer.getTransfer() };

    // Drag Source
    DragSource source = new DragSource(table, DND.DROP_MOVE);
    source.setTransfer(types);
    source.addDragListener(new DragSourceAdapter() {
        public void dragStart(DragSourceEvent event) {
            DragSource ds = (DragSource) event.widget;
            draggingFromTable = ds.getControl();

            LocalSelectionTransfer.getTransfer()
                    .setSelection(new StructuredSelection(((Table) draggingFromTable).getSelection()));
            LocalSelectionTransfer.getTransfer().setSelectionSetTime(event.time & 0xFFFFFFFFL);
        }

        public void dragSetData(DragSourceEvent event) {
            // do nothing
        }
    });

    // Create the drop target
    DropTarget target = new DropTarget(table, DND.DROP_MOVE);
    target.setTransfer(types);
    if (table.getItemCount() == 0)
        target.setDropTargetEffect(null);
    target.addDropListener(new DropTargetAdapter() {
        public void dropAccept(DropTargetEvent event) {
            DropTarget dp = (DropTarget) event.widget;
            if (dp.getControl() == draggingFromTable) {
                event.detail = DND.DROP_NONE;
            }
        }

        public void dragEnter(DropTargetEvent event) {
            // Allow dropping text only
            for (int i = 0, n = event.dataTypes.length; i < n; i++) {
                if (LocalSelectionTransfer.getTransfer().isSupportedType(event.dataTypes[i])) {
                    event.currentDataType = event.dataTypes[i];
                }
            }
            event.operations = DND.DROP_MOVE;
        }

        public void dragOver(DropTargetEvent event) {
            event.feedback = DND.FEEDBACK_SCROLL;
        }

        @SuppressWarnings("unchecked")
        public void drop(DropTargetEvent event) {
            if (!LocalSelectionTransfer.getTransfer().isSupportedType(event.currentDataType))
                return;
            // Get the dropped data
            IStructuredSelection selection = (IStructuredSelection) event.data;
            moveItems(!staged, ((List<TableItem>) selection.toList()).toArray(new TableItem[selection.size()]));
        }
    });

    table.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            super.widgetSelected(e);
            if (e.item == null)
                return;
            TableItem item = (TableItem) e.item;
            updateDiff(staged, getChangedFile(item));
        }
    });
    // Allow double-clicking to toggle staged/unstaged
    table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDown(MouseEvent e) {
            if (e.getSource() == null)
                return;
            Table table = (Table) e.getSource();
            Point point = new Point(e.x, e.y);
            TableItem item = table.getItem(point);
            if (item == null) {
                return;
            }
            // did user click on file image? If so, toggle staged/unstage
            Rectangle imageBounds = item.getBounds(0);
            if (imageBounds.contains(point)) {
                moveItems(staged, new TableItem[] { item });
            }
        }

        @Override
        public void mouseDoubleClick(MouseEvent e) {
            if (e.getSource() == null)
                return;
            Table table = (Table) e.getSource();
            TableItem[] selected = table.getSelection();
            moveItems(staged, selected);
        }
    });
    // Custom drawing so we can truncate filepaths in middle...
    table.addListener(SWT.EraseItem, new Listener() {

        public void handleEvent(Event event) {
            // Only draw the text custom
            if (event.index != 1)
                return;

            event.detail &= ~SWT.FOREGROUND;
        }
    });
    table.addListener(SWT.PaintItem, new Listener() {

        public void handleEvent(Event event) {
            // Only draw the text custom
            if (event.index != 1)
                return;
            TableItem item = (TableItem) event.item;
            String text = item.getText(event.index);

            // Truncate middle of string
            Table theTable = (Table) event.widget;
            int width = theTable.getColumn(event.index).getWidth();
            Point p = event.gc.stringExtent(text); // is text wider than available width?
            if (p.x > width) {
                // chop string in half and drop a few characters
                int middle = text.length() / 2;
                String beginning = text.substring(0, middle - 1);
                String end = text.substring(middle + 2, text.length());
                // Now repeatedly chop off one char from each end until we fit
                // TODO Chop each side separately? it'd take more loops, but text would fit tighter when uneven
                // lengths work better..
                while (event.gc.stringExtent(beginning + "..." + end).x > width) //$NON-NLS-1$
                {
                    if (beginning.length() > 0) {
                        beginning = beginning.substring(0, beginning.length() - 1);
                    } else {
                        break;
                    }
                    if (end.length() > 0) {
                        end = end.substring(1);
                    } else {
                        break;
                    }
                }
                text = beginning + "..." + end; //$NON-NLS-1$
            }
            event.gc.drawText(text, event.x, event.y, true);

            event.detail &= ~SWT.FOREGROUND;
        }
    });

    if (!staged) {
        final Table myTable = table;
        MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
        menuMgr.setRemoveAllWhenShown(true);
        menuMgr.addMenuListener(new IMenuListener() {
            public void menuAboutToShow(IMenuManager manager) {
                TableItem[] selected = myTable.getSelection();
                List<IResource> files = new ArrayList<IResource>();
                final List<ChangedFile> changedFiles = new ArrayList<ChangedFile>();
                for (TableItem item : selected) {
                    ChangedFile file = getChangedFile(item);
                    if (file != null) {
                        changedFiles.add(file);
                        IFile iFile = gitRepository.getFileForChangedFile(file);
                        if (iFile != null) {
                            files.add(iFile);
                        }
                    }
                }

                ContributionItem ci = new ContributionItem() {
                    public void fill(Menu menu, int index) {
                        MenuItem item = new MenuItem(menu, SWT.NONE);
                        item.setText(Messages.CommitDialog_RevertLabel);
                        // need to remove the file(s) from staged table once action runs
                        item.addSelectionListener(new SelectionAdapter() {
                            @Override
                            public void widgetSelected(SelectionEvent e) {
                                // need to make a copy because operation will actually change input files.
                                final List<ChangedFile> copy = new ArrayList<ChangedFile>(changedFiles);
                                for (ChangedFile cf : changedFiles) {
                                    copy.add(new ChangedFile(cf));
                                }

                                gitRepository.index().discardChangesForFiles(changedFiles);

                                PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

                                    public void run() {
                                        // If this file was shown in diff area, we need to blank the diff area!
                                        if (fLastDiffFile != null) {
                                            for (ChangedFile file : copy) {
                                                if (file != null && file.equals(fLastDiffFile)) {
                                                    updateDiff(null, Messages.CommitDialog_NoFileSelected);
                                                }
                                            }
                                        }
                                        removeDraggedFilesFromSource(unstagedTable, copy);
                                    }
                                });
                            }
                        });
                    }
                };
                manager.add(ci);
                // Other plug-ins can contribute there actions here
                manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
            }
        });
        Menu menu = menuMgr.createContextMenu(table);
        table.setMenu(menu);
    }

    return table;
}