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

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

Introduction

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

Prototype

public Object[] toArray();

Source Link

Document

Returns the elements in this selection as an array.

Usage

From source file:com.ibm.research.tours.content.actions.PickRegionActionDelegate.java

License:Open Source License

public void run(IAction action) {
    if (fTargetPart instanceof ITextEditor) {
        IStructuredSelection structuredSelection = (IStructuredSelection) fSelection;
        Object[] selection = structuredSelection.toArray();
        Object file = null;/*from  ww w  . jav  a  2  s . co m*/

        if (selection[0] instanceof IFileEditorInput) {
            IFileEditorInput input = (IFileEditorInput) selection[0];
            file = input.getFile();
        } else if (selection[0] instanceof IClassFileEditorInput) {
            IClassFileEditorInput input = (IClassFileEditorInput) selection[0];
            file = input.getClassFile();
        }

        if (file != null) {
            Region region = new Region(fTextSelection.getOffset(), fTextSelection.getLength());
            ToursContentPlugin.getDefault().getTextRegionClipBoard()
                    .putTextRegion(new TextRegion(file, region));
        }
    }
}

From source file:com.ibm.research.tours.content.dropadapter.LocalSelectionDropAdapter.java

License:Open Source License

public ITourElement[] convertDropData(Object data) {
    IStructuredSelection selection = (IStructuredSelection) LocalSelectionTransfer.getInstance().getSelection();
    Vector<ITourElement> elements = new Vector<ITourElement>();

    for (Object o : selection.toArray()) {
        if (o instanceof IJavaElement) {
            IJavaElement element = (IJavaElement) o;
            int type = element.getElementType();

            switch (type) {
            case IJavaElement.TYPE: // ok
            case IJavaElement.METHOD: // ok
            case IJavaElement.FIELD: // ok
                //case IJavaElement.IMPORT_DECLARATION : // not intresting
                //case IJavaElement.IMPORT_CONTAINER : // not intresting
                //case IJavaElement.PACKAGE_DECLARATION : // not intresting
                //case IJavaElement.PACKAGE_FRAGMENT : // as resource
                //case IJavaElement.JAVA_PROJECT : // as resource
                //case IJavaElement.CLASS_FILE : // as resource
                elements.add(new ResourceURLTourElement(element));
                continue;
            default:
                break;
            }/*from   w  ww .  ja  v a2 s .  com*/
        }

        // This will be processed as a resource
        IResource resource = null;

        if (o instanceof IJavaElement) {
            // don't use IAdaptable as for members only the top level type adapts
            resource = ((IJavaElement) o).getResource();
        } else if (o instanceof IAdaptable) {
            resource = (IResource) ((IAdaptable) o).getAdapter(IResource.class);
        }

        if (resource != null)
            elements.add(new ResourceURLTourElement(resource));
        else {
            MessageBox box = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
            box.setText("Unsupported drop operation");
            box.setMessage(o.getClass().getName());
            box.open();
        }
    }

    return elements.toArray(new ITourElement[0]);
}

From source file:com.ibm.research.tours.dnd.PaletteDragListener.java

License:Open Source License

public void dragStart(DragSourceEvent event) {
    fEntries = new ArrayList<IPaletteEntry>();

    IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection();

    if (!selection.isEmpty()) {
        for (Object o : selection.toArray())
            if (o instanceof IPaletteEntry)
                fEntries.add((IPaletteEntry) o);
    } else {//  www. jav a 2  s  .  c o m
        event.doit = false;
    }
}

From source file:com.ibm.research.tours.editors.TourEditor.java

License:Open Source License

public Object[] getSelection() {
    IStructuredSelection structuredSelection = (IStructuredSelection) fTourViewer.getSelection();
    return structuredSelection.toArray();
}

From source file:com.ibm.research.tours.editors.TourEditor.java

License:Open Source License

