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.topicmapslab.tmcledit.model.dialogs.TopicSelectionDialog.java

License:Open Source License

private void removeSelection() {
    IStructuredSelection sel = (IStructuredSelection) selectedTopicList.getSelection();
    for (Iterator it = sel.iterator(); it.hasNext();) {
        selectedTopics.remove((TopicType) it.next());
    }//from  w w  w.  j a va2 s  . com
    validate();
    availableTopicList.refresh();
    selectedTopicList.refresh();

}

From source file:de.topicmapslab.tmcledit.model.views.ModelView.java

License:Open Source License

/**
 * This is a callback that will allow us to create the viewer and initialize
 * it./*  w  w  w  . java  2s . c  o m*/
 */
@Override
public void createPartControl(Composite parent) {
    viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

    contentProvider = new ModelViewContentProvider(this);
    viewer.setContentProvider(contentProvider);
    viewer.setLabelProvider(new ViewLabelProvider());
    viewer.setSorter(new NameSorter());

    viewer.setInput(getViewSite());
    viewer.expandToLevel(2);
    // Create the help context id for the viewer's control
    PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(),
            "de.topicmapslab.tmcledit.extensions.viewer");
    makeActions();
    hookContextMenu();
    hookDoubleClickAction();
    hookKeyListener();
    contributeToActionBars();

    getSite().setSelectionProvider(this);

    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @SuppressWarnings("unchecked")
        public void selectionChanged(SelectionChangedEvent event) {
            if (currFile == null) {
                currentSelection = new StructuredSelection();
                return;
            }

            IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();

            // notify all selectionListeners in this 
            fireSelectionChanged(sel);
            // getting element for selection service

            if (sel.isEmpty()) {
                currentSelection = new StructuredSelection(currFile);
                createTopicAction.setEnabled(false);
            } else {

                AbstractModelViewNode to = (AbstractModelViewNode) sel.getFirstElement();
                createTopicAction.setEnabled(false);
                createDiagramAction.setEnabled(false);
                createDomainDiagramAction.setEnabled(false);
                switch (to.getId()) {
                case TreeObject.DIAGRAMS:
                    createDiagramAction.setEnabled(true);
                    createDomainDiagramAction.setEnabled(true);
                    break;
                }

                if (to instanceof TreeObject) {
                    createTopicAction.setKindOfTopicType(((TreeObject) to).getKindOfTopicType());
                    if (to.getModel() == null)
                        currentSelection = new StructuredSelection(currFile);
                }
                Iterator<Object> it = sel.iterator();
                List<Object> list = new ArrayList<Object>();
                while (it.hasNext()) {
                    to = (AbstractModelViewNode) it.next();
                    if (to.getModel() != null)
                        list.add((Object) to.getModel());
                }
                currentSelection = new StructuredSelection(list);
            }
            TmcleditEditPlugin.getPlugin().getOnotoaSelectionService().setSelection(currentSelection,
                    ModelView.this);
        }

    });
    initDragAndDrop();
}

From source file:de.topicmapslab.tmcledit.model.views.ModelView.java

License:Open Source License

private void initDragAndDrop() {
    DragSource dragSource = new DragSource(viewer.getTree(), DND.DROP_COPY);
    dragSource.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    dragSource.addDragListener(new DragSourceAdapter() {

        @Override/*  w  ww .j  av a  2  s . c o m*/
        public void dragStart(DragSourceEvent event) {

            IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
            StringBuffer tmp = prepareTopicTypes(sel);

            if (tmp.length() > 0) {
                event.doit = true;
                event.data = tmp.toString();
            } else {
                event.doit = false;
            }

        }

        @Override
        public void dragSetData(DragSourceEvent event) {
            if (viewer == null)
                return;

            IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();

            StringBuffer tmp = prepareTopicTypes(sel);
            if (tmp.length() > 0) {
                event.data = tmp.toString();
            } else {
                event.data = null;
            }
        }

        @SuppressWarnings("unchecked")
        private StringBuffer prepareTopicTypes(IStructuredSelection sel) {
            // concat every model string and ignore other nodes..
            StringBuffer tmp = new StringBuffer();

            if (sel.size() == 1) {
                TreeObject selObj = (TreeObject) sel.getFirstElement();

                if ((selObj.getModel() instanceof AssociationTypeConstraint)
                        || (selObj.getModel() instanceof TopicType)) {
                    tmp.append(selObj.getModel().toString());
                }
            } else {
                Iterator<Object> it = sel.iterator();
                while (it.hasNext()) {
                    Object obj = it.next();
                    if (obj instanceof TreeTopic) {
                        tmp.append(((TreeTopic) obj).getModel().toString());
                        tmp.append("--_--");
                    }
                }
            }
            return tmp;
        }

    });
}

