1 #!/usr/bin/env dub 2 /+ 3 dub.sdl: 4 name "snippet2" 5 dependency "dwt" path="../../../../../../" version="*" 6 libs \ 7 "atk-1.0" \ 8 "cairo" \ 9 "dl" \ 10 "fontconfig" \ 11 "gdk-x11-2.0" \ 12 "gdk_pixbuf-2.0" \ 13 "gio-2.0" \ 14 "glib-2.0" \ 15 "gmodule-2.0" \ 16 "gobject-2.0" \ 17 "gthread-2.0" \ 18 "gtk-x11-2.0" \ 19 "pango-1.0" \ 20 "pangocairo-1.0" \ 21 "X11" \ 22 "Xcomposite" \ 23 "Xcursor" \ 24 "Xdamage" \ 25 "Xext" \ 26 "Xfixes" \ 27 "Xi" \ 28 "Xinerama" \ 29 "Xrandr" \ 30 "Xrender" \ 31 "Xtst" \ 32 platform="linux" 33 +/ 34 /******************************************************************************* 35 * Copyright (c) 2000, 2016 IBM Corporation and others. 36 * All rights reserved. This program and the accompanying materials 37 * are made available under the terms of the Eclipse Public License v1.0 38 * which accompanies this distribution, and is available at 39 * http://www.eclipse.org/legal/epl-v10.html 40 * 41 * Contributors: 42 * IBM Corporation - initial API and implementation 43 * D Port: 44 * alice <stigma@disroot.org> 45 *******************************************************************************/ 46 module org.eclipse.swt.snippets.Snippet2; 47 48 import std.algorithm.comparison : cmp; 49 50 /* 51 * Table example snippet: sort a table by column 52 * 53 * For a list of all SWT example snippets see 54 * http://www.eclipse.org/swt/snippets/ 55 * 56 * @since 3.2 57 */ 58 import org.eclipse.swt.SWT; 59 import org.eclipse.swt.events.SelectionEvent; 60 import org.eclipse.swt.events.SelectionListener; 61 import org.eclipse.swt.layout.all; 62 import org.eclipse.swt.widgets.all; 63 64 void main(string[] args) { 65 Display display = new Display(); 66 Shell shell = new Shell(display); 67 shell.setLayout(new FillLayout()); 68 Table table = new Table(shell, SWT.BORDER); 69 table.setHeaderVisible(true); 70 TableColumn column1 = new TableColumn(table, SWT.NONE); 71 column1.setText("Column 1"); 72 TableColumn column2 = new TableColumn(table, SWT.NONE); 73 column2.setText("Column 2"); 74 TableItem item = new TableItem(table, SWT.NONE); 75 item.setText(["a", "3"]); 76 item = new TableItem(table, SWT.NONE); 77 item.setText(["b", "2"]); 78 item = new TableItem(table, SWT.NONE); 79 item.setText(["c", "1"]); 80 column1.setWidth(100); 81 column2.setWidth(100); 82 83 SelectionListener sortListener = new class SelectionListener { 84 override void widgetSelected(SelectionEvent e) { 85 TableItem[] items = table.getItems(); 86 TableColumn column = cast(TableColumn)e.widget; 87 int index = column is column1 ? 0 : 1; 88 for (int i = 1; i < items.length; i++) { 89 string value1 = items[i].getText(index); 90 for (int j = 0; j < i; j++) { 91 string value2 = items[j].getText(index); 92 if (cmp(value1, value2) < 0) { 93 string[] values = [items[i].getText(0), items[i].getText(1)]; 94 items[i].dispose(); 95 TableItem item1 = new TableItem(table, SWT.NONE, j); 96 item1.setText(values); 97 items = table.getItems(); 98 break; 99 } 100 } 101 } 102 table.setSortColumn(column); 103 } 104 105 override void widgetDefaultSelected(SelectionEvent e) {} 106 }; 107 108 column1.addSelectionListener(sortListener); 109 column2.addSelectionListener(sortListener); 110 table.setSortColumn(column1); 111 table.setSortDirection(SWT.UP); 112 shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300); 113 shell.open(); 114 while (!shell.isDisposed()) { 115 if (!display.readAndDispatch()) display.sleep(); 116 } 117 display.dispose(); 118 }