1    /*
2    Copyright 2001 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    import java.io.*;
7    import java.awt.*;
8    import java.awt.geom.*;
9    import java.util.*;
10   import javax.swing.*;
11   
12   class Persist implements Serializable{
13     private static final long serialVersionUID = Version.getSUID() + 1;
14   
15     static File statefile = new File(Carto.filebase,"persist.sta");
16   
17     public static Persist current = new Persist();
18   
19     public static void save() {
20       current.saveState();
21     }
22   
23     public void saveState() {
24       if (chooser!=null) currentdir = chooser.getCurrentDirectory().toString();
25       prefversion = Carto.runversion;
26       try {
27         FileOutputStream ostream = new FileOutputStream(statefile);
28         ObjectOutputStream out = new ObjectOutputStream(ostream);
29         out.writeObject(this);
30         out.flush();
31         ostream.close();
32       } catch (Exception e) {
33         ErrorLog.log("Error writing persistant data to "+statefile);
34         ErrorLog.exception(e);
35       }
36     }
37   
38     public static void restore() {
39       Object result = null;
40       try {
41         FileInputStream istream = new FileInputStream(statefile);
42         ObjectInputStream in = new ObjectInputStream(istream);
43         result = in.readObject();
44         istream.close();
45       } catch (Exception e) {
46   //      if (!(e instanceof FileNotFoundException)) {
47         ErrorLog.exception(e,"Error reading persistant data from "+statefile+
48           		 "\nGenerating new defalts"+
49           		 "\n(New versions sometimes cause this).");
50   //      } else ErrorLog.exception(e,"Unexprected Error reading persistant data from "+statefile+
51   //        			"\nGenerating new defalts"+
52   
53         result=null;
54       }
55       if (result==null || !(result instanceof Persist)) result = new Persist();
56       current = (Persist)result;
57       current.runid++;
58   
59       Persist update = new Persist();
60       // If new prefs have been added add them to the list.
61       // This only works if pref order stays the same.
62       if (update.prefs.length > current.prefs.length) {
63         PrefEditor[] oldprefs = current.prefs;
64         current.prefs = update.prefs;
65         for (int i=0;i<oldprefs.length;i++)
66           current.prefs[i] = oldprefs[i];
67       }
68       for (int i = 0; i<current.prefs.length;i++) {
69         if (current.prefs[i] instanceof PrefPage)   // Back compat RLH 4/27/01
70           current.prefs[i] = new PrefPageNew((PrefPage)current.prefs[i]);
71         current.prefs[i].apply();
72       }
73   
74       Prefs.prefs = current.prefs;
75     }
76   
77     public String currentdir = null;
78     public int runid = 0;
79     public File last = null;
80   
81     public PrefEditor[] prefs;
82   
83     private transient JFileChooser chooser = null;
84   
85     public Version prefversion = Carto.runversion;
86   
87     public Persist() {
88       currentdir = Carto.filebase.toString();
89       runid = 0;
90       prefs = new PrefEditor[] {
91         new PrefGeneral(),
92         new PrefSeg(),
93         new PrefComp(),
94         new PrefPageNew(),
95         new PrefMorph(),
96         new PrefImage(),
97         new PrefUnits(),
98         new PrefCalibScreen(),
99         new PrefCalibImage(),
100        new PrefSize(),
101        new PrefLayer(),
102        new PrefFont(),
103        new PrefDebug(),
104        new Print(),
105        new PrefArrow()
106          };
107  
108    }
109  
110    public JFileChooser getFileChooser() {
111      if (chooser!=null) return(chooser);
112      return(chooser = new JFileChooser(currentdir));
113    }
114  
115    public void clearChooser() {
116      if (chooser==null) return;
117      currentdir = chooser.getCurrentDirectory().toString();
118      chooser = null;
119    }
120  
121    public int getRunID() {return(runid);}
122  
123  }
124  
125