Mercurial > projects > dwt2
comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet022ComputedListCombo.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 java.util.ArrayList; | |
15 import java.util.Iterator; | |
16 import java.util.List; | |
17 | |
18 import org.eclipse.core.databinding.observable.Realm; | |
19 import org.eclipse.core.databinding.observable.list.ComputedList; | |
20 import org.eclipse.core.databinding.observable.list.IObservableList; | |
21 import org.eclipse.core.databinding.observable.list.WritableList; | |
22 import org.eclipse.core.databinding.observable.value.IObservableValue; | |
23 import org.eclipse.jface.databinding.swt.SWTObservables; | |
24 import org.eclipse.jface.databinding.viewers.ObservableListContentProvider; | |
25 import org.eclipse.jface.layout.GridDataFactory; | |
26 import org.eclipse.jface.layout.GridLayoutFactory; | |
27 import org.eclipse.jface.viewers.ComboViewer; | |
28 import org.eclipse.swt.SWT; | |
29 import org.eclipse.swt.layout.GridLayout; | |
30 import org.eclipse.swt.widgets.Button; | |
31 import org.eclipse.swt.widgets.Combo; | |
32 import org.eclipse.swt.widgets.Composite; | |
33 import org.eclipse.swt.widgets.Display; | |
34 import org.eclipse.swt.widgets.Group; | |
35 import org.eclipse.swt.widgets.Shell; | |
36 | |
37 /** | |
38 * @since 3.2 | |
39 * | |
40 */ | |
41 public class Snippet022ComputedListCombo { | |
42 private static WritableList model; | |
43 | |
44 public static void main(String[] args) { | |
45 Display display = new Display(); | |
46 final Shell shell = new Shell(display); | |
47 shell.setLayout(new GridLayout(1, false)); | |
48 | |
49 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { | |
50 public void run() { | |
51 Snippet022ComputedListCombo snippet = new Snippet022ComputedListCombo(); | |
52 snippet.createModel(); | |
53 snippet.createControls(shell); | |
54 } | |
55 }); | |
56 | |
57 shell.pack(); | |
58 shell.open(); | |
59 while (!shell.isDisposed()) { | |
60 if (!display.readAndDispatch()) | |
61 display.sleep(); | |
62 } | |
63 display.dispose(); | |
64 } | |
65 | |
66 /** | |
67 * | |
68 */ | |
69 protected void createModel() { | |
70 model = new WritableList(); | |
71 model.add(new Thing("Alice", true, false)); | |
72 model.add(new Thing("Beth", true, false)); | |
73 model.add(new Thing("Cathy", true, false)); | |
74 model.add(new Thing("Arthur", false, true)); | |
75 model.add(new Thing("Bob", false, true)); | |
76 model.add(new Thing("Curtis", false, true)); | |
77 model.add(new Thing("Snail", true, true)); | |
78 model.add(new Thing("Nail", false, false)); | |
79 } | |
80 | |
81 /** | |
82 * @param shell | |
83 */ | |
84 protected void createControls(Shell shell) { | |
85 Composite composite = new Composite(shell, SWT.NONE); | |
86 Group group = new Group(composite, SWT.NONE); | |
87 group.setText("Filter"); | |
88 Button male = new Button(group, SWT.CHECK); | |
89 male.setText("Male"); | |
90 Button female = new Button(group, SWT.CHECK); | |
91 female.setText("Female"); | |
92 final IObservableValue femaleObservable = SWTObservables | |
93 .observeSelection(female); | |
94 final IObservableValue maleObservable = SWTObservables | |
95 .observeSelection(male); | |
96 Combo combo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY); | |
97 GridDataFactory.defaultsFor(combo).align(SWT.BEGINNING, SWT.BEGINNING) | |
98 .applyTo(combo); | |
99 ComboViewer viewer = new ComboViewer(combo); | |
100 viewer.setContentProvider(new ObservableListContentProvider()); | |
101 // We should really have an out-of-the box filtered list... | |
102 IObservableList filteredList = new ComputedList() { | |
103 protected List calculate() { | |
104 ArrayList result = new ArrayList(); | |
105 for (Iterator it = model.iterator(); it.hasNext();) { | |
106 Thing thing = (Thing) it.next(); | |
107 if (((Boolean) femaleObservable.getValue()).booleanValue() | |
108 && !thing.female) | |
109 continue; | |
110 if (((Boolean) maleObservable.getValue()).booleanValue() | |
111 && !thing.male) | |
112 continue; | |
113 result.add(thing); | |
114 } | |
115 return result; | |
116 } | |
117 }; | |
118 viewer.setInput(filteredList); | |
119 GridLayoutFactory.swtDefaults().applyTo(group); | |
120 GridLayoutFactory.swtDefaults().applyTo(composite); | |
121 } | |
122 | |
123 static class Thing { | |
124 String name; | |
125 boolean male; | |
126 boolean female; | |
127 | |
128 public Thing(String name, boolean female, boolean male) { | |
129 this.name = name; | |
130 this.female = female; | |
131 this.male = male; | |
132 } | |
133 | |
134 public String toString() { | |
135 return name; | |
136 } | |
137 } | |
138 } |