In Jskad, you can now clear the preferences and return to default values.

This commit is contained in:
dchandler 2003-06-21 01:26:17 +00:00
parent fbb6245fdb
commit 45b87b0fb4
2 changed files with 68 additions and 5 deletions

View file

@ -121,9 +121,14 @@ public class Jskad extends JPanel implements DocumentListener {
private Jskad(LayoutManager lm, boolean isDB) { super(lm, isDB); } private Jskad(LayoutManager lm, boolean isDB) { super(lm, isDB); }
/** Saves user preferences to disk if possible. */ /** Saves user preferences to disk if possible. */
private static void savePreferencesAction() { private void savePreferencesAction() {
try { try {
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) { } catch (IOException ioe) {
System.out.println("IO Exception saving user preferences to " + ThdlOptions.getUserPreferencesPath()); System.out.println("IO Exception saving user preferences to " + ThdlOptions.getUserPreferencesPath());
ioe.printStackTrace(); ioe.printStackTrace();
@ -131,6 +136,22 @@ public class Jskad extends JPanel implements DocumentListener {
} }
} }
/** Clears user preferences by deleting the preferences file on
disk. Prompts the user to quit and reopen Jskad. */
private void clearPreferencesAction() {
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,
"You must now exit this application and restart\nbefore default preferences will take effect.",
"Clearing Preferences",
JOptionPane.PLAIN_MESSAGE);
}
/** pane displaying Jskad's single HTML help file */ /** pane displaying Jskad's single HTML help file */
private static HTMLPane helpPane; private static HTMLPane helpPane;
@ -294,6 +315,17 @@ public class Jskad extends JPanel implements DocumentListener {
editMenu.add(preferencesItem); editMenu.add(preferencesItem);
} }
{
JMenuItem preferencesItem = new JMenuItem("Clear preferences");
preferencesItem.addActionListener(new ThdlActionListener() {
public void theRealActionPerformed(ActionEvent e) {
clearPreferencesAction();
}
});
editMenu.addSeparator();
editMenu.add(preferencesItem);
}
menuBar.add(editMenu); menuBar.add(editMenu);
JMenu toolsMenu = new JMenu("Tools"); JMenu toolsMenu = new JMenu("Tools");
@ -1172,7 +1204,8 @@ public class Jskad extends JPanel implements DocumentListener {
y_size = d.height/4*3; y_size = d.height/4*3;
f.setSize(x_size, y_size); f.setSize(x_size, y_size);
f.setLocation(d.width/8, d.height/8); f.setLocation(d.width/8, d.height/8);
f.getContentPane().add(new Jskad(f)); final Jskad jskad = new Jskad(f);
f.getContentPane().add(jskad);
f.setVisible(true); f.setVisible(true);
/* Make it so that any time the user exits Jskad by /* Make it so that any time the user exits Jskad by
@ -1181,7 +1214,7 @@ public class Jskad extends JPanel implements DocumentListener {
* correct. */ * correct. */
Runtime.getRuntime().addShutdownHook(new Thread() { Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() { public void run() {
savePreferencesAction(); jskad.savePreferencesAction();
} }
} }
); );

View file

@ -372,12 +372,40 @@ public final class ThdlOptions {
} }
} }
/** Deletes the user's preferences file, a file whose path is the
* value of {@link #getUserPreferencesPath()}. You must restart
* the application before this will take effect.
*
* @throws IOException if an IO or security exception occurs
* while deleting from disk. */
public static void clearUserPreferences() throws IOException {
// Avoid saving the preferences to disk at program exit. Do
// this first in case an exception is thrown during deletion.
userProperties = null;
File pf = new File(getUserPreferencesPath());
try {
if (pf.exists() && !pf.delete()) {
throw new IOException("Could not delete the preferences file "
+ pf.getAbsolutePath());
}
} catch (SecurityException e) {
throw new IOException("Could not delete the preferences file because a SecurityManager is in place and your security policy does not allow deleting "
+ pf.getAbsolutePath());
}
}
/** Saves the user's preferences to a file whose path is the value /** Saves the user's preferences to a file whose path is the value
* of {@link #getUserPreferencesPath()}. You must call * of {@link #getUserPreferencesPath()}. You must call
* <code>setUserPreference(..)</code> for this to be effective. * <code>setUserPreference(..)</code> for this to be effective.
*
* @return true on success, false if no preferences exist to be
* saved, such as after calling clearUserPreferences()
*
* @throws IOException if an IO exception occurs while writing to * @throws IOException if an IO exception occurs while writing to
* the disk. */ * the disk. */
public static void saveUserPreferences() throws IOException { public static boolean saveUserPreferences() throws IOException {
if (null != userProperties) { if (null != userProperties) {
userProperties.store(new FileOutputStream(getUserPreferencesPath()), userProperties.store(new FileOutputStream(getUserPreferencesPath()),
" This file was automatically created by a THDL tool.\n" " This file was automatically created by a THDL tool.\n"
@ -400,7 +428,9 @@ public final class ThdlOptions {
+ "# preferences mechanism at this time. Yell for it!\n" + "# preferences mechanism at this time. Yell for it!\n"
+ "# \n" + "# \n"
+ "# Created at:"); // DLC FIXME: document the preferences mechanism. + "# Created at:"); // DLC FIXME: document the preferences mechanism.
return true;
} }
return false;
} }
/** This returns the location of the user's preferences file. /** This returns the location of the user's preferences file.