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

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

Introduction

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

Prototype

@Override
public Iterator iterator();

Source Link

Document

Returns an iterator over the elements of this selection.

Usage

From source file:de.fu_berlin.inf.jtourbus.view.TourBusRoutesView.java

License:Open Source License

/**
 * This is a callback that will allow us to create the viewer and initialize
 * it.// w  w  w.ja  v a2 s  .  co m
 * 
 * @JTourBusStop 0.0, Interaction, createPartControl - Entry Point of the
 *               plugin:
 * 
 * This is the setup-entry-point for this plugin. The Eclipse Platform calls
 * this method when it initializes this view.
 * 
 * The following connections between the classes get established here: - A
 * new treeviewer is created for showing the routes later on. - The
 * associated ContentProvider is created and connected to the viewer. - A
 * label provider is created and connected also. - The view-part subscribes
 * to selection listener here (so when the user selects something new we can
 * change our view)
 */
public void createPartControl(Composite parent) {

    fJavaProject = null;

    if (log != null) {
        log("START");
    }

    { // Create TreeViewer
        fViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
        fContentProvider = new TourBusRoutesContentProvider(getViewSite());
        fViewer.setContentProvider(fContentProvider);
        fViewer.setLabelProvider(new TourBusRoutesLabelProvider());
        fViewer.setInput(getViewSite());

        // Selection Handling
        fViewer.addSelectionChangedListener(new ISelectionChangedListener() {

            public void selectionChanged(SelectionChangedEvent event) {
                Object obj = ((IStructuredSelection) event.getSelection()).getFirstElement();

                fActionNextStop.setEnabled(
                        obj instanceof BusStop && fContentProvider.fTourPlan.getNext((BusStop) obj) != null);
                fActionPreviousStop.setEnabled(obj instanceof BusStop
                        && fContentProvider.fTourPlan.getPrevious((BusStop) obj) != null);
            }
        });

        // Drag and Drop Handling
        int ops = DND.DROP_MOVE;

        Transfer[] types = new Transfer[] { LocalSelectionTransfer.getTransfer() };
        de.fu_berlin.inf.jtourbus.utility.ViewerDropAdapter vda = new de.fu_berlin.inf.jtourbus.utility.ViewerDropAdapter(
                fViewer) {

            public boolean performDrop(Object data) {

                IStructuredSelection selection = (IStructuredSelection) LocalSelectionTransfer.getTransfer()
                        .getSelection();

                @SuppressWarnings("unchecked")
                Vector<BusStop> stops = new Vector(selection.toList());

                BusStop target = (BusStop) getCurrentTarget();
                boolean before = getCurrentLocation() == ViewerDropAdapter.LOCATION_BEFORE;

                BusStop other = (before ? fContentProvider.fTourPlan.getPrevious(target)
                        : fContentProvider.fTourPlan.getNext(target));

                double delta;
                if (other == null) {
                    delta = (before ? -1.0 : 1.0);
                } else {
                    delta = (other.getStopNumber() - target.getStopNumber()) / (stops.size() + 1);
                }

                String targetRoute = target.getRoute();
                double targetStopNumber = target.getStopNumber();
                int i = 0;
                for (BusStop stop : stops) {
                    fContentProvider.fTourPlan.remove(stop);
                    stop.setRoute(targetRoute);
                    stop.setStopNumber(targetStopNumber + delta * ++i);
                    fContentProvider.fTourPlan.add(stop);
                }

                double d = 1;
                for (BusStop stop : fContentProvider.fTourPlan.routes.get(targetRoute)) {
                    if (stop.getStopNumber() != d) {
                        stop.setStopNumber(d);
                        if (!stops.contains(stop)) {
                            stops.add(stop);
                        }
                    }
                    d++;
                }

                UpdateStopsOperation op = new UpdateStopsOperation(stops);
                op.run();

                return true;
            }

            public boolean validateDrop(Object target, int operation, TransferData transferType) {
                return target instanceof BusStop;
            }
        };

        // We do care about DND-sorting in this case.
        vda.setFeedbackEnabled(true);

        fViewer.addDropSupport(ops, types, vda);

        fViewer.addDragSupport(ops, types, new DragSourceListener() {
            public void dragStart(DragSourceEvent arg0) {
                IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection();

                arg0.doit = true;
                Iterator<?> i = selection.iterator();
                while (i.hasNext()) {
                    if (!(i.next() instanceof BusStop)) {
                        arg0.doit = false;
                        break;
                    }
                }
            }

            // List<BusStop> stops;

            public void dragSetData(DragSourceEvent arg0) {
                IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection();
                // List<BusStop>stops = (List<BusStop>)selection.toList();
                LocalSelectionTransfer.getTransfer().setSelection(selection);
            }

            public void dragFinished(DragSourceEvent arg0) {
                // Not needed because of incremental update
                // fActionRefresh.run();
            }
        });
    }

    makeActions();
    hookActions(getViewSite().getActionBars().getToolBarManager());
    hookContextMenu();

}

From source file:de.itemis.gmf.runtime.extensions.actions.AddConnectionsAction.java

License:Open Source License

