Example usage for org.eclipse.jface.viewers TableViewer setContentProvider

List of usage examples for org.eclipse.jface.viewers TableViewer setContentProvider

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers TableViewer setContentProvider.

Prototype

@Override
public void setContentProvider(IContentProvider provider) 

Source Link

Document

Sets the content provider used by this AbstractTableViewer.

Usage

From source file:org.eclipse.emf.cdo.internal.ui.views.CDOWatchListView.java

License:Open Source License

private TableViewer createViewer(Composite parent) {
    TableViewer viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    viewer.getTable().setLayoutData(UIUtil.createGridData());
    viewer.getTable().setHeaderVisible(true);
    viewer.getTable().setLinesVisible(true);
    createColumns(viewer.getTable());/*from ww w .  j  ava2s  .c  om*/

    viewer.setContentProvider(new CDOObjectContainerContentProvider());
    viewer.setLabelProvider(new CDOSubscriptionViewLabelProvider(adapterFactory));
    viewer.setInput(container);

    CDOObjectDropAdapter.support(viewer);
    return viewer;
}

From source file:org.eclipse.emf.diffmerge.ui.util.InconsistencyDialog.java

License:Open Source License

/**
 * Create and return the area where duplicates for the given role as provided in
 * the given collection are displayed/* w  w w.jav  a  2 s  .c  o  m*/
 * @param parent_p a non-null composite
 * @param role_p a non-null role
 * @param duplicates_p a non-null set of objects
 */
protected Control createDuplicateArea(Composite parent_p, Role role_p, Collection<Object> duplicates_p) {
    Group group = new Group(parent_p, SWT.NONE);
    group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    group.setLayout(new GridLayout(1, false));
    String scopeName = (role_p == Role.ANCESTOR) ? Messages.InconsistencyDialog_AncestorScope
            : (role_p == Role.REFERENCE) ? Messages.InconsistencyDialog_ReferenceScope
                    : Messages.InconsistencyDialog_TargetScope;
    group.setText(scopeName);
    TableViewer viewer = new TableViewer(group, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.MULTI);
    viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    viewer.setContentProvider(new ArrayContentProvider());
    viewer.setSorter(new ViewerSorter());
    createCopyMenuItem(viewer);
    viewer.setInput(duplicates_p.toArray());
    return group;
}

From source file:org.eclipse.emf.ecp.ecoreeditor.internal.ui.CreateNewChildDialog.java

License:Open Source License

@Override
protected Control createDialogArea(Composite parentComposite) {
    final ChildrenDescriptorCollector childrenDescriptorCollector = new ChildrenDescriptorCollector();
    final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(parent);

    final Dialog currentDialog = this;
    final List<Action> actions = getNewChildActions(childrenDescriptorCollector.getDescriptors(parent),
            editingDomain, parent);//from  w  ww.j  a v  a2 s .c om

    final TableViewer list = new TableViewer(parentComposite);
    list.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
    list.setContentProvider(new ArrayContentProvider());
    list.setLabelProvider(new LabelProvider() {
        @Override
        public String getText(Object element) {
            final Action action = (Action) element;
            final StringBuilder builder = new StringBuilder(action.getText());
            if (action.getAccelerator() > 0) {
                builder.append(" [");
                builder.append(Character.toUpperCase((char) action.getAccelerator()));
                builder.append("]");
            }
            return builder.toString();
        }

        @Override
        public Image getImage(Object element) {
            return ((Action) element).getImageDescriptor().createImage();
        }
    });
    list.setInput(actions.toArray());
    list.addOpenListener(new IOpenListener() {

        @Override
        public void open(OpenEvent event) {
            final Action action = (Action) ((StructuredSelection) event.getSelection()).getFirstElement();
            action.run();
            currentDialog.close();
        }
    });
    list.getControl().addKeyListener(new KeyListener() {

        @Override
        public void keyReleased(KeyEvent e) {
            // NOP
        }

        @Override
        public void keyPressed(KeyEvent e) {
            for (final Action a : actions) {
                if (a.getAccelerator() == e.keyCode) {
                    a.run();
                    currentDialog.close();
                    break;
                }
            }
        }
    });
    return parentComposite;
}

From source file:org.eclipse.emf.ecp.ide.editor.view.UnknownFeaturesDialog.java

License:Open Source License

