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.e4.tools.emf.ui.internal.common.component.ToolBarContributionEditor.java

License:Open Source License

private Composite createForm(Composite parent, EMFDataBindingContext context, WritableValue master,
        boolean isImport) {
    CTabFolder folder = new CTabFolder(parent, SWT.BOTTOM);

    CTabItem item = new CTabItem(folder, SWT.NONE);
    item.setText(Messages.ModelTooling_Common_TabDefault);

    parent = createScrollableContainer(folder);
    item.setControl(parent.getParent());

    if (getEditor().isShowXMIId() || getEditor().isLiveModel()) {
        ControlFactory.createXMIId(parent, this);
    }/*from   w  w w. j  a v  a  2 s  . c o  m*/

    IWidgetValueProperty textProp = WidgetProperties.text(SWT.Modify);

    if (isImport) {
        ControlFactory.createFindImport(parent, Messages, this, context);
        folder.setSelection(0);
        return folder;
    }

    ControlFactory.createTextField(parent, Messages.ModelTooling_Common_Id, master, context, textProp,
            EMFEditProperties.value(getEditingDomain(),
                    ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID));
    ControlFactory.createTextField(parent, Messages.ModelTooling_UIElement_AccessibilityPhrase, getMaster(),
            context, textProp, EMFEditProperties.value(getEditingDomain(),
                    UiPackageImpl.Literals.UI_ELEMENT__ACCESSIBILITY_PHRASE));

    {
        Label l = new Label(parent, SWT.NONE);
        l.setText(Messages.MenuContributionEditor_ParentId);
        l.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));

        final Text t = new Text(parent, SWT.BORDER);
        TextPasteHandler.createFor(t);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        t.setLayoutData(gd);
        context.bindValue(textProp.observeDelayed(200, t),
                EMFEditProperties
                        .value(getEditingDomain(), MenuPackageImpl.Literals.TOOL_BAR_CONTRIBUTION__PARENT_ID)
                        .observeDetail(getMaster()));

        Button b = new Button(parent, SWT.PUSH | SWT.FLAT);
        b.setText(Messages.ModelTooling_Common_FindEllipsis);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_zoom));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                ToolBarIdDialog dialog = new ToolBarIdDialog(t.getShell(), resource,
                        (MToolBarContribution) getMaster().getValue(), getEditingDomain(), modelService,
                        Messages);
                dialog.open();
            }
        });
    }

    ControlFactory.createTextField(parent, Messages.MenuContributionEditor_Position, master, context, textProp,
            EMFEditProperties.value(getEditingDomain(),
                    MenuPackageImpl.Literals.TOOL_BAR_CONTRIBUTION__POSITION_IN_PARENT));

    // ------------------------------------------------------------
    {
        Label l = new Label(parent, SWT.NONE);
        l.setText(Messages.ToolBarEditor_ToolbarItems);
        l.setLayoutData(new GridData(GridData.END, GridData.BEGINNING, false, false));

        final TableViewer viewer = new TableViewer(parent);
        ObservableListContentProvider cp = new ObservableListContentProvider();
        viewer.setContentProvider(cp);

        GridData gd = new GridData(GridData.FILL_BOTH);
        viewer.getControl().setLayoutData(gd);
        viewer.setLabelProvider(new ComponentLabelProvider(getEditor(), Messages));

        IEMFListProperty prop = EMFEditProperties.list(getEditingDomain(),
                UiPackageImpl.Literals.ELEMENT_CONTAINER__CHILDREN);
        viewer.setInput(prop.observeDetail(master));

        Composite buttonComp = new Composite(parent, SWT.NONE);
        buttonComp.setLayoutData(new GridData(GridData.FILL, GridData.END, false, false));
        GridLayout gl = new GridLayout(2, false);
        gl.marginLeft = 0;
        gl.marginRight = 0;
        gl.marginWidth = 0;
        gl.marginHeight = 0;
        buttonComp.setLayout(gl);

        Button b = new Button(buttonComp, SWT.PUSH | SWT.FLAT);
        b.setText(Messages.ModelTooling_Common_Up);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_arrow_up));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (!viewer.getSelection().isEmpty()) {
                    IStructuredSelection s = (IStructuredSelection) viewer.getSelection();
                    if (s.size() == 1) {
                        Object obj = s.getFirstElement();
                        MElementContainer<?> container = (MElementContainer<?>) getMaster().getValue();
                        int idx = container.getChildren().indexOf(obj) - 1;
                        if (idx >= 0) {
                            if (Util.moveElementByIndex(getEditingDomain(), (MUIElement) obj,
                                    getEditor().isLiveModel(), idx)) {
                                viewer.setSelection(new StructuredSelection(obj));
                            }
                        }

                    }
                }
            }
        });

        b = new Button(buttonComp, SWT.PUSH | SWT.FLAT);
        b.setText(Messages.ModelTooling_Common_Down);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_arrow_down));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (!viewer.getSelection().isEmpty()) {
                    IStructuredSelection s = (IStructuredSelection) viewer.getSelection();
                    if (s.size() == 1) {
                        Object obj = s.getFirstElement();
                        MElementContainer<?> container = (MElementContainer<?>) getMaster().getValue();
                        int idx = container.getChildren().indexOf(obj) + 1;
                        if (idx < container.getChildren().size()) {
                            if (Util.moveElementByIndex(getEditingDomain(), (MUIElement) obj,
                                    getEditor().isLiveModel(), idx)) {
                                viewer.setSelection(new StructuredSelection(obj));
                            }
                        }

                    }
                }
            }
        });

        final ComboViewer childrenDropDown = new ComboViewer(buttonComp);
        childrenDropDown.getControl().setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
        childrenDropDown.setContentProvider(new ArrayContentProvider());
        childrenDropDown.setLabelProvider(new LabelProvider() {
            @Override
            public String getText(Object element) {
                Struct struct = (Struct) element;
                return struct.label;
            }
        });

        Struct defaultStruct = new Struct(Messages.ToolBarEditor_HandledToolItem,
                MenuPackageImpl.Literals.HANDLED_TOOL_ITEM, false);
        childrenDropDown.setInput(new Struct[] { defaultStruct,
                new Struct(Messages.ToolBarEditor_DirectToolItem, MenuPackageImpl.Literals.DIRECT_TOOL_ITEM,
                        false),
                new Struct(Messages.ToolBarEditor_ToolControl, MenuPackageImpl.Literals.TOOL_CONTROL, false),
                new Struct(Messages.ToolBarEditor_Separator, MenuPackageImpl.Literals.TOOL_BAR_SEPARATOR,
                        true) });
        childrenDropDown.setSelection(new StructuredSelection(defaultStruct));

        b = new Button(buttonComp, SWT.PUSH | SWT.FLAT);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_table_add));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (!childrenDropDown.getSelection().isEmpty()) {
                    Struct struct = (Struct) ((IStructuredSelection) childrenDropDown.getSelection())
                            .getFirstElement();
                    EClass eClass = struct.eClass;
                    MToolBarElement eObject = (MToolBarElement) EcoreUtil.create(eClass);
                    setElementId(eObject);

                    Command cmd = AddCommand.create(getEditingDomain(), getMaster().getValue(),
                            UiPackageImpl.Literals.ELEMENT_CONTAINER__CHILDREN, eObject);

                    if (cmd.canExecute()) {
                        getEditingDomain().getCommandStack().execute(cmd);
                        if (!struct.separator) {
                            getEditor().setSelection(eObject);
                        }
                    }
                }
            }
        });

        b = new Button(buttonComp, SWT.PUSH | SWT.FLAT);
        b.setText(Messages.ModelTooling_Common_Remove);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_table_delete));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (!viewer.getSelection().isEmpty()) {
                    List<?> keybinding = ((IStructuredSelection) viewer.getSelection()).toList();
                    Command cmd = RemoveCommand.create(getEditingDomain(), getMaster().getValue(),
                            UiPackageImpl.Literals.ELEMENT_CONTAINER__CHILDREN, keybinding);
                    if (cmd.canExecute()) {
                        getEditingDomain().getCommandStack().execute(cmd);
                    }
                }
            }
        });
    }

    ControlFactory.createCheckBox(parent, Messages.ModelTooling_UIElement_ToBeRendered, getMaster(), context,
            WidgetProperties.selection(),
            EMFEditProperties.value(getEditingDomain(), UiPackageImpl.Literals.UI_ELEMENT__TO_BE_RENDERED));
    ControlFactory.createCheckBox(parent, Messages.ModelTooling_UIElement_Visible, getMaster(), context,
            WidgetProperties.selection(),
            EMFEditProperties.value(getEditingDomain(), UiPackageImpl.Literals.UI_ELEMENT__VISIBLE));

    item = new CTabItem(folder, SWT.NONE);
    item.setText(Messages.ModelTooling_Common_TabSupplementary);

    parent = createScrollableContainer(folder);
    item.setControl(parent.getParent());

    ControlFactory.createStringListWidget(parent, Messages, this, Messages.CategoryEditor_Tags,
            ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__TAGS, VERTICAL_LIST_WIDGET_INDENT);
    ControlFactory.createMapProperties(parent, Messages, this,
            Messages.ModelTooling_Contribution_PersistedState,
            ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__PERSISTED_STATE, VERTICAL_LIST_WIDGET_INDENT);

    if (project == null) {
        createUITreeInspection(folder);
    }

    createContributedEditorTabs(folder, context, getMaster(), MToolBarContribution.class);

    folder.setSelection(0);

    return folder;
}

