Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3b7c0794e075391128854ed532d01bfaa0a04080 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************************
 * Copyright (c) 2008, 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 accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Matthew Hall - initial API and implementation (bug 249992)
 *     Matthew Hall - bug 260329
 *     Simon Scholz <simon.scholz@vogella.com> - Bug 434283
 ******************************************************************************/

package org.eclipse.jface.examples.databinding.snippets;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.SelectObservableValue;
import org.eclipse.jface.databinding.swt.DisplayRealm;
import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
import org.eclipse.jface.databinding.viewers.typed.ViewerProperties;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

/**
 * Demonstrate usage of SelectObservableValue
 */
public class Snippet024SelectObservableValue {
	public static void main(String[] args) {
		final Display display = Display.getDefault();

		Realm.runWithDefault(DisplayRealm.getRealm(display), () -> {
			Shell shell = createShell();

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

		display.dispose();
	}

	private static Shell createShell() {
		Shell shell = new Shell();
		shell.setSize(400, 300);
		shell.setLayout(new GridLayout(2, true));
		shell.setText("Snippet024SelectObservableValue");

		final ListViewer listViewer = new ListViewer(shell, SWT.BORDER);
		listViewer.setContentProvider(ArrayContentProvider.getInstance());
		listViewer.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		final Group group = new Group(shell, SWT.NONE);
		group.setText("Radio Group");
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		group.setLayout(new GridLayout());

		// Data Binding
		Color[] colors = Color.values();

		listViewer.setInput(colors);
		IObservableValue<Color> listViewerSelection = ViewerProperties.singleSelection(Color.class).observe(listViewer);

		SelectObservableValue<Color> radioGroup = new SelectObservableValue<>();
		for (Color color : colors) {
			Button button = new Button(group, SWT.RADIO);
			button.setText(color.toString());
			radioGroup.addOption(color, WidgetProperties.buttonSelection().observe(button));
		}

		DataBindingContext bindingContext = new DataBindingContext();
		bindingContext.bindValue(radioGroup, listViewerSelection);

		shell.open();
		return shell;
	}

	public static class Color {
		public static final Color RED = new Color("Red");
		public static final Color ORANGE = new Color("Orange");
		public static final Color YELLOW = new Color("Yellow");
		public static final Color GREEN = new Color("Green");
		public static final Color BLUE = new Color("Blue");
		public static final Color INDIGO = new Color("Indigo");
		public static final Color VIOLET = new Color("Violet");

		private final String name;

		private Color(String name) {
			this.name = name;
		}

		@Override
		public String toString() {
			return name;
		}

		public static Color[] values() {
			return new Color[] { RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET };
		}
	}
}

Back to the top