Example usage for org.eclipse.jface.viewers TreeViewer setSelection

List of usage examples for org.eclipse.jface.viewers TreeViewer setSelection

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers TreeViewer setSelection.

Prototype

@Override
    protected void setSelection(List<Item> items) 

Source Link

Usage

From source file:org.eclipse.sapphire.workspace.ui.WorkspaceRelativePathPropertyEditorPresentation.java

License:Open Source License

@Override

protected void createContents(final Composite parent) {
    final PropertyEditorPart part = part();
    final Value<?> value = (Value<?>) part.property();
    final Element element = value.element();

    final Text textField = (Text) super.createContents(parent, true);

    final Composite drillDownParent = createMainComposite(parent, new CreateMainCompositeDelegate(part) {
        @Override//from w  w  w. j  ava2s.com
        public boolean getShowLabel() {
            return false;
        }
    });

    drillDownParent.setLayout(glayout(1, 9, 0, 0, 0));

    final DrillDownComposite drillDown = new DrillDownComposite(drillDownParent, SWT.BORDER);
    drillDown.setLayoutData(gdfill());

    final TreeViewer treeViewer = new TreeViewer(drillDown, SWT.NONE);
    final Tree tree = treeViewer.getTree();
    drillDown.setChildTree(treeViewer);

    treeViewer.setContentProvider(new WorkbenchContentProvider());
    treeViewer.setLabelProvider(WorkbenchLabelProvider.getDecoratingWorkbenchLabelProvider());
    treeViewer.setSorter(new ViewerSorter());

    final ValidFileSystemResourceType validFileSystemResourceTypeAnnotation = value.definition()
            .getAnnotation(ValidFileSystemResourceType.class);

    if (validFileSystemResourceTypeAnnotation != null) {
        if (validFileSystemResourceTypeAnnotation.value() == FileSystemResourceType.FOLDER) {
            treeViewer.addFilter(new ContainersOnlyViewerFilter());
        }

        final FileExtensionsService fileExtensionsService = value.service(FileExtensionsService.class);

        if (fileExtensionsService != null) {
            final ExtensionBasedViewerFilter filter = new ExtensionBasedViewerFilter(
                    fileExtensionsService.extensions());

            treeViewer.addFilter(filter);

            final Listener listener = new Listener() {
                @Override
                public void handle(final Event event) {
                    filter.change(fileExtensionsService.extensions());
                    treeViewer.refresh();
                }
            };

            fileExtensionsService.attach(listener);

            tree.addDisposeListener(new DisposeListener() {
                public void widgetDisposed(final DisposeEvent event) {
                    fileExtensionsService.detach(listener);
                }
            });
        }
    }

    treeViewer.addDoubleClickListener(new IDoubleClickListener() {
        public void doubleClick(final DoubleClickEvent event) {
            final IStructuredSelection selection = (IStructuredSelection) event.getSelection();

            if (selection != null) {
                final Object item = selection.getFirstElement();

                if (treeViewer.getExpandedState(item)) {
                    treeViewer.collapseToLevel(item, 1);
                } else {
                    treeViewer.expandToLevel(item, 1);
                }
            }
        }
    });

    final IContainer root;

    if (element instanceof CreateWorkspaceFileOp) {
        root = ((CreateWorkspaceFileOp) element).getRoot().target();
    } else {
        root = ResourcesPlugin.getWorkspace().getRoot();
    }

    treeViewer.setInput(root);

    this.decorator.addEditorControl(drillDown);
    this.decorator.addEditorControl(tree);

    final String val = value.text();

    if (val != null) {
        IPath path = new Path(val);
        IResource resource = root.findMember(val);

        while (resource == null) {
            path = path.removeLastSegments(1);
            resource = root.findMember(path);
        }

        if (resource instanceof IFile
                && validFileSystemResourceTypeAnnotation.value() == FileSystemResourceType.FOLDER) {
            resource = resource.getParent();
        }

        treeViewer.setSelection(new StructuredSelection(resource));
    }

    treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(final SelectionChangedEvent event) {
            final IResource resource;
            final IStructuredSelection selection = (IStructuredSelection) event.getSelection();

            if (selection == null || selection.isEmpty()) {
                resource = (IResource) treeViewer.getInput();
            } else {
                resource = (IResource) selection.getFirstElement();
            }

            String path = resource.getFullPath().makeRelativeTo(root.getFullPath()).toString();

            if (path.startsWith("/") && path.length() > 1) {
                path = path.substring(1);
            }

            textField.setText(path);
        }
    });

    textField.setData(DATA_BINDING, this.binding);

    addControl(tree);
}