private void createTourTree(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));

    composite.setBackground(JFaceColors.getBannerBackground(Display.getDefault()));
    GridLayout layout = new GridLayout();
    layout.marginTop = 3;/*from   w  w w  . ja va  2s  .  c  o  m*/
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    layout.marginLeft = 3;
    layout.marginRight = 0;
    layout.verticalSpacing = 0;
    layout.horizontalSpacing = 0;
    composite.setLayout(layout);

    //fFilteredTree = new FilteredTree(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL,new PatternFilter());
    //fFilteredTree.setBackground(JFaceColors.getBannerBackground(Display.getCurrent()));

    fTourViewer = new TreeViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    fTourViewer.getTree().setBackground(JFaceColors.getBannerBackground(Display.getCurrent()));
    //fTourViewer = fFilteredTree.getViewer();
    fTourViewer.setContentProvider(new TourContentProvider());
    fTourViewer.setLabelProvider(new TourLabelProvider());

    // Load the transfers 
    List<Transfer> transfers = new ArrayList<Transfer>();

    // Add the default tour element transfer for internal dnd
    transfers.add(TourElementTransfer.getInstance());
    transfers.add(PaletteEntryTransfer.getInstance());

    // Add the drop adapters
    for (ITourElementDropAdapter adapter : ToursPlugin.getDefault().getDropAdapters()) {
        Transfer transfer = adapter.getTransfer();

        if (transfer != null && !(transfers.contains(transfer)))
            transfers.add(transfer);
    }

    if (transfers.size() > 0)
        fTourViewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, transfers.toArray(new Transfer[0]),
                new TourDragNDropAdapter(this));

    fTourViewer.addDragSupport(DND.DROP_COPY | DND.DROP_MOVE,
            new Transfer[] { TourElementTransfer.getInstance() }, new TourDragNDropAdapter(this));
    fTourViewer.addDoubleClickListener(new TourTreeDoubleClickListener());
    fTourTreeSelectionChangedListener = new TourTreeSelectionChangedListener(this);
    fTourViewer.addSelectionChangedListener(fTourTreeSelectionChangedListener);

    fTourViewer.addDoubleClickListener(new IDoubleClickListener() {
        public void doubleClick(DoubleClickEvent event) {
            IStructuredSelection structuredSelection = (IStructuredSelection) event.getSelection();
            Object[] selection = structuredSelection.toArray();
            String objectType = selection[0].getClass().getName();

            IDoubleClickActionContribution[] contributions = ToursPlugin.getDefault()
                    .getDoubleClickActionContributions();

            if (contributions.length > 0) {
                for (IDoubleClickActionContribution contribution : contributions) {
                    if (contribution.getObjectContribution().equals(objectType)) {
                        IDoubleClickActionDelegate delegate = contribution.getDoubleClickActionDelegate();
                        delegate.setActivePart(TourEditor.this);
                        delegate.selectionChanged(structuredSelection);
                        delegate.run();
                    }
                }
            }
        }
    });

    fTourTreeAdapter = new TourTreeAdapter(getTour());
    fTourViewer.setInput(fTourTreeAdapter);
    fTourViewer.setExpandedState(fTourTreeAdapter.getTourElements(), true);
    fTourViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
    fTour.addTourListener(fTourListener, false);

    //      final Tree tree = fTourViewer.getTree();
    //       // Disable native tooltip
    //      tree.setToolTipText("");
    //
    //       // Implement a "fake" tooltip
    //       final Listener labelListener = new Listener() {
    //         public void handleEvent(Event event) {
    //           Label label = (Label) event.widget;
    //           Shell shell = label.getShell();
    //           switch (event.type) {
    //           case SWT.MouseDown:
    //             Event e = new Event();
    //             e.item = (TreeItem) label.getData("_TABLEITEM");
    //             // Assuming table is single select, set the selection as if
    //             // the mouse down event went through to the table
    //             tree.setSelection(new TreeItem[] { (TreeItem) e.item });
    //             tree.notifyListeners(SWT.Selection, e);
    //           // fall through
    //           case SWT.MouseExit:
    //             shell.dispose();
    //             break;
    //           }
    //         }
    //       };
    //
    //       Listener tableListener = new Listener() 
    //       {
    //         Shell tip = null;
    //         Label label = null;
    //
    //         public void handleEvent(Event event) {
    //           switch (event.type) {
    //           case SWT.Dispose:
    //           case SWT.KeyDown:
    //           case SWT.MouseMove: {
    //             if (tip == null)
    //               break;
    //             tip.dispose();
    //             tip = null;
    //             label = null;
    //             break;
    //           }
    //           case SWT.MouseHover: {
    //             TreeItem item = tree.getItem(new Point(event.x, event.y));
    //             
    //             if (item != null) 
    //             {
    //               if (tip != null && !tip.isDisposed())
    //                 tip.dispose();
    //               tip = new Shell(Display.getDefault().getActiveShell(), SWT.ON_TOP | SWT.TOOL);
    //               tip.setLayout(new FillLayout());
    //               label = new Label(tip, SWT.NONE);
    //               label.setForeground(Display.getDefault()
    //                   .getSystemColor(SWT.COLOR_INFO_FOREGROUND));
    //               label.setBackground(Display.getDefault()
    //                   .getSystemColor(SWT.COLOR_INFO_BACKGROUND));
    //               label.setData("_TABLEITEM", item);
    //               label.setText("tooltip " + item.getText());
    //               
    //               label.addListener(SWT.MouseExit, labelListener);
    //               label.addListener(SWT.MouseDown, labelListener);
    //               Point size = tip.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    //               Rectangle rect = item.getBounds(0);
    //               Point pt = tree.toDisplay(rect.x, rect.y);
    //               tip.setBounds(pt.x, pt.y, size.x, size.y);
    //               tip.setVisible(true);
    //             }
    //           }
    //           }
    //         }
    //       };
    //       tree.addListener(SWT.Dispose, tableListener);
    //       tree.addListener(SWT.KeyDown, tableListener);
    //       tree.addListener(SWT.MouseMove, tableListener);
    //       tree.addListener(SWT.MouseHover, tableListener);

}

