1    /*
2    Copyright 2000 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    import javax.swing.*;
7    import javax.swing.event.*;
8    import java.io.*;
9    import java.awt.event.*;
10   import java.awt.*;
11   
12   /**Each editstate records the state of an open editor window.
13    *Used to reopen windows as they were when the file was last closed. */
14   public class EditState implements Serializable{
15   
16     private static final long serialVersionUID = Version.getSUID();
17   
18     /**True if editor was closed. Closed windows should be removed from
19      *the desktop, so this shouldn't happen. If it does, nothing is recorded */
20     boolean closed = false;
21   
22     /**True if editor ws maximized */
23     boolean maximized = false;
24   
25     /**True if editor ws minimized */
26     boolean minimized = false;
27   
28     /**The layer of the editor. Saving this may help put the right window on top. */
29     int layer;
30   
31     /**The position of the editor on the desktop. */
32     Rectangle bounds; 
33   
34     /**What is being edited. A segment of composite */
35     Editable target;
36   
37     /**The current scale setting. */
38     double scale = 1;
39   
40     /**The rotation of the editor. Undefined for segments. */
41     double rotation = 0;
42   
43     /**The position of the scrollpane of this editor */
44     Point view;
45   
46     /**Record the states of all editors on a dektop. */
47     static EditState[] saveEditors(JDesktopPane pane) {
48   
49       JInternalFrame[] editors = pane.getAllFrames();
50       EditState[] res = new EditState[editors.length];
51   
52       for (int i=0;i<editors.length;i++)
53         res[i] = new EditState((EditFrame)editors[i]);
54       
55       return(res);
56     }
57   
58     /**Build an EditState recording the state of an editor. */
59     public EditState(EditFrame frame) {
60   
61       if (frame.isClosed()) {
62         closed=true;
63         return;
64       }
65   
66       if (frame.isIcon()) minimized = true;
67       if (frame.isMaximum()) maximized = true;
68       layer = frame.getLayer();
69       bounds = frame.getBounds(null);
70   
71       target = frame.edit.target;
72       view = frame.edit.scroll.getViewport().getViewPosition();
73       scale = frame.edit.scale.fact;
74       if (frame.edit.rose!=null) rotation = frame.edit.rose.theta;
75     }
76   
77     /**Restore the state of our editor.
78      *Add it to the CartoFrame. and set it to be the way it was when last saved. */
79     public void restore(CartoFrame frame) {
80       if (closed) return;
81   
82       if (!frame.carto.complist.contains(target) &&
83           !frame.carto.segmentlist.contains(target)) return;
84   
85       Editor edit = frame.addEditor(target);
86   
87       try {
88         edit.editframe.setBounds(bounds);
89         edit.editframe.setLayer(layer);
90         edit.editframe.setMaximum(maximized);
91         edit.editframe.setIcon(minimized);
92       } catch (java.beans.PropertyVetoException ex) {
93         ErrorLog.exception(ex,"Couldn't set editor state");
94       }
95   
96       edit.scale.change(scale);
97       if (edit.rose!=null) edit.rose.change(rotation);
98       
99       edit.scroll.getViewport().setViewPosition(view);
100    }
101  
102    /**Restore all the edits in the frame */
103    public static void restoreAll(EditState[] edits,CartoFrame frame) {
104      for (int i = 0;i<edits.length;i++)
105        edits[i].restore(frame);
106    }
107  }
108