Mercurial > projects > dwt-samples
comparison jface/snippets/Snippet040TableViewerSorting.d @ 88:9ed020f0c2a5
Fix the snippet040 example
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 19 May 2008 14:30:49 +0200 |
parents | 604fa1feb76a |
children |
comparison
equal
deleted
inserted
replaced
87:604fa1feb76a | 88:9ed020f0c2a5 |
---|---|
9 * Tom Schindl - initial API and implementation | 9 * Tom Schindl - initial API and implementation |
10 * Port to the D programming language: | 10 * Port to the D programming language: |
11 * yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ ) | 11 * yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ ) |
12 *******************************************************************************/ | 12 *******************************************************************************/ |
13 module jface.snippets.Snippet040TableViewerSorting; | 13 module jface.snippets.Snippet040TableViewerSorting; |
14 | |
15 | 14 |
16 // http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet040TableViewerSorting.java?view=markup | 15 // http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet040TableViewerSorting.java?view=markup |
17 | 16 |
18 import dwtx.jface.viewers.CellEditor; | 17 import dwtx.jface.viewers.CellEditor; |
19 import dwtx.jface.viewers.ColumnLabelProvider; | 18 import dwtx.jface.viewers.ColumnLabelProvider; |
30 import dwt.events.SelectionEvent; | 29 import dwt.events.SelectionEvent; |
31 import dwt.layout.FillLayout; | 30 import dwt.layout.FillLayout; |
32 import dwt.widgets.Display; | 31 import dwt.widgets.Display; |
33 import dwt.widgets.Shell; | 32 import dwt.widgets.Shell; |
34 import dwt.dwthelper.utils; | 33 import dwt.dwthelper.utils; |
34 | |
35 /** | 35 /** |
36 * Example usage of ViewerComparator in tables to allow sorting | 36 * Example usage of ViewerComparator in tables to allow sorting |
37 * | 37 * |
38 * @author Tom Schindl <tom.schindl@bestsolution.at> | 38 * @author Tom Schindl <tom.schindl@bestsolution.at> |
39 * | 39 * |
41 | 41 |
42 void main(String[] args) { | 42 void main(String[] args) { |
43 Snippet040TableViewerSorting.main(args); | 43 Snippet040TableViewerSorting.main(args); |
44 } | 44 } |
45 | 45 |
46 public class Snippet040TableViewerSorting { | |
47 | |
48 private class MyContentProvider : IStructuredContentProvider { | 46 private class MyContentProvider : IStructuredContentProvider { |
49 | 47 |
50 public Object[] getElements(Object inputElement) { | 48 public Object[] getElements(Object inputElement) { |
51 return cast (Person[]) inputElement; | 49 return (cast(ArrayWrapperT!(Person)) inputElement).array; |
52 } | 50 } |
53 | 51 |
54 public void dispose() { | 52 public void dispose() { |
55 } | 53 } |
56 | 54 |
93 getViewer().update(element, null); | 91 getViewer().update(element, null); |
94 } | 92 } |
95 | 93 |
96 protected abstract void doSetValue(Object element, Object value); | 94 protected abstract void doSetValue(Object element, Object value); |
97 } | 95 } |
96 | |
97 private abstract class ColumnViewerSorter : ViewerComparator { | |
98 public static final int ASC = 1; | |
99 | |
100 public static final int NONE = 0; | |
101 | |
102 public static final int DESC = -1; | |
103 | |
104 private int direction = 0; | |
105 | |
106 private TableViewerColumn column; | |
107 | |
108 private ColumnViewer viewer; | |
109 | |
110 public this(ColumnViewer viewer_, TableViewerColumn column_) { | |
111 this.column = column_; | |
112 this.viewer = viewer_; | |
113 this.column.getColumn().addSelectionListener(new class() SelectionAdapter { | |
114 public void widgetSelected(SelectionEvent e) { | |
115 this.outer.widgetSelected(e); | |
116 } | |
117 }); | |
118 } | |
119 | |
120 private void widgetSelected(SelectionEvent e){ | |
121 if( viewer.getComparator() !is null ) { | |
122 if( viewer.getComparator() is this ) { | |
123 int tdirection = direction; | |
124 | |
125 if( tdirection is ASC ) { | |
126 setSorter(this, DESC); | |
127 } else if( tdirection is DESC ) { | |
128 setSorter(this, NONE); | |
129 } | |
130 } else { | |
131 setSorter(this, ASC); | |
132 } | |
133 } else { | |
134 setSorter(this, ASC); | |
135 } | |
136 } | |
137 public void setSorter(ColumnViewerSorter sorter, int direction) { | |
138 if( direction is NONE ) { | |
139 column.getColumn().getParent().setSortColumn(null); | |
140 column.getColumn().getParent().setSortDirection(DWT.NONE); | |
141 viewer.setComparator(null); | |
142 } else { | |
143 column.getColumn().getParent().setSortColumn(column.getColumn()); | |
144 sorter.direction = direction; | |
145 | |
146 if( direction is ASC ) { | |
147 column.getColumn().getParent().setSortDirection(DWT.DOWN); | |
148 } else { | |
149 column.getColumn().getParent().setSortDirection(DWT.UP); | |
150 } | |
151 | |
152 if( viewer.getComparator() is sorter ) { | |
153 viewer.refresh(); | |
154 } else { | |
155 viewer.setComparator(sorter); | |
156 } | |
157 | |
158 } | |
159 } | |
160 | |
161 public int compare(Viewer viewer, Object e1, Object e2) { | |
162 return direction * doCompare(viewer, e1, e2); | |
163 } | |
164 | |
165 protected abstract int doCompare(Viewer viewer, Object e1, Object e2); | |
166 } | |
167 | |
168 public class Snippet040TableViewerSorting { | |
169 | |
98 | 170 |
99 public this(Shell shell) { | 171 public this(Shell shell) { |
100 TableViewer v = new TableViewer(shell, DWT.BORDER | DWT.FULL_SELECTION); | 172 TableViewer v = new TableViewer(shell, DWT.BORDER | DWT.FULL_SELECTION); |
101 v.setContentProvider(new MyContentProvider()); | 173 v.setContentProvider(new MyContentProvider()); |
102 | 174 |
113 column.setEditingSupport(new class(v) AbstractEditingSupport { | 185 column.setEditingSupport(new class(v) AbstractEditingSupport { |
114 public this(TableViewer t ){ | 186 public this(TableViewer t ){ |
115 super(t); | 187 super(t); |
116 } | 188 } |
117 protected Object getValue(Object element) { | 189 protected Object getValue(Object element) { |
118 return (cast(Person) element).givenname; | 190 return new ArrayWrapperString((cast(Person) element).givenname); |
119 } | 191 } |
120 protected void doSetValue(Object element, Object value) { | 192 protected void doSetValue(Object element, Object value) { |
121 (cast(Person) element).givenname = value.toString(); | 193 (cast(Person) element).givenname = stringcast(value); |
122 } | 194 } |
123 }); | 195 }); |
124 | 196 |
125 ColumnViewerSorter cSorter = new class(v,column) ColumnViewerSorter { | 197 ColumnViewerSorter cSorter = new class(v,column) ColumnViewerSorter { |
126 this(TableViewer t, TableViewerColumn c ){ | 198 this(TableViewer t, TableViewerColumn c ){ |
146 column.setEditingSupport(new class(v) AbstractEditingSupport { | 218 column.setEditingSupport(new class(v) AbstractEditingSupport { |
147 this(TableViewer t ){ | 219 this(TableViewer t ){ |
148 super(t); | 220 super(t); |
149 } | 221 } |
150 protected Object getValue(Object element) { | 222 protected Object getValue(Object element) { |
151 return (cast(Person) element).surname; | 223 return stringcast((cast(Person) element).surname); |
152 } | 224 } |
153 protected void doSetValue(Object element, Object value) { | 225 protected void doSetValue(Object element, Object value) { |
154 (cast(Person) element).surname = value.toString(); | 226 (cast(Person) element).surname = stringcast(value); |
155 } | 227 } |
156 }); | 228 }); |
157 | 229 |
158 new class(v,column) ColumnViewerSorter { | 230 new class(v,column) ColumnViewerSorter { |
159 this(TableViewer t, TableViewerColumn c ){ | 231 this(TableViewer t, TableViewerColumn c ){ |
179 column.setEditingSupport(new class(v) AbstractEditingSupport { | 251 column.setEditingSupport(new class(v) AbstractEditingSupport { |
180 this(TableViewer t ){ | 252 this(TableViewer t ){ |
181 super(t); | 253 super(t); |
182 } | 254 } |
183 protected Object getValue(Object element) { | 255 protected Object getValue(Object element) { |
184 return (cast(Person) element).email; | 256 return stringcast((cast(Person) element).email); |
185 } | 257 } |
186 protected void doSetValue(Object element, Object value) { | 258 protected void doSetValue(Object element, Object value) { |
187 (cast(Person) element).email = value.toString(); | 259 (cast(Person) element).email = stringcast(value); |
188 } | 260 } |
189 }); | 261 }); |
190 new class(v,column) ColumnViewerSorter { | 262 new class(v,column) ColumnViewerSorter { |
191 this(TableViewer t, TableViewerColumn c ){ | 263 this(TableViewer t, TableViewerColumn c ){ |
192 super(t,c); | 264 super(t,c); |
197 return p1.email.compareToIgnoreCase(p2.email); | 269 return p1.email.compareToIgnoreCase(p2.email); |
198 } | 270 } |
199 }; | 271 }; |
200 | 272 |
201 Person[] model = createModel(); | 273 Person[] model = createModel(); |
202 v.setInput(model); | 274 v.setInput( new ArrayWrapperT!(Person)(model)); |
203 v.getTable().setLinesVisible(true); | 275 v.getTable().setLinesVisible(true); |
204 v.getTable().setHeaderVisible(true); | 276 v.getTable().setHeaderVisible(true); |
205 cSorter.setSorter(cSorter, ColumnViewerSorter.ASC); | 277 cSorter.setSorter(cSorter, ColumnViewerSorter.ASC); |
206 } | 278 } |
207 | 279 |
215 elements[3] = new Person("Wayne", "Beaton", "wayne@eclipse.org"); | 287 elements[3] = new Person("Wayne", "Beaton", "wayne@eclipse.org"); |
216 | 288 |
217 return elements; | 289 return elements; |
218 } | 290 } |
219 | 291 |
220 private static abstract class ColumnViewerSorter : ViewerComparator { | |
221 public static final int ASC = 1; | |
222 | |
223 public static final int NONE = 0; | |
224 | |
225 public static final int DESC = -1; | |
226 | |
227 private int direction = 0; | |
228 | |
229 private TableViewerColumn column; | |
230 | |
231 private ColumnViewer viewer; | |
232 | |
233 public this(ColumnViewer viewer_, TableViewerColumn column_) { | |
234 this.column = column_; | |
235 this.viewer = viewer_; | |
236 this.column.getColumn().addSelectionListener(new class() SelectionAdapter { | |
237 | |
238 public void widgetSelected(SelectionEvent e) { | |
239 if( this.viewer.getComparator() !is null ) { | |
240 if( this.viewer.getComparator() == this ) { | |
241 int tdirection = this.direction; | |
242 | |
243 if( tdirection == ASC ) { | |
244 setSorter(this, DESC); | |
245 } else if( tdirection == DESC ) { | |
246 setSorter(this, NONE); | |
247 } | |
248 } else { | |
249 setSorter(this, ASC); | |
250 } | |
251 } else { | |
252 setSorter(this, ASC); | |
253 } | |
254 } | |
255 }); | |
256 } | |
257 | |
258 public void setSorter(ColumnViewerSorter sorter, int direction) { | |
259 if( direction == NONE ) { | |
260 column.getColumn().getParent().setSortColumn(null); | |
261 column.getColumn().getParent().setSortDirection(DWT.NONE); | |
262 viewer.setComparator(null); | |
263 } else { | |
264 column.getColumn().getParent().setSortColumn(column.getColumn()); | |
265 sorter.direction = direction; | |
266 | |
267 if( direction == ASC ) { | |
268 column.getColumn().getParent().setSortDirection(DWT.DOWN); | |
269 } else { | |
270 column.getColumn().getParent().setSortDirection(DWT.UP); | |
271 } | |
272 | |
273 if( viewer.getComparator() == sorter ) { | |
274 viewer.refresh(); | |
275 } else { | |
276 viewer.setComparator(sorter); | |
277 } | |
278 | |
279 } | |
280 } | |
281 | |
282 public int compare(Viewer viewer, Object e1, Object e2) { | |
283 return direction * doCompare(viewer, e1, e2); | |
284 } | |
285 | |
286 protected abstract int doCompare(Viewer viewer, Object e1, Object e2); | |
287 } | |
288 | |
289 /** | 292 /** |
290 * @param args | 293 * @param args |
291 */ | 294 */ |
292 public static void main(String[] args) { | 295 public static void main(String[] args) { |
293 Display display = new Display(); | 296 Display display = new Display(); |