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.awt.geom.*;
7    import java.awt.*;
8    import java.awt.Graphics2D.*;
9    import java.awt.print.*;
10   import java.util.*;
11   import java.io.*;
12   
13   public class Page extends Box {
14   
15     private static final long serialVersionUID = Version.getSUID();
16   
17     public static double PRINTWIDTH = 1;
18   //  public static String DEFAULTPAGENAME = "Page0";
19   
20     public static int WIDTHMODE = 0;
21     public static int HEIGHTMODE = 1;
22     public static int PIXELMODE = 2;
23     public static int SCALEMODE = 3;
24   
25   //  public static Rectangle2D getDefaultBox() {
26   
27   //  }
28   
29     static Page defpage = null;
30   
31     boolean printer;
32     boolean index;
33     boolean image;
34     double resolution;
35     double presolution;
36     double aspect = -1;      // obsolete 1/27/01
37     int ressetting;
38     int pressetting;
39     Unit printerunit;
40     Unit imageunit;
41     Unit mapunit;
42     File file;
43   
44     Stacking stack = null;
45     boolean usestack = false;
46   
47     public Comp comp = null;
48   
49     public Page() {
50       super(new Point2D.Double(0,0),null,null);
51   //new
52   //      
53       printer = false;
54       index = false;
55       image = true;
56       resolution = 1.0;
57       presolution = 1.0;
58       ressetting = Page.SCALEMODE;
59       pressetting = Page.SCALEMODE;
60       file = new File("");
61       imageunit = Unit.inch;
62       printerunit = Unit.inch;
63       mapunit = Unit.foot;
64     }
65   
66     public Page(Point2D where,View view,Object arg) {
67       super(where,view,null);
68   //((arg!=null && arg instanceof Rectangle2D)?arg:
69   //        	      new Rectangle2D.Double(-PRINTWIDTH/2,-defpage.subaspect*PRINTWIDTH/2,
70   //        				     PRINTWIDTH,defpage.subaspect*PRINTWIDTH)));
71       copy(defpage);
72       reshape();
73       comp = (Comp)arg;
74     }
75     
76     public void copy(Page that) {
77       printerunit = that.printerunit;
78       imageunit = that.imageunit;
79       mapunit = that.mapunit;
80       printer = that.printer;
81       index=that.index;
82       image=that.image;
83       resolution = that.resolution;
84       presolution = that.presolution;
85       ressetting=that.ressetting;
86       pressetting=that.pressetting;
87       file=that.file;
88   
89       super.copy(that);
90     }
91   
92   /*
93     public void setName(String name){
94       if (DEFAULTPAGENAME==null || 0<name.compareTo(DEFAULTPAGENAME)) {
95         DEFAULTPAGENAME = name;
96         Persist.save();
97       }
98       super.setName(name);
99     }
100  */
101  
102  /*
103    static String incSufix(String str) {
104      if (str==null || str.length()==0) return(DEFAULTPAGENAME);
105      if (str.toLowerCase().endsWith(".png"))
106        str = str.substring(0,str.length()-4);
107      if (str==null || str.length()==0) return(DEFAULTPAGENAME);
108      int  i;
109      for (i=str.length()-1;i>=0;i--)
110        if (!Character.isDigit(str.charAt(i))) break;
111      if (i==str.length()-1) return(str+"0");
112      int num= -1;
113      try {
114        if (i>=0) num = Integer.parseInt(str.substring(i+1,str.length()));
115        else return(""+(Integer.parseInt(str)+1));
116      } catch (NumberFormatException e){num= -1;}
117      return(str.substring(0,i+1)+(num+1));
118    }
119  */
120  
121    public void print(View view,PageFormat format) {
122      view.trans.translate(format.getImageableX()/*+format.getImageableWidth()/2.0*/,
123        -format.getImageableY()/*-format.getImageableHeight()/2.0*/);
124      view.trans.scale(format.getImageableWidth()/box.getWidth(),
125          	     format.getImageableWidth()/box.getWidth());
126      view.trans.translate(-box.getX(),box.getY());
127      System.out.println("printing "+this);
128      try {
129        view.trans.concatenate(anchorpoint.position.createInverse());
130      } catch (NoninvertibleTransformException e) {ErrorLog.exception(e);}
131  //    System.out.println("my map unit = "+mapunit+" survey map unit = "+view.mapunit);
132  
133      view.scale=format.getImageableWidth()/getPosScale(mapunit);
134      view.mapscale = new MapScale(view.scale,
135          			 Unit.pixel,
136          			 view.mapunit,
137          			 Print.imagescale);
138  //    System.out.println("---------- scale = "+view.mapscale);
139  
140      view.angle = -anchorpoint.position.angle;
141      view.printing = !index;
142  //    showSelected(view);
143      if (comp.showall) view.visible = Layer.all;
144      else view.visible = comp.visible;
145  
146      if (stack!=null && usestack)
147        view.stacking = stack;
148      else
149        view.stacking = comp.stack;
150  
151  //    comp.viewChanged();
152      comp.paint(view);
153  
154      comp.getSurvey().paint(view);
155    }
156  
157    public void print(View view,PageFormat format,int lines) {
158      view.trans.translate(0,lines);
159      print(view,format);
160    }
161  
162    public PageFormat getFormat(Unit globalmapunit) {
163      PageFormat form = new PageFormat();
164      Paper paper = new Paper();
165      Dimension size = getSize(globalmapunit);
166      paper.setSize(size.getWidth(),size.getHeight());
167      paper.setImageableArea(0,0,size.getWidth(),size.getHeight());
168      form.setPaper(paper);
169      return(form);
170    }
171  
172  //  public Dimension getSize(){
173  //    System.out.println("imageunit = "+imageunit+" imagescale = "+Print.imagescale);
174  //    return(getSize(ressetting,resolution,Unit.pixel.toUnit(imageunit,Print.imagescale)));
175  //  }
176  
177    public Dimension getSize(Unit globalmapunit){
178  //    System.out.println("imageunit = "+imageunit+" imagescale = "+Print.imagescale);
179      return(getSize(ressetting,
180          	   resolution,
181          	   globalmapunit,
182          	   Unit.pixel.toUnit(imageunit,Print.imagescale)));
183    }
184  
185    public Dimension getPSSize(Unit globalmapunit){
186  //    System.out.println("imageunit = "+imageunit+" imagescale = "+Print.imagescale);
187      return(getSize(ressetting,
188          	   resolution,
189          	   globalmapunit,
190          	   Unit.inch.toUnit(imageunit,Print.imagescale)));
191    }
192  
193    public Dimension getPrintSize(Unit globalmapunit){
194      return(getSize(pressetting,presolution,globalmapunit,Unit.inch.toUnit(printerunit)));
195    }
196  
197    public Dimension getSize(int set,double res,Unit globalmapunit,double unit) {
198      if (set==WIDTHMODE) return(new Dimension((int)(res*unit),(int)(res*unit*subaspect)));
199      if (set==HEIGHTMODE) return(new Dimension((int)(res*unit/subaspect),(int)(res*unit)));
200  
201      double scale = 1;
202  
203      if (set==PIXELMODE)
204        scale = getPosScale(globalmapunit)*res/unit;
205  
206      if (set==SCALEMODE)
207        scale = getPosScale(globalmapunit)/res;
208  
209  //    System.out.println("scale = "+scale+" unit = "+unit+" aspect = "+aspect);
210  
211      return(new Dimension((int)(scale*unit),(int)(scale*unit*subaspect)));
212    }
213  
214    public double getPosScale(Unit globalmapunit) {
215      return(box.getWidth()*mapunit.toUnit(globalmapunit)*
216             Math.sqrt(anchorpoint.position.getScaleX()*anchorpoint.position.getScaleY() -
217          	     anchorpoint.position.getShearX()*anchorpoint.position.getShearY()));
218    }
219  
220    public File getFile() {
221      File res = file;
222      if (res==null) res = PrefPage.pickFile();
223      if (res==null) res = new File("");
224      if (!res.getName().toLowerCase().endsWith(".png"))
225        res = new File(res.toString().concat(".png"));
226      return(res);
227    }
228  
229    public void paint(View view) {
230      if (!view.visible.isMember(this)) return;
231      if (!view.printing) super.paint(view);
232    }
233  
234    public int getLevel() {return(super.getLevel()+2);}
235  
236    transient PrefPage prefs = null;
237    transient javax.swing.JCheckBox usestackbox = null;
238  
239    public void getPropertyEdit(Object[] edits,int slot,Set sub, Symbol parent) {
240      prefs = new PrefPage(this);
241      edits[slot] = prefs.getPane(null);
242      ((Component)edits[slot]).setName("Page");
243  
244      Container sbox = new javax.swing.Box(javax.swing.BoxLayout.Y_AXIS);
245  
246      usestackbox = new javax.swing.JCheckBox("Overide default stacking",usestack);
247      sbox.add(usestackbox);
248      
249      if (stack==null) stack = new Stacking();
250      Container res = stack.getPane(comp);
251      sbox.add(res);
252  
253      edits[slot-1] = sbox;
254      sbox.setName("Stacking");
255  
256      super.getPropertyEdit(edits,slot-2,sub,parent);
257    }
258  
259    public void acceptPropertyEdit() {
260      super.acceptPropertyEdit();
261      prefs.save();
262  
263      usestack = usestackbox.isSelected();
264      usestackbox = null;
265  
266      stack.save();
267      stack.clear();
268  
269      prefs = null;
270    }
271  
272    public void abandonPropertyEdit() {
273      prefs = null;
274      usestackbox = null;
275  
276      stack.clear();
277      super.abandonPropertyEdit();
278    }
279  }
280  
281