@Override
protected Control createDialogArea(Composite parent) {
    final Composite composite = (Composite) super.createDialogArea(parent);

    final Label label = new Label(composite, SWT.WRAP);
    label.setText(getDescription());/*  w w  w  .  j a v  a2  s. c o  m*/

    final TableViewer viewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE | SWT.BORDER);
    final Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(false);
    table.setToolTipText(""); //$NON-NLS-1$
    final Listener tableListener = getTableListener(composite, table);
    table.addListener(SWT.Dispose, tableListener);
    table.addListener(SWT.KeyDown, tableListener);
    table.addListener(SWT.MouseMove, tableListener);
    table.addListener(SWT.MouseHover, tableListener);

    viewer.setContentProvider(ArrayContentProvider.getInstance());
    viewer.setInput(getInput());
    createColumns(parent, viewer);

    final GridData gridData = new GridData();
    gridData.verticalAlignment = GridData.FILL;
    gridData.horizontalSpan = 2;
    gridData.grabExcessHorizontalSpace = true;
    gridData.grabExcessVerticalSpace = true;
    gridData.horizontalAlignment = GridData.FILL;
    viewer.getControl().setLayoutData(gridData);

    viewer.refresh(true);
    composite.layout();
    return composite;
}

From source file:org.eclipse.emf.ecp.internal.ui.composites.SelectModelElementCompositeImpl.java

License:Open Source License

/** {@inheritDoc} **/
@Override/*w w w.  j a  v  a2s  . c o  m*/
protected TableViewer createViewer(Composite composite) {
    final TableViewer lv = new TableViewer(composite);
    lv.setLabelProvider(getLabelProvider());
    lv.setContentProvider(ArrayContentProvider.getInstance());
    lv.setInput(getInput());
    return lv;
}

From source file:org.eclipse.emf.ecp.internal.wizards.page.SelectRepositoryPage.java

License:Open Source License

/** {@inheritDoc} */
@Override/*ww  w. j a  v a 2s  .co  m*/
public void createControl(Composite parent) {
    final Composite container = new Composite(parent, SWT.NULL);
    container.setLayout(new GridLayout(1, true));

    final RepositoriesContentProvider contentProvider = new RepositoriesContentProvider(
            ((ShareWizard) getWizard()).getProvider());
    final TableViewer viewer = new TableViewer(container, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    viewer.setContentProvider(contentProvider);
    viewer.setLabelProvider(new RepositoriesLabelProvider(contentProvider));
    viewer.setSorter(new ViewerSorter());
    viewer.setInput(ECPUtil.getECPRepositoryManager());
    viewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            final ECPRepository ecpRepository = (ECPRepository) ((IStructuredSelection) event.getSelection())
                    .getFirstElement();
            ((ShareWizard) getWizard()).setSelectedRepository(ecpRepository);
            setPageComplete(true);
        }
    });

    setControl(container);
    setPageComplete(false);
}

From source file:org.eclipse.emf.ecp.spi.common.ui.composites.SelectModelElementCompositeImpl.java

License:Open Source License

/** {@inheritDoc} **/
@Override//  w  w w .  ja va2 s.  co m
protected TableViewer createViewer(Composite composite) {
    TableViewer lv;
    if (multiSelection) {
        lv = new TableViewer(composite);
    } else {
        lv = new TableViewer(composite, SWT.SINGLE);
    }
    lv.setLabelProvider(getLabelProvider());
    lv.setContentProvider(ArrayContentProvider.getInstance());
    lv.setInput(getInput());
    return lv;
}

From source file:org.eclipse.emf.ecp.view.internal.editor.controls.TableColumnsDMRTableControl.java

License:Open Source License

/**
 * {@inheritDoc}//from   w  ww.ja v  a2  s .  co m
 *
 * @see org.eclipse.emf.ecp.view.spi.core.swt.SimpleControlSWTRenderer#createControl(org.eclipse.swt.widgets.Composite)
 */
