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 try {
|
71
|
399 auto pm = getStatusLineManager().getProgressMonitor();
|
|
400 auto disp = getShell().getDisplay();
|
|
401
|
66
|
402 // Launch the Save runnable
|
|
403 ModalContext.run(new class(fileName) IRunnableWithProgress {
|
|
404 String filename_;
|
|
405 this(String a){
|
|
406 filename_=a;
|
|
407 }
|
|
408 public void run(IProgressMonitor progressMonitor) {
|
|
409 try {
|
71
|
410 progressMonitor.beginTask("Saving", -1/+IProgressMonitor.UNKNOWN+/);
|
66
|
411 library.save(filename_);
|
|
412 progressMonitor.done();
|
|
413 } catch (IOException e) {
|
|
414 showError(Format("Can't save file {}\r{}", library.getFileName(), e.msg ));
|
|
415 }
|
|
416 }
|
71
|
417 }, true, pm, disp );
|
66
|
418 } catch (InterruptedException e) {
|
|
419 } catch (InvocationTargetException e) {
|
|
420 } finally {
|
|
421 // Enable the actions
|
|
422 enableActions(true);
|
|
423 }
|
|
424 }
|
|
425
|
|
426 /**
|
|
427 * Shows an error
|
|
428 *
|
|
429 * @param msg the error
|
|
430 */
|
|
431 public void showError(String msg) {
|
|
432 MessageDialog.openError(getShell(), "Error", msg);
|
|
433 }
|
|
434
|
|
435 /**
|
|
436 * Refreshes the view
|
|
437 */
|
|
438 public void refreshView() {
|
|
439 // Refresh the view
|
|
440 viewer.refresh();
|
|
441
|
|
442 // Repack the columns
|
|
443 for (int i = 0, n = viewer.getTable().getColumnCount(); i < n; i++) {
|
|
444 viewer.getTable().getColumn(i).pack();
|
|
445 }
|
|
446
|
|
447 getStatusLineManager().setMessage(
|
|
448 showBookCountAction.isChecked() ? Format( "Book Count: {}",
|
|
449 library.getBooks().size()) : "");
|
|
450 }
|
|
451
|
|
452 /**
|
|
453 * Checks the current file for unsaved changes. If it has unsaved changes,
|
|
454 * confirms that user wants to overwrite
|
|
455 *
|
|
456 * @return bool
|
|
457 */
|
|
458 public bool checkOverwrite() {
|
|
459 bool proceed = true;
|
|
460 if (library.isDirty()) {
|
|
461 proceed = MessageDialog.openConfirm(getShell(), "Are you sure?",
|
|
462 "You have unsaved changes--are you sure you want to lose them?");
|
|
463 }
|
|
464 return proceed;
|
|
465 }
|
|
466
|
|
467 /**
|
|
468 * Sets the current library dirty
|
|
469 */
|
|
470 public void setLibraryDirty() {
|
|
471 library.setDirty();
|
|
472 }
|
|
473
|
|
474 /**
|
|
475 * Closes the application
|
|
476 */
|
|
477 public bool close() {
|
|
478 if (checkOverwrite()) return super.close();
|
|
479 return false;
|
|
480 }
|
|
481
|
|
482 /**
|
|
483 * Enables or disables the actions
|
|
484 *
|
|
485 * @param enable true to enable, false to disable
|
|
486 */
|
|
487 private void enableActions(bool enable) {
|
|
488 newAction.setEnabled(enable);
|
|
489 openAction.setEnabled(enable);
|
|
490 saveAction.setEnabled(enable);
|
|
491 saveAsAction.setEnabled(enable);
|
|
492 exitAction.setEnabled(enable);
|
|
493 addBookAction.setEnabled(enable);
|
|
494 removeBookAction.setEnabled(enable);
|
|
495 aboutAction.setEnabled(enable);
|
|
496 showBookCountAction.setEnabled(enable);
|
|
497 }
|
|
498 }
|
|
499
|
|
500
|
|
501 /**
|
|
502 * This action class responds to requests to save a file
|
|
503 */
|
|
504 public class SaveAction : Action {
|
|
505 /**
|
|
506 * SaveAction constructor
|
|
507 */
|
|
508 public this() {
|
|
509 super("&Save@Ctrl+S", ImageDescriptor.createFromFile( getImportData!("jface.librarian.save.gif")));
|
|
510 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledSave.gif")));
|
|
511 setToolTipText("Save");
|
|
512 }
|
|
513
|
|
514 /**
|
|
515 * Saves the file
|
|
516 */
|
|
517 public void run() {
|
|
518 Librarian.getApp().saveFile();
|
|
519 }
|
|
520 }
|
|
521
|
|
522
|
|
523
|
|
524
|
|
525 /**
|
|
526 * This action class responds to requests open a file
|
|
527 */
|
|
528 public class OpenAction : Action {
|
|
529 /**
|
|
530 * OpenAction constructor
|
|
531 */
|
|
532 public this() {
|
|
533 super("&Open...@Ctrl+O", ImageDescriptor.createFromFile(getImportData!("jface.librarian.open.gif")));
|
|
534 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledOpen.gif")));
|
|
535 setToolTipText("Open");
|
|
536 }
|
|
537
|
|
538 /**
|
|
539 * Opens an existing file
|
|
540 */
|
|
541 public void run() {
|
|
542 // Use the file dialog
|
|
543 FileDialog dlg = new FileDialog(Librarian.getApp().getShell(), DWT.OPEN);
|
|
544 String fileName = dlg.open();
|
|
545 if (fileName !is null) {
|
|
546 Librarian.getApp().openFile(fileName);
|
|
547 }
|
|
548 }
|
|
549 }
|
|
550
|
|
551 /**
|
|
552 * This action class reponds to requests for a new file
|
|
553 */
|
|
554 public class NewAction : Action {
|
|
555 /**
|
|
556 * NewAction constructor
|
|
557 */
|
|
558 public this() {
|
|
559 super("&New@Ctrl+N", ImageDescriptor.createFromFile(getImportData!("jface.librarian.new.gif")));
|
|
560 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledNew.gif")));
|
|
561 setToolTipText("New");
|
|
562 }
|
|
563
|
|
564 /**
|
|
565 * Creates a new file
|
|
566 */
|
|
567 public void run() {
|
|
568 Librarian.getApp().newFile();
|
|
569 }
|
|
570 }
|
|
571
|
|
572 /**
|
|
573 * This action class responds to requests to save a file as . . .
|
|
574 */
|
|
575 public class SaveAsAction : Action {
|
|
576 /**
|
|
577 * SaveAsAction constructor
|
|
578 */
|
|
579 public this() {
|
|
580 super("Save As...", ImageDescriptor.createFromFile(getImportData!("jface.librarian.saveAs.gif")));
|
|
581 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledSaveAs.gif")));
|
|
582 setToolTipText("Save As");
|
|
583 }
|
|
584
|
|
585 /**
|
|
586 * Saves the file
|
|
587 */
|
|
588 public void run() {
|
|
589 SafeSaveDialog dlg = new SafeSaveDialog(Librarian.getApp().getShell());
|
|
590 String fileName = dlg.open();
|
|
591 if (fileName !is null) {
|
|
592 Librarian.getApp().saveFileAs(fileName);
|
|
593 }
|
|
594 }
|
|
595 }
|
|
596
|
|
597
|
|
598 /**
|
|
599 * This action class deletes a book
|
|
600 */
|
|
601 public class RemoveBookAction : Action {
|
|
602 /**
|
|
603 * RemoveBookAction constructor
|
|
604 */
|
|
605 public this() {
|
|
606 super("&Remove Book@Ctrl+X", ImageDescriptor.createFromFile(getImportData!("jface.librarian.removeBook.gif")));
|
|
607 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledRemoveBook.gif")));
|
|
608 setToolTipText("Remove");
|
|
609 }
|
|
610
|
|
611 /**
|
|
612 * Removes the selected book after confirming
|
|
613 */
|
|
614 public void run() {
|
|
615 if (MessageDialog.openConfirm(Librarian.getApp().getShell(), "Are you sure?",
|
|
616 "Are you sure you want to remove the selected book?")) {
|
|
617 Librarian.getApp().removeSelectedBook();
|
|
618 }
|
|
619 }
|
|
620 }
|
|
621
|
|
622
|
|
623 /**
|
|
624 * This action class adds a book
|
|
625 */
|
|
626 public class AddBookAction : Action {
|
|
627 /**
|
|
628 * AddBookAction constructor
|
|
629 */
|
|
630 public this() {
|
|
631 super("&Add Book@Ctrl+B", ImageDescriptor.createFromFile(getImportData!("jface.librarian.addBook.gif")));
|
|
632 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledAddBook.gif")));
|
|
633 setToolTipText("Add");
|
|
634 }
|
|
635
|
|
636 /**
|
|
637 * Adds a book to the current library
|
|
638 */
|
|
639 public void run() {
|
|
640 Librarian.getApp().addBook();
|
|
641 }
|
|
642 }
|
|
643
|
|
644 /**
|
|
645 * This action class exits the application
|
|
646 */
|
|
647 public class ExitAction : Action {
|
|
648 /**
|
|
649 * ExitAction constructor
|
|
650 */
|
|
651 public this() {
|
|
652 super("E&xit@Alt+F4");
|
|
653 setToolTipText("Exit");
|
|
654 }
|
|
655
|
|
656 /**
|
|
657 * Exits the application
|
|
658 */
|
|
659 public void run() {
|
|
660 Librarian.getApp().close();
|
|
661 }
|
|
662 }
|
|
663
|
|
664 /**
|
|
665 * This action class shows an About box
|
|
666 */
|
|
667 public class AboutAction : Action {
|
|
668 /**
|
|
669 * AboutAction constructor
|
|
670 */
|
|
671 public this() {
|
|
672 super("&About@Ctrl+A", ImageDescriptor.createFromFile(getImportData!("jface.librarian.about.gif")));
|
|
673 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledAbout.gif")));
|
|
674 setToolTipText("About");
|
|
675 }
|
|
676
|
|
677 /**
|
|
678 * Shows an about box
|
|
679 */
|
|
680 public void run() {
|
|
681 MessageDialog.openInformation(Librarian.getApp().getShell(), "About",
|
|
682 "Librarian--to manage your books");
|
|
683 }
|
|
684 }
|
|
685
|
|
686
|
|
687
|
|
688 /**
|
|
689 * This action class determines whether to show the book count
|
|
690 */
|
|
691 public class ShowBookCountAction : Action {
|
|
692 public this() {
|
|
693 super("&Show Book Count@Ctrl+C", IAction.AS_CHECK_BOX);
|
|
694 setChecked(true);
|
|
695 setImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.count.gif")));
|
|
696 setDisabledImageDescriptor(ImageDescriptor.createFromFile(getImportData!("jface.librarian.disabledCount.gif")));
|
|
697 }
|
|
698 }
|
|
699
|
|
700 /**
|
|
701 * This class represents a book
|
|
702 */
|
|
703 public class Book {
|
|
704 private String title;
|
|
705 private String checkedOutTo;
|
|
706
|
|
707 /**
|
|
708 * Book constructor
|
|
709 * @param title the title
|
|
710 */
|
|
711 public this(String title) {
|
|
712 setTitle(title);
|
|
713 }
|
|
714
|
|
715 /**
|
|
716 * Sets the title
|
|
717 * @param title the title
|
|
718 */
|
|
719 public void setTitle(String title) {
|
|
720 this.title = title;
|
|
721 }
|
|
722
|
|
723 /**
|
|
724 * Gets the title
|
|
725 * @return String
|
|
726 */
|
|
727 public String getTitle() {
|
|
728 return title;
|
|
729 }
|
|
730
|
|
731 /**
|
|
732 * Check out
|
|
733 * @param who the person checking this book out
|
|
734 */
|
|
735 public void checkOut(String who) {
|
|
736 checkedOutTo = who;
|
|
737 if (checkedOutTo.length is 0) checkedOutTo = null;
|
|
738 }
|
|
739
|
|
740 public bool isCheckedOut() {
|
|
741 return checkedOutTo !is null && checkedOutTo.length > 0;
|
|
742 }
|
|
743
|
|
744 public void checkIn() {
|
|
745 checkedOutTo = null;
|
|
746 }
|
|
747
|
|
748 /**
|
|
749 * Gets who this book is checked out to
|
|
750 * @return String
|
|
751 */
|
|
752 public String getCheckedOutTo() {
|
|
753 return checkedOutTo;
|
|
754 }
|
|
755 }
|
|
756
|
|
757
|
|
758 /**
|
|
759 * This class provides a facade for the "save"
|
|
760 * FileDialog class. If the selected file already
|
|
761 * exists, the user is asked to confirm before
|
|
762 * overwriting.
|
|
763 */
|
|
764 public class SafeSaveDialog
|
|
765 {
|
|
766 // The wrapped FileDialog
|
|
767 private FileDialog dlg;
|
|
768
|
|
769 /**
|
|
770 * SafeSaveDialog constructor
|
|
771 * @param shell the parent shell
|
|
772 */
|
|
773 public this(Shell shell)
|
|
774 {
|
|
775 dlg = new FileDialog(shell, DWT.SAVE);
|
|
776 }
|
|
777
|
|
778 public String open()
|
|
779 {
|
|
780 // We store the selected file name in fileName
|
|
781 String fileName = null;
|
|
782
|
|
783 // The user has finished when one of the
|
|
784 // following happens:
|
|
785 // 1) The user dismisses the dialog by pressing Cancel
|
|
786 // 2) The selected file name does not exist
|
|
787 // 3) The user agrees to overwrite existing file
|
|
788 bool done = false;
|
|
789
|
|
790 while (!done)
|
|
791 {
|
|
792 // Open the File Dialog
|
|
793 fileName = dlg.open();
|
|
794 if (fileName is null)
|
|
795 {
|
|
796 // User has cancelled, so quit and return
|
|
797 done = true;
|
|
798 }
|
|
799 else
|
|
800 {
|
|
801 // User has selected a file; see if it already exists
|
|
802 FilePath file = new FilePath(fileName);
|
|
803 if (file.exists())
|
|
804 {
|
|
805 // The file already exists; asks for confirmation
|
|
806 MessageBox mb = new MessageBox(dlg.getParent(),
|
|
807 DWT.ICON_WARNING | DWT.YES | DWT.NO);
|
|
808
|
|
809 // We really should read this string from a
|
|
810 // resource bundle
|
|
811 mb.setMessage(fileName ~ " already exists. Do you want to replace it?");
|
|
812
|
|
813 // If they click Yes, we're done and we drop out. If
|
|
814 // they click No, we redisplay the File Dialog
|
|
815 done = mb.open() is DWT.YES;
|
|
816 }
|
|
817 else
|
|
818 {
|
|
819 // File does not exist, so drop out
|
|
820 done = true;
|
|
821 }
|
|
822 }
|
|
823 }
|
|
824 return fileName;
|
|
825 }
|
|
826
|
|
827 public String getFileName()
|
|
828 {
|
|
829 return dlg.getFileName();
|
|
830 }
|
|
831
|
|
832 public String[] getFileNames()
|
|
833 {
|
|
834 return dlg.getFileNames();
|
|
835 }
|
|
836
|
|
837 public String[] getFilterExtensions()
|
|
838 {
|
|
839 return dlg.getFilterExtensions();
|
|
840 }
|
|
841
|
|
842 public String[] getFilterNames()
|
|
843 {
|
|
844 return dlg.getFilterNames();
|
|
845 }
|
|
846
|
|
847 public String getFilterPath()
|
|
848 {
|
|
849 return dlg.getFilterPath();
|
|
850 }
|
|
851
|
|
852 public void setFileName(String string)
|
|
853 {
|
|
854 dlg.setFileName(string);
|
|
855 }
|
|
856
|
|
857 public void setFilterExtensions(String[] extensions)
|
|
858 {
|
|
859 dlg.setFilterExtensions(extensions);
|
|
860 }
|
|
861
|
|
862 public void setFilterNames(String[] names)
|
|
863 {
|
|
864 dlg.setFilterNames(names);
|
|
865 }
|
|
866
|
|
867 public void setFilterPath(String string)
|
|
868 {
|
|
869 dlg.setFilterPath(string);
|
|
870 }
|
|
871
|
|
872 public Shell getParent()
|
|
873 {
|
|
874 return dlg.getParent();
|
|
875 }
|
|
876
|
|
877 public int getStyle()
|
|
878 {
|
|
879 return dlg.getStyle();
|
|
880 }
|
|
881
|
|
882 public String getText()
|
|
883 {
|
|
884 return dlg.getText();
|
|
885 }
|
|
886
|
|
887 public void setText(String string)
|
|
888 {
|
|
889 dlg.setText(string);
|
|
890 }
|
|
891 }
|
|
892
|
|
893
|
|
894 /**
|
|
895 * This class is the cell modifier for the Librarian program
|
|
896 */
|
|
897 public class LibraryCellModifier : ICellModifier {
|
|
898 /**
|
|
899 * Gets whether the specified property can be modified
|
|
900 *
|
|
901 * @param element the book
|
|
902 * @param property the property
|
|
903 * @return bool
|
|
904 */
|
|
905 public bool canModify(Object element, String property) {
|
|
906 return true;
|
|
907 }
|
|
908
|
|
909 /**
|
|
910 * Gets the value for the property
|
|
911 *
|
|
912 * @param element the book
|
|
913 * @param property the property
|
|
914 * @return Object
|
|
915 */
|
|
916 public Object getValue(Object element, String property) {
|
|
917 Book book = cast(Book) element;
|
|
918 if (Librarian.TITLE.equals(property))
|
|
919 return stringcast(book.getTitle());
|
|
920 else if (Librarian.CHECKED_OUT.equals(property))
|
|
921 return Boolean.valueOf(book.isCheckedOut());
|
|
922 else if (Librarian.WHO.equals(property))
|
|
923 return stringcast( book.getCheckedOutTo() is null ? "" : book.getCheckedOutTo());
|
|
924 else
|
|
925 return null;
|
|
926 }
|
|
927
|
|
928 /**
|
|
929 * Modifies the element
|
|
930 *
|
|
931 * @param element the book
|
|
932 * @param property the property
|
|
933 * @param value the new value
|
|
934 */
|
|
935 public void modify(Object element, String property, Object value) {
|
|
936 if ( auto item = cast(Item)element ){
|
|
937 element = item.getData();
|
|
938 }
|
|
939
|
|
940 Book book = cast(Book) element;
|
|
941 if (Librarian.TITLE.equals(property))
|
|
942 book.setTitle(stringcast(value));
|
|
943 else if (Librarian.CHECKED_OUT.equals(property)) {
|
|
944 bool b = (cast(Boolean) value).booleanValue();
|
|
945 if (b)
|
|
946 book.checkOut("[Enter Name]");
|
|
947 else
|
|
948 book.checkIn();
|
|
949 } else if (Librarian.WHO.equals(property))
|
|
950 book.checkOut(stringcast(value));
|
|
951
|
|
952 // Refresh the view
|
|
953 Librarian.getApp().refreshView();
|
|
954
|
|
955 // Set the library dirty
|
|
956 Librarian.getApp().setLibraryDirty();
|
|
957 }
|
|
958 }
|
|
959
|
|
960 /**
|
|
961 * This class holds all the books in a library. It also handles loading from and
|
|
962 * saving to disk
|
|
963 */
|
|
964 public class Library {
|
|
965 private static const String SEP = "|";
|
|
966
|
|
967 // The filename
|
|
968 private String filename;
|
|
969
|
|
970 // The books
|
|
971 private LinkSeq!(Book) books;
|
|
972
|
|
973 // The dirty flag
|
|
974 private bool dirty;
|
|
975
|
|
976 /**
|
|
977 * Library constructor Note the signature :-)
|
|
978 */
|
|
979 public this() {
|
|
980 books = new LinkSeq!(Book);
|
|
981 }
|
|
982
|
|
983 /**
|
|
984 * Loads the library from a file
|
|
985 *
|
|
986 * @param filename the filename
|
|
987 * @throws IOException
|
|
988 */
|
|
989 public void load(String filename) {
|
|
990 auto istr = (new FileInput(filename)).input;
|
|
991 auto lines = new LineIterator!(char)(istr);
|
|
992 String line;
|
|
993 foreach (line; lines ) {
|
|
994 auto tokens = tango.text.Util.delimit(line, SEP);
|
|
995 Book book = null;
|
|
996 if ( tokens.length > 0 ) book = new Book( tokens[0].dup );
|
|
997 if (tokens.length > 1) book.checkOut(tokens[1].dup );
|
|
998 if (book !is null) add(book);
|
|
999 }
|
|
1000 istr.close();
|
|
1001 this.filename = filename;
|
|
1002 dirty = false;
|
|
1003 }
|
|
1004
|
|
1005 /**
|
|
1006 * Saves the library to a file
|
|
1007 *
|
|
1008 * @param filename the filename
|
|
1009 * @throws IOException
|
|
1010 */
|
|
1011 public void save(String filename) {
|
|
1012 scope ostr = (new FileOutput(filename)).output;
|
|
1013 scope printer = new Print!(char)( Format, ostr );
|
|
1014 foreach ( book; books ) {
|
|
1015 printer.formatln( "{}|{}",book.getTitle(), (book.getCheckedOutTo() is null ? "" : book.getCheckedOutTo()));
|
|
1016 }
|
|
1017 ostr.close();
|
|
1018 this.filename = filename;
|
|
1019 dirty = false;
|
|
1020 }
|
|
1021
|
|
1022 /**
|
|
1023 * Adds a book
|
|
1024 *
|
|
1025 * @param book the book to add
|
|
1026 * @return bool
|
|
1027 */
|
|
1028 public bool add(Book book) {
|
|
1029 books.append(book);
|
|
1030 setDirty();
|
|
1031 return true;
|
|
1032 }
|
|
1033
|
|
1034 /**
|
|
1035 * Removes a book
|
|
1036 *
|
|
1037 * @param book the book to remove
|
|
1038 */
|
|
1039 public void remove(Book book) {
|
|
1040 books.remove(book);
|
|
1041 setDirty();
|
|
1042 }
|
|
1043
|
|
1044 /**
|
|
1045 * Gets the books
|
|
1046 *
|
|
1047 * @return Collection
|
|
1048 */
|
|
1049 public LinkSeq!(Book) getBooks() {
|
|
1050 return books;
|
|
1051 }
|
|
1052
|
|
1053 /**
|
|
1054 * Gets the file name
|
|
1055 *
|
|
1056 * @return String
|
|
1057 */
|
|
1058 public String getFileName() {
|
|
1059 return filename;
|
|
1060 }
|
|
1061
|
|
1062 /**
|
|
1063 * Gets whether this file is dirty
|
|
1064 *
|
|
1065 * @return bool
|
|
1066 */
|
|
1067 public bool isDirty() {
|
|
1068 return dirty;
|
|
1069 }
|
|
1070
|
|
1071 /**
|
|
1072 * Sets this file as dirty
|
|
1073 */
|
|
1074 public void setDirty() {
|
|
1075 dirty = true;
|
|
1076 }
|
|
1077 }
|
|
1078
|
|
1079
|
|
1080 /**
|
|
1081 * This class provides the content for the library table
|
|
1082 */
|
|
1083 public class LibraryContentProvider : IStructuredContentProvider {
|
|
1084 /**
|
|
1085 * Gets the books
|
|
1086 *
|
|
1087 * @param inputElement the library
|
|
1088 * @return Object[]
|
|
1089 */
|
|
1090 public Object[] getElements(Object inputElement) {
|
|
1091 return (cast(Library) inputElement).getBooks().toArray();
|
|
1092 }
|
|
1093
|
|
1094 /**
|
|
1095 * Disposes any resources
|
|
1096 */
|
|
1097 public void dispose() {
|
|
1098 // Do nothing
|
|
1099 }
|
|
1100
|
|
1101 /**
|
|
1102 * Called when the input changes
|
|
1103 *
|
|
1104 * @param viewer the viewer
|
|
1105 * @param oldInput the old library
|
|
1106 * @param newInput the new library
|
|
1107 */
|
|
1108 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
|
|
1109 // Ignore
|
|
1110 }
|
|
1111 }
|
|
1112
|
|
1113
|
|
1114 /**
|
|
1115 * This class provides the labels for the library table
|
|
1116 */
|
|
1117 public class LibraryLabelProvider : ITableLabelProvider {
|
|
1118 private Image checked;
|
|
1119 private Image unchecked;
|
|
1120
|
|
1121 /**
|
|
1122 * LibraryLabelProvider constructor
|
|
1123 */
|
|
1124 public this() {
|
|
1125 // Create the check mark images
|
|
1126 checked = new Image(null, new ImageData( new ByteArrayInputStream( cast(byte[])import("jface.librarian.checked.gif"))));
|
|
1127 unchecked = new Image(null,new ImageData( new ByteArrayInputStream( cast(byte[])import("jface.librarian.unchecked.gif"))));
|
|
1128 }
|
|
1129
|
|
1130 /**
|
|
1131 * Gets the column image
|
|
1132 *
|
|
1133 * @param element the book
|
|
1134 * @param columnIndex the column index
|
|
1135 * @return Image
|
|
1136 */
|
|
1137 public Image getColumnImage(Object element, int columnIndex) {
|
|
1138 // For the "Checked Out" column, return the check mark
|
|
1139 // if the book is checked out
|
|
1140 if (columnIndex is 1)
|
|
1141 return (cast(Book) element).isCheckedOut() ? checked : unchecked;
|
|
1142 return null;
|
|
1143 }
|
|
1144
|
|
1145 /**
|
|
1146 * Gets the column text
|
|
1147 *
|
|
1148 * @param element the book
|
|
1149 * @param columnIndex the column index
|
|
1150 * @return String
|
|
1151 */
|
|
1152 public String getColumnText(Object element, int columnIndex) {
|
|
1153 Book book = cast(Book) element;
|
|
1154 String text = null;
|
|
1155 switch (columnIndex) {
|
|
1156 case 0:
|
|
1157 text = book.getTitle();
|
|
1158 break;
|
|
1159 case 2:
|
|
1160 text = book.getCheckedOutTo();
|
|
1161 break;
|
|
1162 default:
|
|
1163 }
|
|
1164 return text is null ? "" : text;
|
|
1165 }
|
|
1166
|
|
1167 /**
|
|
1168 * Adds a listener
|
|
1169 */
|
|
1170 public void addListener(ILabelProviderListener listener) {
|
|
1171 // Ignore
|
|
1172 }
|
|
1173
|
|
1174 /**
|
|
1175 * Disposes any resources
|
|
1176 */
|
|
1177 public void dispose() {
|
|
1178 if (checked !is null) checked.dispose();
|
|
1179 if (unchecked !is null) unchecked.dispose();
|
|
1180 }
|
|
1181
|
|
1182 /**
|
|
1183 * Gets whether this is a label property
|
|
1184 *
|
|
1185 * @param element the book
|
|
1186 * @param property the property
|
|
1187 * @return bool
|
|
1188 */
|
|
1189 public bool isLabelProperty(Object element, String property) {
|
|
1190 return false;
|
|
1191 }
|
|
1192
|
|
1193 /**
|
|
1194 * Removes a listener
|
|
1195 *
|
|
1196 * @param listener the listener
|
|
1197 */
|
|
1198 public void removeListener(ILabelProviderListener listener) {
|
|
1199 // Ignore
|
|
1200 }
|
|
1201 }
|
|
1202
|
|
1203
|
|
1204
|
|
1205
|
|
1206
|
|
1207
|