comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet002UpdateComboRetainSelection.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
comparison
equal deleted inserted replaced
98:48d4ee626868 99:5d5bd660917f
9 * The Pampered Chef, Inc. - initial API and implementation 9 * The Pampered Chef, Inc. - initial API and implementation
10 * Brad Reynolds - bug 116920 10 * Brad Reynolds - bug 116920
11 * Matthew Hall - bug 260329 11 * Matthew Hall - bug 260329
12 ******************************************************************************/ 12 ******************************************************************************/
13 13
14 package org.eclipse.jface.examples.databinding.snippets; 14 module org.eclipse.jface.examples.databinding.snippets.Snippet002UpdateComboRetainSelection;
15
16 import java.lang.all;
15 17
16 import java.beans.PropertyChangeListener; 18 import java.beans.PropertyChangeListener;
17 import java.beans.PropertyChangeSupport; 19 import java.beans.PropertyChangeSupport;
18 import java.util.ArrayList; 20 import java.util.ArrayList;
19 import java.util.Collection; 21 import java.util.Collection;
35 import org.eclipse.swt.widgets.Button; 37 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Combo; 38 import org.eclipse.swt.widgets.Combo;
37 import org.eclipse.swt.widgets.Display; 39 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.Shell; 40 import org.eclipse.swt.widgets.Shell;
39 41
42 import tango.io.Stdout;
43
40 /** 44 /**
41 * Shows how to bind a Combo so that when update its items, the selection is 45 * Shows how to bind a Combo so that when update its items, the selection is
42 * retained if at all possible. 46 * retained if at all possible.
43 * 47 *
44 * @since 3.2 48 * @since 3.2
45 */ 49 */
46 public class Snippet002UpdateComboRetainSelection { 50 public class Snippet002UpdateComboRetainSelection {
47 public static void main(String[] args) { 51 public static void main(String[] args) {
48 final Display display = new Display(); 52 final Display display = new Display();
49 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { 53 Realm.runWithDefault(SWTObservables.getRealm(display), dgRunnable((Display display_) {
50 public void run() { 54 ViewModel viewModel = new ViewModel();
51 ViewModel viewModel = new ViewModel(); 55 Shell shell = (new View(viewModel)).createShell();
52 Shell shell = new View(viewModel).createShell(); 56
53 57 // The SWT event loop
54 // The SWT event loop 58 while (!shell.isDisposed()) {
55 while (!shell.isDisposed()) { 59 if (!display_.readAndDispatch()) {
56 if (!display.readAndDispatch()) { 60 display_.sleep();
57 display.sleep(); 61 }
58 } 62 }
59 } 63
60 64 // Print the results
61 // Print the results 65 Stdout.formatln( "{}", viewModel.getText());
62 System.out.println(viewModel.getText()); 66 }, display));
63 } 67 display.dispose();
64 });
65 display.dispose();
66 } 68 }
67 69
68 // Minimal JavaBeans support 70 // Minimal JavaBeans support
69 public static abstract class AbstractModelObject { 71 public static abstract class AbstractModelObject {
70 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); 72 private PropertyChangeSupport propertyChangeSupport;
73 this(){
74 propertyChangeSupport = new PropertyChangeSupport(this);
75 }
71 76
72 public void addPropertyChangeListener(PropertyChangeListener listener) { 77 public void addPropertyChangeListener(PropertyChangeListener listener) {
73 propertyChangeSupport.addPropertyChangeListener(listener); 78 propertyChangeSupport.addPropertyChangeListener(listener);
74 } 79 }
75 80
89 propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); 94 propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
90 } 95 }
91 } 96 }
92 97
93 // The View's model--the root of our Model graph for this particular GUI. 98 // The View's model--the root of our Model graph for this particular GUI.
94 public static class ViewModel extends AbstractModelObject { 99 public static class ViewModel : AbstractModelObject {
95 private String text = "beef"; 100 private String text = "beef";
96 101
97 private List choices = new ArrayList(); 102 private List choices;
98 { 103 this(){
104 choices = new ArrayList();
99 choices.add("pork"); 105 choices.add("pork");
100 choices.add("beef"); 106 choices.add("beef");
101 choices.add("poultry"); 107 choices.add("poultry");
102 choices.add("vegatables"); 108 choices.add("vegatables");
103 } 109 }
107 } 113 }
108 114
109 public void setChoices(List choices) { 115 public void setChoices(List choices) {
110 List old = this.choices; 116 List old = this.choices;
111 this.choices = choices; 117 this.choices = choices;
112 firePropertyChange("choices", old, choices); 118 firePropertyChange("choices", cast(Object)old, cast(Object)choices);
113 } 119 }
114 120
115 public String getText() { 121 public String getText() {
116 return text; 122 return text;
117 } 123 }
118 124
119 public void setText(String text) { 125 public void setText(String text) {
120 String oldValue = this.text; 126 String oldValue = this.text;
121 this.text = text; 127 this.text = text;
122 firePropertyChange("text", oldValue, text); 128 firePropertyChange("text", stringcast(oldValue), stringcast(text));
123 } 129 }
124 } 130 }
125 131
126 // The GUI view 132 // The GUI view
127 static class View { 133 static class View {
129 /** 135 /**
130 * used to make a new choices array unique 136 * used to make a new choices array unique
131 */ 137 */
132 static int count; 138 static int count;
133 139
134 public View(ViewModel viewModel) { 140 public this(ViewModel viewModel) {
135 this.viewModel = viewModel; 141 this.viewModel = viewModel;
142 }
143 class ResetSelectionListener : SelectionAdapter {
144 public void widgetSelected(SelectionEvent e) {
145 List newList = new ArrayList();
146 newList.add("Chocolate");
147 newList.add("Vanilla");
148 newList.add("Mango Parfait");
149 newList.add("beef");
150 newList.add("Cheesecake");
151 newList.add(Integer.toString(++count));
152 viewModel.setChoices(newList);
153 }
136 } 154 }
137 155
138 public Shell createShell() { 156 public Shell createShell() {
139 // Build a UI 157 // Build a UI
140 Shell shell = new Shell(Display.getCurrent()); 158 Shell shell = new Shell(Display.getCurrent());
141 shell.setLayout(new RowLayout(SWT.VERTICAL)); 159 shell.setLayout(new RowLayout(SWT.VERTICAL));
142 160
143 Combo combo = new Combo(shell, SWT.BORDER | SWT.READ_ONLY); 161 Combo combo = new Combo(shell, SWT.BORDER | SWT.READ_ONLY);
144 Button reset = new Button(shell, SWT.NULL); 162 Button reset = new Button(shell, SWT.NULL);
145 reset.setText("reset collection"); 163 reset.setText("reset collection");
146 reset.addSelectionListener(new SelectionAdapter() { 164 reset.addSelectionListener(new ResetSelectionListener());
147 public void widgetSelected(SelectionEvent e) {
148 List newList = new ArrayList();
149 newList.add("Chocolate");
150 newList.add("Vanilla");
151 newList.add("Mango Parfait");
152 newList.add("beef");
153 newList.add("Cheesecake");
154 newList.add(Integer.toString(++count));
155 viewModel.setChoices(newList);
156 }
157 });
158 165
159 // Print value out first 166 // Print value out first
160 System.out.println(viewModel.getText()); 167 Stdout.formatln("{}", viewModel.getText());
161 168
162 DataBindingContext dbc = new DataBindingContext(); 169 DataBindingContext dbc = new DataBindingContext();
163 170
164 IObservableList list = MasterDetailObservables.detailList(BeansObservables.observeValue(viewModel, "choices"), 171 IObservableList list = MasterDetailObservables.detailList(BeansObservables.observeValue(viewModel, "choices"),
165 getListDetailFactory(), 172 getListDetailFactory(),
166 String.class); 173 Class.fromType!(String));
167 dbc.bindList(SWTObservables.observeItems(combo), list); 174 dbc.bindList(SWTObservables.observeItems(combo), list, null, null );
168 dbc.bindValue(SWTObservables.observeText(combo), BeansObservables.observeValue(viewModel, "text")); 175 dbc.bindValue(SWTObservables.observeText(combo), BeansObservables.observeValue(viewModel, "text"), null, null );
169 176
170 // Open and return the Shell 177 // Open and return the Shell
171 shell.pack(); 178 shell.pack();
172 shell.open(); 179 shell.open();
173 return shell; 180 return shell;
174 } 181 }
175 } 182 }
176 183
177 private static IObservableFactory getListDetailFactory() { 184 private static IObservableFactory getListDetailFactory() {
178 return new IObservableFactory() { 185 return new class() IObservableFactory {
179 public IObservable createObservable(Object target) { 186 public IObservable createObservable(Object target) {
180 WritableList list = WritableList.withElementType(String.class); 187 WritableList list = WritableList.withElementType(Class.fromType!(String));
181 list.addAll((Collection) target); 188 list.addAll(cast(Collection) target);
182 return list; 189 return list;
183 } 190 }
184 }; 191 };
185 } 192 }
186 } 193 }
194
195 void main( String[] args ){
196 Snippet002UpdateComboRetainSelection.main(args);
197 }
198