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

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

Introduction

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

Prototype

public List toList();

Source Link

Document

Returns the elements in this selection as a List.

Usage

From source file:gov.redhawk.sca.internal.ui.handlers.RefreshHandler.java

License:Open Source License

/**
 * {@inheritDoc}//from w  w  w.  ja  v  a 2s .c o m
 */
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    ISelection selection = HandlerUtil.getActiveMenuSelection(event);
    if (selection == null) {
        selection = HandlerUtil.getCurrentSelection(event);
    }
    if (selection instanceof IStructuredSelection) {
        final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        final List<IRefreshable> refreshList = new ArrayList<IRefreshable>();
        for (final Object obj : structuredSelection.toList()) {
            final IRefreshable dataObj = PluginUtil.adapt(IRefreshable.class, obj, true);
            if (dataObj != null) {
                refreshList.add(dataObj);
            }
        }
        final int size = refreshList.size();
        if (size > 0) {
            final IProgressMonitor refreshProgressGroup = Job.getJobManager().createProgressGroup();
            final IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
            refreshProgressGroup.beginTask("Refreshing elements...", size);
            final ScaItemProviderAdapterFactory adapterFactory = new ScaItemProviderAdapterFactory();
            final Display display = HandlerUtil.getActiveShell(event).getDisplay();
            for (final IRefreshable dataObj : refreshList) {
                final IItemLabelProvider lp = (IItemLabelProvider) adapterFactory.adapt(dataObj,
                        IItemLabelProvider.class);
                String label = "";
                if (lp != null) {
                    label = " " + lp.getText(dataObj);
                }
                final Job job = new Job("Refreshing" + label) {

                    @Override
                    protected IStatus run(final IProgressMonitor monitor) {
                        try {
                            dataObj.refresh(monitor, RefreshDepth.CHILDREN);
                            final WorkbenchJob uiJob = new WorkbenchJob(display, "Refresh viewer") {

                                @Override
                                public IStatus runInUIThread(final IProgressMonitor monitor) {
                                    if (activePart instanceof CommonNavigator) {
                                        final CommonNavigator navigator = (CommonNavigator) activePart;
                                        navigator.getCommonViewer().refresh(structuredSelection.toArray());
                                    }
                                    refreshProgressGroup.done();
                                    return Status.OK_STATUS;
                                }
                            };
                            uiJob.setPriority(Job.INTERACTIVE);
                            uiJob.setUser(false);
                            uiJob.schedule();
                        } catch (final InterruptedException e) {
                            return Status.CANCEL_STATUS;
                        }
                        return Status.OK_STATUS;
                    }

                    @Override
                    protected void canceling() {
                        getThread().interrupt();
                        super.canceling();
                    }
                };
                job.setProgressGroup(refreshProgressGroup, 1);
                job.setUser(true);
                job.schedule();
            }
            adapterFactory.dispose();
        }
    }
    return null;
}

From source file:gov.redhawk.sca.internal.ui.handlers.UninstallApplication.java

License:Open Source License