From source file:de.topicmapslab.tmcledit.model.views.pages.AssociationTypeModelPage.java

License:Open Source License

private void hookButtonListeners() {
    control.getAddButton().addSelectionListener(new SelectionAdapter() {
        @Override//w  w  w .  j a  va2  s  .c  o m
        public void widgetSelected(SelectionEvent e) {

            TopicIndexer instance = ModelIndexer.getTopicIndexer();
            List<TopicType> list = new ArrayList<TopicType>();

            for (TopicType tt : instance.getTopicTypes()) {
                if (tt.getKind().getValue() == 0 || tt.getKind().getValue() == 3)
                    list.add(tt);
            }

            for (RoleConstraint rc : getCastedModel().getRoles()) {
                if (rc.getType() != null)
                    list.remove(rc.getType());
            }

            ListSelectionDialog dlg = new ListSelectionDialog(control.getShell(), list,
                    new ArrayContentProvider(), control.new TopicLabelProvider(), "Choose the Role type");

            if (dlg.open() == Dialog.OK) {
                if (dlg.getResult().length == 0)
                    return;

                List<RoleConstraint> rcl = new ArrayList<RoleConstraint>();
                for (Object tt : dlg.getResult()) {
                    RoleConstraint rc = ModelFactory.eINSTANCE.createRoleConstraint();
                    rc.setType((TopicType) tt);
                    rc.setCardMin("1");
                    rc.setCardMax("1");
                    rcl.add(rc);
                }
                AddRoleConstraintCommand cmd = new AddRoleConstraintCommand(getCastedModel(), rcl);
                getCommandStack().execute(cmd);
            }
        }
    });

    control.getNewButton().addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            NewTopicTypeWizard wizard = new NewTopicTypeWizard(KindOfTopicType.ROLE_TYPE);
            WizardDialog dlg = new WizardDialog(control.getShell(), wizard);

            if (dlg.open() == Dialog.OK) {
                CompoundCommand cmd = new CompoundCommand();
                TopicType tt = wizard.getNewTopicType();
                cmd.append(new CreateTopicTypeCommand((TopicMapSchema) getCastedModel().eContainer(), tt));
                RoleConstraint rc = ModelFactory.eINSTANCE.createRoleConstraint();
                rc.setType(tt);
                rc.setCardMin("1");
                rc.setCardMax("1");
                cmd.append(new AddRoleConstraintCommand(getCastedModel(), rc));
                if (cmd.canExecute())
                    getCommandStack().execute(cmd);
            }
        }
    });

    control.getRemoveButton().addSelectionListener(new SelectionAdapter() {
        @SuppressWarnings("unchecked")
        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection sel = (IStructuredSelection) control.getTableViewer().getSelection();

            if (sel.isEmpty())
                return;

            List<RoleConstraint> removeList = new ArrayList<RoleConstraint>();
            Iterator<RoleConstraint> it = sel.iterator();
            while (it.hasNext()) {
                removeList.add(it.next());
            }

            RemoveRoleConstraintCommand cmd = new RemoveRoleConstraintCommand(getCastedModel(), removeList);
            getCommandStack().execute(cmd);
        }
    });

    addButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            NewRoleCombinationConstraintDialog dlg = new NewRoleCombinationConstraintDialog(
                    addButton.getShell(), getCastedModel());
            if (dlg.open() == Dialog.OK) {
                Command cmd = dlg.getCreateCommand();
                getCommandStack().execute(cmd);
            }
        }
    });

    editButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            startRCCEditing();

        }
    });

    removeButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection sel = (IStructuredSelection) roleCombinationViewer.getSelection();

            if (sel.isEmpty())
                return;

            List<RoleCombinationConstraint> removeList = new ArrayList<RoleCombinationConstraint>();
            for (Iterator it = sel.iterator(); it.hasNext();) {
                removeList.add((RoleCombinationConstraint) it.next());
            }
            RemoveRoleCombinationConstraintCommand cmd = new RemoveRoleCombinationConstraintCommand(
                    getCastedModel(), removeList);
            getCommandStack().execute(cmd);
        }
    });
}