@Override
protected Control createControl(final Composite parent) throws DatabindingFailedException {
    final IObservableValue observableValue = Activator.getDefault().getEMFFormsDatabinding().getObservableValue(
            getVElement().getDomainModelReference(), getViewModelContext().getDomainModel());
    structuralFeature = (EStructuralFeature) observableValue.getValueType();
    eObject = (EObject) ((IObserving) observableValue).getObserved();
    observableValue.dispose();

    final Composite composite = new Composite(parent, SWT.NONE);
    composite.setBackgroundMode(SWT.INHERIT_FORCE);
    GridLayoutFactory.fillDefaults().numColumns(1).applyTo(composite);
    final Composite titleComposite = new Composite(composite, SWT.NONE);
    titleComposite.setBackgroundMode(SWT.INHERIT_FORCE);
    GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).applyTo(titleComposite);
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(titleComposite);

    final Label filler = new Label(titleComposite, SWT.NONE);
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, false).applyTo(filler);

    final Composite buttonComposite = new Composite(titleComposite, SWT.NONE);
    buttonComposite.setBackgroundMode(SWT.INHERIT_FORCE);
    GridLayoutFactory.fillDefaults().numColumns(3).equalWidth(true).applyTo(buttonComposite);
    GridDataFactory.fillDefaults().align(SWT.END, SWT.BEGINNING).grab(false, false).applyTo(buttonComposite);

    final Button buttonSort = new Button(buttonComposite, SWT.PUSH);
    buttonSort.setText("Sort"); //$NON-NLS-1$
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(false, false).applyTo(buttonSort);

    final Button buttonAdd = new Button(buttonComposite, SWT.PUSH);
    buttonAdd.setText("Add"); //$NON-NLS-1$
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(false, false).applyTo(buttonAdd);

    final Button buttonRemove = new Button(buttonComposite, SWT.PUSH);
    buttonRemove.setText("Remove"); //$NON-NLS-1$
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(false, false).applyTo(buttonRemove);

    final Composite tableComposite = new Composite(composite, SWT.NONE);
    tableComposite.setBackgroundMode(SWT.INHERIT_FORCE);
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).hint(1, 100)
            .applyTo(tableComposite);
    final TableColumnLayout layout = new TableColumnLayout();
    tableComposite.setLayout(layout);

    final TableViewer viewer = new TableViewer(tableComposite);
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(viewer.getControl());

    viewer.getTable().setHeaderVisible(true);
    viewer.getTable().setLinesVisible(true);
    final TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
    final TableColumn tableColumn = column.getColumn();
    final EMFFormsLabelProvider emfFormsLabelProvider = Activator.getDefault().getEMFFormsLabelProvider();// TODO
    try {
        final IObservableValue labelText = emfFormsLabelProvider.getDisplayName(
                getVElement().getDomainModelReference(), getViewModelContext().getDomainModel());
        final IObservableValue tooltip = emfFormsLabelProvider.getDescription(
                getVElement().getDomainModelReference(), getViewModelContext().getDomainModel());
        viewModelDBC.bindValue(WidgetProperties.text().observe(tableColumn), labelText);
        viewModelDBC.bindValue(WidgetProperties.tooltipText().observe(tableColumn), tooltip);
    } catch (final NoLabelFoundException e) {
        // FIXME Expectations?
        getReportService().report(new RenderingFailedReport(e));
        tableColumn.setText(e.getMessage());
        tableColumn.setToolTipText(e.toString());
    }

    layout.setColumnData(column.getColumn(), new ColumnWeightData(1, true));

    viewer.setLabelProvider(labelProvider);
    viewer.setContentProvider(new ObservableListContentProvider());
    addDragAndDropSupport(viewer, getEditingDomain(eObject));

    final IObservableList list = Activator.getDefault().getEMFFormsDatabinding()
            .getObservableList(getVElement().getDomainModelReference(), getViewModelContext().getDomainModel());
    viewer.setInput(list);

    tableControl = (VTableControl) getViewModelContext().getDomainModel();
    adapter = new TableControlAdapter(parent, viewer);
    tableControl.eAdapters().add(adapter);

    buttonSort.addSelectionListener(new SortSelectionAdapter());

    buttonAdd.addSelectionListener(new AddSelectionAdapter(tableComposite, viewer));

    buttonRemove.addSelectionListener(new RemoveSelectionAdapter(viewer));

    return composite;
}

From source file:org.eclipse.emf.edit.ui.celleditor.FeatureEditorDialog.java

License:Open Source License