/**
 * @see IActionDelegate#selectionChanged(IAction, ISelection)
 *//*w w w .  jav a2  s .co m*/
public void selectionChanged(IAction action, ISelection selection) {
    action.setEnabled(false);
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        selectedEditParts = new ArrayList<IGraphicalEditPart>();
        for (Iterator<?> i = structuredSelection.iterator(); i.hasNext();) {
            Object selectedElement = i.next();
            if (selectedElement instanceof IGraphicalEditPart) {
                IGraphicalEditPart graphicalEditPart = (IGraphicalEditPart) selectedElement;
                selectedEditParts.add(graphicalEditPart);
                action.setEnabled(true);
            }
        }
    }
}

From source file:de.itemis.gmf.tools.popup.actions.GMFToolsAction.java

License:Open Source License

/**
 * @see IActionDelegate#selectionChanged(IAction, ISelection)
 *//*  w  w  w  . jav  a  2 s  .c om*/
public void selectionChanged(IAction action, ISelection selection) {
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        if (!structuredSelection.isEmpty()) {
            gmfModels.clear();
            List<GmfModel> registeredGmfModels = PreferenceUtil.getGmfModels();
            for (Iterator<?> iter = structuredSelection.iterator(); iter.hasNext();) {
                Object selectedElement = iter.next();
                if (selectedElement instanceof IFile) {
                    IFile selectedFile = (IFile) selectedElement;
                    String fileExtension = selectedFile.getFileExtension();
                    if (!this.fileExtension.equals(fileExtension)) {
                        gmfModels.clear();
                        action.setEnabled(false);
                        return;
                    }
                    for (GmfModel registeredGmfModel : registeredGmfModels) {
                        if (registeredGmfModel.hasFile(selectedFile)) {
                            action.setEnabled(true);
                            gmfModels.add(registeredGmfModel);
                            break;
                        }
                    }
                }
            }
            if (!gmfModels.isEmpty()) {
                return;
            }
        }
    }
    action.setEnabled(false);
}

From source file:de.jcup.egradle.eclipse.ide.launch.EGradleLaunchConfigurationPropertiesTab.java

License:Apache License

private void handlePropertiesRemoveButtonSelected() {
    IStructuredSelection sel = (IStructuredSelection) propertiesTable.getSelection();
    propertiesTable.getControl().setRedraw(false);
    for (@SuppressWarnings("unchecked")
    Iterator<PropertyVariable> i = sel.iterator(); i.hasNext();) {
        PropertyVariable var = i.next();
        propertiesTable.remove(var);
    }//from  ww  w  . jav  a 2 s .  c  om
    propertiesTable.getControl().setRedraw(true);
    updateLaunchConfigurationDialog();
}

From source file:de.jwi.ostendoplugin.views.OstendoView.java

License:Open Source License