From source file:de.topicmapslab.tmcledit.model.views.pages.ScopedTopicTypePage.java

License:Open Source License

private void hookButtonListeners() {
    control.getAddButton().addSelectionListener(new SelectionAdapter() {
        @Override//w  ww  . j av  a2 s. c o m
        public void widgetSelected(SelectionEvent e) {

            TopicIndexer instance = ModelIndexer.getTopicIndexer();
            List<TopicType> list = new ArrayList<TopicType>();
            list.addAll(instance.getTypesByKind(KindOfTopicType.TOPIC_TYPE));
            for (ScopeConstraint sc : getCastedModel().getScope()) {
                if (sc.getType() != null)
                    list.remove(sc.getType());
            }

            ListSelectionDialog dlg = new ListSelectionDialog(control.getShell(), list,
                    new ArrayContentProvider(), control.new TopicLabelProvider(), "Choose the Scope type");

            if (dlg.open() == Dialog.OK) {
                if (dlg.getResult().length == 0)
                    return;

                List<ScopeConstraint> scl = new ArrayList<ScopeConstraint>();
                for (Object tt : dlg.getResult()) {
                    ScopeConstraint sc = ModelFactory.eINSTANCE.createScopeConstraint();
                    sc.setType((TopicType) tt);
                    sc.setCardMin("0");
                    sc.setCardMax("1");
                    scl.add(sc);
                }

                AddScopeConstraintsCommand cmd = new AddScopeConstraintsCommand(getCastedModel(), scl);
                getCommandStack().execute(cmd);

            }

        }
    });

    control.getNewButton().addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            NewTopicTypeWizard wizard = new NewTopicTypeWizard(KindOfTopicType.TOPIC_TYPE);
            WizardDialog dlg = new WizardDialog(control.getShell(), wizard);

            if (dlg.open() == Dialog.OK) {
                CompoundCommand cmd = new CompoundCommand();
                TopicType tt = wizard.getNewTopicType();

                cmd.append(new CreateTopicTypeCommand((TopicMapSchema) getCastedModel().eContainer(), tt));

                List<ScopeConstraint> scl = new ArrayList<ScopeConstraint>();
                ScopeConstraint sc = ModelFactory.eINSTANCE.createScopeConstraint();
                sc.setType(tt);
                sc.setCardMin("0");
                sc.setCardMax("1");
                scl.add(sc);
                cmd.append(new AddScopeConstraintsCommand(getCastedModel(), scl));

                if (cmd.canExecute())
                    getCommandStack().execute(cmd);

            }
        }
    });

    control.getRemoveButton().addSelectionListener(new SelectionAdapter() {
        @SuppressWarnings("unchecked")
        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection sel = (IStructuredSelection) control.getTableViewer().getSelection();

            if (sel.isEmpty())
                return;

            List<ScopeConstraint> removeList = new ArrayList<ScopeConstraint>();
            Iterator<ScopeConstraint> it = sel.iterator();
            while (it.hasNext()) {
                removeList.add(it.next());
            }

            RemoveScopeConstraintsCommand cmd = new RemoveScopeConstraintsCommand(getCastedModel(), removeList);
            getCommandStack().execute(cmd);
        }
    });
}

