1    /*
2    Copyright 2000 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    import java.util.*;
7    import java.io.*;
8    import java.awt.*;
9    import javax.swing.*;
10   
11   public class Size extends FileDefaultable implements Serializable{
12   
13     private static final long serialVersionUID = Version.getSUID();
14   
15     public static String PIXEL = "One Pixel";
16     public static String MAJOR = "Major";
17     public static String MEDIUM = "Medium";
18     public static String SMALL = "Small";
19     public static String TINY = "Tiny";
20     public static String STALAB = "Station label";
21     public static String TEXT = "plain text";
22     public static String SHOTLINE = "Shot Line";
23     public static String STAMARK = "Station Marker";
24     public static String HANDLE = "Handle";
25     public static String SECTIONTICK = "Section tick";
26     public static String SECTIONMARGIN = "Section margin";
27     public static String SECTIONLINE1 = "Section Line 1";
28     public static String SECTIONLINE2 = "Section Line 2";
29     public static String ARROW = "Arrow";
30   
31     Unit minvisible;
32     Unit minsize;
33     Unit maxsize;
34     Unit size;
35   
36     String name;
37   
38     public static TreeSet defaults;
39     public static TreeSet mindefaults = new TreeSet();
40     public static TreeSet file = new TreeSet();
41     public static TreeSet fixed = new TreeSet();
42   
43     public static Size none;
44     public static Size handle;
45     public static Size arrow;
46   
47     static {
48       fixed.add(none = new Size("None"));
49       mindefaults.add(handle = new Size(HANDLE,
50           		     new Unit(0,Unit.pixel),
51           		     new Unit(5,Unit.pixel),
52           		     new Unit(Unit.meter),
53           		     new Unit(5,Unit.pixel)));
54       mindefaults.add(new Size(SECTIONTICK,
55           		     new Unit(0,Unit.pixel),
56           		     new Unit(5,Unit.pixel),
57           		     new Unit(Unit.meter),
58           		     new Unit(5,Unit.pixel)));
59       mindefaults.add(new Size(SECTIONMARGIN,
60           		     new Unit(0,Unit.pixel),
61           		     new Unit(2,Unit.pixel),
62           		     new Unit(Unit.meter),
63           		     new Unit(2,Unit.pixel)));
64       mindefaults.add(new Size(TEXT,
65           		     new Unit(0,Unit.pixel),
66           		     new Unit(15,Unit.pixel),
67           		     new Unit(Unit.meter),
68           		     new Unit(15,Unit.pixel)));
69       mindefaults.add(new Size(STAMARK,
70           		     new Unit(0,Unit.pixel),
71           		     new Unit(4,Unit.pixel),
72           		     new Unit(Unit.meter),
73           		     new Unit(4,Unit.pixel)));
74       mindefaults.add(new Size(SHOTLINE,
75           		     new Unit(0,Unit.pixel),
76           		     new Unit(1,Unit.pixel),
77           		     new Unit(Unit.meter),
78           		     new Unit(1,Unit.pixel)));
79       mindefaults.add(new Size(SECTIONLINE1,
80           		     new Unit(0,Unit.pixel),
81           		     new Unit(1,Unit.pixel),
82           		     new Unit(Unit.meter),
83           		     new Unit(1,Unit.pixel)));
84       mindefaults.add(new Size(SECTIONLINE2,
85           		     new Unit(0,Unit.pixel),
86           		     new Unit(2,Unit.pixel),
87           		     new Unit(Unit.meter),
88           		     new Unit(2,Unit.pixel)));
89       mindefaults.add(new Size(STALAB,
90           		     new Unit(0,Unit.pixel),
91           		     new Unit(10,Unit.pixel),
92           		     new Unit(Unit.meter),
93           		     new Unit(10,Unit.pixel)));
94       mindefaults.add(new Size(PIXEL,
95           		     new Unit(0,Unit.pixel),
96           		     new Unit(Unit.pixel),
97           		     new Unit(Unit.meter),
98           		     new Unit(Unit.pixel)));
99       mindefaults.add(new Size(MAJOR,
100          		     new Unit(0,Unit.meter),
101          		     new Unit(4,Unit.pixel),
102          		     new Unit(4,Unit.inch),
103          		     new Unit(Double.POSITIVE_INFINITY,Unit.meter)));
104      mindefaults.add(new Size(MEDIUM,
105          		     new Unit(0,Unit.pixel),
106          		     new Unit(3,Unit.pixel),
107          		     new Unit(2,Unit.inch),
108          		     new Unit(3,Unit.pixel)));
109      mindefaults.add(new Size(SMALL,
110          		     new Unit(0,Unit.pixel),
111          		     new Unit(2,Unit.pixel),
112          		     new Unit(1,Unit.inch),
113          		     new Unit(2,Unit.pixel)));
114      mindefaults.add(new Size(TINY,
115          		     new Unit(0,Unit.pixel),
116          		     new Unit(0.5,Unit.pixel),
117          		     new Unit(0.5,Unit.inch),
118          		     new Unit(0.5,Unit.pixel)));
119      mindefaults.add(arrow = new Size(ARROW,
120          		     new Unit(0,Unit.meter),
121          		     new Unit(0,Unit.pixel),
122          		     new Unit(2,Unit.meter),
123          		     new Unit(Double.POSITIVE_INFINITY,Unit.meter)));
124  
125      defaults = (TreeSet)mindefaults.clone();
126  
127      fixedmap.put(Size.class,fixed);
128      defmap.put(Size.class,defaults);
129      minmap.put(Size.class,mindefaults);
130      filemap.put(Size.class,file);
131    }
132  
133    public Size(String name) {
134      this(name,
135           new Unit(0,Unit.pixel),
136           new Unit(1,Unit.pixel),
137           new Unit(0,Unit.meter),
138           new Unit(1,Unit.pixel)/*,
139          			 Unit.predefined*/);
140    }
141  
142    public Size(String name,Unit minvisible,Unit minsize,Unit size,Unit maxsize) {
143      this.name = name;
144      this.minvisible=minvisible;
145      this.minsize=minsize;
146      this.size=size;
147      this.maxsize=maxsize;
148    }
149  
150    public String getName() {return(name);}
151  
152    public Object clone() {
153      Size res = new Size(new String(name),(Unit)(minvisible.clone()),
154          		     (Unit)(minsize.clone()),(Unit)(size.clone()),(Unit)(maxsize.clone()));
155      res.oldscale= null;
156      return((Object)res);
157    }
158  
159    public void copy(FileDefaultable thatitem) {
160      Size that = (Size)thatitem;
161      name = new String(that.name);
162      minvisible = (Unit)(that.minvisible.clone());
163      minsize = (Unit)that.minsize.clone();
164      size = (Unit)that.size.clone();
165      maxsize = (Unit)that.maxsize.clone();
166      oldscale = null;
167    }
168  
169    transient MapScale oldscale = null;
170    transient double oldval = 0;
171  
172    public double getSize(MapScale scale) {
173      if (scale==oldscale) return(oldval);
174      oldscale=scale;
175      oldval = scale.scale(size);
176  
177      double v = scale.toBase(minvisible);
178      if (oldval<v) return(oldval=0);
179  
180      v = scale.toBase(minsize);
181      if (oldval<v) return(oldval=v);
182  
183      v = scale.toBase(maxsize);
184      if (oldval>v) return(oldval = v);
185  
186      return(oldval);
187    }
188  
189    public boolean edit(JFrame owner) {
190      JPanel panel = new JPanel(); 
191      panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
192  
193      JTextField namefield = new JTextField(name);
194      namefield.setColumns(10);
195      panel.add(namefield);
196  
197      panel.add(minvisible.getEditor("Minimum Visible"));
198      panel.add(minsize.getEditor("Minimum Size"));
199      panel.add(size.getEditor("Scaled Size"));
200      panel.add(maxsize.getEditor("Maximum Size"));
201  
202      if (JOptionPane.showConfirmDialog(owner,panel,"Edit size"+name,JOptionPane.OK_CANCEL_OPTION) ==
203          JOptionPane.OK_OPTION) {
204        name = namefield.getText();
205        minvisible.applyEdit();
206        minsize.applyEdit();
207        size.applyEdit();
208        maxsize.applyEdit();
209        panel = null;
210        return(true);
211      }
212      minvisible.clearEdit();
213      minsize.clearEdit();
214      size.clearEdit();
215      maxsize.clearEdit();
216      panel = null;
217  
218      return(false);
219    }
220  
221    /* for testing only */
222    public static void main(String args[]) {
223      Size s = new Size("Test",
224          	      new Unit(0.005,Unit.inch),new Unit(0.01,Unit.inch),
225          	      new Unit(1,Unit.foot),new Unit(0.1,Unit.foot)/*,Unit.screens*/);
226      while(s.edit(null));
227      System.exit(0);
228    }
229  
230    public Object readResolve() throws ObjectStreamException {
231      return((Object)getGlobal(Size.class));
232    }
233  }
234