/**
 * {@inheritDoc}/* w  w  w. j a  va  2 s. c om*/
 */

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    ISelection sel = HandlerUtil.getActiveMenuSelection(event);
    if (sel == null) {
        sel = HandlerUtil.getCurrentSelection(event);
    }
    if (sel instanceof IStructuredSelection) {
        final IStructuredSelection ss = (IStructuredSelection) sel;
        final List<ScaWaveformFactory> waveforms = new ArrayList<ScaWaveformFactory>();
        for (final Object element : ss.toList()) {
            if (element instanceof ScaWaveformFactory) {
                waveforms.add((ScaWaveformFactory) element);
            }
        }
        final IProgressMonitor progressGroup = Job.getJobManager().createProgressGroup();
        final int size = waveforms.size();
        progressGroup.beginTask("Uninstalling Waveform factories", size);
        final JobChangeAdapter adapter = new JobChangeAdapter() {
            private final AtomicInteger jobList = new AtomicInteger(size);

            @Override
            public void done(final IJobChangeEvent event) {
                final int num = this.jobList.decrementAndGet();
                if (num <= 0) {
                    progressGroup.done();
                }
            }
        };

        for (final ScaWaveformFactory swf : waveforms) {
            final Job job = new Job("Uninstalling Application Job") {

                @Override
                protected IStatus run(final IProgressMonitor monitor) {
                    try {
                        monitor.beginTask("Uninstalling application: " + swf.getName(),
                                IProgressMonitor.UNKNOWN);
                        if (swf.getDomMgr() != null) {
                            swf.getDomMgr().uninstallScaWaveformFactory(swf);
                        }
                    } catch (final InvalidIdentifier e) {
                        return new Status(IStatus.ERROR, ScaUiPlugin.PLUGIN_ID,
                                "Failed to uninstall application.", e);
                    } catch (final ApplicationUninstallationError e) {
                        return new Status(IStatus.ERROR, ScaUiPlugin.PLUGIN_ID,
                                "Failed to uninstall application.", e);
                    } catch (final SystemException e) {
                        return new Status(IStatus.ERROR, ScaUiPlugin.PLUGIN_ID,
                                "Failed to uninstall application.", e);
                    } finally {
                        monitor.done();
                    }
                    return Status.OK_STATUS;
                }

            };
            job.addJobChangeListener(adapter);
            job.setProgressGroup(progressGroup, 1);
            job.schedule();
        }
    }
    return null;
}

From source file:gov.redhawk.sca.ui.actions.ResourceDropAdapterAssistant.java

License:Open Source License

private IStatus handleSelectionDrop(final ScaFileStore store) {
    final ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
    if (selection instanceof IStructuredSelection) {
        final IStructuredSelection ss = (IStructuredSelection) selection;
        final List<IResource> resources = new ArrayList<IResource>();
        final List<ScaFileStore> fileStores = new ArrayList<ScaFileStore>();
        for (final Object obj : ss.toList()) {
            if (obj instanceof IResource) {
                resources.add((IResource) obj);
            } else if (obj instanceof ScaFileStore) {
                fileStores.add((ScaFileStore) obj);
            }/*ww  w . j av  a 2  s .co m*/
        }
        final Job job = new UploadJob(store, resources.toArray(new IResource[resources.size()]), new String[0],
                fileStores.toArray(new ScaFileStore[fileStores.size()]));
        job.schedule();
    }
    return Status.OK_STATUS;
}

From source file:gov.redhawk.sca.ui.ScaPropertiesViewer.java

License:Open Source License

private void createActions() {
    revertAction = new Action("Revert to default") {
        @Override//from w w  w .j a  va  2s .  c  o  m
        public void run() {
            final IStructuredSelection ss = (IStructuredSelection) viewer.getSelection();
            for (final Object o : ss.toList()) {
                if (o instanceof ScaAbstractProperty<?>) {
                    final ScaAbstractProperty<?> prop = (ScaAbstractProperty<?>) o;
                    ScaModelCommand.execute(prop, new ScaModelCommand() {
                        @Override
                        public void execute() {
                            prop.restoreDefaultValue();
                        }
                    });
                }
            }
            viewer.refresh();
            viewer.setSelection(viewer.getSelection());
        }
    };
    revertAction.setImageDescriptor(
            PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_UNDO));
    revertAction.setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
            .getImageDescriptor(ISharedImages.IMG_TOOL_UNDO_DISABLED));

    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(final SelectionChangedEvent event) {
            if (event.getSelection() instanceof IStructuredSelection) {
                final IStructuredSelection ss = (IStructuredSelection) event.getSelection();
                boolean enabled = true;
                for (final Object o : ss.toList()) {
                    if (o instanceof ScaAbstractProperty<?>) {
                        final ScaAbstractProperty<?> prop = (ScaAbstractProperty<?>) o;
                        enabled = !prop.isDefaultValue() && ModelUtil.isSettable(prop);
                    } else {
                        enabled = false;
                    }
                    if (!enabled) {
                        break;
                    }
                }
                revertAction.setEnabled(enabled);
            }
        }
    });
}

From source file:gov.redhawk.ui.port.handlers.PlayUsesPortHandler.java

