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:ac.soton.eventb.atomicitydecomposition.diagram.layout.actions.SquareLayoutAction.java

License:Open Source License

/**
 * Walk the selected objects and creates a new diagram for each visited
 * packages//from   w ww  .  j  a  v a2s  .  co  m
 * 
 * @see IWorkbenchWindowActionDelegate#run
 */
public void run(IAction action) {

    /* Get selection */
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

    // Get selection from the window
    final ISelection selection = window.getSelectionService().getSelection();
    Diagram diagramView = null;

    // get the editing domain
    if (selection instanceof IStructuredSelection) {

        IStructuredSelection structuredSelection = (IStructuredSelection) selection;

        // Walk selection
        for (Iterator i = structuredSelection.iterator(); i.hasNext();) {

            // Try to adapt the selection to a view
            Object selectedObject = i.next();
            if (selectedObject instanceof IAdaptable) {

                // Try to get a View (new notation)
                Object object = ((IAdaptable) selectedObject).getAdapter(View.class);

                diagramView = ((View) object).getDiagram();
            }
        }
    }

    if (diagramView != null) {
        final Diagram diag = diagramView;
        TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(diagramView);
        AbstractEMFOperation operation = new AbstractEMFOperation(ted, KEY_SQUARE_LAYOUT, null) {

            protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {

                LayoutService.getInstance().layout(diag, SquareLayoutProvider.SQUARE_LAYOUT);

                return Status.OK_STATUS;
            }
        };
        try {
            operation.execute(new NullProgressMonitor(), null);
        } catch (Exception e) {
            throw new RuntimeException(e.getCause());
        }
    }
}

From source file:ac.soton.fmusim.components.ui.controls.EditableTableViewerContainer.java

License:Open Source License

/**
 * Contructor./*from  w w w. j  a  v a 2s  . c  o  m*/
 * @param viewer
 */
public EditableTableViewerContainer(TableViewer viewer) {
    super(viewer);

    // swap table parent to composite
    Composite parent = tableViewer.getTable().getParent();
    plate = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(2, false);
    plate.setLayout(layout);
    tableViewer.getTable().setParent(plate);
    tableViewer.getTable().setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));

    // buttons
    Composite buttonPlate = new Composite(plate, SWT.NONE);
    GridLayout buttonLayout = new GridLayout(1, false);
    buttonPlate.setLayout(buttonLayout);

    addButton = new Button(buttonPlate, SWT.PUSH);
    addButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    addButton.setText("Add");
    addButton.addSelectionListener(new SelectionAdapter() {

        @SuppressWarnings("unchecked")
        @Override
        public void widgetSelected(SelectionEvent e) {
            // remember selection
            int idx = tableViewer.getTable().getSelectionIndex();

            // select elements to add
            SelectionDialog selectionDialog = getSelectionDialog();
            //            eventsDialog.setTitle(machine.getName() + " Events");
            //            eventsDialog.setMessage("Please select events for this transition to elaborate");
            if (Dialog.OK == selectionDialog.open()) {
                Object[] result = selectionDialog.getResult();
                if (result != null) {
                    List<Object> newElements = new ArrayList<Object>();
                    for (Object obj : result)
                        newElements.add(obj);

                    Object input = tableViewer.getInput();
                    ((List<Object>) input).addAll(newElements);
                    tableViewer.refresh();

                    // restore selection
                    if (idx >= 0) {
                        tableViewer.getTable().select(idx);
                        tableViewer.getTable().notifyListeners(SWT.Selection,
                                new org.eclipse.swt.widgets.Event());
                    }

                    // notify about the change
                    notifyChanged();
                }
            }
        }
    });

    removeButton = new Button(buttonPlate, SWT.PUSH);
    removeButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    removeButton.setText("Remove");
    removeButton.setEnabled(false);
    removeButton.addSelectionListener(new SelectionAdapter() {

        @SuppressWarnings("rawtypes")
        @Override
        public void widgetSelected(SelectionEvent e) {
            ISelection selection = tableViewer.getSelection();
            if (selection != null && selection instanceof IStructuredSelection) {
                IStructuredSelection sel = (IStructuredSelection) selection;
                List input = (List) tableViewer.getInput();
                for (Iterator it = sel.iterator(); it.hasNext();) {
                    Object element = it.next();
                    input.remove(element);
                }

                // refresh table and buttons
                tableViewer.refresh();
                removeButton.setEnabled(false);

                // notify about the change
                notifyChanged();
            }
        }
    });

    // button enabling behaviour based on the table state
    tableViewer.getTable().addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            removeButton.setEnabled(true);
        }
    });

    buttonPlate.setLayoutData(new GridData(GridData.FILL_VERTICAL));

    enabled = true;
}