From source file:org.eclipse.scada.configuration.component.tools.dialog.MasterSelectionDialog.java

License:Open Source License

@Override
protected Control createDialogArea(final Composite parent) {
    setTitle("Select master server");
    setMessage("Choose a master server from the world model");

    final Composite composite = (Composite) super.createDialogArea(parent);

    final Composite wrapper = new Composite(composite, SWT.NONE);
    wrapper.setLayout(new GridLayout(1, true));
    wrapper.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final ObservablesManager mgr = new ObservablesManager();

    final TreeViewer viewer = new TreeViewer(wrapper);
    viewer.setAutoExpandLevel(2);/*from w w w . ja v a 2  s . c  o m*/
    viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    viewer.addDoubleClickListener(new IDoubleClickListener() {

        @Override
        public void doubleClick(final DoubleClickEvent event) {
            handleDoubleClick();
        }
    });

    final Button add = new Button(wrapper, SWT.RADIO);
    add.setText("Add master server");
    add.setToolTipText("Assign the component to the selected master server in addition");

    this.replace = new Button(wrapper, SWT.RADIO);
    this.replace.setText("Replace all master servers");
    this.replace.setToolTipText("Assign the component soley to the selected master server");
    this.replace.setSelection(true);

    this.delete = new Button(wrapper, SWT.RADIO);
    this.delete.setText("Remove master server");
    this.delete.setToolTipText("Un-assign the component from the selected master server");

    mgr.runAndCollect(new Runnable() {

        @Override
        public void run() {
            createDataModel(viewer);
        }
    });

    if (this.lastSelection != null) {
        viewer.setSelection(new StructuredSelection(this.lastSelection));
        viewer.reveal(this.lastSelection);
    }

    return composite;
}

From source file:org.eclipse.sirius.common.ui.tools.api.dialog.AbstractFolderSelectionDialog.java

License:Open Source License

/**
 * New Folder./* w  ww .  j ava  2 s .c o m*/
 */
protected void newFolderButtonPressed() {
    final NewFolderDialog dialog = new NewFolderDialog(getShell(), fSelectedContainer);
    if (dialog.open() == Window.OK) {
        final TreeViewer treeViewer = getTreeViewer();
        treeViewer.refresh(fSelectedContainer);
        Object createdFolder;
        if (dialog.getResult() != null) {
            createdFolder = dialog.getResult()[0];
            treeViewer.reveal(createdFolder);
            treeViewer.setSelection(new StructuredSelection(createdFolder));
        }
    }
}

From source file:org.eclipse.tcf.te.ui.utils.TreeViewerUtil.java

License:Open Source License

/**
 * Get the filter root for the viewer based on the root path.
 *
 * @param viewer The tree viewer./*from  w  w w.  j  a v  a 2s  . c  om*/
 * @param rootPath The root path of the filter.
 * @return An adjust filter.
 */
private static TreePath getViewFilterRoot(TreeViewer viewer, TreePath rootPath) {
    if (rootPath != null) {
        if (!isEligibleRoot(rootPath, viewer)) {
            return null;
        }
        if (rootPath.getSegmentCount() == 0) {
            viewer.setSelection(StructuredSelection.EMPTY);
            return TreePath.EMPTY;
        }
        return rootPath;
    }
    viewer.setSelection(StructuredSelection.EMPTY);
    return TreePath.EMPTY;
}

From source file:org.eclipse.team.svn.ui.panel.common.SVNHistoryPanel.java

License:Open Source License

