Commit 6803dd94 authored by firegnom's avatar firegnom
Browse files

modified prefs dao to be interface so it can be now have different implementations

parent f30e2ab2
......@@ -38,7 +38,7 @@ import mvplan.dive.*;
import mvplan.gas.Gas;
import mvplan.model.AbstractModel;
import mvplan.model.ModelDAO;
import mvplan.prefs.PrefsDAO;
import mvplan.prefs.PrefsXMLDAO;
import mvplan.segments.SegmentDive;
import mvplan.segments.SegmentAbstract;
import mvplan.updater.*;
......@@ -1043,10 +1043,10 @@ public class MainFrame extends JFrame
if(lastModelFile!=null)
Mvplan.prefs.setLastModelFile(lastModelFile.toString());
PrefsDAO dao=new PrefsDAO();
PrefsXMLDAO dao=new PrefsXMLDAO(Mvplan.prefFile);
try {
if(Mvplan.DEBUG>0) System.out.println("Writing prefs to: "+Mvplan.prefFile);
dao.setPrefs(Mvplan.prefs,Mvplan.prefFile);
dao.setPrefs(Mvplan.prefs);
} catch (Exception ex) {
// TODO no try block here.
JOptionPane.showMessageDialog((this),
......
......@@ -31,7 +31,8 @@ import java.util.ResourceBundle;
import javax.swing.JFrame;
import mvplan.gui.MainFrame;
import mvplan.prefs.Prefs;
import mvplan.prefs.PrefsDAO;
import mvplan.prefs.PrefsException;
import mvplan.prefs.PrefsXMLDAO;
import mvplan.util.*;
import javax.swing.UIManager;
......@@ -118,9 +119,14 @@ public class Mvplan implements IMvplan
// Restore last state from preferences
prefFile = System.getProperty("user.dir")+System.getProperty("file.separator")+PREF_FILE;
// Use Data Access Object for reading preferences from XML encoded file
PrefsDAO dao=new PrefsDAO();
PrefsXMLDAO dao=new PrefsXMLDAO(prefFile);
if (Mvplan.DEBUG >0 ) System.out.println("Restoring preferences from "+prefFile);
prefs = dao.getPrefs(prefFile);
try {
prefs = dao.getPrefs();
} catch (PrefsException e) {
System.err.println("Mvplan: error, could not load preferences file :"+e.getLocalizedMessage());
e.printStackTrace();
}
// Create new preferences if no file found
if (prefs == null) {
prefs = new Prefs();
......
......@@ -29,44 +29,71 @@ import java.beans.*;
import mvplan.prefs.Prefs;
/**
*
*
* @author Guy
*/
public class PrefsDAO {
/*
* Creates a new instance of PrefsDAO
*/
public PrefsDAO() {
}
/*
* Persists Prefs object to XML
*/
public void setPrefs(Prefs p, String fileName) throws FileNotFoundException, SecurityException {
XMLEncoder encoder = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream(fileName)) );
encoder.writeObject(p);
encoder.close();
}
/*
* Read Prefs object from XML file
*/
public Prefs getPrefs(String fileName){
try{
XMLDecoder decoder = new XMLDecoder(
new BufferedInputStream(
new FileInputStream(fileName)));
Prefs p = (Prefs)decoder.readObject();
decoder.close();
p.validatePrefs(); // Check that all is within bounds
return p;
} catch (Exception ex) {
System.out.println("Error reading prefs: "+ex);
return null;
}
}
public class PrefsXMLDAO implements PrefsDAO {
private final String fileName;
/**
* Creates a new instance of PrefsXMLDAO
*/
public PrefsXMLDAO() {
fileName = null;
}
public PrefsXMLDAO(String filename) {
this.fileName = filename;
}
/*
* Persists Prefs object to XML
*/
private void setPrefs(Prefs p, String fileName)
throws FileNotFoundException, SecurityException {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(fileName)));
encoder.writeObject(p);
encoder.close();
}
/*
* Read Prefs object from XML file
*/
private Prefs getPrefs(String fileName) throws FileNotFoundException {
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
new FileInputStream(fileName)));
Prefs p = (Prefs) decoder.readObject();
decoder.close();
p.validatePrefs(); // Check that all is within bounds
return p;
}
public void setPrefs(Prefs p) throws PrefsException {
if (fileName == null) {
throw new PrefsException("Could not save preferences");
}
try {
setPrefs(p, fileName);
} catch (FileNotFoundException e) {
throw new PrefsException("Could not save preferences", e);
} catch (SecurityException e) {
throw new PrefsException("Could not save preferences", e);
}
}
public Prefs getPrefs() throws PrefsException {
if (fileName == null) {
throw new PrefsException("Could not save preferences");
}
try {
return getPrefs(fileName);
} catch (FileNotFoundException e) {
throw new PrefsException("Could not save preferences", e);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment