Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/cybertivity/powerjournal/MainController.java


1   package com.cybertivity.powerjournal;
2   import java.io.File;
3   import java.net.URL;
4   import java.security.NoSuchAlgorithmException;
5   import java.sql.Timestamp;
6   import java.text.ParseException;
7   import java.text.SimpleDateFormat;
8   import java.util.ArrayList;
9   import java.util.Date;
10  import java.util.HashMap;
11  import java.util.Iterator;
12  import java.util.Set;
13  import javax.swing.JDialog;
14  import javax.swing.JFileChooser;
15  import javax.swing.JFrame;
16  import javax.swing.JOptionPane;
17  import com.cybertivity.powerjournal.database.*;
18  import com.cybertivity.powerjournal.framework.*;
19  
20  /**
21   * Title:        PowerJournal
22   * Description:  $Id: MainController.java,v 1.17 2001/12/29 20:07:49 arrowood Exp $
23   * Copyright:    Copyright (c) 2001
24   * Company:      <A HREF="http://www.cybertivity.com">Cybertivity</A>
25   *
26   * @author <A HREF="mailto:chris.arrowood@cybertivity.com">Chris Arrowood</A>
27   * @created November 19, 2001
28   * @version 1.0
29   */
30  
31  public class MainController extends Controller {
32  
33    public final static String APP_NAME = "PowerJournal";
34    public final static String APP_VERSION = "1.0";
35    private MainView mainView = null;
36    private EntryManager entryManager = null;
37    private ConfigManager configManager = null;
38    private JournalManager journalManager = null;
39    public final static String FILE_SEPARATOR = System.getProperty("file.separator");
40    private String powerJournalDefaultDir = System.getProperty("user.home") + FILE_SEPARATOR + ".PowerJournal";
41    private String powerJournalDir = "";
42    private ArrayList journals = new ArrayList();
43    private NewJournalView newJournalView = null;
44    private String currentJournalName = null;
45    private boolean isGuest = true;
46    public final static String ERROR_TITLE_DB = "Database Error";
47    public final static String ERROR_MESSAGE_DB = "Unable to connect to the database.  Please verify your settings.\n\n";
48    public final static String ERROR_MESSAGE_UNSUPPORTED = "Error: MainController recieved an action that has not been implemented. ACTION:";
49    public final static String ERROR_MESSAGE_EXIT = "\n\nThe program will now exit.";
50    public final static String ERROR_MESSAGE_INVALID_ENTRY = "The entry you specified does not exists";
51    public final static String ERROR_TITLE_INVALID_ENTRY = "Invalid Entry";
52    public final static String ERROR_TITLE_ACTION_CANCELLED = "Action Canceled";
53    public final static String ERROR_TITLE_MESSAGE_CANCELLED = "The action was canceled";
54    public final static String DEFAULT_CONFIG_DB_USER = "sa";
55    public final static String DEFAULT_CONFIG_DB_PASS = "";
56    public final static String CARD_SINGLE = "Single";
57    public final static String CARD_MULTI = "Multi";
58    private SingleEntryController singleEntryController = null;
59    private SingleEntryView singleEntryView = null;
60    private MultiEntryController multiEntryController = null;
61    private MultiEntryView multiEntryView = null;
62    private JFrame content = null;
63    public ExportAgent exportAgent = null;
64    private String powerJournalSkinsDir = "";
65  
66  
67    public MainController() {
68      mainView = new MainView(APP_NAME);
69      mainView.addObserver(this);
70      content = mainView.getContent();
71      preparePowerJournalDir();
72      exportAgent = new ExportAgent(powerJournalDir, content);
73      try {
74        configManager = ConfigManager.getInstance();
75        getJournalManager();
76        setInitialSkin();
77      } catch (DBException e) {
78        Dialogs.showError(content, ERROR_TITLE_DB, e.getMessage() + ERROR_MESSAGE_EXIT);
79        System.exit(1);
80      }
81      singleEntryView = new SingleEntryView(APP_NAME, isGuest);
82      singleEntryController = new SingleEntryController(content, singleEntryView, isGuest);
83      singleEntryController.addObserver(this);
84      multiEntryView = new MultiEntryView(APP_NAME, isGuest, new ArrayList());
85      multiEntryController = new MultiEntryController(content, multiEntryView, isGuest);
86      multiEntryController.addObserver(this);
87      mainView.addEntryView(singleEntryView, CARD_SINGLE);
88      mainView.addEntryView(multiEntryView, CARD_MULTI);
89      mainView.showEntryView(CARD_SINGLE);
90      mainView.setVisible(true);
91      mainView.disableControls();
92      showChooseJournalView();
93    }
94  
95  
96    private boolean getJournalManager() throws DBException {
97      boolean success = false;
98      try {
99        String currentDBType = configManager.getParameter(ConfigManager.KEY_DB_TYPE);
100       String driver = null;
101       String url = null;
102       String dbUser = null;
103       String dbPass = null;
104       try {
105         if (currentDBType.equals(ConfigManager.VALUE_DB_VERSION_HSQL)) {
106           driver = DBManager.getDriverString(ConfigManager.VALUE_DB_VERSION_HSQL);
107           url = JournalManager.makeUrl(null, null, ConfigManager.VALUE_DB_VERSION_HSQL, powerJournalDir);
108           dbUser = DEFAULT_CONFIG_DB_USER;
109           dbPass = DEFAULT_CONFIG_DB_PASS;
110           journalManager = JournalManager.getInstance(ConfigManager.VALUE_DB_VERSION_HSQL, url, driver, dbUser, dbPass, true);
111           success = true;
112         } else if (currentDBType.equals(ConfigManager.VALUE_DB_VERSION_MYSQL)) {
113           String dbAddress = configManager.getParameter(ConfigManager.KEY_DB_ADDRESS);
114           String dbPort = configManager.getParameter(ConfigManager.KEY_DB_PORT);
115           driver = DBManager.getDriverString(currentDBType);
116           url = JournalManager.makeUrl(dbAddress, dbPort, currentDBType, powerJournalDir);
117           dbUser = configManager.getParameter(ConfigManager.KEY_DB_USERNAME);
118           dbPass = configManager.getParameter(ConfigManager.KEY_DB_PASSWORD);
119           journalManager = JournalManager.getInstance(currentDBType, url, driver, dbUser, dbPass, true);
120           success = true;
121         }
122       } catch (DBException e) {
123         Dialogs.showError(content, ERROR_TITLE_DB, ERROR_MESSAGE_DB + e.getMessage());
124         mainView.disableControls();
125         showDatabaseOptions();
126       }
127     } catch (DBException e) {
128       Dialogs.showError(content, ERROR_TITLE_DB, e.getMessage() + ERROR_MESSAGE_EXIT);
129       System.exit(1);
130     }
131 
132     return success;
133   }
134 
135 
136 
137   private void showChooseJournalView() {
138     try {
139       journals = journalManager.getAllJournalNames();
140     } catch (DBException ex) {
141       //oops; guess there will not be much of a choice for the user
142     }
143     if (journals.size() == 0) {
144       Dialogs.showInformation(content, "No Journals Found", "No journals were found.  The \"Create New Journal\" dialog will now appear.");
145       showNewJournalView();
146     } else {
147       ChooseJournalView chooseJournalView = new ChooseJournalView(content, journals);
148       chooseJournalView.addObserver(this);
149       chooseJournalView.setVisible(true);
150     }
151   }
152 
153 
154   private void showChangePasswordView() {
155     if (isGuest) {
156       Dialogs.showError(content, "Unauthorized", "You are viewing this journal as a guest.  You are not allowed to change its password.");
157     } else {
158       ChangePasswordView cpv = ChangePasswordView.getInstance(content);
159       cpv.addObserver(this);
160       cpv.setVisible(true);
161     }
162   }
163 
164 
165   private void createNewJournal(String journalName, String password, String passwordConfirmation) throws DBException {
166     if (password.equalsIgnoreCase(passwordConfirmation)) {
167       journalName = journalName.trim();
168       if (JournalManager.isValidJournalName(journalName)) {
169         journalName = journalName.replace(' ', '_');
170         if (!journalManager.journalExists(journalName)) {
171           Integer id = journalManager.createJournal(journalName, passwordConfirmation.toLowerCase());
172           currentJournalName = journalName;
173           isGuest = false;
174           getEntryManager();
175           multiEntryController.setGuestMode(false);
176           singleEntryController.setGuestMode(false);
177           singleEntryController.moveToFirstEntry();
178           journals = journalManager.getAllJournalNames();
179         } else {
180           Dialogs.showError(content, "Create New Journal", "A journal with that name already exists.");
181           showNewJournalView();
182         }
183       } else {
184         Dialogs.showError(content, "Create New Journal", "Invalid journal name.\n\nA valid journal name only uses letters and\nnumbers and must begin with a letter.");
185         showNewJournalView();
186       }
187     } else {
188       Dialogs.showError(content, "Create New Journal", "Your passwords do not match.");
189       showNewJournalView();
190     }
191   }
192 
193 
194   private void getEntryManager() throws DBException {
195     entryManager = EntryManager.getInstance(currentJournalName, journalManager, isGuest, true);
196     //upgrade from v 0.1 if needed
197     if (entryManager.oldDatabaseVersionExists()) {
198       entryManager.importFromPreviousVersion(true);
199       Dialogs.showInformation(content, "Import Successful", "Entries were found from an older version of this software.  They have been imported successfully into your new journal.");
200     }
201     //in case it is new and nothing was imported
202     entryManager.prepareDatabase();
203     singleEntryController.setEntryManager(entryManager);
204     singleEntryController.setGuestMode(isGuest);
205   }
206 
207 
208   private void showNewJournalView() {
209     newJournalView = NewJournalView.getInstance(content);
210     newJournalView.addObserver(this);
211     newJournalView.setVisible(true);
212   }
213 
214 
215 
216   public void handleModelEvent(ModelEvent event) { }
217 
218 
219   /*
220    *  An event from one of the views OR controllers...
221    */
222   public void handleViewEvent(ViewEvent event) {
223     Object initiator = event.getInitiator();
224     String action = event.getField();
225     try {
226       if (initiator instanceof MainView) {
227         handleMainEvent(event);
228       } else if (initiator instanceof DatabaseOptionsView) {
229         handleDatabaseOptionsViewEvent(event);
230       } else if (initiator instanceof NewJournalView) {
231         handleNewJournalViewEvent(event);
232       } else if (initiator instanceof ChooseJournalView) {
233         handleChooseJournalViewEvent(event);
234       } else if (initiator instanceof ChangePasswordView) {
235         handleChangePasswordViewEvent(event);
236       } else if (initiator instanceof SingleEntryController) {
237         handleSingleEntryControllerEvent(event);
238       } else if (initiator instanceof MultiEntryController) {
239         handleMultiEntryControllerEvent(event);
240       } else {
241         throw new java.lang.UnsupportedOperationException(ERROR_MESSAGE_UNSUPPORTED + action);
242       }
243     } catch (Exception ex) {
244       new ExceptionDialog(ex);
245     }
246   }
247 
248 
249   public static void main(String[] args) {
250     MainController mainController1 = new MainController();
251   }
252 
253 
254   private void doEasterEgg() {
255     Dialogs.showInformation(content, "Credits", "Credits: Written and designed by Chris Arrowood.\n\n"
256          + "Sorry for such a lame easter egg, but my time \nhas been consumed with more important parts of this application.  \n Besides, what kind of idiot would hide an easter egg \nin open source program?  Well... I guess I would.  I just like to be fun.\n\n:-)");
257   }
258 
259 
260   private void doAbout() {
261     Dialogs.showInformation(content, "About",
262         APP_NAME + " " + APP_VERSION + "\n\n"
263          + "Licensed under the GNU GENERAL PUBLIC LICENSE,  Version 2, June 1991\n"
264          + "The full text of the GPL license is at http://www.gnu.org/licenses/gpl.txt \n\n"
265          + "Copyright(C) 2001 by Chris Arrowood\n\n"
266          + "This application is written entirely in Java and took me about 6 weeks in late 2001.\n\n"
267          + "If you enjoy this software, send me a postcard from your part of the world.\nThe encouragement will go a long way!\n"
268          + "My postal address can be found at http://powerjournal.sourceforge.net/\n\n"
269          + "Thank you for supporting open source software!"
270         );
271   }
272 
273 
274   private void handleDatabaseOptionsViewEvent(ViewEvent event) {
275     String action = event.getField();
276     Object arg = event.getValue();
277     DatabaseOptionsView view = (DatabaseOptionsView) event.getInitiator();
278     if (action.equals(DatabaseOptionsView.EVENT_CANCEL)) {
279       view.closeDialog();
280     } else if (action.equals(DatabaseOptionsView.EVENT_OK)) {
281       changeDatabases(view);
282     }
283   }
284 
285 
286   private void handleNewJournalViewEvent(ViewEvent event) throws DBException {
287     String action = event.getField();
288     Object arg = event.getValue();
289     NewJournalView view = (NewJournalView) event.getInitiator();
290     if (action.equals(NewJournalView.EVENT_CANCEL)) {
291       if (journalManager.getJournalCount() == 0) {
292         Dialogs.showInformation(content, "No Journals Found", "There are no journals defined.  Please create one before continuing");
293         showNewJournalView();
294       } else if (currentJournalName == null) {
295         view.closeDialog();
296         showChooseJournalView();
297       } else {
298         view.closeDialog();
299       }
300     } else if (action.equals(NewJournalView.EVENT_OK)) {
301       view.closeDialog();
302       createNewJournal(newJournalView.getJournalNameText(), newJournalView.getJournalPasswordText(), newJournalView.getJournalPasswordConfirmText());
303       mainView.enableControls();
304     }
305   }
306 
307 
308   private void changePassword(ChangePasswordView view) throws DBException {
309     if (view.getJournalPasswordText().equalsIgnoreCase(view.getJournalPasswordConfirmText())) {
310       journalManager.changePassword(currentJournalName, view.getJournalPasswordText());
311       Dialogs.showInformation(content, "Change Password", "Your password has been successfully changed.");
312     } else {
313       Dialogs.showError(content, "Change Password", "Your passwords do not match.");
314       showChangePasswordView();
315     }
316   }
317 
318 
319   private void handleChangePasswordViewEvent(ViewEvent event) throws DBException {
320     String action = event.getField();
321     Object arg = event.getValue();
322     ChangePasswordView view = (ChangePasswordView) event.getInitiator();
323     if (action.equals(ChangePasswordView.EVENT_CANCEL)) {
324       view.closeDialog();
325       view.cleanUp();
326     } else if (action.equals(ChangePasswordView.EVENT_OK)) {
327       changePassword(view);
328       view.closeDialog();
329       view.cleanUp();
330     } else {
331       throw new java.lang.UnsupportedOperationException(ERROR_MESSAGE_UNSUPPORTED + action);
332     }
333   }
334 
335 
336   private void handleSingleEntryControllerEvent(ViewEvent event) throws DBException {
337     String action = event.getField();
338     Object arg = event.getValue();
339     SingleEntryController controller = (SingleEntryController) event.getInitiator();
340     if (action.equals(SingleEntryView.EVENT_VIEW_ALL)) {
341       viewAll();
342     } else if (action.equals(SingleEntryView.EVENT_DELETE)) {
343       deleteEntry(singleEntryView);
344     } else {
345       throw new java.lang.UnsupportedOperationException(ERROR_MESSAGE_UNSUPPORTED + action);
346     }
347   }
348 
349 
350   private void handleMultiEntryControllerEvent(ViewEvent event) throws DBException {
351     String action = event.getField();
352     Object arg = event.getValue();
353     MultiEntryController controller = (MultiEntryController) event.getInitiator();
354     if (action.equals(MultiEntryView.EVENT_VIEW_SELECTED)) {
355       Entry entry = entryManager.getEntryById((String) arg);
356       if (entry != null  && !entry.equals("")) {
357         mainView.displayEntry(entry, isGuest);
358       } else {
359         Dialogs.showError(content, ERROR_TITLE_INVALID_ENTRY, ERROR_MESSAGE_INVALID_ENTRY);
360       }
361     } else if (action.equals(MultiEntryView.EVENT_NEW)) {
362       singleEntryController.newEntry();
363       mainView.showEntryView(MainController.CARD_SINGLE);
364     } else if (action.equals(MultiEntryView.EVENT_DELETE)) {
365       deleteEntry(multiEntryView);
366       viewAll();
367       //to refresh
368     } else {
369       throw new java.lang.UnsupportedOperationException(ERROR_MESSAGE_UNSUPPORTED + action);
370     }
371   }
372 
373 
374   private void deleteEntry(EntryView view) {
375     try {
376       Entry previousEntry = null;
377       String currEntryNumber = view.getSelectedEntryNumber();
378       if (currEntryNumber==null || currEntryNumber.length()<1) {
379         Dialogs.showError(content, ERROR_TITLE_INVALID_ENTRY, ERROR_MESSAGE_INVALID_ENTRY);
380         return;
381       }
382       int ans = Dialogs.getConfirm(content, "Delete Current Entry?", "Are you sure you wish to delete the current entry?  You will not be able to recover this entry.");
383       if (ans == JOptionPane.OK_OPTION) {
384         if (currEntryNumber != SingleEntryView.NEW_ENTRY_NUMBER_LABEL) {
385           previousEntry = entryManager.moveToPreviousEntry(view.getSelectedEntryNumber());
386           entryManager.deleteEntry(currEntryNumber);
387         }
388         if (view instanceof MultiEntryView) {
389           mainView.displayEntries(getAllEntries());
390         } else if (view instanceof SingleEntryView) {
391           view.displayEntry(previousEntry, isGuest);
392         }
393       } else {
394         Dialogs.showInformation(content, ERROR_TITLE_ACTION_CANCELLED, ERROR_TITLE_MESSAGE_CANCELLED);
395       }
396     } catch (DBException e) {
397       Dialogs.showError(content, MainController.ERROR_TITLE_DB, e.getMessage());
398     }
399   }
400 
401 
402   private void handleChooseJournalViewEvent(ViewEvent event) throws DBException {
403     String action = event.getField();
404     Object arg = event.getValue();
405     ChooseJournalView view = (ChooseJournalView) event.getInitiator();
406     if (action.equals(ChooseJournalView.EVENT_CANCEL)) {
407       if (currentJournalName == null) {
408         int ans = Dialogs.getConfirm(content, "Exit?", "Do you wish to exit this program?");
409         if (ans == JOptionPane.OK_OPTION) {
410           System.exit(0);
411         }
412       } else {
413         view.closeDialog();
414         view.cleanUp();
415         mainView.enableControls();
416       }
417     } else if (action.equals(ChooseJournalView.EVENT_NEW_JOURNAL)) {
418       view.closeDialog();
419       view.cleanUp();
420       showNewJournalView();
421     } else if (action.equals(ChooseJournalView.EVENT_OK)) {
422       if (view.isGuest()) {
423         isGuest = true;
424         currentJournalName = view.getJournalName();
425         view.closeDialog();
426         getEntryManager();
427         singleEntryController.moveToFirstEntry();
428         multiEntryController.setGuestMode(isGuest);
429         mainView.showEntryView(MainController.CARD_SINGLE);
430         mainView.enableControls();
431       } else {
432         isGuest = false;
433         try {
434           String hashedPassword = journalManager.getHashedPassword(view.getJournalName());
435           String enteredPassword = PasswordLib.hashPassword(view.getJournalPassword().toLowerCase());
436           if (enteredPassword.equals(hashedPassword)) {
437             currentJournalName = view.getJournalName();
438             view.closeDialog();
439             getEntryManager();
440             singleEntryController.moveToFirstEntry();
441             multiEntryController.setGuestMode(isGuest);
442             mainView.showEntryView(MainController.CARD_SINGLE);
443             mainView.enableControls();
444           } else {
445             Dialogs.showError(content, "Invalid Password", "The password you entered is invalid.");
446           }
447         } catch (NoSuchAlgorithmException e) {
448           Dialogs.showError(content, "Security Error", e.getMessage() + ERROR_MESSAGE_EXIT);
449         }
450       }
451     } else {
452       throw new java.lang.UnsupportedOperationException(ERROR_MESSAGE_UNSUPPORTED + action);
453     }
454   }
455 
456 
457   private void changeDatabases(DatabaseOptionsView view) {
458     int export = JOptionPane.NO_OPTION;
459     if (view.formHasChanged()) {
460       if (!view.getDbPasswordText().equals(view.getDbPasswordConfirmText())) {
461         Dialogs.showWarning(content, "Error", "The passwords do not match!");
462         return;
463       }
464       if (isGuest) {
465         export = JOptionPane.NO_OPTION;
466       } else {
467         export = JOptionPane.showConfirmDialog(content, "Would you like to export your entries to the new database?",
468             "Export?", JOptionPane.YES_NO_OPTION);
469       }
470     }
471     try {
472       HashMap allJournalEntries = null;
473       HashMap allJournalConfig = null;
474       if (export == JOptionPane.YES_OPTION) {
475         allJournalEntries = journalManager.exportAllJournalEntries(entryManager);
476         allJournalConfig = journalManager.exportAllJournalsConfig();
477       }
478       String url = null;
479       if (view.getDbType().equals(ConfigManager.VALUE_DB_VERSION_MYSQL)) {
480         url = JournalManager.makeUrl(view.getDbAddressText(), view.getDbPortText(), view.getDbType(), powerJournalDir, false);
481         journalManager = JournalManager.getInstance(view.getDbType(), url, DBManager.getDriverString(view.getDbType()), view.getDbUsernameText(), view.getDbPasswordText(), true);
482       } else if (view.getDbType().equals(ConfigManager.VALUE_DB_VERSION_HSQL)) {
483         url = JournalManager.makeUrl(view.getDbAddressText(), view.getDbPortText(), view.getDbType(), powerJournalDir, true);
484         journalManager = JournalManager.getInstance(view.getDbType(), url, DBManager.getDriverString(view.getDbType()), DEFAULT_CONFIG_DB_USER, DEFAULT_CONFIG_DB_PASS, true);
485       }
486       entryManager = EntryManager.getInstance(currentJournalName, journalManager, isGuest, true);
487       if (view.saveConfig()) {
488         view.closeDialog();
489       }
490       if (view.getDbChanged()) {
491         if (export == JOptionPane.YES_OPTION) {
492           journalManager.createJournals(allJournalConfig);
493           Set keys = allJournalEntries.keySet();
494           Iterator iter = keys.iterator();
495           while (iter.hasNext()) {
496             String journalName = (String) iter.next();
497             ArrayList entries = (ArrayList) allJournalEntries.get(journalName);
498             entryManager.changeJournal(journalName);
499             for (int i = 0; i < entries.size(); i++) {
500               Entry entry = (Entry) entries.get(i);
501               entryManager.insertEntryToTable(entry);
502             }
503           }
504         }
505         entryManager.changeJournal(currentJournalName);
506         entryManager.prepareDatabase();
507         Entry entry = entryManager.moveToFirstEntry();
508         mainView.displayEntry(entry, isGuest);
509         currentJournalName = null;
510         showChooseJournalView();
511       }
512     } catch (DBException e) {
513       Dialogs.showError(content, ERROR_TITLE_DB, ERROR_MESSAGE_DB + e.getMessage());
514     }
515   }
516 
517 
518   private void handleMainEvent(ViewEvent event) {
519     String action = event.getField();
520     Object arg = event.getValue();
521     if (action.equals(MainActionFactory.ACTION_QUIT)) {
522       exit(0);
523     } else if (action.equals(MainView.EVENT_EASTEREGG)) {
524       doEasterEgg();
525     } else if (action.equals(MainActionFactory.ACTION_ABOUT)) {
526       doAbout();
527     } else if (action.equals(MainActionFactory.ACTION_DELETE_JOURNAL)) {
528       deleteJournal();
529 //    } else if (action.equals(MainActionFactory.ACTION_VIEW)) {
530 //      viewEntry();
531     } else if (action.equals(MainActionFactory.ACTION_DB_OPTIONS)) {
532       showDatabaseOptions();
533     } else if (action.equals(MainActionFactory.ACTION_CHANGE_JOURNAL)) {
534       showChooseJournalView();
535     } else if (action.equals(MainActionFactory.ACTION_CHANGE_PASSWORD)) {
536       showChangePasswordView();
537     } else if (action.equals(MainActionFactory.ACTION_NEW_JOURNAL)) {
538       showNewJournalView();
539     } else if (action.equals(MainActionFactory.ACTION_VIEW_ENTRY)) {
540       viewEntry();
541     } else if (action.equals(MainActionFactory.ACTION_VIEW_ALL_ENTRIES)) {
542       viewAll();
543     } else if (action.equals(MainActionFactory.ACTION_EXPORT_CSV)) {
544       exportAgent.exportCSV(getAllEntries(), currentJournalName);
545     } else if (action.equals(MainActionFactory.ACTION_EXPORT_TEXT)) {
546       exportAgent.exportText(getAllEntries(), currentJournalName);
547     } else if (action.equals(MainActionFactory.ACTION_EXPORT_HTML)) {
548       exportAgent.exportHTML(getAllEntries(), currentJournalName);
549     } else if (action.equals(MainActionFactory.ACTION_SKINS_CHOOSE)) {
550       chooseSkin();
551     } else if (action.equals(MainActionFactory.ACTION_SKINS_USE)) {
552       setUseSkinsValue();
553     } else {
554       throw new java.lang.UnsupportedOperationException(ERROR_MESSAGE_UNSUPPORTED + action);
555     }
556   }
557 
558   private void chooseSkin(){
559     JFileChooser fc = new JFileChooser();
560     fc.setCurrentDirectory(new File(powerJournalSkinsDir));
561     int returnVal = fc.showSaveDialog(content);
562     if (returnVal == JFileChooser.APPROVE_OPTION) {
563       File skin =  fc.getSelectedFile();
564       mainView.setUseSkins(true);
565       if (mainView.updateSkin(skin.getAbsolutePath())) {
566         setConfigParameter(configManager.KEY_SKIN_PREFERENCE,skin.getAbsolutePath());
567       }
568     }
569   }
570 
571   private void setUseSkinsValue(){
572     boolean useSkins = mainView.getUseSkins();
573     if (useSkins) {
574       setConfigParameter(configManager.KEY_USE_SKINS,"1");
575       //enable skin from db
576       try{
577         mainView.updateSkin(configManager.getParameter(configManager.KEY_SKIN_PREFERENCE));
578       } catch (DBException e) {
579         Dialogs.showError(content, ERROR_TITLE_DB, ERROR_MESSAGE_DB + e.getMessage());
580         showDatabaseOptions();
581       }
582     }
583     else {
584       setConfigParameter(configManager.KEY_USE_SKINS,"0");
585       Dialogs.showInformation(content,"Skins Disabled","The skins have been disabled, however the change will not take effect until the program is restarted.");
586     }
587   }
588 
589 
590   private void setInitialSkin() throws DBException {
591     String useSkins = configManager.getParameter(ConfigManager.KEY_USE_SKINS);
592     if (useSkins==null) {
593       useSkins="1";
594       setConfigParameter(configManager.KEY_USE_SKINS,"1");
595     }
596     if (useSkins.equals("1")) {
597       mainView.setUseSkins(true);
598       mainView.updateSkin(configManager.getParameter(ConfigManager.KEY_SKIN_PREFERENCE));
599     } else {
600       mainView.setUseSkins(false);
601     }
602   }
603 
604   private void setConfigParameter(String key, String value){
605     try{
606       configManager.setParameter(key,value);
607     } catch (DBException e) {
608       Dialogs.showError(content, ERROR_TITLE_DB, ERROR_MESSAGE_DB + e.getMessage());
609       showDatabaseOptions();
610     }
611   }
612 
613   private ArrayList getAllEntries() {
614     try {
615       return entryManager.getEntries();
616     } catch (DBException e) {
617       Dialogs.showError(content, ERROR_TITLE_DB, ERROR_MESSAGE_DB + e.getMessage());
618       showDatabaseOptions();
619       return null;
620     }
621   }
622 
623 
624   protected String getEntryNumberFromUser() {
625     return JOptionPane.showInputDialog(content, "Enter an entry number:", "View Entry", JOptionPane.QUESTION_MESSAGE);
626   }
627 
628 
629   private void viewAll() {
630     if (!isGuest) {
631       if (singleEntryController.saveCurrentEntry()) {
632         mainView.displayEntries(getAllEntries());
633       }
634     } else {
635       mainView.displayEntries(getAllEntries());
636     }
637   }
638 
639 
640   private void viewEntry() {
641     String entryNumber = getEntryNumberFromUser();
642     if (entryNumber != null && !entryNumber.equals("")) {
643       Entry entry = null;
644       try {
645         Integer i = new Integer(entryNumber);
646         try {
647           entry = entryManager.getEntryById(i.toString());
648         } catch (DBException e) {
649           Dialogs.showError(content, MainController.ERROR_TITLE_DB, e.getMessage());
650         }
651         if (entry != null) {
652           mainView.displayEntry(entry, isGuest);
653         } else {
654           Dialogs.showWarning(content, ERROR_TITLE_INVALID_ENTRY, ERROR_MESSAGE_INVALID_ENTRY);
655         }
656       } catch (NumberFormatException ex) {
657         Dialogs.showWarning(content, ERROR_TITLE_INVALID_ENTRY, ERROR_MESSAGE_INVALID_ENTRY);
658       }
659     }
660   }
661 
662 
663   private void showDatabaseOptions() {
664     try {
665       DatabaseOptionsView databaseOptionsView = DatabaseOptionsView.getInstance(content);
666       databaseOptionsView.addObserver(this);
667       databaseOptionsView.setVisible(true);
668       mainView.enableControls();
669     } catch (DBException e) {
670       Dialogs.showError(content, ERROR_TITLE_DB, e.getMessage() + ERROR_MESSAGE_EXIT);
671       exit(1);
672     }
673   }
674 
675 
676   private void exit(int resultCode) {
677     if (!singleEntryView.getSelectedEntryNumber().equals(SingleEntryView.INITIAL_ENTRY_NUMBER)) {
678       singleEntryController.saveCurrentEntry();
679     }
680     System.exit(resultCode);
681   }
682 
683 
684   private void deleteJournal() {
685     try {
686       if (isGuest) {
687         Dialogs.showError(content, "Unauthorized", "You are viewing this journal as a guest.  You are not allowed to delete it.");
688       } else {
689         int ans = Dialogs.getConfirm(content, "Delete Journal?", "Delete \"" + currentJournalName + "\"?\n\nAre you sure you wish to delete this journal?\nOnce deleted, you will not be able to recover it.");
690         if (ans == JOptionPane.OK_OPTION) {
691           journalManager.deleteJournal(currentJournalName);
692           Dialogs.showInformation(content, "Journal Deleted", "The journal has been deleted.");
693           getJournalManager();
694           //will this create a new Journal if none exists?
695           currentJournalName = null;
696           showChooseJournalView();
697         } else {
698           Dialogs.showInformation(content, ERROR_TITLE_ACTION_CANCELLED, ERROR_TITLE_MESSAGE_CANCELLED);
699         }
700       }
701     } catch (DBException e) {
702       Dialogs.showError(content, ERROR_TITLE_DB, e.getMessage());
703     }
704   }
705 
706 
707 
708   private void preparePowerJournalDir() {
709     ensureDirExists(new File(powerJournalDefaultDir));
710     powerJournalDir = powerJournalDefaultDir;
711     powerJournalSkinsDir = "." + FILE_SEPARATOR + "skins";
712     ensureDirExists(new File(powerJournalSkinsDir));
713   }
714 
715   private void ensureDirExists(File dir){
716     if ((!dir.exists()) || (!dir.isDirectory())) {
717       dir.mkdir();
718     }
719   }
720 
721 }