protected void fetchHistory(final GetLogMessagesOperation msgsOp) {
    msgsOp.setIncludeMerged(SVNTeamPreferences.getMergeBoolean(SVNTeamUIPlugin.instance().getPreferenceStore(),
            SVNTeamPreferences.MERGE_INCLUDE_MERGED_NAME));

    final IStructuredSelection selected = (IStructuredSelection) this.history.getTreeViewer().getSelection();
    IActionOperation showOp = new AbstractActionOperation("Operation_ShowMessages", SVNUIMessages.class) { //$NON-NLS-1$
        protected void runImpl(IProgressMonitor monitor) throws Exception {
            SVNTeamUIPlugin.instance().getWorkbench().getDisplay().syncExec(new Runnable() {
                public void run() {
                    if (msgsOp.getExecutionState() != IActionOperation.OK) {
                        SVNHistoryPanel.this.pending = false;
                        SVNHistoryPanel.this.history.refresh(LogMessagesComposite.REFRESH_ALL);
                        return;
                    }//from   ww w .  j ava 2  s . com
                    SVNHistoryPanel.this.addPage(msgsOp.getMessages());
                    SVNHistoryPanel.this.history.refresh(LogMessagesComposite.REFRESH_ALL);
                    SVNHistoryPanel.this.setPagingEnabled();

                    TreeViewer treeTable = SVNHistoryPanel.this.history.getTreeViewer();
                    if (!treeTable.getTree().isDisposed() && treeTable.getTree().getItems().length > 0) {
                        if (selected.size() != 0) {
                            treeTable.setSelection(selected);
                        } else {
                            TreeItem firstItem = treeTable.getTree().getItem(0);
                            if (((ILogNode) firstItem.getData()).getType() == ILogNode.TYPE_CATEGORY) {
                                firstItem = firstItem.getItem(0);
                            }
                            treeTable.getTree().setSelection(firstItem);
                        }
                        SVNHistoryPanel.this.history.refresh(LogMessagesComposite.REFRESH_UI_AFFECTED);
                        if (SVNHistoryPanel.this.tableViewerListener != null) {
                            SVNHistoryPanel.this.tableViewerListener.selectionChanged(null);
                        }
                    }
                }
            });
        }
    };
    CompositeOperation op = new CompositeOperation(showOp.getId(), showOp.getMessagesClass());
    op.add(msgsOp);
    op.add(showOp);
    UIMonitorUtility.doTaskNowDefault(op, true);
}

From source file:org.eclipse.ui.tests.performance.LabelProviderTest.java

License:Open Source License

protected StructuredViewer createViewer(Shell parent) {
    TreeViewer viewer = new TreeViewer(parent, SWT.FULL_SELECTION);

    viewer.setContentProvider(new ITreeContentProvider() {

        public Object[] getChildren(Object parentElement) {
            return entries;
        }//from   ww  w. j  a  va2s.  c  om

        public Object getParent(Object element) {
            return null;
        }

        public boolean hasChildren(Object element) {
            return false;
        }

        public Object[] getElements(Object inputElement) {
            return entries;
        }

        public void dispose() {
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }
    });

    GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
    viewer.getControl().setLayoutData(data);
    viewer.setSelection(new StructuredSelection(entries[1]));
    return viewer;
}

From source file:org.eclipse.umlgen.dsl.eth.presentation.components.ComponentsEditorDialog.java

License:Open Source License

/**
 * {@inheritDoc}//  w  w w .  j  ava  2 s  .com
 *
 * @see org.eclipse.emf.edit.ui.celleditor.FeatureEditorDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
 */
