comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet031JFaceObservable.d @ 99:5d5bd660917f

build some databind snippets
author Frank Benoit <benoit@tionex.de>
date Wed, 22 Apr 2009 18:59:26 +0200
parents 6086085e153d
children e884642ad36e
comparison
equal deleted inserted replaced
98:48d4ee626868 99:5d5bd660917f
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/ 10 ******************************************************************************/
11 11
12 package org.eclipse.jface.examples.databinding.snippets; 12 module org.eclipse.jface.examples.databinding.snippets.Snippet031JFaceObservable;
13
14 import java.lang.all;
13 15
14 import org.eclipse.core.commands.common.EventManager; 16 import org.eclipse.core.commands.common.EventManager;
15 import org.eclipse.core.databinding.DataBindingContext; 17 import org.eclipse.core.databinding.DataBindingContext;
16 import org.eclipse.core.databinding.observable.Realm; 18 import org.eclipse.core.databinding.observable.Realm;
17 import org.eclipse.core.databinding.property.value.IValueProperty; 19 import org.eclipse.core.databinding.property.value.IValueProperty;
26 import org.eclipse.swt.widgets.Shell; 28 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Text; 29 import org.eclipse.swt.widgets.Text;
28 30
29 public class Snippet031JFaceObservable { 31 public class Snippet031JFaceObservable {
30 32
31 public static final String NAME_PROPERTY = "name_property"; 33 public static final String NAME_PROPERTY = "name_property";
32 34
33 public static void main(String[] args) { 35 public static void main(String[] args) {
34 Display display = new Display(); 36 Display display = new Display();
35 final ViewModel viewModel = new ViewModel(); 37 final ViewModel viewModel = new ViewModel();
36 38
37 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { 39 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
38 public void run() { 40 public void run() {
39 final Shell shell = new View(viewModel).createShell(); 41 final Shell shell = new View(viewModel).createShell();
40 // The SWT event loop 42 // The SWT event loop
41 Display display = Display.getCurrent(); 43 Display display = Display.getCurrent();
42 while (!shell.isDisposed()) { 44 while (!shell.isDisposed()) {
43 if (!display.readAndDispatch()) { 45 if (!display.readAndDispatch()) {
44 display.sleep(); 46 display.sleep();
45 } 47 }
46 } 48 }
47 } 49 }
48 }); 50 });
49 // Print the results 51 // Print the results
50 System.out.println("person.getName() = " 52 System.out.println("person.getName() = "
51 + viewModel.getPerson().getName()); 53 + viewModel.getPerson().getName());
52 } 54 }
53 55
54 // The data model class. This is normally a persistent class of some sort. 56 // The data model class. This is normally a persistent class of some sort.
55 // 57 //
56 // In this example, we extend the EventManager class 58 // In this example, we extend the EventManager class
57 // to manage our listeners and we fire a property change 59 // to manage our listeners and we fire a property change
58 // event when the object state changes. 60 // event when the object state changes.
59 public static class Person extends EventManager { 61 public static class Person extends EventManager {
60 // A property... 62 // A property...
61 String name = "HelloWorld"; 63 String name = "HelloWorld";
62 64
63 public String getName() { 65 public String getName() {
64 return name; 66 return name;
65 } 67 }
66 68
67 public void setName(String name) { 69 public void setName(String name) {
68 fireChange(new PropertyChangeEvent(this, NAME_PROPERTY, this.name, 70 fireChange(new PropertyChangeEvent(this, NAME_PROPERTY, this.name,
69 this.name = name)); 71 this.name = name));
70 } 72 }
71 73
72 public void addPropertyChangeListener(IPropertyChangeListener listener) { 74 public void addPropertyChangeListener(IPropertyChangeListener listener) {
73 addListenerObject(listener); 75 addListenerObject(listener);
74 } 76 }
75 77
76 public void removePropertyChangeListener( 78 public void removePropertyChangeListener(
77 IPropertyChangeListener listener) { 79 IPropertyChangeListener listener) {
78 removeListenerObject(listener); 80 removeListenerObject(listener);
79 } 81 }
80 82
81 private void fireChange(PropertyChangeEvent event) { 83 private void fireChange(PropertyChangeEvent event) {
82 final Object[] list = getListeners(); 84 final Object[] list = getListeners();
83 for (int i = 0; i < list.length; ++i) { 85 for (int i = 0; i < list.length; ++i) {
84 ((IPropertyChangeListener) list[i]).propertyChange(event); 86 ((IPropertyChangeListener) list[i]).propertyChange(event);
85 } 87 }
86 } 88 }
87 89
88 } 90 }
89 91
90 // The View's model--the root of our Model graph for this particular GUI. 92 // The View's model--the root of our Model graph for this particular GUI.
91 // 93 //
92 // Typically each View class has a corresponding ViewModel class. 94 // Typically each View class has a corresponding ViewModel class.
93 // The ViewModel is responsible for getting the objects to edit from the 95 // The ViewModel is responsible for getting the objects to edit from the
94 // DAO. Since this snippet doesn't have any persistent objects to 96 // DAO. Since this snippet doesn't have any persistent objects to
95 // retrieve, this ViewModel just instantiates a model object to edit. 97 // retrieve, this ViewModel just instantiates a model object to edit.
96 static class ViewModel { 98 static class ViewModel {
97 // The model to bind 99 // The model to bind
98 private Person person = new Person(); 100 private Person person = new Person();
99 101
100 public Person getPerson() { 102 public Person getPerson() {
101 return person; 103 return person;
102 } 104 }
103 } 105 }
104 106
105 // The GUI view 107 // The GUI view
106 static class View { 108 static class View {
107 private ViewModel viewModel; 109 private ViewModel viewModel;
108 private Text name; 110 private Text name;
109 111
110 public View(ViewModel viewModel) { 112 public View(ViewModel viewModel) {
111 this.viewModel = viewModel; 113 this.viewModel = viewModel;
112 } 114 }
113 115
114 public Shell createShell() { 116 public Shell createShell() {
115 // Build a UI 117 // Build a UI
116 Display display = Display.getDefault(); 118 Display display = Display.getDefault();
117 Shell shell = new Shell(display); 119 Shell shell = new Shell(display);
118 shell.setLayout(new RowLayout(SWT.VERTICAL)); 120 shell.setLayout(new RowLayout(SWT.VERTICAL));
119 name = new Text(shell, SWT.BORDER); 121 name = new Text(shell, SWT.BORDER);
120 122
121 // Bind it 123 // Bind it
122 DataBindingContext bindingContext = new DataBindingContext(); 124 DataBindingContext bindingContext = new DataBindingContext();
123 Person person = viewModel.getPerson(); 125 Person person = viewModel.getPerson();
124 126
125 IValueProperty nameProperty = JFaceProperties.value(Person.class, 127 IValueProperty nameProperty = JFaceProperties.value(Person.class,
126 "name", NAME_PROPERTY); 128 "name", NAME_PROPERTY);
127 129
128 bindingContext.bindValue(SWTObservables.observeText(name, 130 bindingContext.bindValue(SWTObservables.observeText(name,
129 SWT.Modify), nameProperty.observe(person), null, null); 131 SWT.Modify), nameProperty.observe(person), null, null);
130 132
131 Label label = new Label(shell, SWT.NONE); 133 Label label = new Label(shell, SWT.NONE);
132 bindingContext.bindValue(SWTObservables.observeText(label), 134 bindingContext.bindValue(SWTObservables.observeText(label),
133 nameProperty.observe(person), null, null); 135 nameProperty.observe(person), null, null);
134 136
135 // Open and return the Shell 137 // Open and return the Shell
136 shell.pack(); 138 shell.pack();
137 shell.open(); 139 shell.open();
138 return shell; 140 return shell;
139 } 141 }
140 } 142 }
141 143
142 } 144 }
145 void main( String[] args ){
146 Snippet031JFaceObservable.main(args);
147 }