@Override
protected Control createDialogArea(Composite parent) {
    Composite contents = (Composite) super.createDialogArea(parent);

    GridLayout contentsGridLayout = (GridLayout) contents.getLayout();
    contentsGridLayout.numColumns = 3;/*from w w w .  java  2  s.c  om*/

    GridData contentsGridData = (GridData) contents.getLayoutData();
    contentsGridData.horizontalAlignment = SWT.FILL;
    contentsGridData.verticalAlignment = SWT.FILL;

    Text patternText = null;

    if (choiceOfValues != null) {
        Group filterGroupComposite = new Group(contents, SWT.NONE);
        filterGroupComposite.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Choices_pattern_group"));
        filterGroupComposite.setLayout(new GridLayout(2, false));
        filterGroupComposite.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true, false, 3, 1));

        Label label = new Label(filterGroupComposite, SWT.NONE);
        label.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Choices_pattern_label"));

        patternText = new Text(filterGroupComposite, SWT.BORDER);
        patternText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    }

    Composite choiceComposite = new Composite(contents, SWT.NONE);
    {
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
        data.horizontalAlignment = SWT.END;
        choiceComposite.setLayoutData(data);

        GridLayout layout = new GridLayout();
        data.horizontalAlignment = SWT.FILL;
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        layout.numColumns = 1;
        choiceComposite.setLayout(layout);
    }

    Label choiceLabel = new Label(choiceComposite, SWT.NONE);
    choiceLabel.setText(choiceOfValues == null ? EMFEditUIPlugin.INSTANCE.getString("_UI_Value_label")
            : EMFEditUIPlugin.INSTANCE.getString("_UI_Choices_label"));
    GridData choiceLabelGridData = new GridData();
    choiceLabelGridData.verticalAlignment = SWT.FILL;
    choiceLabelGridData.horizontalAlignment = SWT.FILL;
    choiceLabel.setLayoutData(choiceLabelGridData);

    final Table choiceTable = choiceOfValues == null ? null
            : new Table(choiceComposite, SWT.MULTI | SWT.BORDER);
    if (choiceTable != null) {
        GridData choiceTableGridData = new GridData();
        choiceTableGridData.widthHint = Display.getCurrent().getBounds().width / 5;
        choiceTableGridData.heightHint = Display.getCurrent().getBounds().height / 3;
        choiceTableGridData.verticalAlignment = SWT.FILL;
        choiceTableGridData.horizontalAlignment = SWT.FILL;
        choiceTableGridData.grabExcessHorizontalSpace = true;
        choiceTableGridData.grabExcessVerticalSpace = true;
        choiceTable.setLayoutData(choiceTableGridData);
    }

    final TableViewer choiceTableViewer = choiceOfValues == null ? null : new TableViewer(choiceTable);
    if (choiceTableViewer != null) {
        choiceTableViewer.setContentProvider(new AdapterFactoryContentProvider(new AdapterFactoryImpl()));
        choiceTableViewer.setLabelProvider(labelProvider);
        final PatternFilter filter = new PatternFilter() {
            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            @Override
            protected boolean isParentMatch(Viewer viewer, Object element) {
                return viewer instanceof AbstractTreeViewer && super.isParentMatch(viewer, element);
            }
        };
        choiceTableViewer.addFilter(filter);
        if (patternText != null) {
            patternText.addModifyListener(new ModifyListener() {
                /**
                * 
                */
                private static final long serialVersionUID = 1L;

                public void modifyText(ModifyEvent e) {
                    filter.setPattern(((Text) e.widget).getText());
                    choiceTableViewer.refresh();
                }
            });
        }
        choiceTableViewer.setInput(new ItemProvider(choiceOfValues));
    }

    // We use multi even for a single line because we want to respond to the enter key.
    //
    int style = multiLine ? SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER : SWT.MULTI | SWT.BORDER;
    final Text choiceText = choiceOfValues == null ? new Text(choiceComposite, style) : null;
    if (choiceText != null) {
        GridData choiceTextGridData = new GridData();
        choiceTextGridData.widthHint = Display.getCurrent().getBounds().width / 5;
        choiceTextGridData.verticalAlignment = SWT.BEGINNING;
        choiceTextGridData.horizontalAlignment = SWT.FILL;
        choiceTextGridData.grabExcessHorizontalSpace = true;
        if (multiLine) {
            choiceTextGridData.verticalAlignment = SWT.FILL;
            choiceTextGridData.grabExcessVerticalSpace = true;
        }
        choiceText.setLayoutData(choiceTextGridData);
    }

    Composite controlButtons = new Composite(contents, SWT.NONE);
    GridData controlButtonsGridData = new GridData();
    controlButtonsGridData.verticalAlignment = SWT.FILL;
    controlButtonsGridData.horizontalAlignment = SWT.FILL;
    controlButtons.setLayoutData(controlButtonsGridData);

    GridLayout controlsButtonGridLayout = new GridLayout();
    controlButtons.setLayout(controlsButtonGridLayout);

    new Label(controlButtons, SWT.NONE);

    final Button addButton = new Button(controlButtons, SWT.PUSH);
    addButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Add_label"));
    GridData addButtonGridData = new GridData();
    addButtonGridData.verticalAlignment = SWT.FILL;
    addButtonGridData.horizontalAlignment = SWT.FILL;
    addButton.setLayoutData(addButtonGridData);

    final Button removeButton = new Button(controlButtons, SWT.PUSH);
    removeButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Remove_label"));
    GridData removeButtonGridData = new GridData();
    removeButtonGridData.verticalAlignment = SWT.FILL;
    removeButtonGridData.horizontalAlignment = SWT.FILL;
    removeButton.setLayoutData(removeButtonGridData);

    Label spaceLabel = new Label(controlButtons, SWT.NONE);
    GridData spaceLabelGridData = new GridData();
    spaceLabelGridData.verticalSpan = 2;
    spaceLabel.setLayoutData(spaceLabelGridData);

    final Button upButton = new Button(controlButtons, SWT.PUSH);
    upButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Up_label"));
    GridData upButtonGridData = new GridData();
    upButtonGridData.verticalAlignment = SWT.FILL;
    upButtonGridData.horizontalAlignment = SWT.FILL;
    upButton.setLayoutData(upButtonGridData);

    final Button downButton = new Button(controlButtons, SWT.PUSH);
    downButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Down_label"));
    GridData downButtonGridData = new GridData();
    downButtonGridData.verticalAlignment = SWT.FILL;
    downButtonGridData.horizontalAlignment = SWT.FILL;
    downButton.setLayoutData(downButtonGridData);

    Composite featureComposite = new Composite(contents, SWT.NONE);
    {
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
        data.horizontalAlignment = SWT.END;
        featureComposite.setLayoutData(data);

        GridLayout layout = new GridLayout();
        data.horizontalAlignment = SWT.FILL;
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        layout.numColumns = 1;
        featureComposite.setLayout(layout);
    }

    Label featureLabel = new Label(featureComposite, SWT.NONE);
    featureLabel.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Feature_label"));
    GridData featureLabelGridData = new GridData();
    featureLabelGridData.horizontalSpan = 2;
    featureLabelGridData.horizontalAlignment = SWT.FILL;
    featureLabelGridData.verticalAlignment = SWT.FILL;
    featureLabel.setLayoutData(featureLabelGridData);

    final Table featureTable = new Table(featureComposite, SWT.MULTI | SWT.BORDER);
    GridData featureTableGridData = new GridData();
    featureTableGridData.widthHint = Display.getCurrent().getBounds().width / 5;
    featureTableGridData.heightHint = Display.getCurrent().getBounds().height / 3;
    featureTableGridData.verticalAlignment = SWT.FILL;
    featureTableGridData.horizontalAlignment = SWT.FILL;
    featureTableGridData.grabExcessHorizontalSpace = true;
    featureTableGridData.grabExcessVerticalSpace = true;
    featureTable.setLayoutData(featureTableGridData);

    final TableViewer featureTableViewer = new TableViewer(featureTable);
    featureTableViewer.setContentProvider(contentProvider);
    featureTableViewer.setLabelProvider(labelProvider);
    featureTableViewer.setInput(values);
    if (!values.getChildren().isEmpty()) {
        featureTableViewer.setSelection(new StructuredSelection(values.getChildren().get(0)));
    }

    if (choiceTableViewer != null) {
        choiceTableViewer.addDoubleClickListener(new IDoubleClickListener() {
            public void doubleClick(DoubleClickEvent event) {
                if (addButton.isEnabled()) {
                    addButton.notifyListeners(SWT.Selection, null);
                }
            }
        });

        featureTableViewer.addDoubleClickListener(new IDoubleClickListener() {
            public void doubleClick(DoubleClickEvent event) {
                if (removeButton.isEnabled()) {
                    removeButton.notifyListeners(SWT.Selection, null);
                }
            }
        });
    }

    if (choiceText != null) {
        choiceText.addKeyListener(new KeyAdapter() {
            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            @Override
            public void keyPressed(KeyEvent event) {
                if (!multiLine && (event.character == '\r' || event.character == '\n')) {
                    try {
                        Object value = EcoreUtil.createFromString((EDataType) eClassifier,
                                choiceText.getText());
                        values.getChildren().add(value);
                        choiceText.setText("");
                        featureTableViewer.setSelection(new StructuredSelection(value));
                        event.doit = false;
                    } catch (RuntimeException exception) {
                        // Ignore
                    }
                } else if (event.character == '\33') {
                    choiceText.setText("");
                    event.doit = false;
                }
            }
        });
    }

    upButton.addSelectionListener(new SelectionAdapter() {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void widgetSelected(SelectionEvent event) {
            IStructuredSelection selection = (IStructuredSelection) featureTableViewer.getSelection();
            int minIndex = 0;
            for (Iterator<?> i = selection.iterator(); i.hasNext();) {
                Object value = i.next();
                int index = values.getChildren().indexOf(value);
                values.getChildren().move(Math.max(index - 1, minIndex++), value);
            }
        }
    });

    downButton.addSelectionListener(new SelectionAdapter() {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void widgetSelected(SelectionEvent event) {
            IStructuredSelection selection = (IStructuredSelection) featureTableViewer.getSelection();
            int maxIndex = values.getChildren().size() - selection.size();
            for (Iterator<?> i = selection.iterator(); i.hasNext();) {
                Object value = i.next();
                int index = values.getChildren().indexOf(value);
                values.getChildren().move(Math.min(index + 1, maxIndex++), value);
            }
        }
    });

    addButton.addSelectionListener(new SelectionAdapter() {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        // event is null when choiceTableViewer is double clicked
        @Override
        public void widgetSelected(SelectionEvent event) {
            if (choiceTableViewer != null) {
                IStructuredSelection selection = (IStructuredSelection) choiceTableViewer.getSelection();
                for (Iterator<?> i = selection.iterator(); i.hasNext();) {
                    Object value = i.next();
                    if (!unique || !values.getChildren().contains(value)) {
                        values.getChildren().add(value);
                    }
                }
                featureTableViewer.setSelection(selection);
            } else if (choiceText != null) {
                try {
                    Object value = EcoreUtil.createFromString((EDataType) eClassifier, choiceText.getText());
                    if (!unique || !values.getChildren().contains(value)) {
                        values.getChildren().add(value);
                        choiceText.setText("");
                    }
                    featureTableViewer.setSelection(new StructuredSelection(value));
                } catch (RuntimeException exception) {
                    // Ignore
                }
            }
        }
    });

    removeButton.addSelectionListener(new SelectionAdapter() {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        // event is null when featureTableViewer is double clicked 
        @Override
        public void widgetSelected(SelectionEvent event) {
            IStructuredSelection selection = (IStructuredSelection) featureTableViewer.getSelection();
            Object firstValue = null;
            for (Iterator<?> i = selection.iterator(); i.hasNext();) {
                Object value = i.next();
                if (firstValue == null) {
                    firstValue = value;
                }
                values.getChildren().remove(value);
            }

            if (!values.getChildren().isEmpty()) {
                featureTableViewer.setSelection(new StructuredSelection(values.getChildren().get(0)));
            }

            if (choiceTableViewer != null) {
                choiceTableViewer.setSelection(selection);
            } else if (choiceText != null) {
                if (firstValue != null) {
                    String value = EcoreUtil.convertToString((EDataType) eClassifier, firstValue);
                    choiceText.setText(value);
                }
            }
        }
    });

    return contents;
}