@Override
protected Control createDialogArea(Composite parent) {
    Composite contents = new Composite(parent, SWT.NONE);
    {
        GridLayout layout = new GridLayout();
        layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
        layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
        layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
        layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
        contents.setLayout(layout);
        contents.setLayoutData(new GridData(GridData.FILL_BOTH));
        applyDialogFont(contents);
    }

    GridLayout contentsGridLayout = (GridLayout) contents.getLayout();
    contentsGridLayout.numColumns = 3;

    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 Tree choiceTable = choiceOfValues == null ? null : new Tree(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 TreeViewer choiceTreeViewer = choiceOfValues == null ? null : new TreeViewer(choiceTable);
    if (choiceOfValues != null) {
        choiceTreeViewer.setContentProvider(new ComponentsChoiceAdapterFactoryContentProvider(
                new TreeItemProviderAdapterFactory(), values.getChildren(), object));
        choiceTreeViewer.setLabelProvider(labelProvider);
        final PatternFilter filter = new PatternFilter() {
            @Override
            protected boolean isParentMatch(Viewer viewer, Object element) {
                return viewer instanceof AbstractTreeViewer && super.isParentMatch(viewer, element);
            }
        };
        choiceTreeViewer.addFilter(filter);
        choiceTreeViewer.setComparator(new ViewerComparator());
        assert patternText != null;
        patternText.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                filter.setPattern(((Text) e.widget).getText());
                choiceTreeViewer.refresh();
            }
        });
        choiceTreeViewer.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);

    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 Tree featureTable = new Tree(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 TreeViewer featureTableViewer = new TreeViewer(featureTable);
    featureTableViewer.setContentProvider(contentProvider);
    featureTableViewer.setLabelProvider(labelProvider);
    featureTableViewer.setComparator(new ViewerComparator());
    featureTableViewer.setInput(values);
    if (!values.getChildren().isEmpty()) {
        featureTableViewer.setSelection(new StructuredSelection(values.getChildren().get(0)));
    }

    if (choiceTreeViewer != null) {
        choiceTreeViewer.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("");
                        featureTableViewer.setSelection(new StructuredSelection(value));
                        event.doit = false;
                    } catch (RuntimeException exception) {
                        // Ignore
                    }
                } else if (event.character == '\33') {
                    choiceText.setText("");
                    event.doit = false;
                }
            }
        });
    }

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

    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 (choiceTreeViewer != null) {
                choiceTreeViewer.setSelection(selection);
            } else if (choiceText != null) {
                if (firstValue != null) {
                    String value = EcoreUtil.convertToString((EDataType) eClassifier, firstValue);
                    choiceText.setText(value);
                }
            }
            choiceTreeViewer.refresh();
        }
    });

    return contents;
}

From source file:org.eclipse.umlgen.dsl.eth.presentation.connectors.ConnectorsEditorDialog.java

License:Open Source License

/**
 * {@inheritDoc}/* w  ww  .ja  va  2 s  . c  o  m*/
 *
 * @see org.eclipse.emf.edit.ui.celleditor.FeatureEditorDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
 */
@Override
protected Control createDialogArea(Composite parent) {
    Composite contents = new Composite(parent, SWT.NONE);
    {
        GridLayout layout = new GridLayout();
        layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
        layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
        layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
        layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
        contents.setLayout(layout);
        contents.setLayoutData(new GridData(GridData.FILL_BOTH));
        applyDialogFont(contents);
    }

    GridLayout contentsGridLayout = (GridLayout) contents.getLayout();
    contentsGridLayout.numColumns = 3;

    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 Tree choiceTable = choiceOfValues == null ? null : new Tree(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 TreeViewer choiceTreeViewer = choiceOfValues == null ? null : new TreeViewer(choiceTable);
    if (choiceOfValues != null) {
        choiceTreeViewer.setContentProvider(new ConnectorsChoiceAdapterFactoryContentProvider(
                new TreeItemProviderAdapterFactory(), values.getChildren()));
        choiceTreeViewer.setLabelProvider(
                new ConnectorsChoiceLabelProvider(propertyDescriptor.getLabelProvider(object)));
        final PatternFilter filter = new PatternFilter() {
            @Override
            protected boolean isParentMatch(Viewer viewer, Object element) {
                return viewer instanceof AbstractTreeViewer && super.isParentMatch(viewer, element);
            }
        };
        choiceTreeViewer.addFilter(filter);
        choiceTreeViewer.setComparator(new ViewerComparator());
        assert patternText != null;
        patternText.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                filter.setPattern(((Text) e.widget).getText());
                choiceTreeViewer.refresh();
            }
        });
        choiceTreeViewer.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);

    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 Tree featureTable = new Tree(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 TreeViewer featureTableViewer = new TreeViewer(featureTable);
    featureTableViewer.setContentProvider(contentProvider);
    featureTableViewer.setLabelProvider(labelProvider);
    featureTableViewer.setComparator(new ViewerComparator());
    featureTableViewer.setInput(values);
    if (!values.getChildren().isEmpty()) {
        featureTableViewer.setSelection(new StructuredSelection(values.getChildren().get(0)));
    }

    if (choiceTreeViewer != null) {
        choiceTreeViewer.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("");
                        featureTableViewer.setSelection(new StructuredSelection(value));
                        event.doit = false;
                    } catch (RuntimeException exception) {
                        // Ignore
                    }
                } else if (event.character == '\33') {
                    choiceText.setText("");
                    event.doit = false;
                }
            }
        });
    }

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

    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 (choiceTreeViewer != null) {
                choiceTreeViewer.setSelection(selection);
            } else if (choiceText != null) {
                if (firstValue != null) {
                    String value = EcoreUtil.convertToString((EDataType) eClassifier, firstValue);
                    choiceText.setText(value);
                }
            }
            choiceTreeViewer.refresh();
        }
    });

    return contents;
}

