comparison snippets/table/Snippet96.d @ 102:e962e274e6f9

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