From source file:org.eclipse.e4.tools.emf.ui.internal.common.component.ToolBarEditor.java

License:Open Source License

private Composite createForm(Composite parent, EMFDataBindingContext context, WritableValue master,
        boolean isImport) {
    CTabFolder folder = new CTabFolder(parent, SWT.BOTTOM);

    CTabItem item = new CTabItem(folder, SWT.NONE);
    item.setText(Messages.ModelTooling_Common_TabDefault);

    parent = createScrollableContainer(folder);
    item.setControl(parent.getParent());

    if (getEditor().isShowXMIId() || getEditor().isLiveModel()) {
        ControlFactory.createXMIId(parent, this);
    }/* w  w w . j  a v a  2 s. com*/

    IWidgetValueProperty textProp = WidgetProperties.text(SWT.Modify);

    if (isImport) {
        ControlFactory.createFindImport(parent, Messages, this, context);
        folder.setSelection(0);
        return folder;
    }

    ControlFactory.createTextField(parent, Messages.ModelTooling_Common_Id, master, context, textProp,
            EMFEditProperties.value(getEditingDomain(),
                    ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID));
    ControlFactory.createTextField(parent, Messages.ModelTooling_UIElement_AccessibilityPhrase, getMaster(),
            context, textProp, EMFEditProperties.value(getEditingDomain(),
                    UiPackageImpl.Literals.UI_ELEMENT__ACCESSIBILITY_PHRASE));

    // ------------------------------------------------------------
    {
        Label l = new Label(parent, SWT.NONE);
        l.setText(Messages.ToolBarEditor_ToolbarItems);
        l.setLayoutData(new GridData(GridData.END, GridData.BEGINNING, false, false));

        final TableViewer viewer = new TableViewer(parent);
        ObservableListContentProvider cp = new ObservableListContentProvider();
        viewer.setContentProvider(cp);

        GridData gd = new GridData(GridData.FILL_BOTH);
        viewer.getControl().setLayoutData(gd);
        viewer.setLabelProvider(new ComponentLabelProvider(getEditor(), Messages));

        IEMFListProperty prop = EMFEditProperties.list(getEditingDomain(),
                UiPackageImpl.Literals.ELEMENT_CONTAINER__CHILDREN);
        viewer.setInput(prop.observeDetail(master));

        Composite buttonComp = new Composite(parent, SWT.NONE);
        buttonComp.setLayoutData(new GridData(GridData.FILL, GridData.END, false, false));
        GridLayout gl = new GridLayout(2, false);
        gl.marginLeft = 0;
        gl.marginRight = 0;
        gl.marginWidth = 0;
        gl.marginHeight = 0;
        buttonComp.setLayout(gl);

        Button b = new Button(buttonComp, SWT.PUSH | SWT.FLAT);
        b.setText(Messages.ModelTooling_Common_Up);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_arrow_up));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (!viewer.getSelection().isEmpty()) {
                    IStructuredSelection s = (IStructuredSelection) viewer.getSelection();
                    if (s.size() == 1) {
                        Object obj = s.getFirstElement();
                        MElementContainer<?> container = (MElementContainer<?>) getMaster().getValue();
                        int idx = container.getChildren().indexOf(obj) - 1;
                        if (idx >= 0) {
                            if (Util.moveElementByIndex(getEditingDomain(), (MUIElement) obj,
                                    getEditor().isLiveModel(), idx)) {
                                viewer.setSelection(new StructuredSelection(obj));
                            }
                        }

                    }
                }
            }
        });

        b = new Button(buttonComp, SWT.PUSH | SWT.FLAT);
        b.setText(Messages.ModelTooling_Common_Down);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_arrow_down));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (!viewer.getSelection().isEmpty()) {
                    IStructuredSelection s = (IStructuredSelection) viewer.getSelection();
                    if (s.size() == 1) {
                        Object obj = s.getFirstElement();
                        MElementContainer<?> container = (MElementContainer<?>) getMaster().getValue();
                        int idx = container.getChildren().indexOf(obj) + 1;
                        if (idx < container.getChildren().size()) {
                            if (Util.moveElementByIndex(getEditingDomain(), (MUIElement) obj,
                                    getEditor().isLiveModel(), idx)) {
                                viewer.setSelection(new StructuredSelection(obj));
                            }
                        }

                    }
                }
            }
        });

        final ComboViewer childrenDropDown = new ComboViewer(buttonComp);
        childrenDropDown.getControl().setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
        childrenDropDown.setContentProvider(new ArrayContentProvider());
        childrenDropDown.setLabelProvider(new LabelProvider() {
            @Override
            public String getText(Object element) {
                Struct struct = (Struct) element;
                return struct.label;
            }
        });

        Struct defaultStruct = new Struct(Messages.ToolBarEditor_HandledToolItem,
                MenuPackageImpl.Literals.HANDLED_TOOL_ITEM, false);
        childrenDropDown.setInput(new Struct[] { defaultStruct,
                new Struct(Messages.ToolBarEditor_DirectToolItem, MenuPackageImpl.Literals.DIRECT_TOOL_ITEM,
                        false),
                new Struct(Messages.ToolBarEditor_ToolControl, MenuPackageImpl.Literals.TOOL_CONTROL, false),
                new Struct(Messages.ToolBarEditor_Separator, MenuPackageImpl.Literals.TOOL_BAR_SEPARATOR,
                        true) });
        childrenDropDown.setSelection(new StructuredSelection(defaultStruct));

        b = new Button(buttonComp, SWT.PUSH | SWT.FLAT);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_table_add));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (!childrenDropDown.getSelection().isEmpty()) {
                    Struct struct = (Struct) ((IStructuredSelection) childrenDropDown.getSelection())
                            .getFirstElement();
                    EClass eClass = struct.eClass;
                    handleAddChild(eClass, struct.separator);
                }
            }
        });

        b = new Button(buttonComp, SWT.PUSH | SWT.FLAT);
        b.setText(Messages.ModelTooling_Common_Remove);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_table_delete));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (!viewer.getSelection().isEmpty()) {
                    List<?> keybinding = ((IStructuredSelection) viewer.getSelection()).toList();
                    Command cmd = RemoveCommand.create(getEditingDomain(), getMaster().getValue(),
                            UiPackageImpl.Literals.ELEMENT_CONTAINER__CHILDREN, keybinding);
                    if (cmd.canExecute()) {
                        getEditingDomain().getCommandStack().execute(cmd);
                    }
                }
            }
        });
    }

    ControlFactory.createCheckBox(parent, "To Be Rendered", getMaster(), context, WidgetProperties.selection(), //$NON-NLS-1$
            EMFEditProperties.value(getEditingDomain(), UiPackageImpl.Literals.UI_ELEMENT__TO_BE_RENDERED));
    ControlFactory.createCheckBox(parent, "Visible", getMaster(), context, WidgetProperties.selection(), //$NON-NLS-1$
            EMFEditProperties.value(getEditingDomain(), UiPackageImpl.Literals.UI_ELEMENT__VISIBLE));

    item = new CTabItem(folder, SWT.NONE);
    item.setText(Messages.ModelTooling_Common_TabSupplementary);

    parent = createScrollableContainer(folder);
    item.setControl(parent.getParent());

    ControlFactory.createStringListWidget(parent, Messages, this, Messages.CategoryEditor_Tags,
            ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__TAGS, VERTICAL_LIST_WIDGET_INDENT);
    ControlFactory.createMapProperties(parent, Messages, this,
            Messages.ModelTooling_Contribution_PersistedState,
            ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__PERSISTED_STATE, VERTICAL_LIST_WIDGET_INDENT);

    createContributedEditorTabs(folder, context, getMaster(), MToolBar.class);

    folder.setSelection(0);

    return folder;
}

