Mercurial > projects > dwt2
annotate org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet96.d @ 125:c43718956f21 default tip
Updated the snippets status.
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Thu, 11 Aug 2011 19:55:14 +0200 |
parents | 536e43f63c81 |
children |
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 | |
112 | 55 version(Tango){ |
56 import tango.util.Convert; | |
57 } else { // Phobos | |
58 import std.conv; | |
59 } | |
28 | 60 |
61 | |
62 void main() { | |
63 Display display = new Display(); | |
64 Shell shell = new Shell(display); | |
65 shell.setLayout(new GridLayout()); | |
66 | |
67 // create a a table with 3 columns and fill with data | |
112 | 68 Table table = new Table(shell, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION); |
28 | 69 table.setLayoutData(new GridData(GridData.FILL_BOTH)); |
70 TableColumn column1 = new TableColumn(table, SWT.NONE); | |
71 TableColumn column2 = new TableColumn(table, SWT.NONE); | |
72 TableColumn column3 = new TableColumn(table, SWT.NONE); | |
73 for (int i = 0; i < 100; i++) { | |
74 TableItem item = new TableItem(table, SWT.NONE); | |
112 | 75 item.setText(["cell " ~ to!(String)(i) ~ " 0", "cell " ~ to!(String)(i) ~ " 1", "cell " ~ to!(String)(i) ~ " 2" ]); |
28 | 76 } |
77 column1.pack(); | |
78 column2.pack(); | |
79 column3.pack(); | |
80 | |
81 // create a TableCursor to navigate around the table | |
112 | 82 TableCursor cursor = new TableCursor(table, SWT.NONE); |
28 | 83 // create an editor to edit the cell when the user hits "ENTER" |
84 // while over a cell in the table | |
112 | 85 ControlEditor editor = new ControlEditor(cursor); |
28 | 86 editor.grabHorizontal = true; |
87 editor.grabVertical = true; | |
88 | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
112
diff
changeset
|
89 cursor.addSelectionListener(new class SelectionAdapter { |
28 | 90 // when the TableEditor is over a cell, select the corresponding row in |
91 // the table | |
92 public void widgetSelected(SelectionEvent e) { | |
93 table.setSelection([cursor.getRow()]); | |
94 } | |
95 // when the user hits "ENTER" in the TableCursor, pop up a text editor so that | |
96 // they can change the text of the cell | |
97 public void widgetDefaultSelected(SelectionEvent e) { | |
112 | 98 Text text = new Text(cursor, SWT.NONE); |
28 | 99 TableItem row = cursor.getRow(); |
100 int column = cursor.getColumn(); | |
101 text.setText(row.getText(column)); | |
102 text.addKeyListener(new class(text, cursor) KeyAdapter { | |
103 Text text; | |
104 TableCursor cursor; | |
105 this(Text text_, TableCursor cursor_) | |
106 { | |
107 text = text_; | |
108 cursor = cursor_; | |
109 } | |
110 public void keyPressed(KeyEvent e) { | |
111 // close the text editor and copy the data over | |
112 // when the user hits "ENTER" | |
113 if (e.character == SWT.CR) { | |
114 TableItem row = cursor.getRow(); | |
115 int column = cursor.getColumn(); | |
116 row.setText(column, text.getText()); | |
117 text.dispose(); | |
118 } | |
119 // close the text editor when the user hits "ESC" | |
120 if (e.character == SWT.ESC) { | |
121 text.dispose(); | |
122 } | |
123 } | |
124 }); | |
125 // close the text editor when the user tabs away | |
126 text.addFocusListener(new class(text) FocusAdapter { | |
127 Text text; | |
128 this(Text text_) | |
129 { | |
130 text = text_; | |
131 } | |
132 public void focusLost(FocusEvent e) { | |
133 text.dispose(); | |
134 } | |
135 }); | |
136 editor.setEditor(text); | |
137 text.setFocus(); | |
138 } | |
139 }); | |
140 // Hide the TableCursor when the user hits the "CTRL" or "SHIFT" key. | |
141 // This alows the user to select multiple items in the table. | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
112
diff
changeset
|
142 cursor.addKeyListener(new class KeyAdapter { |
28 | 143 public void keyPressed(KeyEvent e) { |
144 if (e.keyCode == SWT.CTRL | |
145 || e.keyCode == SWT.SHIFT | |
146 || (e.stateMask & SWT.CONTROL) != 0 | |
147 || (e.stateMask & SWT.SHIFT) != 0) { | |
148 cursor.setVisible(false); | |
149 } | |
150 } | |
151 }); | |
152 // When the user double clicks in the TableCursor, pop up a text editor so that | |
153 // they can change the text of the cell | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
112
diff
changeset
|
154 cursor.addMouseListener(new class MouseAdapter { |
28 | 155 public void mouseDown(MouseEvent e) { |
112 | 156 Text text = new Text(cursor, SWT.NONE); |
28 | 157 TableItem row = cursor.getRow(); |
158 int column = cursor.getColumn(); | |
159 text.setText(row.getText(column)); | |
160 text.addKeyListener(new class(text, cursor) KeyAdapter { | |
161 Text text; | |
162 TableCursor cursor; | |
163 this(Text text_, TableCursor cursor_) | |
164 { | |
165 text = text_; | |
166 cursor = cursor_; | |
167 } | |
168 public void keyPressed(KeyEvent e) { | |
169 // close the text editor and copy the data over | |
170 // when the user hits "ENTER" | |
171 if (e.character == SWT.CR) { | |
172 TableItem row = cursor.getRow(); | |
173 int column = cursor.getColumn(); | |
174 row.setText(column, text.getText()); | |
175 text.dispose(); | |
176 } | |
177 // close the text editor when the user hits "ESC" | |
178 if (e.character == SWT.ESC) { | |
179 text.dispose(); | |
180 } | |
181 } | |
182 }); | |
183 // close the text editor when the user clicks away | |
184 text.addFocusListener(new class(text) FocusAdapter { | |
185 Text text; | |
186 this(Text text_) | |
187 { | |
188 text = text_; | |
189 } | |
190 public void focusLost(FocusEvent e) { | |
191 text.dispose(); | |
192 } | |
193 }); | |
194 editor.setEditor(text); | |
195 text.setFocus(); | |
196 } | |
197 }); | |
198 | |
199 // Show the TableCursor when the user releases the "SHIFT" or "CTRL" key. | |
200 // This signals the end of the multiple selection task. | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
112
diff
changeset
|
201 table.addKeyListener(new class KeyAdapter { |
28 | 202 public void keyReleased(KeyEvent e) { |
29
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
203 if (e.keyCode == SWT.CONTROL && (e.stateMask & SWT.SHIFT) != 0) |
28 | 204 return; |
29
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
205 if (e.keyCode == SWT.SHIFT && (e.stateMask & SWT.CONTROL) != 0) |
28 | 206 return; |
207 if (e.keyCode != SWT.CONTROL | |
208 && (e.stateMask & SWT.CONTROL) != 0) | |
209 return; | |
29
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
210 if (e.keyCode != SWT.SHIFT && (e.stateMask & SWT.SHIFT) != 0) |
28 | 211 return; |
212 | |
213 TableItem[] selection = table.getSelection(); | |
214 TableItem row = (selection.length == 0) ? table.getItem(table.getTopIndex()) : selection[0]; | |
215 table.showItem(row); | |
216 cursor.setSelection(row, 0); | |
217 cursor.setVisible(true); | |
218 cursor.setFocus(); | |
219 } | |
220 }); | |
221 | |
222 shell.open(); | |
223 while (!shell.isDisposed()) { | |
224 if (!display.readAndDispatch()) | |
225 display.sleep(); | |
226 } | |
227 display.dispose(); | |
228 } |