From d88141512bc8b12ceafc05604da2548290407c75 Mon Sep 17 00:00:00 2001 From: dchandler Date: Sun, 6 Jul 2003 16:24:29 +0000 Subject: [PATCH] Small changes w.r.t. clearing preferences. Some code cleanup. --- source/options.txt | 11 +++++-- source/org/thdl/tib/input/Jskad.java | 44 ++++++++++++++++----------- source/org/thdl/util/ThdlOptions.java | 31 ++++++++++++++----- 3 files changed, 59 insertions(+), 27 deletions(-) diff --git a/source/options.txt b/source/options.txt index bf799d1..1b61acf 100644 --- a/source/options.txt +++ b/source/options.txt @@ -25,11 +25,16 @@ thdl.number.of.recently.opened.files.to.show = 4 # a recently opened file? thdl.max.chars.in.recently.opened.file.name = 40 -# Set this to the full path of the user preferences file, or to the -# empty string if you wish to use the default values, which are -# system-specific. +# Set this to the full path of the directory containing the user +# preferences file my_thdl_preferences.txt, or to the empty string if +# you wish to use the default values, which are system-specific. thdl.user.options.directory = +# Set this to the full path of the system-wide preferences file if you +# wish to use a system-wide preferences file to customize THDL tools +# for your site. Users' preferences override system-wide preferences. +thdl.system.wide.options.file = + # Set this to true if you want more messages (probably on the console) # about what's going on. thdl.verbose = false diff --git a/source/org/thdl/tib/input/Jskad.java b/source/org/thdl/tib/input/Jskad.java index 98f3ce1..acf6744 100644 --- a/source/org/thdl/tib/input/Jskad.java +++ b/source/org/thdl/tib/input/Jskad.java @@ -130,36 +130,46 @@ public class Jskad extends JPanel implements DocumentListener { /** Saves user preferences to disk if possible. */ private void savePreferencesAction() { - try { - RecentlyOpenedFilesDatabase.storeRecentlyOpenedFilePreferences(); + if (!clearedPrefs) { + try { + RecentlyOpenedFilesDatabase.storeRecentlyOpenedFilePreferences(); - if (!ThdlOptions.saveUserPreferences()) { + if (!ThdlOptions.saveUserPreferences()) { + JOptionPane.showMessageDialog(Jskad.this, + "You previously cleared preferences,\nso you cannot now save them.", + "Cannot Save User Preferences", + JOptionPane.PLAIN_MESSAGE); + } + } catch (IOException ioe) { JOptionPane.showMessageDialog(Jskad.this, - "You previously cleared preferences,\nso you cannot now save them.", - "Cannot Save User Preferences", - JOptionPane.PLAIN_MESSAGE); + "Could not save to your preferences file!", + "Error Saving Preferences", + JOptionPane.ERROR_MESSAGE); } - } catch (IOException ioe) { - System.out.println("IO Exception saving user preferences to " + ThdlOptions.getUserPreferencesPath()); - ioe.printStackTrace(); - ThdlDebug.noteIffyCode(); } } + private static boolean clearedPrefs = false; /** Clears user preferences by deleting the preferences file on disk. Prompts the user to quit and reopen Jskad. */ private void clearPreferencesAction() { + clearedPrefs = true; try { ThdlOptions.clearUserPreferences(); } catch (IOException ioe) { - System.out.println("IO Exception deleting user preferences file " + ThdlOptions.getUserPreferencesPath()); - ioe.printStackTrace(); - ThdlDebug.noteIffyCode(); + JOptionPane.showMessageDialog(Jskad.this, + "Could not delete your preferences file!", + "Error Clearing Preferences", + JOptionPane.ERROR_MESSAGE); + return; + } + if (JOptionPane.YES_OPTION + == JOptionPane.showConfirmDialog(Jskad.this, + "You must exit and restart before default preferences\nwill take effect.\n\nExit now?", + "Clearing Preferences", + JOptionPane.YES_NO_OPTION)) { + exitAction(); } - JOptionPane.showMessageDialog(Jskad.this, - "You must now exit this application and restart\nbefore default preferences will take effect.", - "Clearing Preferences", - JOptionPane.PLAIN_MESSAGE); } /** the File menu */ diff --git a/source/org/thdl/util/ThdlOptions.java b/source/org/thdl/util/ThdlOptions.java index 8dc4063..8261290 100644 --- a/source/org/thdl/util/ThdlOptions.java +++ b/source/org/thdl/util/ThdlOptions.java @@ -83,12 +83,17 @@ public class ThdlOptions { // Look to the System first. String answer = getSystemValue(optionName); if (answer == null) { - answer = userProperties.getProperty(optionName, "false"); + if (null != userProperties) { + answer = userProperties.getProperty(optionName, "false"); + } else { + answer = "false"; + } } return answer.equalsIgnoreCase("true"); } - // FIXMEDOC + /** Returns the value of the system property optionName, or null + if that value is not set. */ private static String getSystemValue(String optionName) { // Look to the System first. String answer = null; @@ -113,7 +118,9 @@ public class ThdlOptions { // Look to the System first. String answer = getSystemValue(optionName); if (answer == null) { - answer = userProperties.getProperty(optionName, null); + if (null != userProperties) { + answer = userProperties.getProperty(optionName, null); + } } return answer; } @@ -167,7 +174,9 @@ public class ThdlOptions { // Look to the System first. String answer = getSystemValue(optionName); if (answer == null) { - answer = userProperties.getProperty(optionName, null); + if (null != userProperties) { + answer = userProperties.getProperty(optionName, null); + } } if (null == answer) { return null; @@ -262,7 +271,7 @@ public class ThdlOptions { // Get the user's properties, if they've set any: userProperties - = tryToGetPropsFromFile("thdl.user.options.directory", // DLC NOW FIXME: put in options.txt + = tryToGetPropsFromFile("thdl.user.options.directory", getUserPreferencesPath(), systemWideProperties, suppressErrors); @@ -326,6 +335,11 @@ public class ThdlOptions { props = getPropertiesFromStream(fis, suppressErrors, defaultProps); + try { + fis.close(); + } catch (IOException e) { + // suppress this error. + } } return props; } @@ -345,11 +359,14 @@ public class ThdlOptions { return getPropertiesFromStream(in, suppressErrors, defaults); } - // FIXMEDOC + /** Returns a Properties object whose default values come from + defaults if defaults is non-null. The properties specified in + the properties file from which in is open for reading override + the default property values. If suppressErrors is true, then + this routine tries to always avoid failure. */ private static Properties getPropertiesFromStream(InputStream in, boolean suppressErrors, Properties defaults) - throws NullPointerException { Properties options; if (defaults == null)