From source file:org.eclipse.e4.tools.emf.ui.internal.common.component.TrimContributionEditor.java

License:Open Source License

private Composite createForm(Composite parent, EMFDataBindingContext context, WritableValue master,
        boolean isImport) {
    CTabFolder folder = new CTabFolder(parent, SWT.BOTTOM);

    CTabItem item = new CTabItem(folder, SWT.NONE);
    item.setText(Messages.ModelTooling_Common_TabDefault);

    parent = createScrollableContainer(folder);
    item.setControl(parent.getParent());

    if (getEditor().isShowXMIId() || getEditor().isLiveModel()) {
        ControlFactory.createXMIId(parent, this);
    }// w  w w. ja v a 2  s. c om

    IWidgetValueProperty textProp = WidgetProperties.text(SWT.Modify);

    if (isImport) {
        ControlFactory.createFindImport(parent, Messages, this, context);
        folder.setSelection(0);
        return folder;
    }

    ControlFactory.createTextField(parent, Messages.ModelTooling_Common_Id, master, context, textProp,
            EMFEditProperties.value(getEditingDomain(),
                    ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__ELEMENT_ID));
    ControlFactory.createTextField(parent, Messages.ModelTooling_UIElement_AccessibilityPhrase, getMaster(),
            context, textProp, EMFEditProperties.value(getEditingDomain(),
                    UiPackageImpl.Literals.UI_ELEMENT__ACCESSIBILITY_PHRASE));
    {
        Label l = new Label(parent, SWT.NONE);
        l.setText(Messages.MenuContributionEditor_ParentId);
        l.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));

        final Text t = new Text(parent, SWT.BORDER);
        TextPasteHandler.createFor(t);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        t.setLayoutData(gd);
        context.bindValue(textProp.observeDelayed(200, t),
                EMFEditProperties
                        .value(getEditingDomain(), MenuPackageImpl.Literals.MENU_CONTRIBUTION__PARENT_ID)
                        .observeDetail(getMaster()));

        Button b = new Button(parent, SWT.PUSH | SWT.FLAT);
        b.setText(Messages.ModelTooling_Common_FindEllipsis);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_zoom));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                TrimIdDialog dialog = new TrimIdDialog(t.getShell(), resource,
                        (MTrimContribution) getMaster().getValue(), getEditingDomain(), modelService, Messages);
                dialog.open();
            }
        });

    }

    ControlFactory.createTextField(parent, Messages.TrimContributionEditor_Position, master, context, textProp,
            EMFEditProperties.value(getEditingDomain(),
                    MenuPackageImpl.Literals.TRIM_CONTRIBUTION__POSITION_IN_PARENT));

    // ------------------------------------------------------------
    {
        Label l = new Label(parent, SWT.NONE);
        l.setText(Messages.TrimContributionEditor_Controls);
        l.setLayoutData(new GridData(GridData.END, GridData.BEGINNING, false, false));

        final TableViewer viewer = new TableViewer(parent);
        viewer.setLabelProvider(new ComponentLabelProvider(getEditor(), Messages));
        viewer.setContentProvider(new ObservableListContentProvider());
        GridData gd = new GridData(GridData.FILL_BOTH);
        viewer.getControl().setLayoutData(gd);

        IEMFListProperty prop = EMFProperties.list(UiPackageImpl.Literals.ELEMENT_CONTAINER__CHILDREN);
        viewer.setInput(prop.observeDetail(getMaster()));

        Composite buttonComp = new Composite(parent, SWT.NONE);
        buttonComp.setLayoutData(new GridData(GridData.FILL, GridData.END, false, false));
        GridLayout gl = new GridLayout(2, false);
        gl.marginLeft = 0;
        gl.marginRight = 0;
        gl.marginWidth = 0;
        gl.marginHeight = 0;
        buttonComp.setLayout(gl);

        Button b = new Button(buttonComp, SWT.PUSH | SWT.FLAT);
        b.setText(Messages.ModelTooling_Common_Up);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_arrow_up));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (!viewer.getSelection().isEmpty()) {
                    IStructuredSelection s = (IStructuredSelection) viewer.getSelection();
                    if (s.size() == 1) {
                        Object obj = s.getFirstElement();
                        MElementContainer<?> container = (MElementContainer<?>) getMaster().getValue();
                        int idx = container.getChildren().indexOf(obj) - 1;
                        if (idx >= 0) {
                            if (Util.moveElementByIndex(getEditingDomain(), (MUIElement) obj,
                                    getEditor().isLiveModel(), idx)) {
                                viewer.setSelection(new StructuredSelection(obj));
                            }
                        }

                    }
                }
            }
        });

        b = new Button(buttonComp, SWT.PUSH | SWT.FLAT);
        b.setText(Messages.ModelTooling_Common_Down);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_arrow_down));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (!viewer.getSelection().isEmpty()) {
                    IStructuredSelection s = (IStructuredSelection) viewer.getSelection();
                    if (s.size() == 1) {
                        Object obj = s.getFirstElement();
                        MElementContainer<?> container = (MElementContainer<?>) getMaster().getValue();
                        int idx = container.getChildren().indexOf(obj) + 1;
                        if (idx < container.getChildren().size()) {
                            if (Util.moveElementByIndex(getEditingDomain(), (MUIElement) obj,
                                    getEditor().isLiveModel(), idx)) {
                                viewer.setSelection(new StructuredSelection(obj));
                            }
                        }

                    }
                }
            }
        });

        final ComboViewer typeViewer = new ComboViewer(buttonComp, SWT.READ_ONLY);
        typeViewer.setContentProvider(new ArrayContentProvider());
        typeViewer.setLabelProvider(new LabelProvider() {
            @Override
            public String getText(Object element) {
                return ((EClass) element).getName();
            }
        });
        typeViewer.setInput(
                new Object[] { MenuPackageImpl.Literals.TOOL_BAR, MenuPackageImpl.Literals.TOOL_CONTROL });
        typeViewer.setSelection(new StructuredSelection(MenuPackageImpl.Literals.TOOL_BAR));

        b = new Button(buttonComp, SWT.PUSH | SWT.FLAT);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_table_add));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                EObject toolbar = EcoreUtil
                        .create((EClass) ((IStructuredSelection) typeViewer.getSelection()).getFirstElement());
                setElementId(toolbar);

                Command cmd = AddCommand.create(getEditingDomain(), getMaster().getValue(),
                        UiPackageImpl.Literals.ELEMENT_CONTAINER__CHILDREN, toolbar);

                if (cmd.canExecute()) {
                    getEditingDomain().getCommandStack().execute(cmd);
                    getEditor().setSelection(toolbar);
                }
            }
        });

        b = new Button(buttonComp, SWT.PUSH | SWT.FLAT);
        b.setText(Messages.ModelTooling_Common_Remove);
        b.setImage(createImage(ResourceProvider.IMG_Obj16_table_delete));
        b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (!viewer.getSelection().isEmpty()) {
                    List<?> elements = ((IStructuredSelection) viewer.getSelection()).toList();

                    Command cmd = RemoveCommand.create(getEditingDomain(), getMaster().getValue(),
                            UiPackageImpl.Literals.ELEMENT_CONTAINER__CHILDREN, elements);
                    if (cmd.canExecute()) {
                        getEditingDomain().getCommandStack().execute(cmd);
                    }
                }
            }
        });
    }

    ControlFactory.createCheckBox(parent, Messages.ModelTooling_UIElement_ToBeRendered, getMaster(), context,
            WidgetProperties.selection(),
            EMFEditProperties.value(getEditingDomain(), UiPackageImpl.Literals.UI_ELEMENT__TO_BE_RENDERED));
    ControlFactory.createCheckBox(parent, Messages.ModelTooling_UIElement_Visible, getMaster(), context,
            WidgetProperties.selection(),
            EMFEditProperties.value(getEditingDomain(), UiPackageImpl.Literals.UI_ELEMENT__VISIBLE));

    item = new CTabItem(folder, SWT.NONE);
    item.setText(Messages.ModelTooling_Common_TabSupplementary);

    parent = createScrollableContainer(folder);
    item.setControl(parent.getParent());

    ControlFactory.createStringListWidget(parent, Messages, this, Messages.CategoryEditor_Tags,
            ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__TAGS, VERTICAL_LIST_WIDGET_INDENT);
    ControlFactory.createMapProperties(parent, Messages, this,
            Messages.ModelTooling_Contribution_PersistedState,
            ApplicationPackageImpl.Literals.APPLICATION_ELEMENT__PERSISTED_STATE, VERTICAL_LIST_WIDGET_INDENT);

    if (project == null) {
        createUITreeInspection(folder);
    }

    createContributedEditorTabs(folder, context, getMaster(), MTrimContribution.class);

    folder.setSelection(0);

    return folder;
}