From source file:org.eclipse.wb.internal.core.databinding.ui.EditSelection.java

License:Open Source License

/**
 * Restore page index, selection and expanded for given page.
 *///from ww w. j a  v  a  2  s.c  om
private static boolean setPageSelection(IDatabindingsProvider databindingsProvider,
        ObserveElementsComposite observeElementsComposite, PageSelection page) {
    List<ObserveType> types = databindingsProvider.getTypes();
    if (page.pageIndex < 0 || page.pageIndex >= types.size()) {
        return false;
    }
    //
    try {
        observeElementsComposite.setRedraw(false);
        // show page
        observeElementsComposite.showPage(types.get(page.pageIndex));
        // prepare master objects
        TreeViewer masterViewer = observeElementsComposite.getMasterViewer();
        ITreeContentProvider masterProvider = (ITreeContentProvider) masterViewer.getContentProvider();
        Object[] masterInput = masterProvider.getElements(masterViewer.getInput());
        // restore master selection
        Object masterSelection = pathToObject(masterProvider, masterInput, page.masterSelection);
        if (masterSelection != null) {
            masterViewer.setSelection(new StructuredSelection(masterSelection));
        }
        // restore master expanded
        Object[] masterExpanded = pathsToObjects(masterProvider, masterInput, page.masterExpanded);
        if (masterExpanded.length > 0) {
            masterViewer.collapseAll();
            masterViewer.setExpandedElements(masterExpanded);
        }
        // prepare properties objects
        TreeViewer propertiesViewer = observeElementsComposite.getPropertiesViewer();
        ITreeContentProvider propertiesProvider = (ITreeContentProvider) propertiesViewer.getContentProvider();
        Object[] propertiesInput = propertiesProvider.getElements(propertiesViewer.getInput());
        // restore properties selection
        Object propertiesSelection = pathToObject(propertiesProvider, propertiesInput,
                page.propertiesSelection);
        if (propertiesSelection != null) {
            propertiesViewer.setSelection(new StructuredSelection(propertiesSelection));
        }
        // restore properties expanded
        Object[] propertiesExpanded = pathsToObjects(propertiesProvider, propertiesInput,
                page.propertiesExpanded);
        if (propertiesExpanded.length > 0) {
            propertiesViewer.collapseAll();
            propertiesViewer.setExpandedElements(propertiesExpanded);
        }
    } finally {
        observeElementsComposite.setRedraw(true);
    }
    //
    return true;
}

From source file:org.eclipse.wst.sse.sieditor.test.ui.v2.common.TestSetSelectionInSourceWithDataTypesEditor.java

License:Open Source License

@Test
public void testPageChnageFromServiceInterfacePageToSource() throws Exception {

    List pages = editor.getPages();

    AbstractEditorPage serviceEditorPage = (AbstractEditorPage) pages.get(0);

    TreeViewer treeViewer = serviceEditorPage.getTreeViewer();

    final TreeItem[] node = { (TreeItem) treeViewer.getTree().getItem(0) };

    StructuredSelection selection = new StructuredSelection((StructureTypeNode) node[0].getData());

    treeViewer.setSelection(selection);

    editor.pageChange(1);// w w  w.j  a  v a2  s .co  m

    assertEquals("Expected cursor position after swtich to SourcePage" //$NON-NLS-1$
            + Expected_Cursor_Position_After_Swtich_To_SourcePage_352,
            Expected_Cursor_Position_After_Swtich_To_SourcePage_352,
            ((org.eclipse.jface.text.TextSelection) editor.getSourcePage().getTextViewer().getSelection())
                    .getOffset());

}