private void makeActions() {
    runAction = new Action() {
        public void run() {
            openEditor();//w ww . j a  v  a  2s  .  c  om
        }
    };

    refreshLogdirAction = new Action() {
        public void run() {
            logdirViewer.refresh();
        }
    };
    refreshLogdirAction.setText("Refresh LogDirs");
    refreshLogdirAction.setImageDescriptor(
            AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/refresh.gif"));

    runAction.setText("Run");
    runAction.setToolTipText("Run Ostendo");
    runAction.setImageDescriptor(
            AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/lrun_obj.gif"));
    runAction.setEnabled(false);

    addIDLAction = new Action() {
        public void run() {
            Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();

            FileDialog dialog = new FileDialog(shell, SWT.OPEN);
            String[] filterExtensions = new String[] { "*.idl", "*" };

            dialog.setFilterExtensions(filterExtensions);

            if (plugin.lastIDLDir != null) {
                dialog.setFilterPath(plugin.lastIDLDir);
            }

            String filePath = dialog.open();

            if (filePath != null) {
                plugin.lastIDLDir = new File(filePath).getParent();

                try {
                    addIDL(filePath);
                    upDateIDLViewer();
                } catch (Exception e) {
                    e.printStackTrace();

                    MessageDialog.openInformation(idlViewer.getControl().getShell(), "Error", e.getMessage());

                }
            }
        }
    };
    addIDLAction.setText("Add IDL");
    addIDLAction.setToolTipText("Add IDL");
    addIDLAction.setImageDescriptor(
            AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/add_file_obj.gif"));

    addLogdirAction = new Action() {
        public void run() {
            Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();

            DirectoryDialog dialog = new DirectoryDialog(shell, SWT.OPEN);
            System.out.println("lastLogDir: " + plugin.lastLogDir);
            if (plugin.lastLogDir != null) {
                dialog.setFilterPath(plugin.lastLogDir);
            }
            String path = dialog.open();
            if (path != null) {
                plugin.lastLogDir = path;
                addLogDir(path);
                upDateLogdirViewer();
            }
        }
    };
    addLogdirAction.setText("Add LogDir");
    addLogdirAction.setToolTipText("Add LogDir");
    addLogdirAction.setImageDescriptor(
            AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/add_folder.gif"));

    addFilePatternAction = new Action() {
        public void run() {
            Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();

            String[] s = { "a1", "b2", "c3" };

            ComboInputDialog comboInputDialog = new ComboInputDialog(shell, "tit", "mes", null, s, null);
            comboInputDialog.open();

        }
    };
    addFilePatternAction.setText("Add FilePattern");
    addFilePatternAction.setToolTipText("Add Log File Pattern");

    delIDLAction = new Action() {
        public void run() {
            ISelection selection1 = idlViewer.getSelection();
            IStructuredSelection selection = ((IStructuredSelection) selection1);

            if (!selection.isEmpty()) {
                Iterator it = selection.iterator();
                while (it.hasNext()) {
                    IDL idl = (IDL) it.next();
                    removeIDL(idl);
                }
                upDateIDLViewer();
            }
        }
    };
    delIDLAction.setText("Remove IDL");

    delLogdirAction = new Action() {
        public void run() {
            ISelection selection1 = logdirViewer.getSelection();
            IStructuredSelection selection = ((IStructuredSelection) selection1);

            if (!selection.isEmpty()) {
                Iterator it = selection.iterator();
                while (it.hasNext()) {
                    LoggedMessage file = (LoggedMessage) it.next();
                    removeLogdir(file);
                }
                upDateLogdirViewer();
            }
        }
    };
    delLogdirAction.setText("Remove Log Dir");

    doubleClickAction = new Action() {
        public void run() {
            ISelection selection = idlViewer.getSelection();
            Object obj = ((IStructuredSelection) selection).getFirstElement();
            showMessage("Double-click detected on " + obj.toString());
        }
    };

    helpAction = new Action() {
        public void run() {
            PlatformUI.getWorkbench().getHelpSystem().displayHelp(Activator.PLUGIN_ID + ".help");
        }
    };
    helpAction.setText("Help");

}

From source file:de.loskutov.anyedit.actions.ConvertAllAction.java

License:Open Source License

@Override
public void selectionChanged(IAction action, ISelection selection) {
    selectedFiles.clear();//from   w w  w  .j a v  a 2  s. co m
    selectedResources.clear();
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) selection;
        Iterator<?> iterator = ssel.iterator();
        while (iterator.hasNext()) {
            // by definition in plugin.xml, we are called only on IFile's
            selectedFiles.add((IFile) iterator.next());
        }
    }
}

From source file:de.loskutov.anyedit.actions.ConvertAllInFolderAction.java

License:Open Source License

@Override
public void selectionChanged(IAction action, ISelection selection) {
    selectedFiles.clear();//from   ww  w .  j a  v a  2s . c  om
    selectedResources.clear();
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) selection;
        Iterator<?> iterator = ssel.iterator();
        while (iterator.hasNext()) {
            IResource next = getResource(iterator.next());
            if (next != null) {
                // we do not check the folders for files here, because this could lead to
                // big delays on large (or distributed) directories during select action
                // any one is accepted, file or folder
                selectedResources.add(next);
            }
        }
    }
    if (action != null) {
        action.setEnabled(getEnablement());
    }
}

From source file:de.loskutov.dh.actions.AddToFilterAction.java

License:Open Source License

private List<String> getTypesToFilter(IStructuredSelection selection) {
    if (selection == null) {
        return null;
    }/*from  ww w  .j  av  a  2 s  . co m*/
    List<String> typesToFilter = new ArrayList<String>();
    for (Iterator<?> iterator = selection.iterator(); iterator.hasNext();) {
        Object elt = iterator.next();
        if (elt instanceof TreeElement<?, ?>) {
            elt = ((TreeElement) elt).getJavaElement();
        }
        if (elt instanceof IField) {
            IField field = (IField) elt;
            String resolvedType = SearchUtils.getResolvedType(field);
            if (resolvedType != null) {
                typesToFilter.add(resolvedType);
            }
        } else if (elt instanceof IType) {
            IType type = (IType) elt;
            typesToFilter.add(type.getFullyQualifiedName());
        }
    }
    return typesToFilter;
}

From source file:de.loskutov.dh.SelectionHelper.java

License:Open Source License

public static <V> List<V> getFromSelection(ISelection selection, Class<V> clazz) {
    List<V> list = new ArrayList<V>();
    if (!(selection instanceof IStructuredSelection)) {
        return list;
    }//ww  w.  ja v a2 s  .  c  o m
    IStructuredSelection sel = (IStructuredSelection) selection;
    for (Iterator<?> iterator = sel.iterator(); iterator.hasNext();) {
        V adapter = getAdapter(iterator.next(), clazz);
        if (adapter != null) {
            list.add(adapter);
        }
    }
    return list;
}

From source file:de.loskutov.dh.views.JavaActionsGroup.java

License:Open Source License

static IStructuredSelection getRealSelection(IStructuredSelection selection1) {
    List<Object> elements = new ArrayList<Object>();
    for (Iterator<?> iterator = selection1.iterator(); iterator.hasNext();) {
        Object object = iterator.next();
        if (object instanceof TreeElement<?, ?>) {
            TreeElement<?, ?> element = (TreeElement<?, ?>) object;
            elements.add(element.getData());
        }/*w ww .  j  av a2 s .  com*/
    }
    return new StructuredSelection(elements);
}