/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.gui.frames;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import pdfdb.app.EventController;
import pdfdb.gui.preferencepanes.AdvancedPane;
import pdfdb.gui.preferencepanes.BottomPane;
import pdfdb.gui.preferencepanes.GeneralPane;
import pdfdb.gui.preferencepanes.SecurityPane;
import pdfdb.settings.UserSettingsManager;
/** The user preferences frame.
* @author ug22cmg */
public class PreferencesFrame extends AppDialog
{
private GeneralPane generalPane;
private SecurityPane securityPane;
private AdvancedPane advancedPane;
/** Initializes the frame as a dialog of 'frame'.
* @param frame The parent frame. */
public PreferencesFrame(Frame frame)
{
super(frame);
}
/** Initializes the components from configuration settings. */
@Override
protected void initializeComponents()
{
initFromSettings();
}
/** Loads settings from the configuration file.*/
private void initFromSettings()
{
UserSettingsManager manager = UserSettingsManager.getInstance();
String searchLocation = manager.get("SEARCH_LOCATION");
String servicesLocation = manager.get("SERVICES_LOCATION");
boolean savePswd = false;
savePswd = Boolean.getBoolean(manager.get("SAVE_PASSWORDS"));
init(searchLocation, savePswd, servicesLocation);
}
/** Performs the actual initialization from raw values.
* @param searchLocation The search location.
* @param savePswd Whether to save the password.
* @param servicesLocation The services file location. */
private void init(String searchLocation, boolean savePswd, String servicesLocation)
{
ActionListener a = EventController.getActionListener();
Container contentPane = super.getContentPane();
JTabbedPane tabbedPane = new JTabbedPane();
BottomPane bottomPane = new BottomPane();
String cmd1 = String.valueOf(EventController.OP_PREFS_CONFIRM);
String cmd2 = String.valueOf(EventController.OP_PREFS_CANCEL);
bottomPane.getApproveButton().setActionCommand(cmd1);
bottomPane.getRejectButton().setActionCommand(cmd2);
bottomPane.getApproveButton().addActionListener(a);
bottomPane.getRejectButton().addActionListener(a);
this.generalPane = new GeneralPane(searchLocation);
this.securityPane = new SecurityPane(savePswd);
this.advancedPane = new AdvancedPane(servicesLocation);
super.setTitle("Preferences");
super.setResizable(false);
super.setSize(new Dimension(500, 400));
super.setPreferredSize(new Dimension(500, 400));
contentPane.setLayout(new BorderLayout());
contentPane.add(tabbedPane, BorderLayout.CENTER);
contentPane.add(bottomPane, BorderLayout.SOUTH);
tabbedPane.addTab("General", generalPane);
tabbedPane.addTab("Security", securityPane);
tabbedPane.addTab("Advanced", advancedPane);
tabbedPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
}
/** Manages a cancel operation */
public void cancel()
{
setResult(REJECTED);
setVisible(false);
}
/** Manages a save operation.
* @throws java.io.IOException If an error occurs while writing to
* the disk. */
public void save() throws IOException
{
File searchLocation = new File(generalPane.getFileLocation());
File servicesLocation = new File(advancedPane.getServicesLocation());
boolean savePswd = securityPane.arePasswordsSaved();
// Perform validation
boolean valid = searchLocation.exists() && searchLocation.isDirectory();
valid = valid && servicesLocation.exists() && servicesLocation.isFile();
if (valid)
{
// Save
UserSettingsManager manager = UserSettingsManager.getInstance();
manager.set("SEARCH_LOCATION", searchLocation.getAbsolutePath());
manager.set("SERVICES_LOCATION", servicesLocation.getAbsolutePath());
manager.set("SAVE_PASSWORDS", String.valueOf(savePswd));
manager.save();
setResult(APPROVED);
setVisible(false);
}
else
{
JOptionPane.showMessageDialog(this,
"Please ensure all files exist and try again.");
}
}
}