From source file:com.ibm.research.tours.editors.TourTreeSelectionChangedListener.java

License:Open Source License

public void selectionChanged(SelectionChangedEvent event) {
    IStructuredSelection structuredSelection = (IStructuredSelection) event.getSelection();

    performSave();//from  ww w  . j a  v  a  2 s  . com
    flastSelectedElement = null;

    if (!structuredSelection.isEmpty()) {
        Object[] selected = structuredSelection.toArray();

        if (selected.length == 1 && selected[0] instanceof ITourElement) {
            flastSelectedElement = (ITourElement) selected[0];
            fEditor.getNotesText().setText(flastSelectedElement.getNotes());
            fEditor.getNotesText().setEditable(true);
            return;
        }
    }

    fEditor.getNotesText().setText("");
    fEditor.getNotesText().setEditable(false);
    flastSelectedElement = null;
}

From source file:com.iw.plugins.spindle.ui.ChooseFromNamespaceWidget.java

License:Mozilla Public License

public ISelection getSelection() {

    IStructuredSelection selection = (IStructuredSelection) super.getSelection();

    if (selection == null || selection.isEmpty()) {

        return selection;

    }/* w  w w. j  a v  a  2s  . c om*/

    Object[] selectionData = selection.toArray();

    resultString = (String) selectionData[0];
    Object dataNamespace = selectionData[1];

    if (dataNamespace instanceof INamespaceFragment) {

        resultNamespace = (INamespaceFragment) selectionData[1];

    } else {

        resultNamespace = null;
    }

    if (resultString == null) {

        return new StructuredSelection();

    }

    return new StructuredSelection(getResultPath());
}

From source file:com.iw.plugins.spindle.ui.ChooseWorkspaceModelWidget.java

License:Mozilla Public License

public ISelection getSelection() {

    IStructuredSelection selection = (IStructuredSelection) super.getSelection();

    if (selection == null || selection.isEmpty()) {

        return selection;

    }//w  ww .  ja  v a 2s.c  o  m

    Object[] selectionData = selection.toArray();

    IStorage selectedStorage = (IStorage) selectionData[0];
    IPackageFragment selectedPackage = (IPackageFragment) selectionData[1];

    resultString = null;
    resultPackage = null;

    if (selectedStorage != null) {

        resultString = selectedStorage.getName();
    }

    if (selectedPackage != null) {

        resultPackage = selectedPackage;

    }

    if (resultString == null) {

        return new StructuredSelection();

    }

    return new StructuredSelection(resultString);
}

From source file:com.iw.plugins.spindle.ui.properties.SearchForLibraryWidget.java

License:Mozilla Public License

public ISelection getSelection() {
    IStructuredSelection selection = (IStructuredSelection) super.getSelection();
    if (selection == null || selection.isEmpty()) {
        return selection;
    }/*  ww  w . ja  v a2s.  c o m*/
    Object[] selectionData = selection.toArray();

    String name = (String) selectionData[0];
    IPackageFragment fragment = (IPackageFragment) selectionData[1];

    String elementName = fragment.getElementName();
    if (elementName == null || elementName.trim().length() == 0) {
        fResultString = "/" + name;
    } else {
        fResultString = "/" + elementName.replace('.', '/') + "/" + name;
    }
    return new StructuredSelection(fResultString);
}

From source file:com.iw.plugins.spindle.ui.widgets.TypeChooserWidget.java

License:Mozilla Public License

public ISelection getSelection() {
    IStructuredSelection selection = (IStructuredSelection) super.getSelection();
    if (selection == null || selection.isEmpty()) {
        return selection;
    }/*from  ww  w  . j  a v a 2 s  .c om*/
    Object[] selectionData = selection.toArray();

    String name = (String) selectionData[0];
    IPackageFragment fragment = (IPackageFragment) selectionData[1];

    fResultType = (IType) fragment.getCompilationUnit(name);
    if (fResultType == null)
        fResultType = (IType) fragment.getClassFile(name);
    return new StructuredSelection(fResultType);
}