Search in sources :

Example 1 with Disposable

use of org.springsource.ide.eclipse.commons.livexp.ui.Disposable in project eclipse-integration-commons by spring-projects.

the class SwtConnect method connectTextBasedFilter.

/**
 * Connect a filterbox model to a treeviewer. This assumes that the filter is text-based. The filter is applied to the labels of the elements in the tree.
 * <p>
 * For the viewer filter to work correctly the ITreeContentProvider must provide a proper implementation of the 'getParent' method. If getParent only
 * returns null the viewer filter will not be able to check whether an element should be shown when a parent element is selected by the search filter.
 * <p>
 * Note: you can use {@link TreeElementWrappingContentProvider} in order to ensure that ITreeContentProvider keeps track of parent nodes properly.
 */
public static void connectTextBasedFilter(TreeViewer viewer, LiveExpression<Filter<String>> searchBoxModel, LabelProvider labels, ITreeContentProvider treeContent) {
    TreeAwareViewerFilter viewerFilter = new TreeAwareViewerFilter(viewer, Filters.acceptAll(), labels, treeContent);
    Disposable disposable = searchBoxModel.onChange(UIValueListener.from((e, filter) -> {
        viewerFilter.setFilter(searchBoxModel.getValue());
        viewer.refresh(true);
    }));
    // TODO: what if there are existing filters?
    viewer.setFilters(viewerFilter);
    viewer.getControl().addDisposeListener(de -> {
        disposable.dispose();
    });
    Stylers stylers = new Stylers(viewer.getTree().getFont());
    viewer.getControl().addDisposeListener(de -> {
        disposable.dispose();
        stylers.dispose();
    });
    ILabelProvider baseLabels = (ILabelProvider) viewer.getLabelProvider();
    // Can't add bolding support without this! Ensure label provider is set before calling this method
    Assert.isNotNull(baseLabels);
    viewer.setLabelProvider(boldMatchedElements(stylers, baseLabels, Filters.delegatingTo(searchBoxModel)));
}
Also used : Disposable(org.springsource.ide.eclipse.commons.livexp.ui.Disposable) Disposable(org.springsource.ide.eclipse.commons.livexp.ui.Disposable) Text(org.eclipse.swt.widgets.Text) StyledString(org.eclipse.jface.viewers.StyledString) Stylers(org.springsource.ide.eclipse.commons.livexp.ui.Stylers) Assert(org.eclipse.core.runtime.Assert) Filter(org.springsource.ide.eclipse.commons.livexp.util.Filter) UIValueListener(org.springsource.ide.eclipse.commons.livexp.core.UIValueListener) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) StyledCellLabelProvider(org.eclipse.jface.viewers.StyledCellLabelProvider) Styler(org.eclipse.jface.viewers.StyledString.Styler) LiveExpression(org.springsource.ide.eclipse.commons.livexp.core.LiveExpression) ViewerCell(org.eclipse.jface.viewers.ViewerCell) ModifyListener(org.eclipse.swt.events.ModifyListener) Duration(java.time.Duration) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Filters(org.springsource.ide.eclipse.commons.livexp.util.Filters) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) ValueListener(org.springsource.ide.eclipse.commons.livexp.core.ValueListener) Label(org.eclipse.swt.widgets.Label) LabelProvider(org.eclipse.jface.viewers.LabelProvider) LiveVariable(org.springsource.ide.eclipse.commons.livexp.core.LiveVariable) IRegion(org.eclipse.jface.text.IRegion) Stylers(org.springsource.ide.eclipse.commons.livexp.ui.Stylers) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider)

Example 2 with Disposable

use of org.springsource.ide.eclipse.commons.livexp.ui.Disposable in project eclipse-integration-commons by spring-projects.

the class SwtConnect method connect.

public static void connect(Text text, LiveVariable<String> model) {
    if (!text.isDisposed()) {
        text.addDisposeListener(de -> model.dispose());
        ModifyListener widgetListener = (me) -> {
            if (!text.isDisposed()) {
                model.setValue(text.getText());
            }
        };
        text.addModifyListener(widgetListener);
        Disposable disconnect = model.onChange(UIValueListener.from((e, v) -> {
            String oldText = text.getText();
            String newText = model.getValue();
            if (newText == null) {
                newText = "";
            }
            if (!oldText.equals(newText)) {
                text.setText(newText);
            }
        }));
        text.addDisposeListener(de -> disconnect.dispose());
        model.onDispose(de -> text.removeModifyListener(widgetListener));
    }
}
Also used : Disposable(org.springsource.ide.eclipse.commons.livexp.ui.Disposable) Text(org.eclipse.swt.widgets.Text) StyledString(org.eclipse.jface.viewers.StyledString) Stylers(org.springsource.ide.eclipse.commons.livexp.ui.Stylers) Assert(org.eclipse.core.runtime.Assert) Filter(org.springsource.ide.eclipse.commons.livexp.util.Filter) UIValueListener(org.springsource.ide.eclipse.commons.livexp.core.UIValueListener) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) StyledCellLabelProvider(org.eclipse.jface.viewers.StyledCellLabelProvider) Styler(org.eclipse.jface.viewers.StyledString.Styler) LiveExpression(org.springsource.ide.eclipse.commons.livexp.core.LiveExpression) ViewerCell(org.eclipse.jface.viewers.ViewerCell) ModifyListener(org.eclipse.swt.events.ModifyListener) Duration(java.time.Duration) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Filters(org.springsource.ide.eclipse.commons.livexp.util.Filters) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) ValueListener(org.springsource.ide.eclipse.commons.livexp.core.ValueListener) Label(org.eclipse.swt.widgets.Label) LabelProvider(org.eclipse.jface.viewers.LabelProvider) LiveVariable(org.springsource.ide.eclipse.commons.livexp.core.LiveVariable) IRegion(org.eclipse.jface.text.IRegion) Disposable(org.springsource.ide.eclipse.commons.livexp.ui.Disposable) ModifyListener(org.eclipse.swt.events.ModifyListener) StyledString(org.eclipse.jface.viewers.StyledString)

