| 1 |
/******************************************************************************* |
|---|
| 2 |
* Copyright (c) 2006 Tom Schindl 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 |
* Tom Schindl - initial API and implementation |
|---|
| 10 |
* Port to the D programming language: |
|---|
| 11 |
* yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ ) |
|---|
| 12 |
*******************************************************************************/ |
|---|
| 13 |
module jface.snippets.Snippet040TableViewerSorting; |
|---|
| 14 |
|
|---|
| 15 |
// http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet040TableViewerSorting.java?view=markup |
|---|
| 16 |
|
|---|
| 17 |
import dwtx.jface.viewers.CellEditor; |
|---|
| 18 |
import dwtx.jface.viewers.ColumnLabelProvider; |
|---|
| 19 |
import dwtx.jface.viewers.ColumnViewer; |
|---|
| 20 |
import dwtx.jface.viewers.EditingSupport; |
|---|
| 21 |
import dwtx.jface.viewers.IStructuredContentProvider; |
|---|
| 22 |
import dwtx.jface.viewers.TableViewer; |
|---|
| 23 |
import dwtx.jface.viewers.TableViewerColumn; |
|---|
| 24 |
import dwtx.jface.viewers.TextCellEditor; |
|---|
| 25 |
import dwtx.jface.viewers.Viewer; |
|---|
| 26 |
import dwtx.jface.viewers.ViewerComparator; |
|---|
| 27 |
import dwt.DWT; |
|---|
| 28 |
import dwt.events.SelectionAdapter; |
|---|
| 29 |
import dwt.events.SelectionEvent; |
|---|
| 30 |
import dwt.layout.FillLayout; |
|---|
| 31 |
import dwt.widgets.Display; |
|---|
| 32 |
import dwt.widgets.Shell; |
|---|
| 33 |
import dwt.dwthelper.utils; |
|---|
| 34 |
|
|---|
| 35 |
/** |
|---|
| 36 |
* Example usage of ViewerComparator in tables to allow sorting |
|---|
| 37 |
* |
|---|
| 38 |
* @author Tom Schindl <tom.schindl@bestsolution.at> |
|---|
| 39 |
* |
|---|
| 40 |
*/ |
|---|
| 41 |
|
|---|
| 42 |
void main(String[] args) { |
|---|
| 43 |
Snippet040TableViewerSorting.main(args); |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
private class MyContentProvider : IStructuredContentProvider { |
|---|
| 47 |
|
|---|
| 48 |
public Object[] getElements(Object inputElement) { |
|---|
| 49 |
return (cast(ArrayWrapperT!(Person)) inputElement).array; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
public void dispose() { |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
public class Person { |
|---|
| 61 |
public String givenname; |
|---|
| 62 |
public String surname; |
|---|
| 63 |
public String email; |
|---|
| 64 |
|
|---|
| 65 |
public this(String givenname, String surname, String email) { |
|---|
| 66 |
this.givenname = givenname; |
|---|
| 67 |
this.surname = surname; |
|---|
| 68 |
this.email = email; |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
protected abstract class AbstractEditingSupport : EditingSupport { |
|---|
| 74 |
private TextCellEditor editor; |
|---|
| 75 |
|
|---|
| 76 |
public this(TableViewer viewer) { |
|---|
| 77 |
super(viewer); |
|---|
| 78 |
this.editor = new TextCellEditor(viewer.getTable()); |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
protected bool canEdit(Object element) { |
|---|
| 82 |
return true; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
protected CellEditor getCellEditor(Object element) { |
|---|
| 86 |
return editor; |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
protected void setValue(Object element, Object value) { |
|---|
| 90 |
doSetValue(element, value); |
|---|
| 91 |
getViewer().update(element, null); |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
protected abstract void doSetValue(Object element, Object value); |
|---|
| 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 |
|
|---|
| 170 |
|
|---|
| 171 |
public this(Shell shell) { |
|---|
| 172 |
TableViewer v = new TableViewer(shell, DWT.BORDER | DWT.FULL_SELECTION); |
|---|
| 173 |
v.setContentProvider(new MyContentProvider()); |
|---|
| 174 |
|
|---|
| 175 |
TableViewerColumn column = new TableViewerColumn(v, DWT.NONE); |
|---|
| 176 |
column.getColumn().setWidth(200); |
|---|
| 177 |
column.getColumn().setText("Givenname"); |
|---|
| 178 |
column.getColumn().setMoveable(true); |
|---|
| 179 |
column.setLabelProvider(new class() ColumnLabelProvider { |
|---|
| 180 |
public String getText(Object element) { |
|---|
| 181 |
return (cast(Person) element).givenname; |
|---|
| 182 |
} |
|---|
| 183 |
}); |
|---|
| 184 |
|
|---|
| 185 |
column.setEditingSupport(new class(v) AbstractEditingSupport { |
|---|
| 186 |
public this(TableViewer t ){ |
|---|
| 187 |
super(t); |
|---|
| 188 |
} |
|---|
| 189 |
protected Object getValue(Object element) { |
|---|
| 190 |
return new ArrayWrapperString((cast(Person) element).givenname); |
|---|
| 191 |
} |
|---|
| 192 |
protected void doSetValue(Object element, Object value) { |
|---|
| 193 |
(cast(Person) element).givenname = stringcast(value); |
|---|
| 194 |
} |
|---|
| 195 |
}); |
|---|
| 196 |
|
|---|
| 197 |
ColumnViewerSorter cSorter = new class(v,column) ColumnViewerSorter { |
|---|
| 198 |
this(TableViewer t, TableViewerColumn c ){ |
|---|
| 199 |
super(t,c); |
|---|
| 200 |
} |
|---|
| 201 |
protected int doCompare(Viewer viewer, Object e1, Object e2) { |
|---|
| 202 |
Person p1 = cast(Person) e1; |
|---|
| 203 |
Person p2 = cast(Person) e2; |
|---|
| 204 |
return p1.givenname.compareToIgnoreCase(p2.givenname); |
|---|
| 205 |
} |
|---|
| 206 |
}; |
|---|
| 207 |
|
|---|
| 208 |
column = new TableViewerColumn(v, DWT.NONE); |
|---|
| 209 |
column.getColumn().setWidth(200); |
|---|
| 210 |
column.getColumn().setText("Surname"); |
|---|
| 211 |
column.getColumn().setMoveable(true); |
|---|
| 212 |
column.setLabelProvider(new class() ColumnLabelProvider { |
|---|
| 213 |
public String getText(Object element) { |
|---|
| 214 |
return (cast(Person) element).surname; |
|---|
| 215 |
} |
|---|
| 216 |
}); |
|---|
| 217 |
|
|---|
| 218 |
column.setEditingSupport(new class(v) AbstractEditingSupport { |
|---|
| 219 |
this(TableViewer t ){ |
|---|
| 220 |
super(t); |
|---|
| 221 |
} |
|---|
| 222 |
protected Object getValue(Object element) { |
|---|
| 223 |
return stringcast((cast(Person) element).surname); |
|---|
| 224 |
} |
|---|
| 225 |
protected void doSetValue(Object element, Object value) { |
|---|
| 226 |
(cast(Person) element).surname = stringcast(value); |
|---|
| 227 |
} |
|---|
| 228 |
}); |
|---|
| 229 |
|
|---|
| 230 |
new class(v,column) ColumnViewerSorter { |
|---|
| 231 |
this(TableViewer t, TableViewerColumn c ){ |
|---|
| 232 |
super(t,c); |
|---|
| 233 |
} |
|---|
| 234 |
protected int doCompare(Viewer viewer, Object e1, Object e2) { |
|---|
| 235 |
Person p1 = cast(Person) e1; |
|---|
| 236 |
Person p2 = cast(Person) e2; |
|---|
| 237 |
return p1.surname.compareToIgnoreCase(p2.surname); |
|---|
| 238 |
} |
|---|
| 239 |
}; |
|---|
| 240 |
|
|---|
| 241 |
column = new TableViewerColumn(v, DWT.NONE); |
|---|
| 242 |
column.getColumn().setWidth(200); |
|---|
| 243 |
column.getColumn().setText("E-Mail"); |
|---|
| 244 |
column.getColumn().setMoveable(true); |
|---|
| 245 |
column.setLabelProvider(new class() ColumnLabelProvider { |
|---|
| 246 |
public String getText(Object element) { |
|---|
| 247 |
return (cast(Person) element).email; |
|---|
| 248 |
} |
|---|
| 249 |
}); |
|---|
| 250 |
|
|---|
| 251 |
column.setEditingSupport(new class(v) AbstractEditingSupport { |
|---|
| 252 |
this(TableViewer t ){ |
|---|
| 253 |
super(t); |
|---|
| 254 |
} |
|---|
| 255 |
protected Object getValue(Object element) { |
|---|
| 256 |
return stringcast((cast(Person) element).email); |
|---|
| 257 |
} |
|---|
| 258 |
protected void doSetValue(Object element, Object value) { |
|---|
| 259 |
(cast(Person) element).email = stringcast(value); |
|---|
| 260 |
} |
|---|
| 261 |
}); |
|---|
| 262 |
new class(v,column) ColumnViewerSorter { |
|---|
| 263 |
this(TableViewer t, TableViewerColumn c ){ |
|---|
| 264 |
super(t,c); |
|---|
| 265 |
} |
|---|
| 266 |
protected int doCompare(Viewer viewer, Object e1, Object e2) { |
|---|
| 267 |
Person p1 = cast(Person) e1; |
|---|
| 268 |
Person p2 = cast(Person) e2; |
|---|
| 269 |
return p1.email.compareToIgnoreCase(p2.email); |
|---|
| 270 |
} |
|---|
| 271 |
}; |
|---|
| 272 |
|
|---|
| 273 |
Person[] model = createModel(); |
|---|
| 274 |
v.setInput( new ArrayWrapperT!(Person)(model)); |
|---|
| 275 |
v.getTable().setLinesVisible(true); |
|---|
| 276 |
v.getTable().setHeaderVisible(true); |
|---|
| 277 |
cSorter.setSorter(cSorter, ColumnViewerSorter.ASC); |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
private Person[] createModel() { |
|---|
| 281 |
Person[] elements = new Person[4]; |
|---|
| 282 |
elements[0] = new Person("Tom", "Schindl", |
|---|
| 283 |
"tom.schindl@bestsolution.at"); |
|---|
| 284 |
elements[1] = new Person("Boris", "Bokowski", |
|---|
| 285 |
"Boris_Bokowski@ca.ibm.com"); |
|---|
| 286 |
elements[2] = new Person("Tod", "Creasey", "Tod_Creasey@ca.ibm.com"); |
|---|
| 287 |
elements[3] = new Person("Wayne", "Beaton", "wayne@eclipse.org"); |
|---|
| 288 |
|
|---|
| 289 |
return elements; |
|---|
| 290 |
} |
|---|
| 291 |
|
|---|
| 292 |
/** |
|---|
| 293 |
* @param args |
|---|
| 294 |
*/ |
|---|
| 295 |
public static void main(String[] args) { |
|---|
| 296 |
Display display = new Display(); |
|---|
| 297 |
|
|---|
| 298 |
Shell shell = new Shell(display); |
|---|
| 299 |
shell.setLayout(new FillLayout()); |
|---|
| 300 |
new Snippet040TableViewerSorting(shell); |
|---|
| 301 |
shell.open(); |
|---|
| 302 |
|
|---|
| 303 |
while (!shell.isDisposed()) { |
|---|
| 304 |
if (!display.readAndDispatch()) |
|---|
| 305 |
display.sleep(); |
|---|
| 306 |
} |
|---|
| 307 |
display.dispose(); |
|---|
| 308 |
} |
|---|
| 309 |
|
|---|
| 310 |
} |
|---|