From source file:de.topicmapslab.tmcledit.model.views.pages.TopicTypePage.java

License:Open Source License

private void hookReifierListener() {
    reifiesControl.getAddButton().addSelectionListener(new SelectionAdapter() {
        @Override/*from w  w w  . ja  va  2  s  . co  m*/
        public void widgetSelected(SelectionEvent e) {

            TopicIndexer instance = ModelIndexer.getTopicIndexer();
            List<TopicType> list = new ArrayList<TopicType>();
            list.addAll(instance.getTopicTypes());
            list.removeAll(instance.getTypesByKind(KindOfTopicType.TOPIC_TYPE));

            for (TopicReifiesConstraint rc : getCastedModel().getTopicReifiesConstraints()) {
                if (rc.getType() != null)
                    list.remove(rc.getType());
            }

            ListSelectionDialog dlg = new ListSelectionDialog(reifiesControl.getShell(), list,
                    new ArrayContentProvider(), reifiesControl.new TopicLabelProvider(),
                    "Choose the reifyable type");

            if (dlg.open() == Dialog.OK) {
                if (dlg.getResult().length == 0)
                    return;

                List<TopicReifiesConstraint> trcl = new ArrayList<TopicReifiesConstraint>();
                for (Object tt : dlg.getResult()) {
                    TopicReifiesConstraint trc = ModelFactory.eINSTANCE.createTopicReifiesConstraint();
                    trc.setType((TopicType) tt);
                    trc.setCardMin("0");
                    trc.setCardMax("1");
                    trcl.add(trc);
                }
                AddTopicReifiesConstraintsCommand cmd = new AddTopicReifiesConstraintsCommand(getCastedModel(),
                        trcl);
                getCommandStack().execute(cmd);

            }

        }
    });

    reifiesControl.getRemoveButton().addSelectionListener(new SelectionAdapter() {
        @SuppressWarnings("unchecked")
        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection sel = (IStructuredSelection) reifiesControl.getTableViewer().getSelection();

            if (sel.isEmpty())
                return;

            List<TopicReifiesConstraint> removeList = new ArrayList<TopicReifiesConstraint>();
            Iterator<TopicReifiesConstraint> it = sel.iterator();
            while (it.hasNext()) {
                removeList.add(it.next());
            }

            RemoveTopicReifiesConstraintsCommand cmd = new RemoveTopicReifiesConstraintsCommand(
                    getCastedModel(), removeList);
            getCommandStack().execute(cmd);
        }
    });

    reifiesControl.getTableViewer().addFilter(new ViewerFilter() {
        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            TopicReifiesConstraint trc = (TopicReifiesConstraint) element;

            return trc.getType() != null;
        }
    });

    cannotReifyButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            if (cannotReifyButton.getSelection()) {
                getCommandStack().execute(new SetCannotReifyConstraint(getCastedModel()));
            } else {
                getCommandStack().execute(new RemoveTopicReifiesConstraintsCommand(getCastedModel(),
                        getCastedModel().getTopicReifiesConstraints()));
            }
            reifiesControl.setEnabled(!cannotReifyButton.getSelection());
        }
    });
}

From source file:de.topicmapslab.tmcledit.model.views.widgets.AnnotationWidget.java

License:Open Source License

private void removeAnnotation() {
    IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
    if (sel.isEmpty())
        return;//  w  w w  . j a va2  s  . co m
    Annotation a;
    if (sel.size() == 1) {
        a = (Annotation) sel.getFirstElement();
        cmdStack.execute(new RemoveAnnotationCommand(model, a));
    } else {
        CompoundCommand ccmd = new CompoundCommand();
        Iterator it = sel.iterator();
        while (it.hasNext()) {
            a = (Annotation) it.next();
            ccmd.append(new RemoveAnnotationCommand(model, a));
        }
        cmdStack.execute(ccmd);
    }
    viewer.refresh();
}