License:Open Source License

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    // Get the currently selected item
    final ISelection sel = HandlerUtil.getActiveMenuSelection(event);
    // Get the filter parameter for the command
    final String var = event.getParameter(PlayUsesPortHandler.FILTER_PARAM);
    // Check if we should send each play command individually or as a group
    final boolean sendList = Boolean.parseBoolean(event.getParameter(PlayUsesPortHandler.LIST_PARAM));
    // Get the ports variable for the command - this is only used via programmatic command execution
    final Object ports = HandlerUtil.getVariable(event, PlayUsesPortHandler.PORTS_VAR);

    String message = null;//from  w  ww .  j  a  va  2 s .  c o m
    final List<ScaUsesPort> portList = new ArrayList<ScaUsesPort>();

    // First, check if we're called from a menu
    if ((sel != null) && (sel instanceof IStructuredSelection)) {
        final IStructuredSelection ss = (IStructuredSelection) sel;
        for (final Object element : ss.toList()) {
            if (element instanceof ScaUsesPort) {
                final ScaUsesPort port = (ScaUsesPort) element;
                portList.add(port);
            } else {
                final Object obj = Platform.getAdapterManager().getAdapter(element, ScaUsesPort.class);
                if (obj instanceof ScaUsesPort) {
                    portList.add((ScaUsesPort) obj);
                }
            }
        }

        // If it's called programmatically, ports should be a list of ScaPort objects
    } else if ((ports != null) && (ports instanceof List)) {
        for (final Object element : (List<?>) ports) {
            if (element instanceof ScaUsesPort) {
                final ScaUsesPort port = ((ScaUsesPort) element);
                portList.add(port);
            } else {
                final Object obj = Platform.getAdapterManager().getAdapter(element, ScaUsesPort.class);
                if (obj instanceof ScaUsesPort) {
                    portList.add((ScaUsesPort) obj);
                }
            }
        }
    } else {
        message = "Unable to determine what to play";
    }

    // Check if we found any ports to play
    if (portList.size() > 0) {
        message = playPort(var, portList, sendList);
    }

    // Any errors will be put into message
    if (message != null) {
        MessageDialog.openInformation(HandlerUtil.getActiveShell(event), "Unable to Play", message);
    }
    return null;
}

From source file:gov.redhawk.ui.port.handlers.PlotUsesPortHandler.java

License:Open Source License

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    // Get the currently selected item
    final ISelection sel = HandlerUtil.getActiveMenuSelection(event);
    // Get the filter parameter for the command
    final String var = event.getParameter(PlotUsesPortHandler.FILTER_PARAM);
    // Check if we should send each play command individually or as a group
    final boolean sendList = Boolean.parseBoolean(event.getParameter(PlotUsesPortHandler.LIST_PARAM));
    // Get the ports variable for the command - this is only used via programmatic command execution
    final Object ports = HandlerUtil.getVariable(event, PlotUsesPortHandler.PORTS_VAR);

    String message = null;//w  ww  .  jav a  2  s.c o m
    final List<ScaUsesPort> portList = new ArrayList<ScaUsesPort>();

    // First, check if we're called from a menu
    if ((sel != null) && (sel instanceof IStructuredSelection)) {
        final IStructuredSelection ss = (IStructuredSelection) sel;
        for (final Object element : ss.toList()) {
            if (element instanceof ScaUsesPort) {
                final ScaUsesPort port = ((ScaUsesPort) element);
                portList.add(port);
            } else {
                final Object obj = Platform.getAdapterManager().getAdapter(element, ScaUsesPort.class);
                if (obj instanceof ScaUsesPort) {
                    portList.add((ScaUsesPort) obj);
                }
            }
        }

        // If it's called programmatically, ports should be a list of ScaPort objects
    } else if ((ports != null) && (ports instanceof List)) {
        for (final Object element : (List<?>) ports) {
            if (element instanceof ScaUsesPort) {
                final ScaUsesPort port = ((ScaUsesPort) element);
                portList.add(port);
            } else {
                final Object obj = Platform.getAdapterManager().getAdapter(element, ScaUsesPort.class);
                if (obj instanceof ScaUsesPort) {
                    portList.add((ScaUsesPort) obj);
                }
            }
        }
    } else {
        message = "Unable to determine what to plot";
    }

    // Check if we found any ports to play
    if (portList.size() > 0) {
        message = plotPort(var, portList, sendList);
    }

    // Any errors will be put into message
    if (message != null) {
        MessageDialog.openInformation(HandlerUtil.getActiveShell(event), "Unable to Display", message);
    }
    return null;
}

