org.eclipse.jface.examples.databinding.snippets.Snippet015DelayTextModifyEvents.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.jface.examples.databinding.snippets.Snippet015DelayTextModifyEvents.java

Source

/************************************************************************************************************
 * Copyright (c) 2007, 2018 Matthew Hall and others.
 *
 * This program and the
 * accompanying materials are made available under the terms of the Eclipse Public License 2.0 which
t https://www.eclipse.org/legal/epl-2.0/
t
t SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *       Matthew Hall - initial API and implementation (bug 180746)
 *       Boris Bokowski, IBM - initial API and implementation
 *      Matthew Hall - bugs 260329, 264286
 ***********************************************************************************************************/
package org.eclipse.jface.examples.databinding.snippets;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.Observables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.DisplayRealm;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
import org.eclipse.jface.internal.databinding.provisional.swt.ControlUpdater;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.resource.FontDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.LocalResourceManager;
import org.eclipse.jface.resource.ResourceManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Snippet015DelayTextModifyEvents {

    private static void createControls(Shell shell) {
        final Label field1 = createLabel(shell, SWT.NONE, "Field 1 ");

        Text text1 = new Text(shell, SWT.BORDER);
        GridDataFactory.fillDefaults().grab(true, false).hint(200, SWT.DEFAULT).applyTo(text1);
        createLabel(shell, SWT.NONE, "200 ms delay");

        final Label field2 = createLabel(shell, SWT.NONE, "Field 2 ");

        Text text2 = new Text(shell, SWT.BORDER);
        GridDataFactory.fillDefaults().grab(true, false).hint(200, SWT.DEFAULT).applyTo(text2);

        createLabel(shell, SWT.NONE, "2000 ms delay");

        final ISWTObservableValue<String> delayed1 = WidgetProperties.text(SWT.Modify).observeDelayed(200, text1);
        final ISWTObservableValue<String> delayed2 = WidgetProperties.text(SWT.Modify).observeDelayed(2000, text2);

        // (In a real application,you would want to dispose the resource manager
        // when you are done with it)
        ResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources());
        final Font shellFont = shell.getFont();
        final Font italicFont = resourceManager
                .createFont(FontDescriptor.createFrom(shellFont).setStyle(SWT.ITALIC));

        final IObservableValue<Boolean> stale1 = Observables.observeStale(delayed1);
        new ControlUpdater(field2) {
            @Override
            protected void updateControl() {
                field2.setFont(stale1.getValue() ? italicFont : shellFont);
            }
        };

        final IObservableValue<Boolean> stale2 = Observables.observeStale(delayed2);
        new ControlUpdater(field1) {
            @Override
            protected void updateControl() {
                field1.setFont(stale2.getValue() ? italicFont : shellFont);
            }
        };

        String info = "Pending changes are applied immediately if the observed control loses focus.";
        GridDataFactory.fillDefaults().span(3, 1).hint(300, SWT.DEFAULT)
                .applyTo(createLabel(shell, SWT.WRAP, info));

        DataBindingContext dbc = new DataBindingContext();

        dbc.bindValue(delayed1, delayed2);

        // Sometimes it is useful to do a manual value flush when of the delayed
        // observables. This can be done in the following two ways.

        text2.addTraverseListener(e -> {
            if (e.detail == SWT.TRAVERSE_RETURN) {
                // The DataBindingContext update methods can also be used to update
                // observables in bulk
                dbc.updateTargets();
            }
        });

        text1.addTraverseListener(e -> {
            if (e.detail == SWT.TRAVERSE_RETURN) {
                // When the setValue method is called on a delayed observable
                // the change is immediately propagated to its listeners without
                // any delay. All other pending changes are cancelled.
                delayed1.setValue(text1.getText());
            }
        });
    }

    private static Label createLabel(Composite parent, int style, String text) {
        Label label = new Label(parent, style);
        label.setText(text);
        return label;
    }

    public static void main(String[] args) {
        final Display display = new Display();

        Realm.runWithDefault(DisplayRealm.getRealm(display), () -> {
            Shell shell = new Shell();
            shell.setLayout(new GridLayout(3, false));

            createControls(shell);

            shell.pack();
            shell.open();
            while (!shell.isDisposed())
                if (!display.readAndDispatch())
                    display.sleep();
        });

        display.dispose();
    }

}