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