From source file:ac.soton.multisim.ui.wizards.controls.EditableTableViewerContainer.java

License:Open Source License

/**
 * Contructor.// ww  w .j  a va 2 s .c o  m
 * @param viewer
 */
public EditableTableViewerContainer(TableViewer viewer) {
    super(viewer);

    // swap table parent to composite
    Composite parent = tableViewer.getTable().getParent();
    plate = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(2, false);
    plate.setLayout(layout);
    tableViewer.getTable().setParent(plate);
    tableViewer.getTable().setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));

    // buttons
    Composite buttonPlate = new Composite(plate, SWT.NONE);
    GridLayout buttonLayout = new GridLayout(1, false);
    buttonPlate.setLayout(buttonLayout);

    addButton = new Button(buttonPlate, SWT.PUSH);
    addButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    addButton.setText("Add");
    addButton.addSelectionListener(new SelectionAdapter() {

        @SuppressWarnings("unchecked")
        @Override
        public void widgetSelected(SelectionEvent e) {
            SelectionDialog selectionDialog = getSelectionDialog();
            if (Dialog.OK == selectionDialog.open()) {
                Object[] result = selectionDialog.getResult();
                if (result != null) {
                    List<Object> newElements = new ArrayList<Object>();
                    for (Object obj : result)
                        newElements.add(obj);

                    // remember selection
                    int idx = tableViewer.getTable().getSelectionIndex();

                    Object input = tableViewer.getInput();
                    ((List<Object>) input).addAll(newElements);
                    tableViewer.refresh();

                    // restore selection
                    if (idx >= 0) {
                        tableViewer.getTable().select(idx);
                        tableViewer.getTable().notifyListeners(SWT.Selection,
                                new org.eclipse.swt.widgets.Event());
                    }

                    // notify about the change
                    notifyChanged();
                }
            }
        }
    });

    removeButton = new Button(buttonPlate, SWT.PUSH);
    removeButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    removeButton.setText("Remove");
    removeButton.setEnabled(false);
    removeButton.addSelectionListener(new SelectionAdapter() {

        @SuppressWarnings("rawtypes")
        @Override
        public void widgetSelected(SelectionEvent e) {
            ISelection selection = tableViewer.getSelection();
            if (selection != null && selection instanceof IStructuredSelection) {
                IStructuredSelection sel = (IStructuredSelection) selection;
                List input = (List) tableViewer.getInput();
                for (Iterator it = sel.iterator(); it.hasNext();) {
                    Object element = it.next();
                    input.remove(element);
                }

                // refresh table and buttons
                tableViewer.refresh();
                removeButton.setEnabled(false);

                // notify about the change
                notifyChanged();
            }
        }
    });

    // button enabling behaviour based on the table state
    tableViewer.getTable().addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            removeButton.setEnabled(true);
        }
    });

    buttonPlate.setLayoutData(new GridData(GridData.FILL_VERTICAL));

    enabled = true;
}

From source file:alma.acs.eventbrowser.parts.EventDetailPart.java

License:Open Source License