From source file:org.eclipse.emf.eef.runtime.ui.widgets.EEFFeatureEditorDialog.java

License:Open Source License

@Override
protected Control createDialogArea(Composite parent) {

    Composite contents = (Composite) super.createDialogArea(parent);

    GridLayout contentsGridLayout = (GridLayout) contents.getLayout();
    contentsGridLayout.numColumns = 3;// w  ww. j a  v  a  2s.  c  o m

    GridData contentsGridData = (GridData) contents.getLayoutData();
    contentsGridData.horizontalAlignment = SWT.FILL;
    contentsGridData.verticalAlignment = SWT.FILL;

    Composite choiceComposite = new Composite(contents, SWT.NONE);
    {
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
        data.horizontalAlignment = SWT.END;
        choiceComposite.setLayoutData(data);

        GridLayout layout = new GridLayout();
        data.horizontalAlignment = SWT.FILL;
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        layout.numColumns = 1;
        choiceComposite.setLayout(layout);
    }

    Label choiceLabel = new Label(choiceComposite, SWT.NONE);
    choiceLabel.setText(choiceOfValues == null ? EMFEditUIPlugin.INSTANCE.getString("_UI_Value_label") //$NON-NLS-1$
            : EMFEditUIPlugin.INSTANCE.getString("_UI_Choices_label")); //$NON-NLS-1$
    GridData choiceLabelGridData = new GridData();
    choiceLabelGridData.verticalAlignment = SWT.FILL;
    choiceLabelGridData.horizontalAlignment = SWT.FILL;
    choiceLabel.setLayoutData(choiceLabelGridData);

    final Table choiceTable = choiceOfValues == null ? null
            : new Table(choiceComposite, SWT.MULTI | SWT.BORDER);
    if (choiceTable != null) {
        GridData choiceTableGridData = new GridData();
        choiceTableGridData.widthHint = Display.getCurrent().getBounds().width / 5;
        choiceTableGridData.heightHint = Display.getCurrent().getBounds().height / 3;
        choiceTableGridData.verticalAlignment = SWT.FILL;
        choiceTableGridData.horizontalAlignment = SWT.FILL;
        choiceTableGridData.grabExcessHorizontalSpace = true;
        choiceTableGridData.grabExcessVerticalSpace = true;
        choiceTable.setLayoutData(choiceTableGridData);
    }

    final TableViewer choiceTableViewer = choiceOfValues == null ? null : new TableViewer(choiceTable);
    if (choiceTableViewer != null) {
        choiceTableViewer.setContentProvider(new AdapterFactoryContentProvider(new AdapterFactoryImpl()));
        choiceTableViewer.setLabelProvider(labelProvider);
        choiceTableViewer.setInput(new ItemProvider(choiceOfValues));
        if (filters != null) {
            for (ViewerFilter filter : filters) {
                choiceTableViewer.addFilter(filter);
            }
        }
        // business rules
        List<Button> checkButtons = new ArrayList<Button>();
        if (brFilters != null && !brFilters.isEmpty()) {
            String currentModel = EEFRuntimeUIMessages.EEFFeatureEditorDialog_current_model_filter_title;
            String referencedModels = EEFRuntimeUIMessages.EEFFeatureEditorDialog_referenced_models_filter_title;
            String differentContainer = EEFRuntimeUIMessages.EEFFeatureEditorDialog_different_container_filter_title;
            for (int i = 0; i < brFilters.size(); i++) {
                String filterName = null;
                if (brFilters.get(i) instanceof BusinessViewerFilter) {
                    final BusinessViewerFilter viewerFilter = (BusinessViewerFilter) brFilters.get(i);

                    Button filteredContent = new Button(choiceComposite, SWT.CHECK);
                    filterName = viewerFilter.getName();
                    if (filterName != null) {
                        filteredContent.setText(filterName);
                    } else {
                        filteredContent.setText(EEFRuntimeUIMessages.EEFFeatureEditorDialog_filter_name);
                    }

                    filteredContent.setData(viewerFilter);
                    checkButtons.add(filteredContent);
                }

                // selection "contenu filtre"

                if (filterName != null && !filterName.equals(currentModel)
                        && !filterName.equals(referencedModels) && !filterName.equals(differentContainer)) {
                    checkButtons.get(i).setSelection(true);
                    choiceTableViewer.addFilter((ViewerFilter) checkButtons.get(i).getData());
                    for (int j = 0; j < choiceTable.getColumns().length; j++) {
                        choiceTable.getColumn(j).pack();
                    }
                }
            }

            // selection listener for business rules
            for (final Button b : checkButtons) {
                b.addSelectionListener(new SelectionAdapter() {
                    public void widgetSelected(SelectionEvent e) {
                        if (b.getSelection() == false) {
                            choiceTableViewer.removeFilter((ViewerFilter) b.getData());
                        } else {
                            choiceTableViewer.addFilter((ViewerFilter) b.getData());
                        }
                        for (int i = 0; i < choiceTable.getColumns().length; i++) {
                            choiceTable.getColumn(i).pack();
                        }
                    }

                });
            }
        }

    }

    // We use multi even for a single line because we want to respond to the
    // enter key.
    //
    int style = multiLine ? SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER : SWT.MULTI | SWT.BORDER;
    final Text choiceText = choiceOfValues == null ? new Text(choiceComposite, style) : null;
    if (choiceText != null) {
        GridData choiceTextGridData = new GridData();
        choiceTextGridData.widthHint = Display.getCurrent().getBounds().width / 5;
        choiceTextGridData.verticalAlignment = SWT.BEGINNING;
        choiceTextGridData.horizontalAlignment = SWT.FILL;
        choiceTextGridData.grabExcessHorizontalSpace = true;
        if (multiLine) {
            choiceTextGridData.verticalAlignment = SWT.FILL;
            choiceTextGridData.grabExcessVerticalSpace = true;
        }
        choiceText.setLayoutData(choiceTextGridData);
    }

    Composite controlButtons = new Composite(contents, SWT.NONE);
    GridData controlButtonsGridData = new GridData();
    controlButtonsGridData.verticalAlignment = SWT.FILL;
    controlButtonsGridData.horizontalAlignment = SWT.FILL;
    controlButtons.setLayoutData(controlButtonsGridData);

    GridLayout controlsButtonGridLayout = new GridLayout();
    controlButtons.setLayout(controlsButtonGridLayout);

    new Label(controlButtons, SWT.NONE);

    final Button addButton = new Button(controlButtons, SWT.PUSH);
    addButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Add_label")); //$NON-NLS-1$
    GridData addButtonGridData = new GridData();
    addButtonGridData.verticalAlignment = SWT.FILL;
    addButtonGridData.horizontalAlignment = SWT.FILL;
    addButton.setLayoutData(addButtonGridData);

    final Button removeButton = new Button(controlButtons, SWT.PUSH);
    removeButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Remove_label")); //$NON-NLS-1$
    GridData removeButtonGridData = new GridData();
    removeButtonGridData.verticalAlignment = SWT.FILL;
    removeButtonGridData.horizontalAlignment = SWT.FILL;
    removeButton.setLayoutData(removeButtonGridData);

    Label spaceLabel = new Label(controlButtons, SWT.NONE);
    GridData spaceLabelGridData = new GridData();
    spaceLabelGridData.verticalSpan = 2;
    spaceLabel.setLayoutData(spaceLabelGridData);

    final Button upButton = new Button(controlButtons, SWT.PUSH);
    upButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Up_label")); //$NON-NLS-1$
    GridData upButtonGridData = new GridData();
    upButtonGridData.verticalAlignment = SWT.FILL;
    upButtonGridData.horizontalAlignment = SWT.FILL;
    upButton.setLayoutData(upButtonGridData);

    final Button downButton = new Button(controlButtons, SWT.PUSH);
    downButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Down_label")); //$NON-NLS-1$
    GridData downButtonGridData = new GridData();
    downButtonGridData.verticalAlignment = SWT.FILL;
    downButtonGridData.horizontalAlignment = SWT.FILL;
    downButton.setLayoutData(downButtonGridData);

    Composite featureComposite = new Composite(contents, SWT.NONE);
    {
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
        data.horizontalAlignment = SWT.END;
        featureComposite.setLayoutData(data);

        GridLayout layout = new GridLayout();
        data.horizontalAlignment = SWT.FILL;
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        layout.numColumns = 1;
        featureComposite.setLayout(layout);
    }

    Label featureLabel = new Label(featureComposite, SWT.NONE);
    featureLabel.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Feature_label")); //$NON-NLS-1$
    GridData featureLabelGridData = new GridData();
    featureLabelGridData.horizontalSpan = 2;
    featureLabelGridData.horizontalAlignment = SWT.FILL;
    featureLabelGridData.verticalAlignment = SWT.FILL;
    featureLabel.setLayoutData(featureLabelGridData);

    final Table featureTable = new Table(featureComposite, SWT.MULTI | SWT.BORDER);
    GridData featureTableGridData = new GridData();
    featureTableGridData.widthHint = Display.getCurrent().getBounds().width / 5;
    featureTableGridData.heightHint = Display.getCurrent().getBounds().height / 3;
    featureTableGridData.verticalAlignment = SWT.FILL;
    featureTableGridData.horizontalAlignment = SWT.FILL;
    featureTableGridData.grabExcessHorizontalSpace = true;
    featureTableGridData.grabExcessVerticalSpace = true;
    featureTable.setLayoutData(featureTableGridData);

    final TableViewer featureTableViewer = new TableViewer(featureTable);
    featureTableViewer.setContentProvider(contentProvider);
    featureTableViewer.setLabelProvider(labelProvider);
    featureTableViewer.setInput(values);
    if (!values.getChildren().isEmpty()) {
        featureTableViewer.setSelection(new StructuredSelection(values.getChildren().get(0)));
    }

    if (choiceTableViewer != null) {
        choiceTableViewer.addDoubleClickListener(new IDoubleClickListener() {
            public void doubleClick(DoubleClickEvent event) {
                if (addButton.isEnabled()) {
                    addButton.notifyListeners(SWT.Selection, null);
                }
            }
        });

        featureTableViewer.addDoubleClickListener(new IDoubleClickListener() {
            public void doubleClick(DoubleClickEvent event) {
                if (removeButton.isEnabled()) {
                    removeButton.notifyListeners(SWT.Selection, null);
                }
            }
        });
    }

    if (choiceText != null) {
        choiceText.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent event) {
                if (!multiLine && (event.character == '\r' || event.character == '\n')) {
                    try {
                        Object value = EcoreUtil.createFromString((EDataType) eClassifier,
                                choiceText.getText());
                        values.getChildren().add(value);
                        choiceText.setText(""); //$NON-NLS-1$
                        featureTableViewer.setSelection(new StructuredSelection(value));
                        event.doit = false;
                    } catch (RuntimeException exception) {
                        // Ignore
                    }
                } else if (event.character == '\33') {
                    choiceText.setText(""); //$NON-NLS-1$
                    event.doit = false;
                }
            }
        });
    }

    upButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            IStructuredSelection selection = (IStructuredSelection) featureTableViewer.getSelection();
            int minIndex = 0;
            for (Iterator<?> i = selection.iterator(); i.hasNext();) {
                Object value = i.next();
                int index = values.getChildren().indexOf(value);
                values.getChildren().move(Math.max(index - 1, minIndex++), value);
            }
        }
    });

    downButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            IStructuredSelection selection = (IStructuredSelection) featureTableViewer.getSelection();
            int maxIndex = values.getChildren().size() - selection.size();
            for (Iterator<?> i = selection.iterator(); i.hasNext();) {
                Object value = i.next();
                int index = values.getChildren().indexOf(value);
                values.getChildren().move(Math.min(index + 1, maxIndex++), value);
            }
        }
    });

    addButton.addSelectionListener(new SelectionAdapter() {
        // event is null when choiceTableViewer is double clicked
        @Override
        public void widgetSelected(SelectionEvent event) {
            if (choiceTableViewer != null) {
                IStructuredSelection selection = (IStructuredSelection) choiceTableViewer.getSelection();
                for (Iterator<?> i = selection.iterator(); i.hasNext();) {
                    Object value = i.next();
                    if (!values.getChildren().contains(value)) {
                        values.getChildren().add(value);
                    }
                }
                featureTableViewer.setSelection(selection);
            } else if (choiceText != null) {
                try {
                    Object value = EcoreUtil.createFromString((EDataType) eClassifier, choiceText.getText());
                    values.getChildren().add(value);
                    choiceText.setText(""); //$NON-NLS-1$
                    featureTableViewer.setSelection(new StructuredSelection(value));
                } catch (RuntimeException exception) {
                    // Ignore
                }
            }
        }
    });

    removeButton.addSelectionListener(new SelectionAdapter() {
        // event is null when featureTableViewer is double clicked
        @Override
        public void widgetSelected(SelectionEvent event) {
            IStructuredSelection selection = (IStructuredSelection) featureTableViewer.getSelection();
            Object firstValue = null;
            for (Iterator<?> i = selection.iterator(); i.hasNext();) {
                Object value = i.next();
                if (firstValue == null) {
                    firstValue = value;
                }
                values.getChildren().remove(value);
            }

            if (!values.getChildren().isEmpty()) {
                featureTableViewer.setSelection(new StructuredSelection(values.getChildren().get(0)));
            }

            if (choiceTableViewer != null) {
                choiceTableViewer.setSelection(selection);
            } else if (choiceText != null) {
                if (firstValue != null) {
                    String value = EcoreUtil.convertToString((EDataType) eClassifier, firstValue);
                    choiceText.setText(value);
                }
            }
        }
    });

    return contents;
}