1 #!/usr/bin/env dub 2 /+ 3 dub.sdl: 4 name "snippet96" 5 dependency "dwt" path="../../../../../../" 6 libs \ 7 "atk-1.0" \ 8 "cairo" \ 9 "dl" \ 10 "fontconfig" \ 11 "gdk-x11-2.0" \ 12 "gdk_pixbuf-2.0" \ 13 "glib-2.0" \ 14 "gmodule-2.0" \ 15 "gnomeui-2" \ 16 "gnomevfs-2" \ 17 "gobject-2.0" \ 18 "gthread-2.0" \ 19 "gtk-x11-2.0" \ 20 "pango-1.0" \ 21 "pangocairo-1.0" \ 22 "X11" \ 23 "Xcomposite" \ 24 "Xcursor" \ 25 "Xdamage" \ 26 "Xext" \ 27 "Xfixes" \ 28 "Xi" \ 29 "Xinerama" \ 30 "Xrandr" \ 31 "Xrender" \ 32 "Xtst" \ 33 platform="linux" 34 +/ 35 36 /******************************************************************************* 37 * Copyright (c) 2000, 2004 IBM Corporation and others. 38 * All rights reserved. This program and the accompanying materials 39 * are made available under the terms of the Eclipse Public License v1.0 40 * which accompanies this distribution, and is available at 41 * http://www.eclipse.org/legal/epl-v10.html 42 * 43 * Contributors: 44 * IBM Corporation - initial API and implementation 45 * Port to the D programming language: 46 * yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ ) 47 *******************************************************************************/ 48 49 module org.eclipse.swt.snippets.Snippet96; 50 51 // http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet96.java?view=co 52 53 /* 54 * TableCursor example snippet: navigate a table cells with arrow keys. 55 * Edit when user hits Return key. Exit edit mode by hitting Escape (cancels edit) 56 * or Return (applies edit to table). 57 * 58 * For a list of all SWT example snippets see 59 * http://www.eclipse.org/swt/snippets/ 60 */ 61 import org.eclipse.swt.SWT; 62 import org.eclipse.swt.widgets.Button; 63 import org.eclipse.swt.widgets.Display; 64 import org.eclipse.swt.widgets.Event; 65 import org.eclipse.swt.widgets.Label; 66 import org.eclipse.swt.widgets.Listener; 67 import org.eclipse.swt.widgets.Shell; 68 import org.eclipse.swt.widgets.Table; 69 import org.eclipse.swt.widgets.TableItem; 70 import org.eclipse.swt.widgets.TableColumn; 71 import org.eclipse.swt.widgets.Text; 72 73 import org.eclipse.swt.custom.TableCursor; 74 import org.eclipse.swt.custom.ControlEditor; 75 76 import org.eclipse.swt.layout.GridData; 77 import org.eclipse.swt.layout.GridLayout; 78 79 import org.eclipse.swt.events.SelectionEvent; 80 import org.eclipse.swt.events.SelectionAdapter; 81 import org.eclipse.swt.events.KeyEvent; 82 import org.eclipse.swt.events.KeyAdapter; 83 import org.eclipse.swt.events.FocusEvent; 84 import org.eclipse.swt.events.FocusAdapter; 85 import org.eclipse.swt.events.MouseEvent; 86 import org.eclipse.swt.events.MouseAdapter; 87 88 import java.lang.all; 89 90 version(Tango){ 91 import tango.util.Convert; 92 } else { // Phobos 93 import std.conv; 94 } 95 96 97 void main() { 98 Display display = new Display(); 99 Shell shell = new Shell(display); 100 shell.setLayout(new GridLayout()); 101 102 // create a a table with 3 columns and fill with data 103 Table table = new Table(shell, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION); 104 table.setLayoutData(new GridData(GridData.FILL_BOTH)); 105 TableColumn column1 = new TableColumn(table, SWT.NONE); 106 TableColumn column2 = new TableColumn(table, SWT.NONE); 107 TableColumn column3 = new TableColumn(table, SWT.NONE); 108 for (int i = 0; i < 100; i++) { 109 TableItem item = new TableItem(table, SWT.NONE); 110 item.setText(["cell " ~ to!(String)(i) ~ " 0", "cell " ~ to!(String)(i) ~ " 1", "cell " ~ to!(String)(i) ~ " 2" ]); 111 } 112 column1.pack(); 113 column2.pack(); 114 column3.pack(); 115 116 // create a TableCursor to navigate around the table 117 TableCursor cursor = new TableCursor(table, SWT.NONE); 118 // create an editor to edit the cell when the user hits "ENTER" 119 // while over a cell in the table 120 ControlEditor editor = new ControlEditor(cursor); 121 editor.grabHorizontal = true; 122 editor.grabVertical = true; 123 124 cursor.addSelectionListener(new class SelectionAdapter { 125 // when the TableEditor is over a cell, select the corresponding row in 126 // the table 127 override 128 public void widgetSelected(SelectionEvent e) { 129 table.setSelection([cursor.getRow()]); 130 } 131 // when the user hits "ENTER" in the TableCursor, pop up a text editor so that 132 // they can change the text of the cell 133 override 134 public void widgetDefaultSelected(SelectionEvent e) { 135 Text text = new Text(cursor, SWT.NONE); 136 TableItem row = cursor.getRow(); 137 int column = cursor.getColumn(); 138 text.setText(row.getText(column)); 139 text.addKeyListener(new class(text, cursor) KeyAdapter { 140 Text text; 141 TableCursor cursor; 142 this(Text text_, TableCursor cursor_) 143 { 144 text = text_; 145 cursor = cursor_; 146 } 147 override 148 public void keyPressed(KeyEvent e) { 149 // close the text editor and copy the data over 150 // when the user hits "ENTER" 151 if (e.character == SWT.CR) { 152 TableItem row = cursor.getRow(); 153 int column = cursor.getColumn(); 154 row.setText(column, text.getText()); 155 text.dispose(); 156 } 157 // close the text editor when the user hits "ESC" 158 if (e.character == SWT.ESC) { 159 text.dispose(); 160 } 161 } 162 }); 163 // close the text editor when the user tabs away 164 text.addFocusListener(new class(text) FocusAdapter { 165 Text text; 166 this(Text text_) 167 { 168 text = text_; 169 } 170 override 171 public void focusLost(FocusEvent e) { 172 text.dispose(); 173 } 174 }); 175 editor.setEditor(text); 176 text.setFocus(); 177 } 178 }); 179 // Hide the TableCursor when the user hits the "CTRL" or "SHIFT" key. 180 // This alows the user to select multiple items in the table. 181 cursor.addKeyListener(new class KeyAdapter { 182 override 183 public void keyPressed(KeyEvent e) { 184 if (e.keyCode == SWT.CTRL 185 || e.keyCode == SWT.SHIFT 186 || (e.stateMask & SWT.CONTROL) != 0 187 || (e.stateMask & SWT.SHIFT) != 0) { 188 cursor.setVisible(false); 189 } 190 } 191 }); 192 // When the user double clicks in the TableCursor, pop up a text editor so that 193 // they can change the text of the cell 194 cursor.addMouseListener(new class MouseAdapter { 195 override 196 public void mouseDown(MouseEvent e) { 197 Text text = new Text(cursor, SWT.NONE); 198 TableItem row = cursor.getRow(); 199 int column = cursor.getColumn(); 200 text.setText(row.getText(column)); 201 text.addKeyListener(new class(text, cursor) KeyAdapter { 202 Text text; 203 TableCursor cursor; 204 this(Text text_, TableCursor cursor_) 205 { 206 text = text_; 207 cursor = cursor_; 208 } 209 override 210 public void keyPressed(KeyEvent e) { 211 // close the text editor and copy the data over 212 // when the user hits "ENTER" 213 if (e.character == SWT.CR) { 214 TableItem row = cursor.getRow(); 215 int column = cursor.getColumn(); 216 row.setText(column, text.getText()); 217 text.dispose(); 218 } 219 // close the text editor when the user hits "ESC" 220 if (e.character == SWT.ESC) { 221 text.dispose(); 222 } 223 } 224 }); 225 // close the text editor when the user clicks away 226 text.addFocusListener(new class(text) FocusAdapter { 227 Text text; 228 this(Text text_) 229 { 230 text = text_; 231 } 232 override 233 public void focusLost(FocusEvent e) { 234 text.dispose(); 235 } 236 }); 237 editor.setEditor(text); 238 text.setFocus(); 239 } 240 }); 241 242 // Show the TableCursor when the user releases the "SHIFT" or "CTRL" key. 243 // This signals the end of the multiple selection task. 244 table.addKeyListener(new class KeyAdapter { 245 override 246 public void keyReleased(KeyEvent e) { 247 if (e.keyCode == SWT.CONTROL && (e.stateMask & SWT.SHIFT) != 0) 248 return; 249 if (e.keyCode == SWT.SHIFT && (e.stateMask & SWT.CONTROL) != 0) 250 return; 251 if (e.keyCode != SWT.CONTROL 252 && (e.stateMask & SWT.CONTROL) != 0) 253 return; 254 if (e.keyCode != SWT.SHIFT && (e.stateMask & SWT.SHIFT) != 0) 255 return; 256 257 TableItem[] selection = table.getSelection(); 258 TableItem row = (selection.length == 0) ? table.getItem(table.getTopIndex()) : selection[0]; 259 table.showItem(row); 260 cursor.setSelection(row, 0); 261 cursor.setVisible(true); 262 cursor.setFocus(); 263 } 264 }); 265 266 shell.open(); 267 while (!shell.isDisposed()) { 268 if (!display.readAndDispatch()) 269 display.sleep(); 270 } 271 display.dispose(); 272 }