@PostConstruct
public void createPartControl(Composite parent, EMenuService menuService) {
    try {/*from www  .j a v  a2  s . co m*/
        em = EventModel.getInstance();
    } catch (Throwable thr) {
        thr.printStackTrace();
        IStatus someStatus = statusReporter.newStatus(IStatus.ERROR, "Connection with NCs failed.", thr);
        statusReporter.report(someStatus, StatusReporter.SHOW);
        throw new RuntimeException(thr);
    }
    logger = em.getLogger();

    viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

    Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    GridLayout gridLayout = new GridLayout();
    gridLayout.marginHeight = 0;
    gridLayout.marginWidth = 0;
    gridLayout.verticalSpacing = 0;
    parent.setLayout(gridLayout);

    TableViewerColumn tvcol = new TableViewerColumn(viewer, SWT.NONE, 0);
    tvcol.setLabelProvider(new DetailNameLabelProvider());
    TableColumn col = tvcol.getColumn();
    col.setText("Name");
    col.setWidth(200);
    col.setAlignment(SWT.LEFT);

    tvcol = new TableViewerColumn(viewer, SWT.NONE, 1);
    tvcol.setLabelProvider(new DetailTypeLabelProvider());
    col = tvcol.getColumn();
    col.setText("Type");
    col.setWidth(100);
    col.setAlignment(SWT.LEFT);

    tvcol = new TableViewerColumn(viewer, SWT.NONE, 2);
    tvcol.setLabelProvider(new DetailValueLabelProvider());
    col = tvcol.getColumn();
    col.setText("Value");
    col.setWidth(200);
    col.setAlignment(SWT.LEFT);

    GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getTable());

    viewer.setContentProvider(new DetailContentProvider());

    hookContextMenu(menuService);

    // Attach a selection listener to our table, which will post selections to the ESelectionService
    // to be processed by CopyDetailsToClipboardHandler
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();

            List<ParsedAnyData> parsedAnyList = new ArrayList<ParsedAnyData>();
            for (Iterator<?> iterator = selection.iterator(); iterator.hasNext();) {
                ParsedAnyData parsedAny = (ParsedAnyData) iterator.next();
                parsedAnyList.add(parsedAny);
            }
            selectionService.setSelection(parsedAnyList.toArray(new ParsedAnyData[0]));
        }
    });
}

From source file:alma.acs.eventbrowser.views.EventDetailTestHarness.java

License:Open Source License

public EventDetailTestHarness(Shell shell) {
    final TableViewer v = new TableViewer(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

    viewer = v;//from ww  w.j ava  2  s  . co m

    Table table = v.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    GridLayout gridLayout = new GridLayout();
    gridLayout.marginHeight = 0;
    gridLayout.marginWidth = 0;
    gridLayout.verticalSpacing = 0;
    shell.setLayout(gridLayout);

    TableViewerColumn tvcol = new TableViewerColumn(v, SWT.NONE, 0);
    tvcol.setLabelProvider(new DetailNameLabelProvider());
    TableColumn col = tvcol.getColumn();
    col.setText("Name");
    col.setWidth(180);
    col.setAlignment(SWT.LEFT);

    tvcol = new TableViewerColumn(v, SWT.NONE, 1);
    tvcol.setLabelProvider(new DetailTypeLabelProvider());
    col = tvcol.getColumn();
    col.setText("Type");
    col.setWidth(100);
    col.setAlignment(SWT.LEFT);

    tvcol = new TableViewerColumn(v, SWT.NONE, 2);
    tvcol.setLabelProvider(new DetailValueLabelProvider());
    col = tvcol.getColumn();
    col.setText("Value");
    col.setWidth(50);
    col.setAlignment(SWT.LEFT);

    GridDataFactory.fillDefaults().grab(true, true).applyTo(v.getTable());

    v.setContentProvider(new MyContentProvider());
    ParsedAnyData[] model = createModel();
    v.setInput(model);
    v.getTable().setLinesVisible(true);

    Menu popUpMenu = new Menu(shell, SWT.POP_UP);
    viewer.getControl().setMenu(popUpMenu);
    MenuItem copyItem = new MenuItem(popUpMenu, SWT.PUSH);
    copyItem.setText("Copy");
    cb = new Clipboard(Display.getDefault());
    copyItem.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            copySelection();

        }

        @SuppressWarnings("unchecked")
        private void copySelection() {
            List<ParsedAnyData> personList = new ArrayList<ParsedAnyData>();
            ISelection selection = viewer.getSelection();
            if (selection != null && selection instanceof IStructuredSelection) {
                IStructuredSelection sel = (IStructuredSelection) selection;
                for (Iterator iterator = sel.iterator(); iterator.hasNext();) {
                    ParsedAnyData person = (ParsedAnyData) iterator.next();
                    personList.add(person);
                }
            } else
                return;
            StringBuilder sb = new StringBuilder();
            for (ParsedAnyData person : personList) {
                sb.append(parsedAnyDataToString(person));
            }
            TextTransfer textTransfer = TextTransfer.getInstance();
            cb.setContents(new Object[] { sb.toString() }, new Transfer[] { textTransfer });
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }

        private String parsedAnyDataToString(ParsedAnyData person) {
            return person.getName() + "\t" + person.getType() + "\t" + person.getValue()
                    + System.getProperty("line.separator");
        }
    });
}

