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

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

Introduction

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

Prototype

@Override
public ISelection getSelection() 

Source Link

Document

The AbstractTreeViewer implementation of this method returns the result as an ITreeSelection.

Usage

From source file:org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.preferences.RemoteProfilesPreferencePage.java

License:Open Source License

@Override
protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    composite.setLayoutData(gd);//  w  w w  .  j ava  2s .co  m

    GridLayout gl = new GridLayout(2, false);
    composite.setLayout(gl);

    PatternFilter patternFilter = new PatternFilter() {
        // show all children of matching profiles or profiles with matching connection node
        @Override
        protected boolean isLeafMatch(Viewer viewer, Object element) {
            TreeViewer treeViewer = (TreeViewer) viewer;
            TracePackageContentProvider contentProvider = (TracePackageContentProvider) treeViewer
                    .getContentProvider();
            Object parentElement = element;
            while (!(parentElement instanceof RemoteImportProfileElement)) {
                parentElement = contentProvider.getParent(parentElement);
                if (parentElement instanceof TracePackageTraceElement) {
                    // don't show children of trace element
                    return false;
                }
            }
            RemoteImportProfileElement profile = (RemoteImportProfileElement) parentElement;
            if (super.isLeafMatch(viewer, profile)) {
                return true;
            }
            for (Object child : contentProvider.getChildren(profile)) {
                if ((child instanceof RemoteImportConnectionNodeElement)
                        && (super.isLeafMatch(viewer, child))) {
                    return true;
                }
            }
            return false;
        }
    };

    final FilteredTree filteredTree = new FilteredTree(composite,
            SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER, patternFilter, true);
    gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.heightHint = 0;
    filteredTree.setLayoutData(gd);
    final TreeViewer treeViewer = filteredTree.getViewer();
    fTreeViewer = treeViewer;

    treeViewer.setContentProvider(new TracePackageContentProvider() {
        @Override
        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof ArrayList) {
                return ((ArrayList<?>) inputElement).toArray();
            }
            return super.getElements(inputElement);
        }

        @Override
        public boolean hasChildren(Object element) {
            if (element instanceof TracePackageTraceElement) {
                return false;
            }
            return super.hasChildren(element);
        }
    });

    treeViewer.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            if (element instanceof TracePackageTraceElement) {
                for (TracePackageElement files : ((TracePackageTraceElement) element).getChildren()) {
                    if (files instanceof TracePackageFilesElement) {
                        return ((TracePackageFilesElement) files).getFileName();
                    }
                }
            } else if (element instanceof TracePackageElement) {
                return ((TracePackageElement) element).getText();
            }
            return super.getText(element);
        }

        @Override
        public Image getImage(Object element) {
            if (element instanceof TracePackageTraceElement) {
                for (TracePackageElement files : ((TracePackageTraceElement) element).getChildren()) {
                    return files.getImage();
                }
            } else if (element instanceof TracePackageElement) {
                return ((TracePackageElement) element).getImage();
            }
            return super.getImage(element);
        }
    });

    treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            TracePackageElement element = (TracePackageElement) (selection.size() == 1
                    ? selection.getFirstElement()
                    : null);
            fDetailsPanel.refreshDetailsPanel(element);
            enableButtons(selection);
            fSelectedProfileName = null;
            while (element != null) {
                if (element instanceof RemoteImportProfileElement) {
                    fSelectedProfileName = ((RemoteImportProfileElement) element).getProfileName();
                }
                element = element.getParent();
            }
        }
    });

    createGlobalActions();
    createContextMenu();

    fProfiles = readProfiles(PROFILE_FILE_PATH, new NullProgressMonitor());

    treeViewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
    treeViewer.setInput(fProfiles);
    treeViewer.expandAll();

    Composite buttonBar = createVerticalButtonBar(composite);
    gd = new GridData(SWT.CENTER, SWT.BEGINNING, false, false);
    gd.verticalIndent = filteredTree.getFilterControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).y
            + gl.verticalSpacing;
    buttonBar.setLayoutData(gd);
    enableButtons((IStructuredSelection) treeViewer.getSelection());

    Composite details = new Composite(composite, SWT.NONE);
    gd = new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1);
    details.setLayoutData(gd);
    gl = new GridLayout(2, false);
    gl.marginWidth = 0;
    details.setLayout(gl);

    Label label = new Label(details, SWT.NONE);
    label.setText(RemoteMessages.RemoteProfilesPreferencePage_DetailsPanelLabel);
    gd = new GridData(SWT.BEGINNING, SWT.CENTER, false, false, 2, 1);
    label.setLayoutData(gd);

    fDetailsPanel = new DetailsPanel(details);

    validate();

    for (RemoteImportProfileElement profile : fProfiles) {
        if (profile.getProfileName().equals(fSelectedProfileName)) {
            fTreeViewer.setSelection(new StructuredSelection(profile));
        }
    }

    Dialog.applyDialogFont(composite);
    return composite;
}

