Mercurial > projects > dwt2
comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet007ColorLabelProvider.d @ 100:e884642ad36e
more work on examples
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 23 Apr 2009 00:02:38 +0200 |
parents | 5d5bd660917f |
children |
comparison
equal
deleted
inserted
replaced
99:5d5bd660917f | 100:e884642ad36e |
---|---|
59 persons.add(new Person("Elliot Smith", Person.MALE)); | 59 persons.add(new Person("Elliot Smith", Person.MALE)); |
60 persons.add(new Person("Diana Krall", Person.FEMALE)); | 60 persons.add(new Person("Diana Krall", Person.FEMALE)); |
61 persons.add(new Person("David Gilmour", Person.MALE)); | 61 persons.add(new Person("David Gilmour", Person.MALE)); |
62 | 62 |
63 final Display display = new Display(); | 63 final Display display = new Display(); |
64 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { | 64 Realm.runWithDefault(SWTObservables.getRealm(display), dgRunnable(() { |
65 public void run() { | 65 Shell shell = new Shell(display); |
66 Shell shell = new Shell(display); | 66 shell.setText("Gender Bender"); |
67 shell.setText("Gender Bender"); | 67 shell.setLayout(new GridLayout()); |
68 shell.setLayout(new GridLayout()); | 68 |
69 | 69 Table table = new Table(shell, SWT.SINGLE | SWT.H_SCROLL |
70 Table table = new Table(shell, SWT.SINGLE | SWT.H_SCROLL | 70 | SWT.V_SCROLL | SWT.BORDER); |
71 | SWT.V_SCROLL | SWT.BORDER); | 71 GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true); |
72 GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true); | 72 table.setLayoutData(gridData); |
73 table.setLayoutData(gridData); | 73 table.setHeaderVisible(true); |
74 table.setHeaderVisible(true); | 74 table.setLinesVisible(true); |
75 table.setLinesVisible(true); | 75 TableColumn column = new TableColumn(table, SWT.NONE); |
76 TableColumn column = new TableColumn(table, SWT.NONE); | 76 column.setText("No"); |
77 column.setText("No"); | 77 column.setWidth(20); |
78 column.setWidth(20); | 78 column = new TableColumn(table, SWT.NONE); |
79 column = new TableColumn(table, SWT.NONE); | 79 column.setText("Name"); |
80 column.setText("Name"); | 80 column.setWidth(100); |
81 column.setWidth(100); | 81 final TableViewer viewer = new TableViewer(table); |
82 final TableViewer viewer = new TableViewer(table); | 82 |
83 | 83 IObservableList observableList = Observables |
84 IObservableList observableList = Observables | 84 .staticObservableList(persons); |
85 .staticObservableList(persons); | 85 ObservableListContentProvider contentProvider = new ObservableListContentProvider(); |
86 ObservableListContentProvider contentProvider = new ObservableListContentProvider(); | 86 |
87 | 87 viewer.setContentProvider(contentProvider); |
88 viewer.setContentProvider(contentProvider); | 88 |
89 | 89 // this does not have to correspond to the columns in the table, |
90 // this does not have to correspond to the columns in the table, | 90 // we just list all attributes that affect the table content. |
91 // we just list all attributes that affect the table content. | 91 IObservableMap[] attributes = BeansObservables.observeMaps( |
92 IObservableMap[] attributes = BeansObservables.observeMaps( | 92 contentProvider.getKnownElements(), Class.fromType!(Person), |
93 contentProvider.getKnownElements(), Person.class, | 93 [ "name", "gender" ]); |
94 new String[] { "name", "gender" }); | 94 |
95 | 95 class ColorLabelProvider : ObservableMapLabelProvider |
96 class ColorLabelProvider extends ObservableMapLabelProvider | 96 , ITableColorProvider { |
97 implements ITableColorProvider { | 97 Color male; |
98 Color male = display.getSystemColor(SWT.COLOR_BLUE); | 98 Color female; |
99 | 99 |
100 Color female = new Color(display, 255, 192, 203); | 100 this(IObservableMap[] attributes) { |
101 | 101 super(attributes); |
102 ColorLabelProvider(IObservableMap[] attributes) { | 102 male = display.getSystemColor(SWT.COLOR_BLUE); |
103 super(attributes); | 103 female = new Color(display, 255, 192, 203); |
104 } | |
105 | |
106 // to drive home the point that attributes does not have to | |
107 // match | |
108 // the columns | |
109 // in the table, we change the column text as follows: | |
110 public String getColumnText(Object element, int index) { | |
111 if (index is 0) { | |
112 return Integer | |
113 .toString(persons.indexOf(element) + 1); | |
104 } | 114 } |
105 | 115 return (cast(Person) element).getName(); |
106 // to drive home the point that attributes does not have to | 116 } |
107 // match | 117 |
108 // the columns | 118 public Color getBackground(Object element, int index) { |
109 // in the table, we change the column text as follows: | 119 return null; |
110 public String getColumnText(Object element, int index) { | 120 } |
111 if (index == 0) { | 121 |
112 return Integer | 122 public Color getForeground(Object element, int index) { |
113 .toString(persons.indexOf(element) + 1); | 123 if (index is 0) |
114 } | 124 return null; |
115 return ((Person) element).getName(); | 125 Person person = cast(Person) element; |
126 return (person.getGender() is Person.MALE) ? male | |
127 : female; | |
128 } | |
129 | |
130 public void dispose() { | |
131 super.dispose(); | |
132 female.dispose(); | |
133 } | |
134 } | |
135 viewer.setLabelProvider(new ColorLabelProvider(attributes)); | |
136 | |
137 viewer.setInput(cast(Object)observableList); | |
138 | |
139 table.getColumn(0).pack(); | |
140 | |
141 Button button = new Button(shell, SWT.PUSH); | |
142 button.setText("Toggle Gender"); | |
143 button.addSelectionListener(new class() SelectionAdapter { | |
144 public void widgetSelected(SelectionEvent arg0) { | |
145 StructuredSelection selection = cast(StructuredSelection) viewer | |
146 .getSelection(); | |
147 if (selection !is null && !selection.isEmpty()) { | |
148 Person person = cast(Person) selection | |
149 .getFirstElement(); | |
150 person | |
151 .setGender((person.getGender() is Person.MALE) ? Person.FEMALE | |
152 : Person.MALE); | |
116 } | 153 } |
117 | 154 } |
118 public Color getBackground(Object element, int index) { | 155 }); |
119 return null; | 156 |
120 } | 157 shell.setSize(300, 400); |
121 | 158 shell.open(); |
122 public Color getForeground(Object element, int index) { | 159 |
123 if (index == 0) | 160 while (!shell.isDisposed()) { |
124 return null; | 161 if (!display.readAndDispatch()) |
125 Person person = (Person) element; | 162 display.sleep(); |
126 return (person.getGender() == Person.MALE) ? male | |
127 : female; | |
128 } | |
129 | |
130 public void dispose() { | |
131 super.dispose(); | |
132 female.dispose(); | |
133 } | |
134 } | |
135 viewer.setLabelProvider(new ColorLabelProvider(attributes)); | |
136 | |
137 viewer.setInput(observableList); | |
138 | |
139 table.getColumn(0).pack(); | |
140 | |
141 Button button = new Button(shell, SWT.PUSH); | |
142 button.setText("Toggle Gender"); | |
143 button.addSelectionListener(new SelectionAdapter() { | |
144 public void widgetSelected(SelectionEvent arg0) { | |
145 StructuredSelection selection = (StructuredSelection) viewer | |
146 .getSelection(); | |
147 if (selection != null && !selection.isEmpty()) { | |
148 Person person = (Person) selection | |
149 .getFirstElement(); | |
150 person | |
151 .setGender((person.getGender() == Person.MALE) ? Person.FEMALE | |
152 : Person.MALE); | |
153 } | |
154 } | |
155 }); | |
156 | |
157 shell.setSize(300, 400); | |
158 shell.open(); | |
159 | |
160 while (!shell.isDisposed()) { | |
161 if (!display.readAndDispatch()) | |
162 display.sleep(); | |
163 } | |
164 } | 163 } |
165 }); | 164 })); |
166 display.dispose(); | 165 display.dispose(); |
167 } | 166 } |
168 | 167 |
169 static class Person { | 168 static class Person { |
170 static final int MALE = 0; | 169 static final int MALE = 0; |
171 | |
172 static final int FEMALE = 1; | 170 static final int FEMALE = 1; |
173 | |
174 private String name; | 171 private String name; |
175 | |
176 private int gender; | 172 private int gender; |
177 | 173 private PropertyChangeSupport changeSupport; |
178 private PropertyChangeSupport changeSupport = new PropertyChangeSupport( | 174 |
179 this); | 175 this(String name, int gender) { |
180 | 176 changeSupport = new PropertyChangeSupport(this); |
181 Person(String name, int gender) { | |
182 this.name = name; | 177 this.name = name; |
183 this.gender = gender; | 178 this.gender = gender; |
184 } | 179 } |
185 | 180 |
186 /** | 181 /** |