From source file:ar.com.tadp.xml.rinzo.jdt.wizards.BindingFilesWizardPage.java

License:Open Source License

public void createControl(Composite root) {
    Composite parent = new Composite(root, NONE);
    GridLayout gridLayout = new GridLayout(2, false);
    parent.setLayout(gridLayout);/*from  ww  w . ja  va2s.co m*/
    parent.setLayoutData(new GridData(GridData.FILL_BOTH));

    this.listViewer = new ListViewer(parent);
    GridData layoutData = new GridData(GridData.FILL_BOTH);
    layoutData.grabExcessHorizontalSpace = true;
    this.listViewer.getControl().setLayoutData(layoutData);
    this.listViewer.setContentProvider(new TableViewerSupport.ListContentProvider());
    this.listViewer.setInput(this.files);

    Composite buttonParent = new Composite(parent, SWT.NULL);
    FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
    fillLayout.spacing = 2;
    buttonParent.setLayout(fillLayout);
    Button buttonAdd = new Button(buttonParent, SWT.PUSH);
    buttonAdd.setText("Add");
    Button buttonRemove = new Button(buttonParent, SWT.PUSH);
    buttonRemove.setText("Remove");

    buttonAdd.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            // ElementTreeSelectionDialog selectionDialog = new
            // ElementTreeSelectionDialog(null, new
            // WorkbenchLabelProvider(), new WorkbenchContentProvider());
            // selectionDialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
            // selectionDialog.open();

            FilteredElementTreeSelectionDialog dialog = new FilteredElementTreeSelectionDialog(null,
                    new WorkbenchLabelProvider(), new WorkbenchContentProvider());
            dialog.setTitle("Binding files selection");
            dialog.setMessage("Select the files to be used as JAXB binding files.");
            dialog.setInitialFilter("*.xjb");
            dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
            dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
            ArrayList<IResource> usedJars = new ArrayList<IResource>();
            dialog.addFilter(new ArchiveFileFilter(usedJars, true, true));
            if (dialog.open() == Window.OK) {
                Object[] elements = dialog.getResult();
                IPath[] res = new IPath[elements.length];
                for (int i = 0; i < res.length; i++) {
                    IResource elem = (IResource) elements[i];
                    files.add(elem.getLocation().toOSString());
                }
            }
            listViewer.refresh(false);
        }
    });

    buttonRemove.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = (IStructuredSelection) listViewer.getSelection();
            for (Iterator iterator = selection.iterator(); iterator.hasNext();) {
                String element = (String) iterator.next();
                files.remove(element);
            }
            listViewer.refresh(false);
        }
    });

    this.setControl(parent);
}

From source file:aspectminingtool.views.FanInSeeds.ViewPartFanInSeeds.java

License:Open Source License

/**
 * Create the actions.//from  www . j  av a  2  s .  co m
 */