From source file:org.eclipse.e4.tools.ui.designer.wizards.part.AbstractDataContextSelectionWizardPage.java

License:Open Source License

protected TableViewer createDataContextViewer(Composite parent) {
    final TableViewer tableViewer = new TableViewer(parent,
            SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    tableViewer.setContentProvider(new IStructuredContentProvider() {

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }/*from   ww  w  .  j av  a 2 s. co m*/

        public void dispose() {
        }

        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof Object[]) {
                return (Object[]) inputElement;
            }
            return new Object[0];
        }
    });
    tableViewer.setLabelProvider(new PDCTypeLabelProvider());

    return tableViewer;
}

From source file:org.eclipse.e4.ui.internal.workbench.renderers.swt.BasicPartList.java

License:Open Source License

protected TableViewer createTableViewer(Composite parent, int style) {
    Table table = new Table(parent, SWT.SINGLE | (style & ~SWT.MULTI));
    table.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false));
    TableViewer tableViewer = new TableViewer(table);
    tableViewer.addFilter(new NamePatternFilter());
    tableViewer.setContentProvider(ArrayContentProvider.getInstance());
    tableViewer.setLabelProvider(new BasicStackListLabelProvider());

    ColumnViewerToolTipSupport.enableFor(tableViewer);
    table.addListener(SWT.Dispose, new Listener() {
        public void handleEvent(Event event) {
            for (Image image : images.values()) {
                image.dispose();//ww w.j a v  a 2  s .  c om
            }
        }
    });
    return tableViewer;
}

