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