public void createActions() {
    openItemActionMethodsTable = new Action("Open") {
        public void run() {
            IStructuredSelection sel = (IStructuredSelection) tableViewerLeft.getSelection();
            Iterator iter = sel.iterator();
            while (iter.hasNext()) {
                MethodDescription methodDescription = (MethodDescription) iter.next();
                if (methodDescription != null) {
                    String id = methodDescription.getMethod().getClass_id();
                    openResource(id);
                }

            }
        }
    };

    deleteAction = new Action("Delete") {
        public void run() {
            IStructuredSelection sel = (IStructuredSelection) tableViewerLeft.getSelection();
            Iterator iter = sel.iterator();
            while (iter.hasNext()) {
                MethodDescription methodDescription = (MethodDescription) iter.next();
                if (methodDescription != null) {
                    ((SeedsModel) model).removeMethodDescription(methodDescription);
                }

            }

        }
    };

    selectAllActionMethodsTable = new Action("Select All") {
        public void run() {
            selectAll(tableViewerLeft);
        }
    };

    openItemActionCallsTable = new Action("Open") {
        public void run() {
            IStructuredSelection sel = (IStructuredSelection) tableViewerRight.getSelection();
            Iterator iter = sel.iterator();
            while (iter.hasNext()) {
                Call_Counted callCounted = (Call_Counted) iter.next();
                String name = callCounted.getCaller_id();
                int index = name.indexOf("//");
                name = name.substring(0, index);
                openResource(name);

            }
        }
    };

    selectAllActionCallsTable = new Action("Select All") {
        public void run() {
            selectAll(tableViewerRight);
        }
    };

    // Add selection listener.
    tableViewerLeft.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection sel = (IStructuredSelection) tableViewerLeft.getSelection();
            openItemActionMethodsTable.setEnabled(sel.size() > 0);
            selectAllActionMethodsTable.setEnabled(sel.size() > 0);
            deleteAction.setEnabled(sel.size() > 0);
        }
    });

    tableViewerRight.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection sel = (IStructuredSelection) tableViewerLeft.getSelection();
            selectAllActionCallsTable.setEnabled(sel.size() > 0);
            openItemActionCallsTable.setEnabled(sel.size() > 0);
        }
    });

}

From source file:aspectminingtool.views.RedirectorFinderSeeds.ViewPartClassesSeeds.java

License:Open Source License

/**
 * Create the actions.//from www  . jav  a  2s . c  o  m
 */
public void createActionsTableLeft() {

    selectAllActionsLeft = new SelectAllAction(tableViewerLeft);

    deleteAction = new Action("Delete") {
        public void run() {
            IStructuredSelection sel = (IStructuredSelection) tableViewerLeft.getSelection();
            Iterator iter = sel.iterator();
            while (iter.hasNext()) {
                SeedClassDescription classDescription = (SeedClassDescription) iter.next();
                if (classDescription != null) {
                    ((SeedsClassGeneralModel) model).removeMethodDescription(classDescription);
                }

            }

        }
    };

    selectAllActionsRight = new SelectAllAction(tableViewerRight);

    // Add selection listener.
    tableViewerLeft.addSelectionChangedListener(new MenuLeftChangeListener(tableViewerLeft,
            selectAllActionsLeft, openClassActionTableL, deleteAction));

}

From source file:aspectminingtool.views.SeedsGeneral.ViewPartSeeds.java

License:Open Source License

/**
 * Create the actions./*from  w w  w .  ja  v  a2s . c om*/
 */
public void createActionsTableLeft() {

    selectAllActionsLeft = new SelectAllAction(tableViewerLeft);

    deleteAction = new Action("Delete") {
        public void run() {
            IStructuredSelection sel = (IStructuredSelection) tableViewerLeft.getSelection();
            Iterator iter = sel.iterator();
            while (iter.hasNext()) {
                SeedDescription methodDescription = (SeedDescription) iter.next();
                if (methodDescription != null) {
                    ((SeedsGeneralModel) model).removeMethodDescription(methodDescription);
                }

            }

        }
    };

    selectAllActionsRight = new SelectAllAction(tableViewerRight);

    // Add selection listener.
    tableViewerLeft.addSelectionChangedListener(new MenuLeftChangeListener(tableViewerLeft,
            selectAllActionsLeft, openClassActionTableL, deleteAction));

}

From source file:aspectminingtool.views.Sinergia.Seeds.ViewPartSinergiaSeedsDesc.java

License:Open Source License

/**
 * Create the actions.// w ww .  j av a 2 s.c o  m
 */
public void createActionsTableLeft() {

    selectAllTableLeft = new SelectAllAction(tableViewerLeft);

    deleteAction = new Action("Delete") {
        public void run() {
            IStructuredSelection sel = (IStructuredSelection) tableViewerLeft.getSelection();
            Iterator iter = sel.iterator();
            while (iter.hasNext()) {
                SeedDescription methodDescription = (SeedDescription) iter.next();
                if (methodDescription != null) {
                    ((SinergiaDescriptionResultsModel) model).removeMethodDescription(methodDescription);
                }

            }

        }
    };

    // Add selection listener.
    tableViewerLeft.addSelectionChangedListener(
            new MenuLeftChangeListener(tableViewerLeft, selectAllTableLeft, openActionTableLeft, deleteAction));
}