From source file:org.eclipse.e4.xwt.tests.snipppets.Snippet007ColorLabelProvider.java

License:Open Source License

/**
 * @param args/*from   w w  w. ja va2s  .co m*/
 */
public static void main(String[] args) {
    final List persons = new ArrayList();
    persons.add(new Person("Fiona Apple", Person.FEMALE));
    persons.add(new Person("Elliot Smith", Person.MALE));
    persons.add(new Person("Diana Krall", Person.FEMALE));
    persons.add(new Person("David Gilmour", Person.MALE));

    final Display display = new Display();
    Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
        public void run() {
            Shell shell = new Shell(display);
            shell.setText("Gender Bender");
            shell.setLayout(new GridLayout());

            Table table = new Table(shell, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
            GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
            table.setLayoutData(gridData);
            table.setHeaderVisible(true);
            table.setLinesVisible(true);
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setText("No");
            column.setWidth(20);
            column = new TableColumn(table, SWT.NONE);
            column.setText("Name");
            column.setWidth(100);
            final TableViewer viewer = new TableViewer(table);

            IObservableList observableList = Observables.staticObservableList(persons);
            ObservableListContentProvider contentProvider = new ObservableListContentProvider();

            viewer.setContentProvider(contentProvider);

            // this does not have to correspond to the columns in the table,
            // we just list all attributes that affect the table content.
            IObservableMap[] attributes = BeansObservables.observeMaps(contentProvider.getKnownElements(),
                    Person.class, new String[] { "name", "gender" });

            class ColorLabelProvider extends ObservableMapLabelProvider implements ITableColorProvider {
                Color male = display.getSystemColor(SWT.COLOR_BLUE);

                Color female = new Color(display, 255, 192, 203);

                ColorLabelProvider(IObservableMap[] attributes) {
                    super(attributes);
                }

                // to drive home the point that attributes does not have to
                // match
                // the columns
                // in the table, we change the column text as follows:
                public String getColumnText(Object element, int index) {
                    if (index == 0) {
                        return Integer.toString(persons.indexOf(element) + 1);
                    }
                    return ((Person) element).getName();
                }

                public Color getBackground(Object element, int index) {
                    return null;
                }

                public Color getForeground(Object element, int index) {
                    if (index == 0)
                        return null;
                    Person person = (Person) element;
                    return (person.getGender() == Person.MALE) ? male : female;
                }

                public void dispose() {
                    super.dispose();
                    female.dispose();
                }
            }
            viewer.setLabelProvider(new ColorLabelProvider(attributes));

            viewer.setInput(observableList);

            table.getColumn(0).pack();

            Button button = new Button(shell, SWT.PUSH);
            button.setText("Toggle Gender");
            button.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent arg0) {
                    StructuredSelection selection = (StructuredSelection) viewer.getSelection();
                    if (selection != null && !selection.isEmpty()) {
                        Person person = (Person) selection.getFirstElement();
                        person.setGender((person.getGender() == Person.MALE) ? Person.FEMALE : Person.MALE);
                    }
                }
            });

            shell.setSize(300, 400);
            shell.open();

            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
        }
    });
    display.dispose();
}

