| 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 | | |
|---|
| | 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 |
|---|
| | 64 | final Table table = new Table(shell, DWT.BORDER | DWT.MULTI | DWT.FULL_SELECTION); |
|---|
| | 65 | table.setLayoutData(new GridData(GridData.FILL_BOTH)); |
|---|
| | 66 | TableColumn column1 = new TableColumn(table, DWT.NONE); |
|---|
| | 67 | TableColumn column2 = new TableColumn(table, DWT.NONE); |
|---|
| | 68 | TableColumn column3 = new TableColumn(table, DWT.NONE); |
|---|
| | 69 | for (int i = 0; i < 100; i++) { |
|---|
| | 70 | TableItem item = new TableItem(table, DWT.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, DWT.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 | |
|---|
| 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 { |
|---|
| | 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, DWT.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 { |
|---|
| 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 { |
|---|
| | 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 == DWT.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 == DWT.ESC) { |
|---|
| | 128 | text.dispose(); |
|---|
| | 129 | } |
|---|
| | 130 | } |
|---|
| | 131 | }); |
|---|
| | 132 | // close the text editor when the user tabs away |
|---|
| | 133 | text.addFocusListener(new class(text) FocusAdapter { |
|---|
| 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 { |
|---|
| | 154 | } |
|---|
| | 155 | public void keyPressed(KeyEvent e) { |
|---|
| | 156 | if (e.keyCode == DWT.CTRL |
|---|
| | 157 | || e.keyCode == DWT.SHIFT |
|---|
| | 158 | || (e.stateMask & DWT.CONTROL) != 0 |
|---|
| | 159 | || (e.stateMask & DWT.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 { |
|---|
| 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 { |
|---|
| | 173 | } |
|---|
| | 174 | |
|---|
| | 175 | public void mouseDown(MouseEvent e) { |
|---|
| | 176 | final Text text = new Text(cursor, DWT.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 { |
|---|
| 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 { |
|---|
| | 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 == DWT.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 == DWT.ESC) { |
|---|
| | 199 | text.dispose(); |
|---|
| | 200 | } |
|---|
| | 201 | } |
|---|
| | 202 | }); |
|---|
| | 203 | // close the text editor when the user clicks away |
|---|
| | 204 | text.addFocusListener(new class(text) FocusAdapter { |
|---|
| 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(); |
|---|
| | 228 | } |
|---|
| | 229 | public void keyReleased(KeyEvent e) { |
|---|
| | 230 | if (e.keyCode == DWT.CONTROL && (e.stateMask & DWT.SHIFT) != 0) |
|---|
| | 231 | return; |
|---|
| | 232 | if (e.keyCode == DWT.SHIFT && (e.stateMask & DWT.CONTROL) != 0) |
|---|
| | 233 | return; |
|---|
| | 234 | if (e.keyCode != DWT.CONTROL |
|---|
| | 235 | && (e.stateMask & DWT.CONTROL) != 0) |
|---|
| | 236 | return; |
|---|
| | 237 | if (e.keyCode != DWT.SHIFT && (e.stateMask & DWT.SHIFT) != 0) |
|---|
| | 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(); |
|---|