From source file:org.eclipse.ui.internal.views.log.LogView.java

License:Open Source License

private void asyncRefresh(final boolean activate) {
    if (fTree.isDisposed())
        return;/*from   w  ww .j a v  a  2s.c om*/
    Display display = fTree.getDisplay();
    final ViewPart view = this;
    if (display != null) {
        display.asyncExec(new Runnable() {
            public void run() {
                if (!fTree.isDisposed()) {
                    TreeViewer viewer = fFilteredTree.getViewer();
                    viewer.refresh();
                    viewer.expandToLevel(2);
                    fDeleteLogAction.setEnabled(
                            fInputFile.exists() && fInputFile.equals(Platform.getLogFileLocation().toFile()));
                    fOpenLogAction.setEnabled(fInputFile.exists());
                    fExportLogAction.setEnabled(fInputFile.exists());
                    fExportLogEntryAction.setEnabled(!viewer.getSelection().isEmpty());
                    if (activate && fActivateViewAction.isChecked()) {
                        IWorkbenchWindow window = Activator.getDefault().getWorkbench()
                                .getActiveWorkbenchWindow();
                        if (window != null) {
                            IWorkbenchPage page = window.getActivePage();
                            if (page != null) {
                                page.bringToTop(view);
                            }
                        }
                    }
                }
            }
        });
    }
}

From source file:org.eclipse.ui.internal.views.markers.UIUpdateJob.java

License:Open Source License

public IStatus runInUIThread(IProgressMonitor monitor) {
    if (monitor.isCanceled()) {
        return Status.CANCEL_STATUS;
    }/*from w ww.  j a v  a 2 s . c om*/
    TreeViewer viewer = view.getViewer();
    if (viewer.getControl().isDisposed()) {
        return Status.CANCEL_STATUS;
    }

    Markers clone = view.getActiveViewerInputClone();
    try {
        updating = true;
        monitor.beginTask(MarkerMessages.MarkerView_19, IProgressMonitor.UNKNOWN);

        if (monitor.isCanceled()) {
            return Status.CANCEL_STATUS;
        }

        //view.indicateUpdating(MarkerMessages.MarkerView_19,
        //      true);

        // If there is only one category and the user has no saved state
        // show it
        if (view.getBuilder().isShowingHierarchy() && view.getCategoriesToExpand().isEmpty()) {
            MarkerCategory[] categories = clone.getCategories();
            if (categories != null && categories.length == 1)
                view.getCategoriesToExpand().add(categories[0].getDescription());
        }

        if (monitor.isCanceled())
            return Status.CANCEL_STATUS;
        /* 
         * always use a clone for Thread safety. We avoid setting the clone
         * as new input as we would offset the benefits of optimization in
         * TreeViewer.
         */
        clone = view.createViewerInputClone();
        if (clone == null) {
            // do not update yet,we are changing
            return Status.CANCEL_STATUS;
        }
        /*
         * we prefer not to check for cancellation beyond this since we
         * have to show correct marker counts on UI, not an updating message.
         */
        IContentProvider contentProvider = viewer.getContentProvider();
        contentProvider.inputChanged(viewer, view.getViewerInput(), clone);
        viewer.getTree().setRedraw(false);
        viewer.refresh(true);
        if (!monitor.isCanceled()) {
            //do not expand if canceled
            view.reexpandCategories();
        }
        if (view.getBuilder().readChangeFlags()[0]) {
            // indicate changes
        }
        //show new counts
        view.updateTitle();

        lastUpdateTime = System.currentTimeMillis();
    } finally {
        viewer.getTree().setRedraw(true);
        view.updateStatusLine((IStructuredSelection) viewer.getSelection());
        //view.updateCategoryLabels();
        updating = false;
    }
    monitor.done();
    return Status.OK_STATUS;
}

From source file:org.eclipse.ui.tests.navigator.ResourceNavigatorTest.java

License:Open Source License