From source file:org.eclipse.e4mf.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 .j a  va2  s .c  o m

    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() {
            @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() {
                public void modifyText(ModifyEvent e) {
                    filter.setPattern(((Text) e.widget).getText());
                    choiceTableViewer.refresh();
                }
            });
        }
        if (unique) {
            choiceTableViewer.addFilter(new ViewerFilter() {

                @Override
                public boolean select(Viewer viewer, Object parentElement, Object element) {
                    return !values.getChildren().contains(element);
                }
            });
        }
        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);
    final EList<Object> children = values.getChildren();
    if (!children.isEmpty()) {
        featureTableViewer.setSelection(new StructuredSelection(children.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());
                        children.add(value);
                        choiceText.setText("");
                        featureTableViewer.refresh();
                        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() {
        @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 = children.indexOf(value);
                children.move(Math.max(index - 1, minIndex++), value);
            }
        }
    });

    downButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            IStructuredSelection selection = (IStructuredSelection) featureTableViewer.getSelection();
            int maxIndex = children.size() - 1;
            List<?> objects = selection.toList();
            for (ListIterator<?> i = objects.listIterator(objects.size()); i.hasPrevious();) {
                Object value = i.previous();
                int index = children.indexOf(value);
                children.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 (!unique || !children.contains(value)) {
                        children.add(value);
                    }
                }
                featureTableViewer.refresh();
                featureTableViewer.setSelection(selection);
                choiceTableViewer.refresh();
            } else if (choiceText != null) {
                try {
                    Object value = EcoreUtil.createFromString((EDataType) eClassifier, choiceText.getText());
                    if (!unique || !children.contains(value)) {
                        children.add(value);
                        choiceText.setText("");
                    }
                    featureTableViewer.refresh();
                    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;
                }
                children.remove(value);
            }

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

            if (choiceTableViewer != null) {
                choiceTableViewer.refresh();
                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.eavp.viz.service.connections.preferences.TableComponentComposite.java

License:Open Source License

/**
 * Creates a new {@code TableViewer} to show a {@link TableComponent}.
 * //from   w ww.j a  v  a2  s .  com
 * @param parent
 *            The containing {@code Composite}.
 * @return The created JFace {@code TableViewer}.
 */
private TableViewer createTableViewer(Composite parent) {
    // Create the TableViewer and the underlying Table Control.
    TableViewer viewer = new TableViewer(this,
            SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
    // Set some properties for the table.
    final Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    // Set up the content provider for the viewer. Now the viewer's input
    // can be set.
    viewer.setContentProvider(new TableComponentContentProvider());

    // Enable tool tips for the Table's cells.
    ColumnViewerToolTipSupport.enableFor(viewer, ToolTip.NO_RECREATE);

    // Add a selection listener to tell the TableComponent which rows are
    // selected.
    ISelectionChangedListener listener = new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {

            // To delete rows (later), the TableComponent expects indices.
            // Thus, we must keep track of selected indicies by updating the
            // class' list.

            // To select rows, the TableComponent expects an
            // ArrayList<Integer> containing the row IDs, not indices. Thus,
            // we must convert the int[] of indices into an
            // ArrayList<Integer> of IDs to select.
            int[] selected = table.getSelectionIndices();
            List<Integer> rowIds = tableComponent.getRowIds();
            ArrayList<Integer> rows = new ArrayList<Integer>(selected.length);
            selectedIndices.clear();
            for (int i : selected) {
                selectedIndices.add(i);
                rows.add(rowIds.get(i));
            }
            tableComponent.setSelectedRows(rows);

            // Enable/disable the remove action if the selection is
            // non/empty.
            removeRowAction.setEnabled(!event.getSelection().isEmpty());

            return;
        }
    };
    viewer.addSelectionChangedListener(listener);

    // Use a custom comparer to just check references rather than calling
    // equals(Object), which causes strange behavior when multiple rows have
    // the same values.
    viewer.setComparer(new IElementComparer() {
        @Override
        public boolean equals(Object a, Object b) {
            return a == b;
        }

        @Override
        public int hashCode(Object element) {
            return element.hashCode();
        }
    });

    return viewer;
}

From source file:org.eclipse.ecf.internal.presence.ui.dialogs.ChatRoomSelectionDialog.java

License:Open Source License

protected Control createDialogArea(Composite parent) {
    Composite main = new Composite(parent, SWT.NONE);
    main.setLayout(new GridLayout());
    main.setLayoutData(new GridData(GridData.FILL_BOTH));

    TableViewer viewer = new TableViewer(main,
            SWT.VIRTUAL | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
    Table table = viewer.getTable();// w w  w. j  a  v a2 s . c  o  m

    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    TableColumn tc = new TableColumn(table, SWT.NONE);
    tc.setText(Messages.ChatRoomSelectionDialog_ROOM_NAME_COLUMN);
    //      tc.pack();
    //      int width = tc.getWidth();
    tc.setWidth(X_INITIAL_SIZE / 3);
    tc = new TableColumn(table, SWT.NONE);
    tc.setText(Messages.ChatRoomSelectionDialog_SUBJECT_COLUMN);
    tc.pack();
    int width;
    width = tc.getWidth();
    tc.setWidth(width + (width / 4));
    tc = new TableColumn(table, SWT.NONE);
    tc.setText(Messages.ChatRoomSelectionDialog_DESCRIPTION_COLUMN);
    tc.pack();
    width = tc.getWidth();
    tc.setWidth(width + (width / 4));
    tc = new TableColumn(table, SWT.NONE);
    tc.setText(Messages.ChatRoomSelectionDialog_MEMBERS_COLUMN);
    tc.pack();
    tc = new TableColumn(table, SWT.NONE);
    tc.setText(Messages.ChatRoomSelectionDialog_MODERATED_COLUMN);
    tc.pack();
    tc = new TableColumn(table, SWT.NONE);
    tc.setText(Messages.ChatRoomSelectionDialog_PERSISTENT_COLUMN);
    tc.pack();
    tc = new TableColumn(table, SWT.NONE);
    tc.setText(Messages.ChatRoomSelectionDialog_ACCOUNT_COLUMN);
    tc.pack();

    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            ISelection selection = event.getSelection();
            IStructuredSelection ss = (IStructuredSelection) ((selection instanceof IStructuredSelection)
                    ? selection
                    : null);
            Object firstElement = (ss == null) ? null : ss.getFirstElement();
            if (!event.getSelection().isEmpty() && !loadingRoom.equals(firstElement)) {
                ChatRoomSelectionDialog.this.getButton(Window.OK).setEnabled(true);
            }
        }

    });

    viewer.setContentProvider(new DeferredContentProvider(new Comparator() {
        public int compare(Object r1, Object r2) {
            Room room1 = (Room) r1;
            Room room2 = (Room) r2;
            return room1.getRoomInfo().getName().compareTo(room2.getRoomInfo().getName());
        }
    }));
    viewer.setLabelProvider(new ChatRoomLabelProvider());

    rooms.addAll(Arrays.asList(new Room[] { loadingRoom }));

    viewer.setInput(rooms);

    this.setTitle(Messages.ChatRoomSelectionDialog_TITLE);
    this.setMessage(Messages.ChatRoomSelectionDialog_MESSAGE);

    viewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent e) {
            IStructuredSelection s = (IStructuredSelection) e.getSelection();
            Object o = s.getFirstElement();
            if (o instanceof Room) {
                selectedRoom = (Room) o;
            }
        }

    });

    viewer.addDoubleClickListener(new IDoubleClickListener() {

        public void doubleClick(DoubleClickEvent event) {
            if (selectedRoom != null) {
                ChatRoomSelectionDialog.this.okPressed();
            }
        }

    });

    startRetrieveJob();

    applyDialogFont(parent);
    return parent;
}