From source file:gov.redhawk.ui.port.playaudio.internal.handlers.PlayPortHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveMenuSelection(event);
    if (selection == null) {
        selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
    }/* w  w  w. j  ava  2 s  .c o  m*/
    if (selection != null) {
        List<?> elements = selection.toList();
        final List<ScaUsesPort> ports = new ArrayList<ScaUsesPort>();
        for (Object obj : elements) {
            ScaUsesPort port = PluginUtil.adapt(ScaUsesPort.class, obj);
            if (port != null) {
                ports.add(port);
            }
        }
        if (!ports.isEmpty()) {
            Activator.getDefault().playPorts(ports);
        }
    }
    return null;
}

From source file:gov.redhawk.ui.views.internal.monitor.handler.MonitorPortHandler.java

License:Open Source License

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (selection instanceof IStructuredSelection) {
        final IStructuredSelection ss = (IStructuredSelection) selection;
        final IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
        try {/*from w  w  w. j a  v  a 2 s . co m*/
            final PortMonitorView view = (PortMonitorView) window.getActivePage().showView(PortMonitorView.ID);
            for (final Object obj : ss.toList()) {
                final ScaPort<?, ?> port = PluginUtil.adapt(ScaPort.class, obj, true);
                if (port != null) {
                    view.addMonitor(port);
                }
            }
        } catch (final PartInitException e) {
            PortsEditPlugin.getPlugin().getLog().log(new Status(e.getStatus().getSeverity(),
                    "gov.redhawk.ui.views.monitor.ports", e.getLocalizedMessage(), e));
        }

    }
    // TODO Auto-generated method stub
    return null;
}

From source file:gov.redhawk.ui.views.internal.monitor.handler.MonitorPortSupplierHandler.java

License:Open Source License

/**
 * {@inheritDoc}//  www  .  j  a v a 2  s .  c  o m
 */
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (selection instanceof IStructuredSelection) {
        final IStructuredSelection ss = (IStructuredSelection) selection;
        final IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
        try {
            final PortMonitorView view = (PortMonitorView) window.getActivePage().showView(PortMonitorView.ID);
            for (final Object obj : ss.toList()) {
                final ScaPortContainer scaObj = PluginUtil.adapt(ScaPortContainer.class, obj, true);
                if (scaObj != null) {
                    if (scaObj.isSetPorts()) {
                        view.addMonitor(scaObj);
                    } else {
                        final Job fetchPorts = new Job("Fetching Ports") {

                            @Override
                            protected IStatus run(final IProgressMonitor monitor) {
                                scaObj.fetchPorts(monitor);
                                return Status.OK_STATUS;
                            }

                        };
                        fetchPorts.schedule();
                        view.addMonitor(scaObj);
                    }

                }
            }
        } catch (final PartInitException e) {
            PortsEditPlugin.getPlugin().getLog().log(new Status(e.getStatus().getSeverity(),
                    "gov.redhawk.ui.views.monitor.ports", e.getLocalizedMessage(), e));
        }

    }
    // TODO Auto-generated method stub
    return null;
}

From source file:gov.redhawk.ui.views.internal.monitor.handler.RefreshHandler.java

License:Open Source License

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (selection instanceof IStructuredSelection) {
        final IStructuredSelection ss = (IStructuredSelection) selection;
        for (final Object obj : ss.toList()) {
            if (obj instanceof Monitor) {
                refresh((Monitor) obj);
            } else if (obj instanceof PortConnectionMonitor) {
                PortConnectionMonitor child = (PortConnectionMonitor) obj;
                EObject container = child.eContainer();
                if (container instanceof Monitor) {
                    refresh((Monitor) container);
                }/*w w  w . j av  a2  s.c  om*/
            }
        }
    }
    return null;
}