comparison snippets/table/Snippet96.d @ 100:d06432c0c931

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