Example 3 with Disposable

use of org.springsource.ide.eclipse.commons.livexp.ui.Disposable in project sts4 by spring-projects.

the class GotoSymbolDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    Composite dialogArea = new Composite(parent, SWT.NONE);
    dialogArea.addDisposeListener(de -> {
        for (Disposable d : disposables) {
            d.dispose();
        }
    });
    if (parent.getLayout() instanceof GridLayout) {
        dialogArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    }
    GridLayout layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    dialogArea.setLayout(layout);
    Text pattern = new Text(dialogArea, SWT.SINGLE | SWT.BORDER | SWT.SEARCH | SWT.ICON_CANCEL);
    // pattern.getAccessible().addAccessibleListener(new AccessibleAdapter() {
    // public void getName(AccessibleEvent e) {
    // e.result = LegacyActionTools.removeMnemonics(headerLabel)
    // .getText());
    // }
    // });
    pattern.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    pattern.setMessage(model.getSearchBoxHintMessage());
    SwtConnect.connect(pattern, model.getSearchBox());
    TreeViewer viewer = new TreeViewer(dialogArea, SWT.SINGLE);
    ColumnViewerToolTipSupport.enableFor(viewer);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getControl());
    viewer.setContentProvider(new SymbolsContentProvider());
    viewer.setLabelProvider(new GotoSymbolsLabelProvider(viewer.getTree().getFont()));
    viewer.setUseHashlookup(true);
    disposables.add(model.getSymbols().onChange(UIValueListener.from((e, v) -> {
        if (!viewer.getControl().isDisposed())
            viewer.refresh();
    })));
    // TODO: somehow show selection in local file, (but not in other file ?)
    // viewer.addSelectionChangedListener(event -> {
    // IStructuredSelection selection = (IStructuredSelection) event.getSelection();
    // if (selection.isEmpty()) {
    // return;
    // }
    // SymbolInformation symbolInformation = (SymbolInformation) selection.getFirstElement();
    // Location location = symbolInformation.getLocation();
    // 
    // IResource targetResource = LSPEclipseUtils.findResourceFor(location.getUri());
    // if (targetResource == null) {
    // return;
    // }
    // IDocument targetDocument = FileBuffers.getTextFileBufferManager()
    // .getTextFileBuffer(targetResource.getFullPath(), LocationKind.IFILE).getDocument();
    // if (targetDocument != null) {
    // try {
    // int offset = LSPEclipseUtils.toOffset(location.getRange().getStart(), targetDocument);
    // int endOffset = LSPEclipseUtils.toOffset(location.getRange().getEnd(), targetDocument);
    // fTextEditor.selectAndReveal(offset, endOffset - offset);
    // } catch (BadLocationException e) {
    // LanguageServerPlugin.logError(e);
    // }
    // }
    // });
    installWidgetListeners(pattern, viewer);
    Label statusLabel = new Label(dialogArea, SWT.NONE);
    statusLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    SwtConnect.connect(statusLabel, model.getStatus(), Duration.ofMillis(500));
    viewer.setInput(model);
    return dialogArea;
}
Also used : Disposable(org.springsource.ide.eclipse.commons.livexp.ui.Disposable) GridLayout(org.eclipse.swt.layout.GridLayout) Composite(org.eclipse.swt.widgets.Composite) TreeViewer(org.eclipse.jface.viewers.TreeViewer) GridData(org.eclipse.swt.layout.GridData) Label(org.eclipse.swt.widgets.Label) Text(org.eclipse.swt.widgets.Text)

Aggregations

TreeViewer (org.eclipse.jface.viewers.TreeViewer)3 Label (org.eclipse.swt.widgets.Label)3 Text (org.eclipse.swt.widgets.Text)3 Disposable (org.springsource.ide.eclipse.commons.livexp.ui.Disposable)3 Duration (java.time.Duration)2 Assert (org.eclipse.core.runtime.Assert)2 IRegion (org.eclipse.jface.text.IRegion)2 ILabelProvider (org.eclipse.jface.viewers.ILabelProvider)2 ITreeContentProvider (org.eclipse.jface.viewers.ITreeContentProvider)2 LabelProvider (org.eclipse.jface.viewers.LabelProvider)2 StyledCellLabelProvider (org.eclipse.jface.viewers.StyledCellLabelProvider)2 StyledString (org.eclipse.jface.viewers.StyledString)2 Styler (org.eclipse.jface.viewers.StyledString.Styler)2 ViewerCell (org.eclipse.jface.viewers.ViewerCell)2 ModifyListener (org.eclipse.swt.events.ModifyListener)2 LiveExpression (org.springsource.ide.eclipse.commons.livexp.core.LiveExpression)2 LiveVariable (org.springsource.ide.eclipse.commons.livexp.core.LiveVariable)2 UIValueListener (org.springsource.ide.eclipse.commons.livexp.core.UIValueListener)2 ValueListener (org.springsource.ide.eclipse.commons.livexp.core.ValueListener)2 Stylers (org.springsource.ide.eclipse.commons.livexp.ui.Stylers)2