Mercurial > projects > dwt-samples
annotate dwtexamples/addressbook/AddressBook.d @ 65:a84ba4d1820f
restructure the dsss.conf and resources
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 11 Apr 2008 21:15:36 +0200 |
parents | 96a7ecc03d98 |
children |
rev | line source |
---|---|
0 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2006 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 | |
21 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
0 | 12 *******************************************************************************/ |
13 module dwtexamples.addressbook.AddressBook; | |
14 | |
15 import dwt.DWT; | |
16 import dwt.events.MenuAdapter; | |
17 import dwt.events.MenuEvent; | |
18 import dwt.events.SelectionAdapter; | |
19 import dwt.events.SelectionEvent; | |
20 import dwt.events.ShellAdapter; | |
21 import dwt.events.ShellEvent; | |
22 import dwt.graphics.Cursor; | |
23 import dwt.layout.FillLayout; | |
24 import dwt.widgets.Display; | |
25 import dwt.widgets.FileDialog; | |
26 import dwt.widgets.Menu; | |
27 import dwt.widgets.MenuItem; | |
28 import dwt.widgets.MessageBox; | |
29 import dwt.widgets.Shell; | |
30 import dwt.widgets.Table; | |
31 import dwt.widgets.TableColumn; | |
32 import dwt.widgets.TableItem; | |
33 | |
34 import dwt.dwthelper.ResourceBundle; | |
35 | |
36 import tango.core.Exception; | |
37 import tango.io.FilePath; | |
38 import tango.io.File; | |
39 import tango.io.FileConduit; | |
40 import tango.io.stream.FileStream; | |
41 import TextUtil = tango.text.Util; | |
42 import Unicode = tango.text.Unicode; | |
43 | |
11
b61e7baf0574
Reworked the DrawingBoard for better organization. And also removed the linker fixes in the source files and moved it to dsss.conf by adding buildflags+=-L-lDD-tango-util.
Jesse Phillips <Jesse.K.Phillips+D@gmail.com>
parents:
3
diff
changeset
|
44 import dwtexamples.addressbook.SearchDialog; |
b61e7baf0574
Reworked the DrawingBoard for better organization. And also removed the linker fixes in the source files and moved it to dsss.conf by adding buildflags+=-L-lDD-tango-util.
Jesse Phillips <Jesse.K.Phillips+D@gmail.com>
parents:
3
diff
changeset
|
45 import dwtexamples.addressbook.DataEntryDialog; |
b61e7baf0574
Reworked the DrawingBoard for better organization. And also removed the linker fixes in the source files and moved it to dsss.conf by adding buildflags+=-L-lDD-tango-util.
Jesse Phillips <Jesse.K.Phillips+D@gmail.com>
parents:
3
diff
changeset
|
46 import dwtexamples.addressbook.FindListener; |
0 | 47 |
48 void main() { | |
49 Display display = new Display(); | |
50 auto application = new AddressBook(); | |
51 Shell shell = application.open(display); | |
52 while(!shell.isDisposed()){ | |
53 if(!display.readAndDispatch()){ | |
54 display.sleep(); | |
55 } | |
56 } | |
57 display.dispose(); | |
58 } | |
59 | |
60 /** | |
61 * AddressBookExample is an example that uses <code>org.eclipse.swt</code> | |
62 * libraries to implement a simple address book. This application has | |
63 * save, load, sorting, and searching functions common | |
64 * to basic address books. | |
65 */ | |
66 public class AddressBook { | |
67 | |
68 private static ResourceBundle resAddressBook; | |
65
a84ba4d1820f
restructure the dsss.conf and resources
Frank Benoit <benoit@tionex.de>
parents:
21
diff
changeset
|
69 private static const char[] resAddressBookData = cast(char[]) import( "dwtexamples.addressbook.addressbook.properties" ); |
0 | 70 private Shell shell; |
71 | |
72 private Table table; | |
73 private SearchDialog searchDialog; | |
74 | |
75 private FilePath file; | |
76 private bool isModified; | |
77 | |
78 private char[][] copyBuffer; | |
79 | |
80 private int lastSortColumn= -1; | |
81 | |
82 private static const char[] DELIMITER = "\t"; | |
83 private static char[][] columnNames; | |
84 | |
85 public this(){ | |
86 if( resAddressBook is null ){ | |
87 resAddressBook = ResourceBundle.getBundleFromData(resAddressBookData); | |
88 columnNames = [ | |
89 resAddressBook.getString("Last_name"), | |
90 resAddressBook.getString("First_name"), | |
91 resAddressBook.getString("Business_phone"), | |
92 resAddressBook.getString("Home_phone"), | |
93 resAddressBook.getString("Email"), | |
94 resAddressBook.getString("Fax") ]; | |
95 } | |
96 } | |
97 | |
98 | |
99 public Shell open(Display display) { | |
100 shell = new Shell(display); | |
101 shell.setLayout(new FillLayout()); | |
102 | |
103 shell.addShellListener( new class() ShellAdapter { | |
104 public void shellClosed(ShellEvent e) { | |
105 e.doit = closeAddressBook(); | |
106 } | |
107 }); | |
108 | |
109 createMenuBar(); | |
110 | |
111 searchDialog = new SearchDialog(shell); | |
112 searchDialog.setSearchAreaNames(columnNames); | |
113 searchDialog.setSearchAreaLabel(resAddressBook.getString("Column")); | |
114 searchDialog.addFindListener(new class() FindListener { | |
115 public bool find() { | |
116 return findEntry(); | |
117 } | |
118 }); | |
119 | |
120 table = new Table(shell, DWT.SINGLE | DWT.BORDER | DWT.FULL_SELECTION); | |
121 table.setHeaderVisible(true); | |
122 table.setMenu(createPopUpMenu()); | |
123 table.addSelectionListener(new class() SelectionAdapter { | |
124 public void widgetDefaultSelected(SelectionEvent e) { | |
125 TableItem[] items = table.getSelection(); | |
126 if (items.length > 0) editEntry(items[0]); | |
127 } | |
128 }); | |
129 for(int i = 0; i < columnNames.length; i++) { | |
130 TableColumn column = new TableColumn(table, DWT.NONE); | |
131 column.setText(columnNames[i]); | |
132 column.setWidth(150); | |
133 int columnIndex = i; | |
134 column.addSelectionListener(new class(columnIndex) SelectionAdapter { | |
135 int c; | |
136 this( int c ){ this.c = c; } | |
137 public void widgetSelected(SelectionEvent e) { | |
138 sort(c); | |
139 } | |
140 }); | |
141 } | |
142 | |
143 newAddressBook(); | |
144 | |
145 shell.setSize(table.computeSize(DWT.DEFAULT, DWT.DEFAULT).x, 300); | |
146 shell.open(); | |
147 return shell; | |
148 } | |
149 | |
150 private bool closeAddressBook() { | |
151 if(isModified) { | |
152 //ask user if they want to save current address book | |
153 MessageBox box = new MessageBox(shell, DWT.ICON_WARNING | DWT.YES | DWT.NO | DWT.CANCEL); | |
154 box.setText(shell.getText()); | |
155 box.setMessage(resAddressBook.getString("Close_save")); | |
156 | |
157 int choice = box.open(); | |
158 if(choice is DWT.CANCEL) { | |
159 return false; | |
160 } else if(choice is DWT.YES) { | |
161 if (!save()) return false; | |
162 } | |
163 } | |
164 | |
165 TableItem[] items = table.getItems(); | |
166 for (int i = 0; i < items.length; i ++) { | |
167 items[i].dispose(); | |
168 } | |
169 | |
170 return true; | |
171 } | |
172 /** | |
173 * Creates the menu at the top of the shell where most | |
174 * of the programs functionality is accessed. | |
175 * | |
176 * @return The <code>Menu</code> widget that was created | |
177 */ | |
178 private Menu createMenuBar() { | |
179 Menu menuBar = new Menu(shell, DWT.BAR); | |
180 shell.setMenuBar(menuBar); | |
181 | |
182 //create each header and subMenu for the menuBar | |
183 createFileMenu(menuBar); | |
184 createEditMenu(menuBar); | |
185 createSearchMenu(menuBar); | |
186 createHelpMenu(menuBar); | |
187 | |
188 return menuBar; | |
189 } | |
190 | |
191 /** | |
192 * Converts an encoded <code>char[]</code> to a char[] array representing a table entry. | |
193 */ | |
194 private char[][] decodeLine(char[] line) { | |
195 char[][] toks = TextUtil.split( line, DELIMITER ); | |
196 while( toks.length < table.getColumnCount() ){ | |
197 toks ~= ""; | |
198 } | |
199 return toks[ 0 .. table.getColumnCount() ]; | |
200 } | |
201 private void displayError(char[] msg) { | |
202 MessageBox box = new MessageBox(shell, DWT.ICON_ERROR); | |
203 box.setMessage(msg); | |
204 box.open(); | |
205 } | |
206 private void editEntry(TableItem item) { | |
207 DataEntryDialog dialog = new DataEntryDialog(shell); | |
208 dialog.setLabels(columnNames); | |
209 char[][] values = new char[][table.getColumnCount()]; | |
210 for (int i = 0; i < values.length; i++) { | |
211 values[i] = item.getText(i); | |
212 } | |
213 dialog.setValues(values); | |
214 values = dialog.open(); | |
215 if (values !is null) { | |
216 item.setText(values); | |
217 isModified = true; | |
218 } | |
219 } | |
220 private char[] encodeLine(char[][] tableItems) { | |
221 char[] line = ""; | |
222 for (int i = 0; i < tableItems.length - 1; i++) { | |
223 line ~= tableItems[i] ~ DELIMITER; | |
224 } | |
225 line ~= tableItems[tableItems.length - 1] ~ "\n"; | |
226 | |
227 return line; | |
228 } | |
229 private bool findEntry() { | |
230 Cursor waitCursor = new Cursor(shell.getDisplay(), DWT.CURSOR_WAIT); | |
231 shell.setCursor(waitCursor); | |
232 | |
233 bool matchCase = searchDialog.getMatchCase(); | |
234 bool matchWord = searchDialog.getMatchWord(); | |
235 char[] searchString = searchDialog.getSearchString(); | |
236 int column = searchDialog.getSelectedSearchArea(); | |
237 | |
238 searchString = matchCase ? searchString : Unicode.toLower( searchString ); | |
239 | |
240 bool found = false; | |
241 if (searchDialog.getSearchDown()) { | |
242 for(int i = table.getSelectionIndex() + 1; i < table.getItemCount(); i++) { | |
243 found = findMatch(searchString, table.getItem(i), column, matchWord, matchCase); | |
244 if ( found ){ | |
245 table.setSelection(i); | |
246 break; | |
247 } | |
248 } | |
249 } else { | |
250 for(int i = table.getSelectionIndex() - 1; i > -1; i--) { | |
251 found = findMatch(searchString, table.getItem(i), column, matchWord, matchCase); | |
252 if ( found ){ | |
253 table.setSelection(i); | |
254 break; | |
255 } | |
256 } | |
257 } | |
258 | |
259 shell.setCursor(cast(Cursor)null); | |
260 waitCursor.dispose(); | |
261 | |
262 return found; | |
263 } | |
264 private bool findMatch(char[] searchString, TableItem item, int column, bool matchWord, bool matchCase) { | |
265 | |
266 char[] tableText = matchCase ? item.getText(column) : Unicode.toLower( item.getText(column)); | |
267 if (matchWord) { | |
268 if (tableText !is null && tableText==searchString) { | |
269 return true; | |
270 } | |
271 | |
272 } else { | |
273 if(tableText!is null && TextUtil.containsPattern( tableText, searchString)) { | |
274 return true; | |
275 } | |
276 } | |
277 return false; | |
278 } | |
279 private void newAddressBook() { | |
280 shell.setText(resAddressBook.getString("Title_bar") ~ resAddressBook.getString("New_title")); | |
281 *(cast(Object*)&file) = null; | |
282 ///cast(Object)file = null; | |
283 isModified = false; | |
284 } | |
285 private void newEntry() { | |
286 DataEntryDialog dialog = new DataEntryDialog(shell); | |
287 dialog.setLabels(columnNames); | |
288 char[][] data = dialog.open(); | |
289 if (data !is null) { | |
290 TableItem item = new TableItem(table, DWT.NONE); | |
291 item.setText(data); | |
292 isModified = true; | |
293 } | |
294 } | |
295 | |
296 private void openAddressBook() { | |
297 FileDialog fileDialog = new FileDialog(shell, DWT.OPEN); | |
298 | |
299 fileDialog.setFilterExtensions(["*.adr;", "*.*"]); | |
300 fileDialog.setFilterNames([ | |
301 resAddressBook.getString("Book_filter_name") ~ " (*.adr)", | |
302 resAddressBook.getString("All_filter_name") ~ " (*.*)"]); | |
303 char[] name = fileDialog.open(); | |
304 | |
305 if(name is null) return; | |
306 FilePath file = new FilePath(name); | |
307 if (!file.exists()) { | |
308 displayError(resAddressBook.getString("File")~file.toString()~" "~resAddressBook.getString("Does_not_exist")); | |
309 return; | |
310 } | |
311 | |
312 Cursor waitCursor = new Cursor(shell.getDisplay(), DWT.CURSOR_WAIT); | |
313 shell.setCursor(waitCursor); | |
314 | |
315 char[][] data; | |
316 try { | |
317 scope ioFile = new File (file); | |
318 data = TextUtil.splitLines (cast(char[]) ioFile.read); | |
319 } catch (IOException e ) { | |
320 displayError(resAddressBook.getString("IO_error_read") ~ "\n" ~ file.toString()); | |
321 return; | |
322 } finally { | |
323 | |
324 shell.setCursor(cast(Cursor)null); | |
325 waitCursor.dispose(); | |
326 } | |
327 | |
328 char[][][] tableInfo = new char[][][](data.length,table.getColumnCount()); | |
329 foreach( idx, line; data ){ | |
330 char[][] linetoks = decodeLine(line); | |
331 tableInfo[ idx ] = linetoks; | |
332 } | |
333 /+ | |
334 int writeIndex = 0; | |
335 for (int i = 0; i < data.length; i++) { | |
336 char[][] line = decodeLine(data[i]); | |
337 if (line !is null) tableInfo[writeIndex++] = line; | |
338 } | |
339 if (writeIndex !is data.length) { | |
340 char[][][] result = new char[][writeIndex][table.getColumnCount()]; | |
341 System.arraycopy(tableInfo, 0, result, 0, writeIndex); | |
342 tableInfo = result; | |
343 } | |
344 +/ | |
345 tango.core.Array.sort( tableInfo, new RowComparator(0)); | |
346 | |
347 for (int i = 0; i < tableInfo.length; i++) { | |
348 TableItem item = new TableItem(table, DWT.NONE); | |
349 item.setText(tableInfo[i]); | |
350 } | |
351 shell.setText(resAddressBook.getString("Title_bar")~fileDialog.getFileName()); | |
352 isModified = false; | |
353 this.file = file; | |
354 } | |
355 private bool save() { | |
356 if(file is null) return saveAs(); | |
357 | |
358 Cursor waitCursor = new Cursor(shell.getDisplay(), DWT.CURSOR_WAIT); | |
359 shell.setCursor(waitCursor); | |
360 | |
361 TableItem[] items = table.getItems(); | |
362 char[][] lines = new char[][items.length]; | |
363 for(int i = 0; i < items.length; i++) { | |
364 char[][] itemText = new char[][table.getColumnCount()]; | |
365 for (int j = 0; j < itemText.length; j++) { | |
366 itemText[j] = items[i].getText(j); | |
367 } | |
368 lines[i] = encodeLine(itemText); | |
369 } | |
370 | |
371 FileOutput fileOutput; | |
372 bool result = true; | |
373 try { | |
374 fileOutput = new FileOutput( file.toString ); | |
375 for (int i = 0; i < lines.length; i++) { | |
376 fileOutput.write(lines[i]); | |
377 } | |
378 } catch(IOException e ) { | |
379 displayError(resAddressBook.getString("IO_error_write") ~ "\n" ~ file.toString()); | |
380 result = false; | |
3 | 381 } catch(Exception e2 ) { |
0 | 382 displayError(resAddressBook.getString("error_write") ~ "\n" ~ e2.toString()); |
383 result = false; | |
384 } finally { | |
385 shell.setCursor(null); | |
386 waitCursor.dispose(); | |
387 | |
388 } | |
389 | |
390 if(fileOutput !is null) { | |
391 try { | |
392 fileOutput.close(); | |
393 } catch(IOException e) { | |
394 displayError(resAddressBook.getString("IO_error_close") ~ "\n" ~ file.toString()); | |
395 return false; | |
396 } | |
397 } | |
398 if( !result ){ | |
399 return false; | |
400 } | |
401 | |
402 shell.setText(resAddressBook.getString("Title_bar")~file.toString()); | |
403 isModified = false; | |
404 return true; | |
405 } | |
406 private bool saveAs() { | |
407 | |
408 FileDialog saveDialog = new FileDialog(shell, DWT.SAVE); | |
409 saveDialog.setFilterExtensions(["*.adr;", "*.*"]); | |
410 saveDialog.setFilterNames(["Address Books (*.adr)", "All Files "]); | |
411 | |
412 saveDialog.open(); | |
413 char[] name = saveDialog.getFileName(); | |
414 if(!name) return false; | |
415 | |
416 if( TextUtil.locatePatternPrior( name, ".adr" ) !is name.length - 4) { | |
417 name ~= ".adr"; | |
418 } | |
419 | |
420 FilePath file = new FilePath(saveDialog.getFilterPath() ); | |
421 file.append( name ); | |
422 if(file.exists()) { | |
423 MessageBox box = new MessageBox(shell, DWT.ICON_WARNING | DWT.YES | DWT.NO); | |
424 box.setText(resAddressBook.getString("Save_as_title")); | |
425 box.setMessage(resAddressBook.getString("File") ~ file.toString()~" "~resAddressBook.getString("Query_overwrite")); | |
426 if(box.open() !is DWT.YES) { | |
427 return false; | |
428 } | |
429 } | |
430 this.file = file; | |
431 return save(); | |
432 } | |
433 private void sort(int column) { | |
434 if(table.getItemCount() <= 1) return; | |
435 | |
436 TableItem[] items = table.getItems(); | |
437 char[][][] data = new char[][][](items.length, table.getColumnCount()); | |
438 for(int i = 0; i < items.length; i++) { | |
439 for(int j = 0; j < table.getColumnCount(); j++) { | |
440 data[i][j] = items[i].getText(j); | |
441 } | |
442 } | |
443 | |
444 tango.core.Array.sort(data, new RowComparator(column)); | |
445 | |
446 if (lastSortColumn !is column) { | |
447 table.setSortColumn(table.getColumn(column)); | |
448 table.setSortDirection(DWT.DOWN); | |
449 for (int i = 0; i < data.length; i++) { | |
450 items[i].setText(data[i]); | |
451 } | |
452 lastSortColumn = column; | |
453 } else { | |
454 // reverse order if the current column is selected again | |
455 table.setSortDirection(DWT.UP); | |
456 int j = data.length -1; | |
457 for (int i = 0; i < data.length; i++) { | |
458 items[i].setText(data[j--]); | |
459 } | |
460 lastSortColumn = -1; | |
461 } | |
462 | |
463 } | |
464 /** | |
465 * Creates all the items located in the File submenu and | |
466 * associate all the menu items with their appropriate | |
467 * functions. | |
468 * | |
469 * @param menuBar Menu | |
470 * the <code>Menu</code> that file contain | |
471 * the File submenu. | |
472 */ | |
473 private void createFileMenu(Menu menuBar) { | |
474 //File menu. | |
475 MenuItem item = new MenuItem(menuBar, DWT.CASCADE); | |
476 item.setText(resAddressBook.getString("File_menu_title")); | |
477 Menu menu = new Menu(shell, DWT.DROP_DOWN); | |
478 item.setMenu(menu); | |
479 /** | |
480 * Adds a listener to handle enabling and disabling | |
481 * some items in the Edit submenu. | |
482 */ | |
483 menu.addMenuListener(new class() MenuAdapter { | |
484 public void menuShown(MenuEvent e) { | |
485 Menu menu = cast(Menu)e.widget; | |
486 MenuItem[] items = menu.getItems(); | |
487 items[1].setEnabled(table.getSelectionCount() !is 0); // edit contact | |
488 items[5].setEnabled((file !is null) && isModified); // save | |
489 items[6].setEnabled(table.getItemCount() !is 0); // save as | |
490 } | |
491 }); | |
492 | |
493 | |
494 //File -> New Contact | |
495 MenuItem subItem = new MenuItem(menu, DWT.NONE); | |
496 subItem.setText(resAddressBook.getString("New_contact")); | |
497 subItem.setAccelerator(DWT.MOD1 + 'N'); | |
498 subItem.addSelectionListener(new class() SelectionAdapter { | |
499 public void widgetSelected(SelectionEvent e) { | |
500 newEntry(); | |
501 } | |
502 }); | |
503 subItem = new MenuItem(menu, DWT.NONE); | |
504 subItem.setText(resAddressBook.getString("Edit_contact")); | |
505 subItem.setAccelerator(DWT.MOD1 + 'E'); | |
506 subItem.addSelectionListener(new class() SelectionAdapter { | |
507 public void widgetSelected(SelectionEvent e) { | |
508 TableItem[] items = table.getSelection(); | |
509 if (items.length is 0) return; | |
510 editEntry(items[0]); | |
511 } | |
512 }); | |
513 | |
514 | |
515 new MenuItem(menu, DWT.SEPARATOR); | |
516 | |
517 //File -> New Address Book | |
518 subItem = new MenuItem(menu, DWT.NONE); | |
519 subItem.setText(resAddressBook.getString("New_address_book")); | |
520 subItem.setAccelerator(DWT.MOD1 + 'B'); | |
521 subItem.addSelectionListener(new class() SelectionAdapter { | |
522 public void widgetSelected(SelectionEvent e) { | |
523 if (closeAddressBook()) { | |
524 newAddressBook(); | |
525 } | |
526 } | |
527 }); | |
528 | |
529 //File -> Open | |
530 subItem = new MenuItem(menu, DWT.NONE); | |
531 subItem.setText(resAddressBook.getString("Open_address_book")); | |
532 subItem.setAccelerator(DWT.MOD1 + 'O'); | |
533 subItem.addSelectionListener(new class() SelectionAdapter { | |
534 public void widgetSelected(SelectionEvent e) { | |
535 if (closeAddressBook()) { | |
536 openAddressBook(); | |
537 } | |
538 } | |
539 }); | |
540 | |
541 //File -> Save. | |
542 subItem = new MenuItem(menu, DWT.NONE); | |
543 subItem.setText(resAddressBook.getString("Save_address_book")); | |
544 subItem.setAccelerator(DWT.MOD1 + 'S'); | |
545 subItem.addSelectionListener(new class() SelectionAdapter { | |
546 public void widgetSelected(SelectionEvent e) { | |
547 save(); | |
548 } | |
549 }); | |
550 | |
551 //File -> Save As. | |
552 subItem = new MenuItem(menu, DWT.NONE); | |
553 subItem.setText(resAddressBook.getString("Save_book_as")); | |
554 subItem.setAccelerator(DWT.MOD1 + 'A'); | |
555 subItem.addSelectionListener(new class() SelectionAdapter { | |
556 public void widgetSelected(SelectionEvent e) { | |
557 saveAs(); | |
558 } | |
559 }); | |
560 | |
561 | |
562 new MenuItem(menu, DWT.SEPARATOR); | |
563 | |
564 //File -> Exit. | |
565 subItem = new MenuItem(menu, DWT.NONE); | |
566 subItem.setText(resAddressBook.getString("Exit")); | |
567 subItem.addSelectionListener(new class() SelectionAdapter { | |
568 public void widgetSelected(SelectionEvent e) { | |
569 shell.close(); | |
570 } | |
571 }); | |
572 } | |
573 | |
574 /** | |
575 * Creates all the items located in the Edit submenu and | |
576 * associate all the menu items with their appropriate | |
577 * functions. | |
578 * | |
579 * @param menuBar Menu | |
580 * the <code>Menu</code> that file contain | |
581 * the Edit submenu. | |
582 * | |
583 * @see #createSortMenu() | |
584 */ | |
585 private MenuItem createEditMenu(Menu menuBar) { | |
586 //Edit menu. | |
587 MenuItem item = new MenuItem(menuBar, DWT.CASCADE); | |
588 item.setText(resAddressBook.getString("Edit_menu_title")); | |
589 Menu menu = new Menu(shell, DWT.DROP_DOWN); | |
590 item.setMenu(menu); | |
591 | |
592 /** | |
593 * Add a listener to handle enabling and disabling | |
594 * some items in the Edit submenu. | |
595 */ | |
596 menu.addMenuListener(new class() MenuAdapter { | |
597 public void menuShown(MenuEvent e) { | |
598 Menu menu = cast(Menu)e.widget; | |
599 MenuItem[] items = menu.getItems(); | |
600 int count = table.getSelectionCount(); | |
601 items[0].setEnabled(count !is 0); // edit | |
602 items[1].setEnabled(count !is 0); // copy | |
603 items[2].setEnabled(copyBuffer !is null); // paste | |
604 items[3].setEnabled(count !is 0); // delete | |
605 items[5].setEnabled(table.getItemCount() !is 0); // sort | |
606 } | |
607 }); | |
608 | |
609 //Edit -> Edit | |
610 MenuItem subItem = new MenuItem(menu, DWT.PUSH); | |
611 subItem.setText(resAddressBook.getString("Edit")); | |
612 subItem.setAccelerator(DWT.MOD1 + 'E'); | |
613 subItem.addSelectionListener(new class() SelectionAdapter { | |
614 public void widgetSelected(SelectionEvent e) { | |
615 TableItem[] items = table.getSelection(); | |
616 if (items.length is 0) return; | |
617 editEntry(items[0]); | |
618 } | |
619 }); | |
620 | |
621 //Edit -> Copy | |
622 subItem = new MenuItem(menu, DWT.NONE); | |
623 subItem.setText(resAddressBook.getString("Copy")); | |
624 subItem.setAccelerator(DWT.MOD1 + 'C'); | |
625 subItem.addSelectionListener(new class() SelectionAdapter { | |
626 public void widgetSelected(SelectionEvent e) { | |
627 TableItem[] items = table.getSelection(); | |
628 if (items.length is 0) return; | |
629 copyBuffer = new char[][table.getColumnCount()]; | |
630 for (int i = 0; i < copyBuffer.length; i++) { | |
631 copyBuffer[i] = items[0].getText(i); | |
632 } | |
633 } | |
634 }); | |
635 | |
636 //Edit -> Paste | |
637 subItem = new MenuItem(menu, DWT.NONE); | |
638 subItem.setText(resAddressBook.getString("Paste")); | |
639 subItem.setAccelerator(DWT.MOD1 + 'V'); | |
640 subItem.addSelectionListener(new class() SelectionAdapter { | |
641 public void widgetSelected(SelectionEvent e) { | |
642 if (copyBuffer is null) return; | |
643 TableItem item = new TableItem(table, DWT.NONE); | |
644 item.setText(copyBuffer); | |
645 isModified = true; | |
646 } | |
647 }); | |
648 | |
649 //Edit -> Delete | |
650 subItem = new MenuItem(menu, DWT.NONE); | |
651 subItem.setText(resAddressBook.getString("Delete")); | |
652 subItem.addSelectionListener(new class() SelectionAdapter { | |
653 public void widgetSelected(SelectionEvent e) { | |
654 TableItem[] items = table.getSelection(); | |
655 if (items.length is 0) return; | |
656 items[0].dispose(); | |
657 isModified = true; } | |
658 }); | |
659 | |
660 new MenuItem(menu, DWT.SEPARATOR); | |
661 | |
662 //Edit -> Sort(Cascade) | |
663 subItem = new MenuItem(menu, DWT.CASCADE); | |
664 subItem.setText(resAddressBook.getString("Sort")); | |
665 Menu submenu = createSortMenu(); | |
666 subItem.setMenu(submenu); | |
667 | |
668 return item; | |
669 | |
670 } | |
671 | |
672 /** | |
673 * Creates all the items located in the Sort cascading submenu and | |
674 * associate all the menu items with their appropriate | |
675 * functions. | |
676 * | |
677 * @return Menu | |
678 * The cascading menu with all the sort menu items on it. | |
679 */ | |
680 private Menu createSortMenu() { | |
681 Menu submenu = new Menu(shell, DWT.DROP_DOWN); | |
682 MenuItem subitem; | |
683 for(int i = 0; i < columnNames.length; i++) { | |
684 subitem = new MenuItem (submenu, DWT.NONE); | |
685 subitem.setText(columnNames [i]); | |
686 int column = i; | |
687 subitem.addSelectionListener(new class(column) SelectionAdapter { | |
688 int c; | |
689 this(int c){ this.c = c; } | |
690 public void widgetSelected(SelectionEvent e) { | |
691 sort(c); | |
692 } | |
693 }); | |
694 } | |
695 | |
696 return submenu; | |
697 } | |
698 | |
699 /** | |
700 * Creates all the items located in the Search submenu and | |
701 * associate all the menu items with their appropriate | |
702 * functions. | |
703 * | |
704 * @param menuBar Menu | |
705 * the <code>Menu</code> that file contain | |
706 * the Search submenu. | |
707 */ | |
708 private void createSearchMenu(Menu menuBar) { | |
709 //Search menu. | |
710 MenuItem item = new MenuItem(menuBar, DWT.CASCADE); | |
711 item.setText(resAddressBook.getString("Search_menu_title")); | |
712 Menu searchMenu = new Menu(shell, DWT.DROP_DOWN); | |
713 item.setMenu(searchMenu); | |
714 | |
715 //Search -> Find... | |
716 item = new MenuItem(searchMenu, DWT.NONE); | |
717 item.setText(resAddressBook.getString("Find")); | |
718 item.setAccelerator(DWT.MOD1 + 'F'); | |
719 item.addSelectionListener(new class() SelectionAdapter { | |
720 public void widgetSelected(SelectionEvent e) { | |
721 searchDialog.setMatchCase(false); | |
722 searchDialog.setMatchWord(false); | |
723 searchDialog.setSearchDown(true); | |
724 searchDialog.setSearchString(""); | |
725 searchDialog.setSelectedSearchArea(0); | |
726 searchDialog.open(); | |
727 } | |
728 }); | |
729 | |
730 //Search -> Find Next | |
731 item = new MenuItem(searchMenu, DWT.NONE); | |
732 item.setText(resAddressBook.getString("Find_next")); | |
733 item.setAccelerator(DWT.F3); | |
734 item.addSelectionListener(new class() SelectionAdapter { | |
735 public void widgetSelected(SelectionEvent e) { | |
736 searchDialog.open(); | |
737 } | |
738 }); | |
739 } | |
740 | |
741 /** | |
742 * Creates all items located in the popup menu and associates | |
743 * all the menu items with their appropriate functions. | |
744 * | |
745 * @return Menu | |
746 * The created popup menu. | |
747 */ | |
748 private Menu createPopUpMenu() { | |
749 Menu popUpMenu = new Menu(shell, DWT.POP_UP); | |
750 | |
751 /** | |
752 * Adds a listener to handle enabling and disabling | |
753 * some items in the Edit submenu. | |
754 */ | |
755 popUpMenu.addMenuListener(new class() MenuAdapter { | |
756 public void menuShown(MenuEvent e) { | |
757 Menu menu = cast(Menu)e.widget; | |
758 MenuItem[] items = menu.getItems(); | |
759 int count = table.getSelectionCount(); | |
760 items[2].setEnabled(count !is 0); // edit | |
761 items[3].setEnabled(count !is 0); // copy | |
762 items[4].setEnabled(copyBuffer !is null); // paste | |
763 items[5].setEnabled(count !is 0); // delete | |
764 items[7].setEnabled(table.getItemCount() !is 0); // find | |
765 } | |
766 }); | |
767 | |
768 //New | |
769 MenuItem item = new MenuItem(popUpMenu, DWT.PUSH); | |
770 item.setText(resAddressBook.getString("Pop_up_new")); | |
771 item.addSelectionListener(new class() SelectionAdapter { | |
772 public void widgetSelected(SelectionEvent e) { | |
773 newEntry(); | |
774 } | |
775 }); | |
776 | |
777 new MenuItem(popUpMenu, DWT.SEPARATOR); | |
778 | |
779 //Edit | |
780 item = new MenuItem(popUpMenu, DWT.PUSH); | |
781 item.setText(resAddressBook.getString("Pop_up_edit")); | |
782 item.addSelectionListener(new class() SelectionAdapter { | |
783 public void widgetSelected(SelectionEvent e) { | |
784 TableItem[] items = table.getSelection(); | |
785 if (items.length is 0) return; | |
786 editEntry(items[0]); | |
787 } | |
788 }); | |
789 | |
790 //Copy | |
791 item = new MenuItem(popUpMenu, DWT.PUSH); | |
792 item.setText(resAddressBook.getString("Pop_up_copy")); | |
793 item.addSelectionListener(new class() SelectionAdapter { | |
794 public void widgetSelected(SelectionEvent e) { | |
795 TableItem[] items = table.getSelection(); | |
796 if (items.length is 0) return; | |
797 copyBuffer = new char[][table.getColumnCount()]; | |
798 for (int i = 0; i < copyBuffer.length; i++) { | |
799 copyBuffer[i] = items[0].getText(i); | |
800 } | |
801 } | |
802 }); | |
803 | |
804 //Paste | |
805 item = new MenuItem(popUpMenu, DWT.PUSH); | |
806 item.setText(resAddressBook.getString("Pop_up_paste")); | |
807 item.addSelectionListener(new class() SelectionAdapter { | |
808 public void widgetSelected(SelectionEvent e) { | |
809 if (copyBuffer is null) return; | |
810 TableItem item = new TableItem(table, DWT.NONE); | |
811 item.setText(copyBuffer); | |
812 isModified = true; | |
813 } | |
814 }); | |
815 | |
816 //Delete | |
817 item = new MenuItem(popUpMenu, DWT.PUSH); | |
818 item.setText(resAddressBook.getString("Pop_up_delete")); | |
819 item.addSelectionListener(new class() SelectionAdapter { | |
820 public void widgetSelected(SelectionEvent e) { | |
821 TableItem[] items = table.getSelection(); | |
822 if (items.length is 0) return; | |
823 items[0].dispose(); | |
824 isModified = true; | |
825 } | |
826 }); | |
827 | |
828 new MenuItem(popUpMenu, DWT.SEPARATOR); | |
829 | |
830 //Find... | |
831 item = new MenuItem(popUpMenu, DWT.PUSH); | |
832 item.setText(resAddressBook.getString("Pop_up_find")); | |
833 item.addSelectionListener(new class() SelectionAdapter { | |
834 public void widgetSelected(SelectionEvent e) { | |
835 searchDialog.open(); | |
836 } | |
837 }); | |
838 | |
839 return popUpMenu; | |
840 } | |
841 | |
842 /** | |
843 * Creates all the items located in the Help submenu and | |
844 * associate all the menu items with their appropriate | |
845 * functions. | |
846 * | |
847 * @param menuBar Menu | |
848 * the <code>Menu</code> that file contain | |
849 * the Help submenu. | |
850 */ | |
851 private void createHelpMenu(Menu menuBar) { | |
852 | |
853 //Help Menu | |
854 MenuItem item = new MenuItem(menuBar, DWT.CASCADE); | |
855 item.setText(resAddressBook.getString("Help_menu_title")); | |
856 Menu menu = new Menu(shell, DWT.DROP_DOWN); | |
857 item.setMenu(menu); | |
858 | |
859 //Help -> About Text Editor | |
860 MenuItem subItem = new MenuItem(menu, DWT.NONE); | |
861 subItem.setText(resAddressBook.getString("About")); | |
862 subItem.addSelectionListener(new class() SelectionAdapter { | |
863 public void widgetSelected(SelectionEvent e) { | |
864 MessageBox box = new MessageBox(shell, DWT.NONE); | |
865 box.setText(resAddressBook.getString("About_1") ~ shell.getText()); | |
866 box.setMessage(shell.getText() ~ resAddressBook.getString("About_2")); | |
867 box.open(); | |
868 } | |
869 }); | |
870 } | |
871 | |
872 | |
873 /** | |
874 * To compare entries (rows) by the given column | |
875 */ | |
876 private class RowComparator /*: Comparator*/ { | |
877 private int column; | |
878 | |
879 /** | |
880 * Constructs a RowComparator given the column index | |
881 * @param col The index (starting at zero) of the column | |
882 */ | |
883 public this(int col) { | |
884 column = col; | |
885 } | |
886 | |
887 /** | |
888 * Compares two rows (type char[][]) using the specified | |
889 * column entry. | |
890 * @param obj1 First row to compare | |
891 * @param obj2 Second row to compare | |
892 * @return negative if obj1 less than obj2, positive if | |
893 * obj1 greater than obj2, and zero if equal. | |
894 */ | |
895 public bool compare(char[][] row1, char[][] row2) { | |
896 return row1[column] < row2[column]; | |
897 } | |
898 | |
899 alias compare opCall; | |
900 } | |
901 | |
902 } |