public void testSelectReveal() throws Throwable {
    setupView();/*from www. j a va2  s .  c  om*/
    setupResources();

    ISetSelectionTarget part = (ISetSelectionTarget) view;
    TreeViewer tree = ((ResourceNavigator) view).getViewer();

    // Set the selection in the navigator
    IStructuredSelection sel1 = new StructuredSelection(f1);
    part.selectReveal(sel1);
    // Get the selection the tree has
    IStructuredSelection treeSel1 = (IStructuredSelection) tree.getSelection();
    assertTrue("First selection wrong size, should be only one.", treeSel1.size() == 1);
    IResource resource1 = (IResource) treeSel1.getFirstElement();
    assertTrue("First selection contains wrong file resource.", resource1.equals(f1));

    // Set the selection in the navigator
    IStructuredSelection sel2 = new StructuredSelection(p2);
    part.selectReveal(sel2);
    // Get the selection the tree has
    IStructuredSelection treeSel2 = (IStructuredSelection) tree.getSelection();
    assertTrue("Second selection wrong size, should be only one.", treeSel2.size() == 1);
    IResource resource2 = (IResource) treeSel2.getFirstElement();
    assertTrue("Second selection contains wrong project resource.", resource2.equals(p2));
}

From source file:org.eclipse.ui.trace.internal.TracingDoubleClickListener.java

License:Open Source License

public void doubleClick(final DoubleClickEvent event) {
    // auto-expand or collapse the selected node
    TreeViewer traceComponentViewer = (TreeViewer) event.getViewer();
    final Object selectedItem = ((IStructuredSelection) traceComponentViewer.getSelection()).getFirstElement();
    final boolean expandedState = traceComponentViewer.getExpandedState(selectedItem);
    if (selectedItem instanceof TracingNode) {
        traceComponentViewer.setExpandedState(selectedItem, !expandedState);
    }/* w  w  w.  j a va2  s  .c  o m*/
}

From source file:org.eclipse.ui.views.navigator.ResourceNavigator.java

License:Open Source License

public void createPartControl(Composite parent) {
    TreeViewer viewer = createViewer(parent);
    this.viewer = viewer;

    if (memento != null) {
        restoreFilters();/*w w  w. j  a  v  a  2s. co m*/
        restoreLinkingEnabled();
    }
    frameList = createFrameList();
    initDragAndDrop();
    updateTitle();

    initContextMenu();

    initResourceComparator();
    initWorkingSetFilter();

    // make sure input is set after sorters and filters,
    // to avoid unnecessary refreshes
    viewer.setInput(getInitialInput());

    // make actions after setting input, because some actions
    // look at the viewer for enablement (e.g. the Up action)
    makeActions();

    // Fill the action bars and update the global action handlers'
    // enabled state to match the current selection.
    getActionGroup().fillActionBars(getViewSite().getActionBars());
    updateActionBars((IStructuredSelection) viewer.getSelection());

    getSite().setSelectionProvider(viewer);
    getSite().getPage().addPartListener(partListener);
    IWorkingSetManager workingSetManager = getPlugin().getWorkbench().getWorkingSetManager();
    workingSetManager.addPropertyChangeListener(propertyChangeListener);

    if (memento != null) {
        restoreState(memento);
    }
    memento = null;

    // Set help for the view
    getSite().getWorkbenchWindow().getWorkbench().getHelpSystem().setHelp(viewer.getControl(),
            getHelpContextId());
}

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

License:Open Source License

/**
 * {@inheritDoc}/*  w w w .ja  va 2s  .  co 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 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}//from  w w  w. ja va2s .  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.swing.laf.ui.JarSelectionDialog.java

License:Open Source License

@Override
protected void updateOKStatus() {
    TreeViewer viewer = getTreeViewer();
    m_expanded = viewer.getExpandedElements();
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    m_selection = selection.getFirstElement();
    super.updateOKStatus();
}

From source file:org.eclipse.wst.dtd.ui.views.contentoutline.DTDContentOutlineConfiguration.java

License:Open Source License

public TransferDragSourceListener[] getTransferDragSourceListeners(final TreeViewer treeViewer) {
    if (fTransferDragSourceListeners == null) {
        fTransferDragSourceListeners = new TransferDragSourceListener[] { new TransferDragSourceListener() {
            public void dragFinished(DragSourceEvent event) {
                LocalSelectionTransfer.getTransfer().setSelection(null);
            }//w  w  w . jav a2 s  . com

            public void dragSetData(DragSourceEvent event) {
            }

            public void dragStart(DragSourceEvent event) {
                LocalSelectionTransfer.getTransfer().setSelection(treeViewer.getSelection());
            }

            public Transfer getTransfer() {
                return LocalSelectionTransfer.getTransfer();
            }
        } };
    }

    return fTransferDragSourceListeners;
}