Commit 3c6b47d5 authored by firegnom's avatar firegnom
Browse files

working on unified way of saving prefferences

parent 6803dd94
......@@ -39,6 +39,7 @@ import mvplan.gas.Gas;
import mvplan.model.AbstractModel;
import mvplan.model.ModelDAO;
import mvplan.prefs.PrefsXMLDAO;
import mvplan.prefs.PrefsXStreamDAO;
import mvplan.segments.SegmentDive;
import mvplan.segments.SegmentAbstract;
import mvplan.updater.*;
......@@ -1043,10 +1044,10 @@ public class MainFrame extends JFrame
if(lastModelFile!=null)
Mvplan.prefs.setLastModelFile(lastModelFile.toString());
PrefsXMLDAO dao=new PrefsXMLDAO(Mvplan.prefFile);
PrefsXStreamDAO dao=new PrefsXStreamDAO(Mvplan.prefFile);
try {
if(Mvplan.DEBUG>0) System.out.println("Writing prefs to: "+Mvplan.prefFile);
dao.setPrefs(Mvplan.prefs);
dao.savePrefs(Mvplan.prefs);
} catch (Exception ex) {
// TODO no try block here.
JOptionPane.showMessageDialog((this),
......
......@@ -33,6 +33,7 @@ import mvplan.gui.MainFrame;
import mvplan.prefs.Prefs;
import mvplan.prefs.PrefsException;
import mvplan.prefs.PrefsXMLDAO;
import mvplan.prefs.PrefsXStreamDAO;
import mvplan.util.*;
import javax.swing.UIManager;
......@@ -119,10 +120,10 @@ 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
PrefsXMLDAO dao=new PrefsXMLDAO(prefFile);
PrefsXStreamDAO dao=new PrefsXStreamDAO(prefFile);
if (Mvplan.DEBUG >0 ) System.out.println("Restoring preferences from "+prefFile);
try {
prefs = dao.getPrefs();
prefs = dao.loadPrefs();
} catch (PrefsException e) {
System.err.println("Mvplan: error, could not load preferences file :"+e.getLocalizedMessage());
e.printStackTrace();
......@@ -131,7 +132,9 @@ public class Mvplan implements IMvplan
if (prefs == null) {
prefs = new Prefs();
prefs.setDefaultPrefs();
}
}
MvplanInstance.setPrefs(prefs);
// Open main screen
frame = new MainFrame();
......@@ -176,4 +179,8 @@ public class Mvplan implements IMvplan
return Mvplan.DEBUG;
}
public void setPrefs(Prefs p) {
Mvplan.prefs = p;
}
}
......@@ -29,6 +29,9 @@ import java.beans.*;
import mvplan.prefs.Prefs;
/**
* This implementation of Prefs dao is saving preferences to xml file , it uses
* XMLEncoder and XMLDecoder to save preferences .
*
*
* @author Guy
*/
......@@ -36,46 +39,25 @@ public class PrefsXMLDAO implements PrefsDAO {
private final String fileName;
/**
* Creates a new instance of PrefsXMLDAO
* Creates a new instance of PrefsXMLDAO
* @param filename filename where preferences will be saved or loaded
*/
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
/**
* Save Prefs object to XML file using XMLEncoder
*/
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 {
public void savePrefs(Prefs p) throws PrefsException {
if (fileName == null) {
throw new PrefsException("Could not save preferences");
}
try {
setPrefs(p, fileName);
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(fileName)));
encoder.writeObject(p);
encoder.close();
} catch (FileNotFoundException e) {
throw new PrefsException("Could not save preferences", e);
} catch (SecurityException e) {
......@@ -84,12 +66,20 @@ public class PrefsXMLDAO implements PrefsDAO {
}
public Prefs getPrefs() throws PrefsException {
/**
* Load Prefs object from XML file using XMLDecoder
*/
public Prefs loadPrefs() throws PrefsException {
if (fileName == null) {
throw new PrefsException("Could not save preferences");
}
try {
return getPrefs(fileName);
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 (FileNotFoundException e) {
throw new PrefsException("Could not save preferences", e);
......
......@@ -5,13 +5,20 @@
<artifactId>mvplan_core</artifactId>
<packaging>jar</packaging>
<name>MV-Plan Lib</name>
<name>MV-Plan Lib</name>
<parent>
<groupId>mvplan</groupId>
<artifactId>mvplan-parent</artifactId>
<version>1.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
<groupId>mvplan</groupId>
<artifactId>mvplan-parent</artifactId>
<version>1.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.2</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -86,7 +86,7 @@ public class TextProfilePrinter extends ProfilePrinter <StringBuffer>{
// Print settings heading
textArea.append(mvplan.getResource("mvplan.gui.text.ProfilePrinter.settings.text")+"="+
(int)Math.round(MvplanInstance.getPrefs().getGfLow()*100.)+"-"+(int)Math.round(MvplanInstance.getPrefs().getGfHigh()*100.));
textArea.append(" "+ mvplan.getResource("mvplan.gui.text.ProfilePrinter.factors.text") + MvplanInstance.getPrefs().getPrefs().getFactorComp() + "/"+ MvplanInstance.getPrefs().getPrefs().getFactorDecomp());
textArea.append(" "+ mvplan.getResource("mvplan.gui.text.ProfilePrinter.factors.text") + MvplanInstance.getPrefs().getFactorComp() + "/"+ MvplanInstance.getPrefs().getFactorDecomp());
if (MvplanInstance.getPrefs().getGfMultilevelMode())
textArea.append(" "+mvplan.getResource("mvplan.gui.text.ProfilePrinter.multilevel.text"));
textArea.append(" "+mvplan.getResource("mvplan.gui.text.ProfilePrinter.pph2o.text")+"="+
......@@ -155,8 +155,8 @@ public class TextProfilePrinter extends ProfilePrinter <StringBuffer>{
// Print settings heading
textArea.append(mvplan.getResource("mvplan.gui.text.ProfilePrinter.settings.text")+"="+
(int)Math.round(MvplanInstance.getPrefs().getGfLow()*100.)+"-"+(int)Math.round(MvplanInstance.getPrefs().getGfHigh()*100.));
if( MvplanInstance.getPrefs().getPrefs().isUsingFactors())
textArea.append(" "+ mvplan.getResource("mvplan.gui.text.ProfilePrinter.factors.text") + MvplanInstance.getPrefs().getPrefs().getFactorComp() + "/"+ MvplanInstance.getPrefs().getPrefs().getFactorDecomp());
if( MvplanInstance.getPrefs().isUsingFactors())
textArea.append(" "+ mvplan.getResource("mvplan.gui.text.ProfilePrinter.factors.text") + MvplanInstance.getPrefs().getFactorComp() + "/"+ MvplanInstance.getPrefs().getFactorDecomp());
if (MvplanInstance.getPrefs().getGfMultilevelMode())
textArea.append(" "+mvplan.getResource("mvplan.gui.text.ProfilePrinter.multilevel.text"));
textArea.append(" "+profile.getModel().getModelName());
......
......@@ -33,7 +33,6 @@ import mvplan.prefs.Prefs;
public class Gas implements Comparable, Serializable, Cloneable
{
private static Prefs prefs = MvplanInstance.getPrefs();
private double fHe,fO2; // Gas fractions
private double mod; // Maximum operating depth
private double volume; // Accumulate gas volume
......@@ -73,6 +72,7 @@ public class Gas implements Comparable, Serializable, Cloneable
*/
public static boolean validate(double fHe, double fO2, double mod) {
boolean passed=true;
Prefs prefs = MvplanInstance.getPrefs();
// Check individual fields for bounds
passed = (passed && validate("fHe",fHe));
passed = (passed && validate("fO2",fO2));
......@@ -96,18 +96,21 @@ public class Gas implements Comparable, Serializable, Cloneable
* Method to get a maximum MOD based on O2 fraction
*/
public static double getMaxMod(double o) {
Prefs prefs = MvplanInstance.getPrefs();
return (prefs.getMaxMOD()/o * prefs.getPConversion())-prefs.getPConversion();
}
/*
* Method to get a MOD based on O2 fraction and maximum ppO2
*/
public static double getMod(double fO2, double ppO2) {
Prefs prefs = MvplanInstance.getPrefs();
return (ppO2/fO2 * prefs.getPConversion())-prefs.getPConversion();
}
/*
* Method to get a ppO2 based on O2 fraction and MOD
*/
public static double getppO2(double f, double m) {
Prefs prefs = MvplanInstance.getPrefs();
return ( (m+prefs.getPConversion())*f/prefs.getPConversion());
}
......
......@@ -16,6 +16,7 @@ public interface IMvplan {
public String getResource(String res);
public String getAppName();
public Prefs getPrefs();
public void setPrefs(Prefs p);
public int getDebug();
public void init();
}
......@@ -33,6 +33,11 @@ public class MvplanInstance {
}
return null;
}
public static void setPrefs(Prefs p) {
if (mvplan != null) {
mvplan.setPrefs(p);
}
}
public static IMvplan getMvplan() {
return mvplan;
......
......@@ -44,8 +44,8 @@ public class Compartment implements Serializable
// Initialise compartment pressures to zero
ppHe=0.0d;
ppN2=0.0d;
factorComp=Prefs.getPrefs().getFactorComp();
factorDecomp=Prefs.getPrefs().getFactorDecomp();
factorComp=MvplanInstance.getPrefs().getFactorComp();
factorDecomp=MvplanInstance.getPrefs().getFactorDecomp();
}
/**
......
......@@ -41,7 +41,6 @@ import mvplan.util.PressureConverter;
public class Prefs implements Serializable
{
private static Prefs thisPrefs;
public final static int METRIC=0;
public final static int IMPERIAL=1;
......@@ -115,14 +114,7 @@ public class Prefs implements Serializable
private String modelClass; // Deco model class name
public Prefs() {
thisPrefs=this;
}
// Singleton pattern
public static Prefs getPrefs() {
return thisPrefs;
}
public void setDefaultPrefs()
{
......
......@@ -25,11 +25,11 @@ public interface PrefsDAO {
/**
* Persists Prefs object
*/
public void setPrefs(Prefs p) throws PrefsException;
public void savePrefs(Prefs p) throws PrefsException;
/**
* Read Prefs object
*/
public Prefs getPrefs() throws PrefsException;
public Prefs loadPrefs() throws PrefsException;
}
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* The GNU General Public License can be read at http://www.gnu.org/licenses/licenses.html
*/
package mvplan.prefs;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import mvplan.gas.Gas;
import mvplan.segments.SegmentDive;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
/**
*
* @author Maciej Kaniewski
*/
public class PrefsXStreamDAO implements PrefsDAO {
private final String fileName;
private final XStream x;
/**
* Creates a new instance of PrefsXStreamDAO
*/
public PrefsXStreamDAO(String filename) {
this.fileName = filename;
x = new XStream(new DomDriver());
x.alias("Preferences", Prefs.class);
x.alias("Gas", Gas.class);
x.alias("SegmentDive", SegmentDive.class);
}
/**
* Save Preferences to XML file using XStream library, file is defined in constructor
*/
public void savePrefs(Prefs p) throws PrefsException {
if (fileName == null) {
throw new PrefsException("Could not save preferences");
}
try {
x.toXML(p, new BufferedOutputStream(new FileOutputStream(fileName)));
} catch (FileNotFoundException e) {
throw new PrefsException("Could not save preferences", e);
}
}
/**
* Load Preferences from XML file using XStream library file is defined in constructor
*/
public Prefs loadPrefs() throws PrefsException {
if (fileName == null) {
throw new PrefsException("Could not save preferences");
}
try {
Prefs p = (Prefs) x.fromXML(new BufferedInputStream(
new FileInputStream(fileName)));
p.validatePrefs(); // Check that all is within bounds
return p;
} catch (FileNotFoundException e) {
throw new PrefsException("Could not save preferences", e);
}
catch (RuntimeException 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