From source file:org.eclipse.ecf.tutorial.scribbleshare.ScribbleView.java

License:Open Source License

public void createPartControl(Composite parent) {
    Composite backgroundComposite = new Composite(parent, SWT.NONE);
    GridLayout backgroundGridLayout = new GridLayout(3, false);
    backgroundGridLayout.marginHeight = 0;
    backgroundGridLayout.marginBottom = 0;
    backgroundGridLayout.marginLeft = 0;
    backgroundGridLayout.marginRight = 0;
    backgroundGridLayout.marginWidth = 0;
    backgroundGridLayout.horizontalSpacing = 0;
    backgroundComposite.setLayout(backgroundGridLayout);
    backgroundComposite.setLayoutData(new GridData(GridData.FILL_BOTH));

    Composite paletteComposite = new Composite(backgroundComposite, SWT.NONE);
    backgroundGridLayout = new GridLayout();
    backgroundGridLayout.marginHeight = 0;
    backgroundGridLayout.marginBottom = 0;
    backgroundGridLayout.marginLeft = 0;
    backgroundGridLayout.marginRight = 0;
    backgroundGridLayout.marginWidth = 0;
    backgroundGridLayout.horizontalSpacing = 0;
    paletteComposite.setLayout(backgroundGridLayout);
    GridData toolboxGridData = new GridData(GridData.FILL_VERTICAL);
    toolboxGridData.widthHint = 60;/*from  w  w w . j  a  va 2s  .  co m*/
    paletteComposite.setLayoutData(toolboxGridData);

    final TableViewer toolbox = new TableViewer(paletteComposite, SWT.FLAT | SWT.FULL_SELECTION);
    toolboxGridData = new GridData(GridData.FILL_BOTH);
    toolbox.getTable().setLayoutData(toolboxGridData);
    toolbox.setLabelProvider(new ToolboxLabelProvider());
    toolbox.setContentProvider(new ListContentProvider());
    toolbox.setInput(createTools());
    toolbox.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            currentTool = (AbstractTool) ((StructuredSelection) toolbox.getSelection()).getFirstElement();
            // Apply the drawSettings to the currently selected tool.
            currentTool.setDrawSettings(drawSettings);
        }
    });

    // Create the UI widgets to modify the DrawSettings instance.
    createSettings(paletteComposite);

    Label separator = new Label(backgroundComposite, SWT.SEPARATOR | SWT.VERTICAL/* SWT.NONE */);
    separator.setLayoutData(new GridData(GridData.FILL_VERTICAL));

    canvas = new Canvas(backgroundComposite, SWT.NONE);
    canvas.setLayoutData(new GridData(GridData.FILL_BOTH));
    display = parent.getDisplay();
    canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
    canvas.addPaintListener(new PaintListener() {

        public void paintControl(PaintEvent e) {
            for (Iterator i = tools.iterator(); i.hasNext();) {
                AbstractTool at = (AbstractTool) i.next();
                at.draw(canvas);
            }
        }

    });
    Listener listener = new Listener() {
        public void handleEvent(Event event) {
            if (currentTool != null) {
                // Have the tool interpret the mouse events.
                currentTool.handleUIEvent(event, canvas);

                // If the tool interaction is complete, send the tool to
                // other clients for rendering.
                if (currentTool.isComplete()) {
                    tools.add(currentTool);

                    sendTool(currentTool);
                    // Only do this once per Tool.
                    currentTool.setComplete(false);
                } /*else {
                   if (currentTool instanceof Pencil) {
                      tools.add(currentTool);
                   }
                  }*/
            }
        }
    };
    canvas.addListener(SWT.MouseDown, listener);
    canvas.addListener(SWT.MouseMove, listener);
    canvas.addListener(SWT.MouseUp, listener);
}