From source file:de.tub.tfs.henshin.tgg.interpreter.gui.LoadHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    if (event != null && event.getTrigger() instanceof Event) {
        int shift = SWT.SHIFT & ((Event) event.getTrigger()).stateMask;
        if (shift != 0) {// press shift while clicking button to clear cache
            cacheTimes.clear();//from  w w w. j  a va 2  s .c  o m
            cacheFiles.clear();
        }
    }

    // clear all lists from possible previous execution
    loadQueue.clear();
    trSystems.clear();
    trFileNames.clear();

    // Find grammar files to load:
    ISelection sel = HandlerUtil.getCurrentSelection(event);
    if (sel != null && sel instanceof IStructuredSelection) {
        IStructuredSelection structSel = (IStructuredSelection) sel;
        @SuppressWarnings("unchecked")
        Iterator<Object> it = structSel.iterator();
        for (; it.hasNext();) {
            Object obj = it.next();
            if (obj instanceof IFile) {
                IFile file = (IFile) obj;
                if (file.getFileExtension().equals(henshinExt)) {
                    loadQueue.add(file);
                }
            }
            if (obj instanceof IContainer) {
                IResource[] resArr;
                try {
                    resArr = ((IContainer) obj).members();
                    for (int i = 0; i < resArr.length; i++) {
                        if (resArr[i] instanceof IFile) {
                            IFile file = (IFile) resArr[i];
                            if (file.getFileExtension().equals(henshinExt)) {
                                loadQueue.add(file);
                            }
                        }
                    }
                } catch (CoreException e) {
                    e.printStackTrace();
                }
            }
            for (IFile f : loadQueue) {
                trFileNames.add(f.getName());
            }
        }
    }
    saveCachedQueue();
    System.out.println("Registered Grammars will be loaded when needed.");

    return null;
}

From source file:de.tub.tfs.henshin.tgg.interpreter.gui.TransHandler.java

License:Open Source License

protected Queue<IFile> retrieveFilesForTranslation(ExecutionEvent event) {
    // Find files to translate:
    Queue<IFile> transQueue = new LinkedList<IFile>();
    ISelection sel = HandlerUtil.getCurrentSelection(event);
    if (sel != null && sel instanceof IStructuredSelection) {
        IStructuredSelection structSel = (IStructuredSelection) sel;
        for (Iterator<Object> it = structSel.iterator(); it.hasNext();) {
            Object obj = it.next();
            if (obj instanceof IFile) {
                IFile file = (IFile) obj;
                transQueue.add(file);/*from  w  w  w. j  a v a2s. c o m*/
                IPath path = file.getFullPath().removeLastSegments(1);
                loadConfigFile(path);
            }
            if (obj instanceof IContainer) {
                useOutputFolder = true;
                IResource[] resArr;
                try {
                    resArr = ((IContainer) obj).members();
                    for (int i = 0; i < resArr.length; i++) {
                        if (resArr[i] instanceof IFile) {
                            IFile file = (IFile) resArr[i];
                            transQueue.add(file);

                            IPath path = file.getFullPath().removeLastSegments(1);
                            loadConfigFile(path);
                        }

                    }
                } catch (CoreException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return transQueue;
}

From source file:de.uniluebeck.itm.spyglass.gui.configuration.PluginManagerPreferencePage.java

License:Open Source License

@SuppressWarnings("unchecked")
private List<Plugin> getSelectedPlugins(final IStructuredSelection selection) {
    final List<Plugin> list = new ArrayList<Plugin>();
    for (final Iterator it = selection.iterator(); it.hasNext();) {
        list.add((Plugin) it.next());/*from ww  w  .ja v a2 s.  co  m*/
    }
    return list;
}