1    /*
2    Copyright 2001 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.awt.event.*;
9    import java.awt.*;
10   import java.io.*;
11   import java.util.*;
12   
13   public class PrefPage extends PrefEditor implements ActionListener,Serializable,ItemListener {
14   
15     private static final long serialVersionUID = Version.getSUID();
16   
17     transient JRadioButton widthbox;
18     transient JRadioButton hightbox;
19     transient JRadioButton unitbox;
20     transient JRadioButton scalebox;
21     transient JRadioButton pwidthbox;
22     transient JRadioButton phightbox;
23     transient JRadioButton pscalebox;
24     transient JLabel filelab;
25     transient JComboBox mapunitbox = null;
26     transient JComboBox imageunitbox = null;
27     transient JComboBox printerunitbox = null;
28   
29     public boolean printer;
30     public boolean index;
31     public boolean image;
32     public double resolution;
33     public double presolution;
34   //  public double aspect;
35     public int ressetting;
36     public int pressetting;
37     public File file;
38   
39     Page target;
40   
41     public Unit imageunit = Unit.inch;
42     public Unit printerunit = Unit.inch;
43   
44     public Unit mapunit = Unit.foot;
45   
46     public PrefPage() {
47       name = "Page";
48     }
49   
50     public PrefPage(Page target) {
51       this();
52       this.target=target;
53       printer = target.printer;
54       index = target.index;
55       image = target.image;
56       resolution = target.resolution;
57       presolution = target.presolution;
58   //    aspect = target.subaspect;
59       ressetting = target.ressetting;
60       pressetting = target.pressetting;
61       file = target.file;
62       imageunit = target.imageunit;
63       printerunit = target.printerunit;
64       mapunit = target.mapunit;
65     }
66   
67     /**
68      *Prefpages in files are obsolete.
69      *They are used internaly by page, so they still exist,
70      *But they are always transient.
71      *The correct Page preference item is PrefPageNew.
72      *readResolve fixes old files. 4/28/01
73      */
74     public Object readResolve() throws ObjectStreamException {
75       return(new PrefPageNew(this));
76     }
77   
78     public void newPane() throws NoSuchFieldException {
79   
80       pane = new javax.swing.Box(BoxLayout.Y_AXIS);
81   
82       if (imageunit==null) imageunit = Unit.inch;
83   //      if (Page.defimageunit==null) Page.defimageunit = Unit.inch;
84   //      imageunit = Page.defimageunit;
85   //    }
86   //    if (printerunit==null) printerunit = Unit.inch;
87   //      if (Page.defprinterunit==null) Page.defprinterunit = Unit.inch;
88   //      printerunit = Page.defprinterunit;
89   //    }
90       if (mapunit==null) mapunit = Carto.defmapunit;
91   
92   //    if (target==null) setParm("aspect","Aspect ratio");
93       mapunitbox = (JComboBox)setUnit("mapunit",Unit.predefined,"World Units");
94       mapunitbox.addItemListener(this);
95   
96       JComponent pbox = new JPanel();
97       pbox.setLayout(new BoxLayout(pbox,BoxLayout.X_AXIS));
98       pbox.setAlignmentX(0);
99       Container subbox = new javax.swing.Box(BoxLayout.Y_AXIS);
100  
101  //    setParm(subbox,"printer","Print to printer");
102  //    printerunitbox = (JComboBox)setUnit(subbox,"printerunit",Unit.predefined,"Printer units");
103  //    printerunitbox.addItemListener(this);
104  //    setParm(subbox,"presolution","Printer scale");
105  //    pbox.add(subbox);
106  
107      ButtonGroup group = new ButtonGroup();
108  //    subbox = new javax.swing.Box(BoxLayout.Y_AXIS);
109  //    pwidthbox = addRadio(subbox,pressetting==Page.WIDTHMODE,"    Width ("+printerunit.plural+")");
110  //    phightbox = addRadio(subbox,pressetting==Page.HEIGHTMODE,"    Height ("+printerunit.plural+")");
111  //    pscalebox = addRadio(subbox,pressetting==Page.SCALEMODE,
112  //        		 "    scale ("+mapunit.plural+"/"+printerunit.name+")");
113  //    group.add(pwidthbox);
114  //    group.add(phightbox);
115  //    group.add(pscalebox);
116  //    pbox.add(subbox);
117  
118  //    pane.add(pbox);
119  //    pbox = new JPanel();
120      pbox.setLayout(new BoxLayout(pbox,BoxLayout.X_AXIS));
121      pbox.setAlignmentX(0);
122  
123  //    subbox = new javax.swing.Box(BoxLayout.Y_AXIS);
124  
125      setParm(subbox,"image","Print");
126      imageunitbox = (JComboBox)setUnit(subbox,"imageunit",Unit.predefined,"Image units");
127      imageunitbox.addItemListener(this);
128      setParm(subbox,"resolution","Image Scale");
129      pbox.add(subbox);
130  
131  //    group = new ButtonGroup();
132      subbox = new javax.swing.Box(BoxLayout.Y_AXIS);
133      widthbox = addRadio(subbox,ressetting==Page.WIDTHMODE,"    Width ("+imageunit.plural+")");
134      hightbox = addRadio(subbox,ressetting==Page.HEIGHTMODE,"    Height ("+imageunit.plural+")");
135      unitbox = addRadio(subbox,ressetting==Page.PIXELMODE,"    pixels/"+mapunit.name);
136      scalebox = addRadio(subbox,ressetting==Page.SCALEMODE,
137          		"    scale ("+mapunit.plural+"/"+imageunit.name+")");
138      group.add(widthbox);
139      group.add(hightbox);
140      group.add(unitbox);
141      group.add(scalebox);
142  
143      pbox.add(subbox);
144      pane.add(pbox);
145  
146      setParm("index","Index page (shows other pages)");
147  
148      pbox = new JPanel();
149      pbox.setLayout(new BoxLayout(pbox,BoxLayout.X_AXIS));
150      pbox.setAlignmentX(0);
151  
152      if (file==null)
153        filelab = new JLabel("");
154      else filelab = new JLabel(file.getName());
155      pbox.add(filelab);
156  
157      JButton filebutton = new JButton("Change File");
158      filebutton.addActionListener(this);
159      pbox.add(filebutton);
160  
161      pane.add(pbox);
162      pane.setName("Page");
163    }
164  
165    public void itemStateChanged(ItemEvent ev) {
166      if (ev.getItemSelectable() == imageunitbox) {
167        widthbox.setText("    Width ("+((Unit)imageunitbox.getSelectedItem()).plural+")");
168        hightbox.setText("    Height ("+((Unit)imageunitbox.getSelectedItem()).plural+")");
169      }
170      else
171        scalebox.setText("    scale ("+((Unit)mapunitbox.getSelectedItem()).plural+"/"+
172          		((Unit)imageunitbox.getSelectedItem()).name+")");
173  
174  //    if (ev.getItemSelectable() == printerunitbox) {
175  //      pwidthbox.setText("    Width ("+((Unit)printerunitbox.getSelectedItem()).plural+")");
176  //      phightbox.setText("    Height ("+((Unit)printerunitbox.getSelectedItem()).plural+")");
177  //    }
178  //    else
179  //      scalebox.setText("    scale ("+((Unit)mapunitbox.getSelectedItem()).plural+"/"+
180  //        		((Unit)imageunitbox.getSelectedItem()).name+")");
181  
182      if (ev.getItemSelectable() == mapunitbox)
183        unitbox.setText("    pixels/"+((Unit)mapunitbox.getSelectedItem()).name);
184    }
185  
186    public void save() {
187      if (pane==null) return;
188      super.save();
189      if (widthbox.isSelected()) ressetting = Page.WIDTHMODE;
190      else if (hightbox.isSelected()) ressetting = Page.HEIGHTMODE;
191      else if (unitbox.isSelected()) ressetting = Page.PIXELMODE;
192      else if (scalebox.isSelected()) ressetting = Page.SCALEMODE;
193  //    if (pwidthbox.isSelected()) pressetting = Page.WIDTHMODE;
194  //    else if (phightbox.isSelected()) pressetting = Page.HEIGHTMODE;
195  //    else if (pscalebox.isSelected()) pressetting = Page.SCALEMODE;
196      apply();
197    }
198  
199    public void apply() {
200      if (target!=null) {
201  //      target.printer = printer;
202        target.image = image;
203        target.index = index;
204        target.imageunit = imageunit;
205  //      target.printerunit = printerunit;
206        target.mapunit = mapunit;
207        target.resolution = resolution;
208  //      target.presolution = presolution;
209  //      if (target.subaspect!=aspect) {
210  //        aspect = target.subaspect;
211  //        if(target.box==null)
212  //          target.box = new SavableRectangle2D(-Page.PRINTWIDTH/2,-aspect*Page.PRINTWIDTH/2,
213  //        				      Page.PRINTWIDTH,aspect*Page.PRINTWIDTH);
214  
215  //        target.reshape();
216  //      }     
217        target.ressetting = ressetting;
218  //      target.pressetting = pressetting;
219        target.file = file;
220      }
221    }
222  
223    public void actionPerformed(ActionEvent e) {
224      file = pickFile();
225      filelab.setText(file.getName());
226    }
227  
228    public static File pickFile() {
229      JFileChooser choose = Persist.current.getFileChooser();
230  
231      choose.setFileFilter(new FileTypes(FileTypes.IMAGE));
232      choose.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
233      choose.rescanCurrentDirectory();
234      if (choose.showOpenDialog(null)!=choose.APPROVE_OPTION) {
235        Persist.current.clearChooser();
236        return(null);
237      }
238      File res = choose.getSelectedFile();
239      Persist.current.clearChooser();
240      return(res);
241    }
242  }
243