66
|
1 module jface.Librarian;
|
|
2
|
|
3 import dwtx.core.runtime.IProgressMonitor;
|
|
4 import dwtx.jface.action.MenuManager;
|
|
5 import dwtx.jface.action.StatusLineManager;
|
|
6 import dwtx.jface.action.ToolBarManager;
|
|
7 import dwtx.jface.action.CoolBarManager;
|
|
8 import dwtx.jface.action.Action;
|
|
9 import dwtx.jface.action.IAction;
|
|
10 import dwtx.jface.dialogs.MessageDialog;
|
|
11 import dwtx.jface.operation.IRunnableWithProgress;
|
|
12 import dwtx.jface.operation.ModalContext;
|
|
13 import dwtx.jface.util.IPropertyChangeListener;
|
|
14 import dwtx.jface.util.PropertyChangeEvent;
|
|
15 import dwtx.jface.viewers.TableViewer;
|
|
16 import dwtx.jface.viewers.Viewer;
|
|
17 import dwtx.jface.viewers.ITableLabelProvider;
|
|
18 import dwtx.jface.viewers.IStructuredContentProvider;
|
|
19 import dwtx.jface.viewers.ICellModifier;
|
|
20 import dwtx.jface.viewers.ILabelProviderListener;
|
|
21 import dwtx.jface.viewers.TextCellEditor;
|
|
22 import dwtx.jface.viewers.CellEditor;
|
|
23 import dwtx.jface.viewers.CheckboxCellEditor;
|
|
24 import dwtx.jface.action.Separator;
|
|
25 import dwtx.jface.viewers.IStructuredSelection;
|
|
26 import dwtx.jface.window.ApplicationWindow;
|
|
27 import dwtx.jface.resource.ImageDescriptor;
|
|
28
|
|
29 import dwt.DWT;
|
|
30
|
|
31 import dwt.graphics.Image;
|
|
32 import dwt.graphics.ImageData;
|
|
33 import dwt.layout.GridData;
|
|
34 import dwt.layout.GridLayout;
|
|
35 import dwt.widgets.Display;
|
|
36 import dwt.widgets.FileDialog;
|
|
37 import dwt.widgets.MessageBox;
|
|
38 import dwt.widgets.Shell;
|
|
39 import dwt.widgets.Table;
|
|
40 import dwt.widgets.TableColumn;
|
|
41 import dwt.widgets.Control;
|
|
42 import dwt.widgets.Composite;
|
|
43 import dwt.widgets.Item;
|
|
44
|
|
45 import dwt.dwthelper.utils;
|
|
46 import dwt.dwthelper.ByteArrayInputStream;
|
|
47
|
|
48 import tango.text.convert.Format;
|
|
49 import tango.util.collection.LinkSeq;
|
|
50 import tango.io.FilePath;
|
|
51 import tango.io.File;
|
|
52 import tango.io.Print;
|
|
53 import tango.io.stream.FileStream;
|
|
54 import tango.text.Util;
|
|
55 import tango.text.stream.LineIterator;
|
|
56 import tango.util.log.Trace;
|
|
57
|
67
|
58 version(JIVE) import jive.stacktrace;
|
|
59
|
66
|
60 /++
|
|
61 Known Problem:
|
|
62 Blocks in Save Method
|
|
63 Not prepared for windows.
|
|
64 ++/
|
|
65
|
|
66 /**
|
|
67 * The application entry point
|
|
68 *
|
|
69 * @param args the command line arguments
|
|
70 */
|
|
71 void main() {
|
|
72 (new Librarian()).run();
|
|
73 }
|
|
74
|
|
75
|
|
76 /**
|
|
77 * This class keeps track of you library, and who you've loaned books to
|
|
78 */
|
|
79 public class Librarian : ApplicationWindow {
|
|
80 // A static instance to the running application
|
|
81 private static Librarian APP;
|
|
82
|
|
83 // Table column names/properties
|
|
84 public static const String TITLE = "Title";
|
|
85 public static const String CHECKED_OUT = "?";
|
|
86 public static const String WHO = "By Whom";
|
|
87 public static const String[] PROPS = [ TITLE, CHECKED_OUT, WHO ];
|
|
88
|
|
89 // The viewer
|
|
90 private TableViewer viewer;
|
|
91
|
|
92 // The current library
|
|
93 private Library library;
|
|
94
|
|
95 // The actions
|
|
96 private NewAction newAction;
|
|
97 private OpenAction openAction;
|
|
98 private SaveAction saveAction;
|
|
99 private SaveAsAction saveAsAction;
|
|
100 private ExitAction exitAction;
|
|
101 private AddBookAction addBookAction;
|
|
102 private RemoveBookAction removeBookAction;
|
|
103 private AboutAction aboutAction;
|
|
104 private ShowBookCountAction showBookCountAction;
|
|
105
|
|
106 /**
|
|
107 * Gets the running application
|
|
108 */
|
|
109 public static Librarian getApp() {
|
|
110 return APP;
|
|
111 }
|
|
112
|
|
113 /**
|
|
114 * Librarian constructor
|
|
115 */
|
|
116 public this() {
|
|
117 super(null);
|
|
118
|
|
119 APP = this;
|
|
120
|
|
121 // Create the data model
|
|
122 library = new Library();
|
|
123
|
|
124 // Create the actions
|
|
125 newAction = new NewAction();
|
|
126 openAction = new OpenAction();
|
|
127 saveAction = new SaveAction();
|
|
128 saveAsAction = new SaveAsAction();
|
|
129 exitAction = new ExitAction();
|
|
130 addBookAction = new AddBookAction();
|
|
131 removeBookAction = new RemoveBookAction();
|
|
132 aboutAction = new AboutAction();
|
|
133 showBookCountAction = new ShowBookCountAction();
|
|
134
|
|
135 addMenuBar();
|
|
136 //addCoolBar(DWT.NONE);
|
|
137 addToolBar(DWT.FLAT);
|
|
138 addStatusLine();
|
|
139 }
|
|
140
|
|
141 /**
|
|
142 * Runs the application
|
|
143 */
|
|
144 public void run() {
|
|
145 // Don't return from open() until window closes
|
|
146 setBlockOnOpen(true);
|
|
147
|
|
148 // Open the main window
|
|
149 open();
|
|
150
|
|
151 // Dispose the display
|
|
152 Display.getCurrent().dispose();
|
|
153 }
|
|
154
|
|
155 /**
|
|
156 * Configures the shell
|
|
157 *
|
|
158 * @param shell the shell
|
|
159 */
|
|
160 protected void configureShell(Shell shell) {
|
|
161 super.configureShell(shell);
|
|
162
|
|
163 // Set the title bar text
|
|
164 shell.setText("Librarian");
|
|
165 }
|
|
166
|
|
167 /**
|
|
168 * Creates the main window's contents
|
|
169 *
|
|
170 * @param parent the main window
|
|
171 * @return Control
|
|
172 */
|
|
173 protected Control createContents(Composite parent) {
|
|
174 Composite composite = new Composite(parent, DWT.NONE);
|
|
175 composite.setLayout(new GridLayout(1, false));
|
|
176
|
|
177 viewer = new TableViewer(composite, DWT.FULL_SELECTION | DWT.BORDER);
|
|
178 Table table = viewer.getTable();
|
|
179 table.setLayoutData(new GridData(GridData.FILL_BOTH));
|
|
180
|
|
181 // Set up the viewer
|
|
182 viewer.setContentProvider(new LibraryContentProvider());
|
|
183 viewer.setLabelProvider(new LibraryLabelProvider());
|
|
184 viewer.setInput(library);
|
|
185 viewer.setColumnProperties(PROPS);
|
|
186 viewer.setCellEditors( [ cast(CellEditor) new TextCellEditor(table),
|
|
187 new CheckboxCellEditor(table), new TextCellEditor(table)]);
|
|
188 viewer.setCellModifier(new LibraryCellModifier());
|
|
189
|
|
190 // Set up the table
|
|
191 for (int i = 0, n = PROPS.length; i < n; i++)
|
|
192 (new TableColumn(table, DWT.LEFT)).setText(PROPS[i]);
|
|
193 table.setHeaderVisible(true);
|
|
194 table.setLinesVisible(true);
|
|
195
|
|
196 // Add code to hide or display the book count based on the action
|
|
197 showBookCountAction.addPropertyChangeListener(new class IPropertyChangeListener {
|
|
198 public void propertyChange(PropertyChangeEvent event) {
|
|
199 // The value has changed; refresh the view
|
|
200 refreshView();
|
|
201 }
|
|
202 });
|
|
203
|
|
204 // Rfresh the view to get the columns right-sized
|
|
205 refreshView();
|
|
206
|
|
207 return composite;
|
|
208 }
|
|
209
|
|
210 /**
|
|
211 * Creates the menu for the application
|
|
212 *
|
|
213 * @return MenuManager
|
|
214 */
|
|
215 protected MenuManager createMenuManager() {
|
|
216 // Create the main menu
|
|
217 MenuManager mm = new MenuManager();
|
|
218
|
|
219 // Create the File menu
|
|
220 MenuManager fileMenu = new MenuManager("File");
|
|
221 mm.add(fileMenu);
|
|
222
|
|
223 // Add the actions to the File menu
|
|
224 fileMenu.add(newAction);
|
|
225 fileMenu.add(openAction);
|
|
226 fileMenu.add(saveAction);
|
|
227 fileMenu.add(saveAsAction);
|
|
228 fileMenu.add(new Separator());
|
|
229 fileMenu.add(exitAction);
|
|
230
|
|
231 // Create the Book menu
|
|
232 MenuManager bookMenu = new MenuManager("Book");
|
|
233 mm.add(bookMenu);
|
|
234
|
|
235 // Add the actions to the Book menu
|
|
236 bookMenu.add(addBookAction);
|
|
237 bookMenu.add(removeBookAction);
|
|
238
|
|
239 // Create the View menu
|
|
240 MenuManager viewMenu = new MenuManager("View");
|
|
241 mm.add(viewMenu);
|
|
242
|
|
243 // Add the actions to the View menu
|
|
244 viewMenu.add(showBookCountAction);
|
|
245
|
|
246 // Create the Help menu
|
|
247 MenuManager helpMenu = new MenuManager("Help");
|
|
248 mm.add(helpMenu);
|
|
249
|
|
250 // Add the actions to the Help menu
|
|
251 helpMenu.add(aboutAction);
|
|
252
|
|
253 return mm;
|
|
254 }
|
|
255
|
|
256 /**
|
|
257 * Creates the toolbar for the application
|
|
258 */
|
|
259 protected ToolBarManager createToolBarManager(int style) {
|
|
260 // Create the toolbar manager
|
|
261 ToolBarManager tbm = new ToolBarManager(style);
|
|
262
|
|
263 // Add the file actions
|
|
264 tbm.add(newAction);
|
|
265 tbm.add(openAction);
|
|
266 tbm.add(saveAction);
|
|
267 tbm.add(saveAsAction);
|
|
268
|
|
269 // Add a separator
|
|
270 tbm.add(new Separator());
|
|
271
|
|
272 // Add the book actions
|
|
273 tbm.add(addBookAction);
|
|
274 tbm.add(removeBookAction);
|
|
275
|
|
276 // Add a separator
|
|
277 tbm.add(new Separator());
|
|
278
|
|
279 // Add the show book count, which will appear as a toggle button
|
|
280 tbm.add(showBookCountAction);
|
|
281
|
|
282 // Add a separator
|
|
283 tbm.add(new Separator());
|
|
284
|
|
285 // Add the about action
|
|
286 tbm.add(aboutAction);
|
|
287
|
|
288 return tbm;
|
|
289 }
|
|
290 /+
|
|
291 /**
|
|
292 * Creates the coolbar for the application
|
|
293 */
|
|
294 protected CoolBarManager createCoolBarManager(int style) {
|
|
295 // Create the coolbar manager
|
|
296 CoolBarManager cbm = new CoolBarManager(style);
|
|
297
|
|
298 // Add the toolbar
|
|
299 cbm.add(createToolBarManager(DWT.FLAT));
|
|
300
|
|
301 return cbm;
|
|
302 }
|
|
303 +/
|
|
304 /**
|
|
305 * Creates the status line manager
|
|
306 */
|
|
307 protected StatusLineManager createStatusLineManager() {
|
|
308 return new StatusLineManager();
|
|
309 }
|
|
310
|
|
311 /**
|
|
312 * Adds a book
|
|
313 */
|
|
314 public void addBook() {
|
|
315 library.add(new Book("[Enter Title]"));
|
|
316 refreshView();
|
|
317 }
|
|
318
|
|
319 /**
|
|
320 * Removes the selected book
|
|
321 */
|
|
322 public void removeSelectedBook() {
|
|
323 Book book = cast(Book) (cast(IStructuredSelection) viewer.getSelection())
|
|
324 .getFirstElement();
|
|
325 if (book !is null) library.remove(book);
|
|
326 refreshView();
|
|
327 }
|
|
328
|
|
329 /**
|
|
330 * Opens a file
|
|
331 *
|
|
332 * @param fileName the file name
|
|
333 */
|
|
334 public void openFile(String fileName) {
|
|
335 if (checkOverwrite()) {
|
|
336 // Disable the actions, so user can't change library while loading
|
|
337 enableActions(false);
|
|
338
|
|
339 library = new Library();
|
|
340 try {
|
|
341 // Launch the Open runnable
|
|
342 ModalContext.run(new class(fileName) IRunnableWithProgress {
|
|
343 String fileName_;
|
|
344 this(String a){
|
|
345 fileName_=a;
|
|
346 }
|
|
347 public void run(IProgressMonitor progressMonitor) {
|
|
348 try {
|
|
349 progressMonitor.beginTask("Loading", IProgressMonitor.UNKNOWN);
|
|
350 library.load(fileName_);
|
|
351 progressMonitor.done();
|
|
352 viewer.setInput(library);
|
|
353 refreshView();
|
|
354 } catch (IOException e) {
|
|
355 showError( Format("Can't load file {}\r{}", fileName_, e.msg));
|
|
356 }
|
|
357 }
|
|
358 }, true, getStatusLineManager().getProgressMonitor(), getShell()
|
|
359 .getDisplay());
|
|
360 } catch (InterruptedException e) {
|
|
361 } catch (InvocationTargetException e) {
|
|
362 } finally {
|
|
363 // Enable actions
|
|
364 enableActions(true);
|
|
365 }
|
|
366 }
|
|
367 }
|
|
368
|
|
369 /**
|
|
370 * Creates a new file
|
|
371 */
|
|
372 public void newFile() {
|
|
373 if (checkOverwrite()) {
|
|
374 library = new Library();
|
|
375 viewer.setInput(library);
|
|
376 }
|
|
377 }
|
|
378
|
|
379 /**
|
|
380 * Saves the current file
|
|
381 */
|
|
382 public void saveFile() {
|
|
383 String fileName = library.getFileName();
|
|
384 if (fileName is null) {
|
|
385 fileName = (new SafeSaveDialog(getShell())).open();
|
|
386 }
|
|
387 saveFileAs(fileName);
|
|
388 }
|
|
389
|
|
390 /**
|
|
391 * Saves the current file using the specified file name
|
|
392 *
|
|
393 * @param fileName the file name
|
|
394 */
|
|
395 public void saveFileAs(String fileName) {
|
|
396 // Disable the actions, so user can't change file while it's saving
|
|
397 enableActions(false);
|
|
398 Trace.formatln( "trc {}",__LINE__ );
|
|
399 try {
|
|
400 // Launch the Save runnable
|
|
401 ModalContext.run(new class(fileName) IRunnableWithProgress {
|
|
402 String filename_;
|
|
403 this(String a){
|
|
404 Trace.formatln( "trc {}",__LINE__ );
|
|
405 filename_=a;
|
|
406 }
|
|
407 public void run(IProgressMonitor progressMonitor) {
|
|
408 Trace.formatln( "trc {}",__LINE__ );
|
|
409 try {
|
|
410 progressMonitor.beginTask("Saving", IProgressMonitor.UNKNOWN);
|
|
411 library.save(filename_);
|
|
412 progressMonitor.done();
|
|
413 } catch (IOException e) {
|
|
414 Trace.formatln( "trc {}",__LINE__ );
|
|
415 showError(Format("Can't save file {}\r{}", library.getFileName(), e.msg ));
|
|
416 }
|
|
417 Trace.formatln( "trc {}",__LINE__ );
|
|
418 }
|
|
419 }, true, getStatusLineManager().getProgressMonitor(), getShell()
|
|
420 .getDisplay());
|
|
421 Trace.formatln( "trc {}",__LINE__ );
|
|
422 } catch (InterruptedException e) {
|
|
423 } catch (InvocationTargetException e) {
|
|
424 } finally {
|
|
425 Trace.formatln( "trc {}",__LINE__ );
|
|
426 // Enable the actions
|
|
427 enableActions(true);
|
|
428 }
|
|
429 Trace.formatln( "trc {}",__LINE__ );
|
|
430 }
|
|
431
|
|
432 /**
|
|
433 * Shows an error
|
|
434 *
|
|
435 * @param msg the error
|
|
436 */
|
|
437 public void showError(String msg) {
|
|
438 MessageDialog.openError(getShell(), "Error", msg);
|
|
439 }
|
|
440
|
|
441 /**
|
|
442 * Refreshes the view
|
|
443 */
|
|
444 public void refreshView() {
|
|
445 // Refresh the view
|
|
446 viewer.refresh();
|
|
447
|
|
448 // Repack the columns
|
|
449 for (int i = 0, n = viewer.getTable().getColumnCount(); i < n; i++) {
|
|
450 viewer.getTable().getColumn(i).pack();
|
|
451 }
|
|
452
|
|
453 getStatusLineManager().setMessage(
|
|
454 showBookCountAction.isChecked() ? Format( "Book Count: {}",
|
|
455 library.getBooks().size()) : "");
|
|
456 }
|
|
457
|
|
458 /**
|
|
459 * Checks the current file for unsaved changes. If it has unsaved changes,
|
|
460 * confirms that user wants to overwrite
|
|
461 *
|
|
462 * @return bool
|
|
463 */
|
|
464 public bool checkOverwrite() {
|
|
465 bool proceed = true;
|
|
466 if (library.isDirty()) {
|
|
467 proceed = MessageDialog.openConfirm(getShell(), "Are you sure?",
|
|
468 "You have unsaved changes--are you sure you want to lose them?");
|
|
469 }
|
|
470 return proceed;
|
|
471 }
|
|
472
|
|
473 /**
|
|
474 * Sets the current library dirty
|
|
475 */
|
|
476 public void setLibraryDirty() {
|
|
477 library.setDirty();
|
|
478 }
|
|
479
|
|
480 /**
|
|
481 * Closes the application
|
|
482 */
|
|
483 public bool close() {
|
|
484 if (checkOverwrite()) return super.close();
|
|
485 return false;
|
|
486 }
|
|
487
|
|
488 /**
|
|
489 * Enables or disables the actions
|
|
490 *
|
|
491 * @param enable true to enable, false to disable
|
|
492 */
|
|
493 private void enableActions(bool enable) {
|
|
494 newAction.setEnabled(enable);
|
|
495 openAction.setEnabled(enable);
|
|
496 saveAction.setEnabled(enable);
|
|
497 saveAsAction.setEnabled(enable);
|
|
498 exitAction.setEnabled(enable);
|
|
499 addBookAction.setEnabled(enable);
|
|
500 removeBookAction.setEnabled(enable);
|
|
501 aboutAction.setEnabled(enable);
|
|
502 showBookCountAction.setEnabled(enable);
|
|
503 }
|
|
504 }
|
|
505
|
|
506
|
|
507 /**
|
|
508 * This action class responds to requests to save a file
|
|
509 */
|
|
510 public class SaveAction : Action {
|
|
511 /**
|
|
512 * SaveAction constructor
|
|
513 */
|
|
514 public this() {
|
|
515 super("&Save@Ctrl+S", ImageDescriptor.createFromFile( getImportData!("jface.librarian.save.gif")));
|
|
516 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledSave.gif")));
|
|
517 setToolTipText("Save");
|
|
518 }
|
|
519
|
|
520 /**
|
|
521 * Saves the file
|
|
522 */
|
|
523 public void run() {
|
|
524 Librarian.getApp().saveFile();
|
|
525 }
|
|
526 }
|
|
527
|
|
528
|
|
529
|
|
530
|
|
531 /**
|
|
532 * This action class responds to requests open a file
|
|
533 */
|
|
534 public class OpenAction : Action {
|
|
535 /**
|
|
536 * OpenAction constructor
|
|
537 */
|
|
538 public this() {
|
|
539 super("&Open...@Ctrl+O", ImageDescriptor.createFromFile(getImportData!("jface.librarian.open.gif")));
|
|
540 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledOpen.gif")));
|
|
541 setToolTipText("Open");
|
|
542 }
|
|
543
|
|
544 /**
|
|
545 * Opens an existing file
|
|
546 */
|
|
547 public void run() {
|
|
548 // Use the file dialog
|
|
549 FileDialog dlg = new FileDialog(Librarian.getApp().getShell(), DWT.OPEN);
|
|
550 String fileName = dlg.open();
|
|
551 if (fileName !is null) {
|
|
552 Librarian.getApp().openFile(fileName);
|
|
553 }
|
|
554 }
|
|
555 }
|
|
556
|
|
557 /**
|
|
558 * This action class reponds to requests for a new file
|
|
559 */
|
|
560 public class NewAction : Action {
|
|
561 /**
|
|
562 * NewAction constructor
|
|
563 */
|
|
564 public this() {
|
|
565 super("&New@Ctrl+N", ImageDescriptor.createFromFile(getImportData!("jface.librarian.new.gif")));
|
|
566 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledNew.gif")));
|
|
567 setToolTipText("New");
|
|
568 }
|
|
569
|
|
570 /**
|
|
571 * Creates a new file
|
|
572 */
|
|
573 public void run() {
|
|
574 Librarian.getApp().newFile();
|
|
575 }
|
|
576 }
|
|
577
|
|
578 /**
|
|
579 * This action class responds to requests to save a file as . . .
|
|
580 */
|
|
581 public class SaveAsAction : Action {
|
|
582 /**
|
|
583 * SaveAsAction constructor
|
|
584 */
|
|
585 public this() {
|
|
586 super("Save As...", ImageDescriptor.createFromFile(getImportData!("jface.librarian.saveAs.gif")));
|
|
587 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledSaveAs.gif")));
|
|
588 setToolTipText("Save As");
|
|
589 }
|
|
590
|
|
591 /**
|
|
592 * Saves the file
|
|
593 */
|
|
594 public void run() {
|
|
595 SafeSaveDialog dlg = new SafeSaveDialog(Librarian.getApp().getShell());
|
|
596 String fileName = dlg.open();
|
|
597 if (fileName !is null) {
|
|
598 Librarian.getApp().saveFileAs(fileName);
|
|
599 }
|
|
600 }
|
|
601 }
|
|
602
|
|
603
|
|
604 /**
|
|
605 * This action class deletes a book
|
|
606 */
|
|
607 public class RemoveBookAction : Action {
|
|
608 /**
|
|
609 * RemoveBookAction constructor
|
|
610 */
|
|
611 public this() {
|
|
612 super("&Remove Book@Ctrl+X", ImageDescriptor.createFromFile(getImportData!("jface.librarian.removeBook.gif")));
|
|
613 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledRemoveBook.gif")));
|
|
614 setToolTipText("Remove");
|
|
615 }
|
|
616
|
|
617 /**
|
|
618 * Removes the selected book after confirming
|
|
619 */
|
|
620 public void run() {
|
|
621 if (MessageDialog.openConfirm(Librarian.getApp().getShell(), "Are you sure?",
|
|
622 "Are you sure you want to remove the selected book?")) {
|
|
623 Librarian.getApp().removeSelectedBook();
|
|
624 }
|
|
625 }
|
|
626 }
|
|
627
|
|
628
|
|
629 /**
|
|
630 * This action class adds a book
|
|
631 */
|
|
632 public class AddBookAction : Action {
|
|
633 /**
|
|
634 * AddBookAction constructor
|
|
635 */
|
|
636 public this() {
|
|
637 super("&Add Book@Ctrl+B", ImageDescriptor.createFromFile(getImportData!("jface.librarian.addBook.gif")));
|
|
638 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledAddBook.gif")));
|
|
639 setToolTipText("Add");
|
|
640 }
|
|
641
|
|
642 /**
|
|
643 * Adds a book to the current library
|
|
644 */
|
|
645 public void run() {
|
|
646 Librarian.getApp().addBook();
|
|
647 }
|
|
648 }
|
|
649
|
|
650 /**
|
|
651 * This action class exits the application
|
|
652 */
|
|
653 public class ExitAction : Action {
|
|
654 /**
|
|
655 * ExitAction constructor
|
|
656 */
|
|
657 public this() {
|
|
658 super("E&xit@Alt+F4");
|
|
659 setToolTipText("Exit");
|
|
660 }
|
|
661
|
|
662 /**
|
|
663 * Exits the application
|
|
664 */
|
|
665 public void run() {
|
|
666 Librarian.getApp().close();
|
|
667 }
|
|
668 }
|
|
669
|
|
670 /**
|
|
671 * This action class shows an About box
|
|
672 */
|
|
673 public class AboutAction : Action {
|
|
674 /**
|
|
675 * AboutAction constructor
|
|
676 */
|
|
677 public this() {
|
|
678 super("&About@Ctrl+A", ImageDescriptor.createFromFile(getImportData!("jface.librarian.about.gif")));
|
|
679 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledAbout.gif")));
|
|
680 setToolTipText("About");
|
|
681 }
|
|
682
|
|
683 /**
|
|
684 * Shows an about box
|
|
685 */
|
|
686 public void run() {
|
|
687 MessageDialog.openInformation(Librarian.getApp().getShell(), "About",
|
|
688 "Librarian--to manage your books");
|
|
689 }
|
|
690 }
|
|
691
|
|
692
|
|
693
|
|
694 /**
|
|
695 * This action class determines whether to show the book count
|
|
696 */
|
|
697 public class ShowBookCountAction : Action {
|
|
698 public this() {
|
|
699 super("&Show Book Count@Ctrl+C", IAction.AS_CHECK_BOX);
|
|
700 setChecked(true);
|
|
701 setImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.count.gif")));
|
|
702 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledCount.gif")));
|
|
703 }
|
|
704 }
|
|
705
|
|
706 /**
|
|
707 * This class represents a book
|
|
708 */
|
|
709 public class Book {
|
|
710 private String title;
|
|
711 private String checkedOutTo;
|
|
712
|
|
713 /**
|
|
714 * Book constructor
|
|
715 * @param title the title
|
|
716 */
|
|
717 public this(String title) {
|
|
718 setTitle(title);
|
|
719 }
|
|
720
|
|
721 /**
|
|
722 * Sets the title
|
|
723 * @param title the title
|
|
724 */
|
|
725 public void setTitle(String title) {
|
|
726 this.title = title;
|
|
727 }
|
|
728
|
|
729 /**
|
|
730 * Gets the title
|
|
731 * @return String
|
|
732 */
|
|
733 public String getTitle() {
|
|
734 return title;
|
|
735 }
|
|
736
|
|
737 /**
|
|
738 * Check out
|
|
739 * @param who the person checking this book out
|
|
740 */
|
|
741 public void checkOut(String who) {
|
|
742 checkedOutTo = who;
|
|
743 if (checkedOutTo.length is 0) checkedOutTo = null;
|
|
744 }
|
|
745
|
|
746 public bool isCheckedOut() {
|
|
747 return checkedOutTo !is null && checkedOutTo.length > 0;
|
|
748 }
|
|
749
|
|
750 public void checkIn() {
|
|
751 checkedOutTo = null;
|
|
752 }
|
|
753
|
|
754 /**
|
|
755 * Gets who this book is checked out to
|
|
756 * @return String
|
|
757 */
|
|
758 public String getCheckedOutTo() {
|
|
759 return checkedOutTo;
|
|
760 }
|
|
761 }
|
|
762
|
|
763
|
|
764 /**
|
|
765 * This class provides a facade for the "save"
|
|
766 * FileDialog class. If the selected file already
|
|
767 * exists, the user is asked to confirm before
|
|
768 * overwriting.
|
|
769 */
|
|
770 public class SafeSaveDialog
|
|
771 {
|
|
772 // The wrapped FileDialog
|
|
773 private FileDialog dlg;
|
|
774
|
|
775 /**
|
|
776 * SafeSaveDialog constructor
|
|
777 * @param shell the parent shell
|
|
778 */
|
|
779 public this(Shell shell)
|
|
780 {
|
|
781 dlg = new FileDialog(shell, DWT.SAVE);
|
|
782 }
|
|
783
|
|
784 public String open()
|
|
785 {
|
|
786 // We store the selected file name in fileName
|
|
787 String fileName = null;
|
|
788
|
|
789 // The user has finished when one of the
|
|
790 // following happens:
|
|
791 // 1) The user dismisses the dialog by pressing Cancel
|
|
792 // 2) The selected file name does not exist
|
|
793 // 3) The user agrees to overwrite existing file
|
|
794 bool done = false;
|
|
795
|
|
796 while (!done)
|
|
797 {
|
|
798 // Open the File Dialog
|
|
799 fileName = dlg.open();
|
|
800 if (fileName is null)
|
|
801 {
|
|
802 // User has cancelled, so quit and return
|
|
803 done = true;
|
|
804 }
|
|
805 else
|
|
806 {
|
|
807 // User has selected a file; see if it already exists
|
|
808 FilePath file = new FilePath(fileName);
|
|
809 if (file.exists())
|
|
810 {
|
|
811 // The file already exists; asks for confirmation
|
|
812 MessageBox mb = new MessageBox(dlg.getParent(),
|
|
813 DWT.ICON_WARNING | DWT.YES | DWT.NO);
|
|
814
|
|
815 // We really should read this string from a
|
|
816 // resource bundle
|
|
817 mb.setMessage(fileName ~ " already exists. Do you want to replace it?");
|
|
818
|
|
819 // If they click Yes, we're done and we drop out. If
|
|
820 // they click No, we redisplay the File Dialog
|
|
821 done = mb.open() is DWT.YES;
|
|
822 }
|
|
823 else
|
|
824 {
|
|
825 // File does not exist, so drop out
|
|
826 done = true;
|
|
827 }
|
|
828 }
|
|
829 }
|
|
830 return fileName;
|
|
831 }
|
|
832
|
|
833 public String getFileName()
|
|
834 {
|
|
835 return dlg.getFileName();
|
|
836 }
|
|
837
|
|
838 public String[] getFileNames()
|
|
839 {
|
|
840 return dlg.getFileNames();
|
|
841 }
|
|
842
|
|
843 public String[] getFilterExtensions()
|
|
844 {
|
|
845 return dlg.getFilterExtensions();
|
|
846 }
|
|
847
|
|
848 public String[] getFilterNames()
|
|
849 {
|
|
850 return dlg.getFilterNames();
|
|
851 }
|
|
852
|
|
853 public String getFilterPath()
|
|
854 {
|
|
855 return dlg.getFilterPath();
|
|
856 }
|
|
857
|
|
858 public void setFileName(String string)
|
|
859 {
|
|
860 dlg.setFileName(string);
|
|
861 }
|
|
862
|
|
863 public void setFilterExtensions(String[] extensions)
|
|
864 {
|
|
865 dlg.setFilterExtensions(extensions);
|
|
866 }
|
|
867
|
|
868 public void setFilterNames(String[] names)
|
|
869 {
|
|
870 dlg.setFilterNames(names);
|
|
871 }
|
|
872
|
|
873 public void setFilterPath(String string)
|
|
874 {
|
|
875 dlg.setFilterPath(string);
|
|
876 }
|
|
877
|
|
878 public Shell getParent()
|
|
879 {
|
|
880 return dlg.getParent();
|
|
881 }
|
|
882
|
|
883 public int getStyle()
|
|
884 {
|
|
885 return dlg.getStyle();
|
|
886 }
|
|
887
|
|
888 public String getText()
|
|
889 {
|
|
890 return dlg.getText();
|
|
891 }
|
|
892
|
|
893 public void setText(String string)
|
|
894 {
|
|
895 dlg.setText(string);
|
|
896 }
|
|
897 }
|
|
898
|
|
899
|
|
900 /**
|
|
901 * This class is the cell modifier for the Librarian program
|
|
902 */
|
|
903 public class LibraryCellModifier : ICellModifier {
|
|
904 /**
|
|
905 * Gets whether the specified property can be modified
|
|
906 *
|
|
907 * @param element the book
|
|
908 * @param property the property
|
|
909 * @return bool
|
|
910 */
|
|
911 public bool canModify(Object element, String property) {
|
|
912 return true;
|
|
913 }
|
|
914
|
|
915 /**
|
|
916 * Gets the value for the property
|
|
917 *
|
|
918 * @param element the book
|
|
919 * @param property the property
|
|
920 * @return Object
|
|
921 */
|
|
922 public Object getValue(Object element, String property) {
|
|
923 Book book = cast(Book) element;
|
|
924 if (Librarian.TITLE.equals(property))
|
|
925 return stringcast(book.getTitle());
|
|
926 else if (Librarian.CHECKED_OUT.equals(property))
|
|
927 return Boolean.valueOf(book.isCheckedOut());
|
|
928 else if (Librarian.WHO.equals(property))
|
|
929 return stringcast( book.getCheckedOutTo() is null ? "" : book.getCheckedOutTo());
|
|
930 else
|
|
931 return null;
|
|
932 }
|
|
933
|
|
934 /**
|
|
935 * Modifies the element
|
|
936 *
|
|
937 * @param element the book
|
|
938 * @param property the property
|
|
939 * @param value the new value
|
|
940 */
|
|
941 public void modify(Object element, String property, Object value) {
|
|
942 if ( auto item = cast(Item)element ){
|
|
943 element = item.getData();
|
|
944 }
|
|
945
|
|
946 Book book = cast(Book) element;
|
|
947 if (Librarian.TITLE.equals(property))
|
|
948 book.setTitle(stringcast(value));
|
|
949 else if (Librarian.CHECKED_OUT.equals(property)) {
|
|
950 bool b = (cast(Boolean) value).booleanValue();
|
|
951 if (b)
|
|
952 book.checkOut("[Enter Name]");
|
|
953 else
|
|
954 book.checkIn();
|
|
955 } else if (Librarian.WHO.equals(property))
|
|
956 book.checkOut(stringcast(value));
|
|
957
|
|
958 // Refresh the view
|
|
959 Librarian.getApp().refreshView();
|
|
960
|
|
961 // Set the library dirty
|
|
962 Librarian.getApp().setLibraryDirty();
|
|
963 }
|
|
964 }
|
|
965
|
|
966 /**
|
|
967 * This class holds all the books in a library. It also handles loading from and
|
|
968 * saving to disk
|
|
969 */
|
|
970 public class Library {
|
|
971 private static const String SEP = "|";
|
|
972
|
|
973 // The filename
|
|
974 private String filename;
|
|
975
|
|
976 // The books
|
|
977 private LinkSeq!(Book) books;
|
|
978
|
|
979 // The dirty flag
|
|
980 private bool dirty;
|
|
981
|
|
982 /**
|
|
983 * Library constructor Note the signature :-)
|
|
984 */
|
|
985 public this() {
|
|
986 books = new LinkSeq!(Book);
|
|
987 }
|
|
988
|
|
989 /**
|
|
990 * Loads the library from a file
|
|
991 *
|
|
992 * @param filename the filename
|
|
993 * @throws IOException
|
|
994 */
|
|
995 public void load(String filename) {
|
|
996 auto istr = (new FileInput(filename)).input;
|
|
997 auto lines = new LineIterator!(char)(istr);
|
|
998 String line;
|
|
999 foreach (line; lines ) {
|
|
1000 auto tokens = tango.text.Util.delimit(line, SEP);
|
|
1001 Book book = null;
|
|
1002 if ( tokens.length > 0 ) book = new Book( tokens[0].dup );
|
|
1003 if (tokens.length > 1) book.checkOut(tokens[1].dup );
|
|
1004 if (book !is null) add(book);
|
|
1005 }
|
|
1006 istr.close();
|
|
1007 this.filename = filename;
|
|
1008 dirty = false;
|
|
1009 }
|
|
1010
|
|
1011 /**
|
|
1012 * Saves the library to a file
|
|
1013 *
|
|
1014 * @param filename the filename
|
|
1015 * @throws IOException
|
|
1016 */
|
|
1017 public void save(String filename) {
|
|
1018 scope ostr = (new FileOutput(filename)).output;
|
|
1019 scope printer = new Print!(char)( Format, ostr );
|
|
1020 foreach ( book; books ) {
|
|
1021 printer.formatln( "{}|{}",book.getTitle(), (book.getCheckedOutTo() is null ? "" : book.getCheckedOutTo()));
|
|
1022 }
|
|
1023 ostr.close();
|
|
1024 this.filename = filename;
|
|
1025 dirty = false;
|
|
1026 }
|
|
1027
|
|
1028 /**
|
|
1029 * Adds a book
|
|
1030 *
|
|
1031 * @param book the book to add
|
|
1032 * @return bool
|
|
1033 */
|
|
1034 public bool add(Book book) {
|
|
1035 books.append(book);
|
|
1036 setDirty();
|
|
1037 return true;
|
|
1038 }
|
|
1039
|
|
1040 /**
|
|
1041 * Removes a book
|
|
1042 *
|
|
1043 * @param book the book to remove
|
|
1044 */
|
|
1045 public void remove(Book book) {
|
|
1046 books.remove(book);
|
|
1047 setDirty();
|
|
1048 }
|
|
1049
|
|
1050 /**
|
|
1051 * Gets the books
|
|
1052 *
|
|
1053 * @return Collection
|
|
1054 */
|
|
1055 public LinkSeq!(Book) getBooks() {
|
|
1056 return books;
|
|
1057 }
|
|
1058
|
|
1059 /**
|
|
1060 * Gets the file name
|
|
1061 *
|
|
1062 * @return String
|
|
1063 */
|
|
1064 public String getFileName() {
|
|
1065 return filename;
|
|
1066 }
|
|
1067
|
|
1068 /**
|
|
1069 * Gets whether this file is dirty
|
|
1070 *
|
|
1071 * @return bool
|
|
1072 */
|
|
1073 public bool isDirty() {
|
|
1074 return dirty;
|
|
1075 }
|
|
1076
|
|
1077 /**
|
|
1078 * Sets this file as dirty
|
|
1079 */
|
|
1080 public void setDirty() {
|
|
1081 dirty = true;
|
|
1082 }
|
|
1083 }
|
|
1084
|
|
1085
|
|
1086 /**
|
|
1087 * This class provides the content for the library table
|
|
1088 */
|
|
1089 public class LibraryContentProvider : IStructuredContentProvider {
|
|
1090 /**
|
|
1091 * Gets the books
|
|
1092 *
|
|
1093 * @param inputElement the library
|
|
1094 * @return Object[]
|
|
1095 */
|
|
1096 public Object[] getElements(Object inputElement) {
|
|
1097 return (cast(Library) inputElement).getBooks().toArray();
|
|
1098 }
|
|
1099
|
|
1100 /**
|
|
1101 * Disposes any resources
|
|
1102 */
|
|
1103 public void dispose() {
|
|
1104 // Do nothing
|
|
1105 }
|
|
1106
|
|
1107 /**
|
|
1108 * Called when the input changes
|
|
1109 *
|
|
1110 * @param viewer the viewer
|
|
1111 * @param oldInput the old library
|
|
1112 * @param newInput the new library
|
|
1113 */
|
|
1114 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
|
|
1115 // Ignore
|
|
1116 }
|
|
1117 }
|
|
1118
|
|
1119
|
|
1120 /**
|
|
1121 * This class provides the labels for the library table
|
|
1122 */
|
|
1123 public class LibraryLabelProvider : ITableLabelProvider {
|
|
1124 private Image checked;
|
|
1125 private Image unchecked;
|
|
1126
|
|
1127 /**
|
|
1128 * LibraryLabelProvider constructor
|
|
1129 */
|
|
1130 public this() {
|
|
1131 // Create the check mark images
|
|
1132 checked = new Image(null, new ImageData( new ByteArrayInputStream( cast(byte[])import("jface.librarian.checked.gif"))));
|
|
1133 unchecked = new Image(null,new ImageData( new ByteArrayInputStream( cast(byte[])import("jface.librarian.unchecked.gif"))));
|
|
1134 }
|
|
1135
|
|
1136 /**
|
|
1137 * Gets the column image
|
|
1138 *
|
|
1139 * @param element the book
|
|
1140 * @param columnIndex the column index
|
|
1141 * @return Image
|
|
1142 */
|
|
1143 public Image getColumnImage(Object element, int columnIndex) {
|
|
1144 // For the "Checked Out" column, return the check mark
|
|
1145 // if the book is checked out
|
|
1146 if (columnIndex is 1)
|
|
1147 return (cast(Book) element).isCheckedOut() ? checked : unchecked;
|
|
1148 return null;
|
|
1149 }
|
|
1150
|
|
1151 /**
|
|
1152 * Gets the column text
|
|
1153 *
|
|
1154 * @param element the book
|
|
1155 * @param columnIndex the column index
|
|
1156 * @return String
|
|
1157 */
|
|
1158 public String getColumnText(Object element, int columnIndex) {
|
|
1159 Book book = cast(Book) element;
|
|
1160 String text = null;
|
|
1161 switch (columnIndex) {
|
|
1162 case 0:
|
|
1163 text = book.getTitle();
|
|
1164 break;
|
|
1165 case 2:
|
|
1166 text = book.getCheckedOutTo();
|
|
1167 break;
|
|
1168 default:
|
|
1169 }
|
|
1170 return text is null ? "" : text;
|
|
1171 }
|
|
1172
|
|
1173 /**
|
|
1174 * Adds a listener
|
|
1175 */
|
|
1176 public void addListener(ILabelProviderListener listener) {
|
|
1177 // Ignore
|
|
1178 }
|
|
1179
|
|
1180 /**
|
|
1181 * Disposes any resources
|
|
1182 */
|
|
1183 public void dispose() {
|
|
1184 if (checked !is null) checked.dispose();
|
|
1185 if (unchecked !is null) unchecked.dispose();
|
|
1186 }
|
|
1187
|
|
1188 /**
|
|
1189 * Gets whether this is a label property
|
|
1190 *
|
|
1191 * @param element the book
|
|
1192 * @param property the property
|
|
1193 * @return bool
|
|
1194 */
|
|
1195 public bool isLabelProperty(Object element, String property) {
|
|
1196 return false;
|
|
1197 }
|
|
1198
|
|
1199 /**
|
|
1200 * Removes a listener
|
|
1201 *
|
|
1202 * @param listener the listener
|
|
1203 */
|
|
1204 public void removeListener(ILabelProviderListener listener) {
|
|
1205 // Ignore
|
|
1206 }
|
|
1207 }
|
|
1208
|
|
1209
|
|
1210
|
|
1211
|
|
1212
|
|
1213
|