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    
9    /**
10    * This class isn't used.<br>
11    * I don't think it ever was, it will be
12    * deleted eventually.
13    */
14   public class Scaler implements Cloneable,Serializable {
15   
16     private static final long serialVersionUID = Version.getSUID();
17   
18     public static int CONSTANT = 0;
19     public static int SCALED = 1;
20   
21     static int SCALESLOTS = 5;
22   
23     public double[] scale = new double[SCALESLOTS-1];
24     public double[] a = new double[SCALESLOTS];
25     public double[] b = new double[SCALESLOTS];
26   //  public int[] code = new int[SCALESLOTS];
27     public int pieces = 0;
28   
29     public Scaler() {}
30   
31     public Scaler(int mode,double size) {
32       pieces = 1;
33       a[0] = size;
34       b[0] = -mode;
35     }
36   
37     public Scaler(double maxscale,double minsize,double scaledsize,double maxsize) {
38       pieces = 0;
39       if (maxsize>=0) {
40         a[0]=maxsize;
41         b[0]=0;
42         scale[pieces++] = scaledsize/maxsize;
43       }
44       a[pieces] = scaledsize;
45       b[pieces] = -1;
46       if (minsize>0) {
47         scale[pieces++] = scaledsize/minsize;
48         a[pieces] = minsize;
49         b[pieces] = 0;
50       }
51       if (maxscale>0) {
52         scale[pieces++] = maxscale;
53         a[pieces]=0;
54         b[pieces]=0;
55       }
56       if (pieces>=3 && scale[1]>=scale[2]) {
57         a[2]=a[3];
58         scale[1]=scale[2];
59         pieces--;
60       }
61       if (pieces>=2 && scale[0]>=scale[1]) {
62         a[1]=a[2];
63         a[2]=a[3];
64         scale[0]=scale[1];
65         scale[1]=scale[2];
66         pieces--;
67       }
68     }
69   
70     public Scaler(String description) {
71       pieces = 0;
72       StringTokenizer tokens = new StringTokenizer(description);
73       String token;
74       try {
75         while (true) {
76           a[pieces] = Double.parseDouble(tokens.nextToken());
77           b[pieces] = Double.parseDouble(tokens.nextToken());
78           try {
79             scale[pieces] = Double.parseDouble(tokens.nextToken());
80             pieces++;
81           } catch (NoSuchElementException e){break;}
82         }
83       }
84       catch (NoSuchElementException e){
85         ErrorLog.log("malformed scaler");
86         ErrorLog.exception(e);
87       }
88     }
89   
90     public Object clone() {
91       Scaler res = new Scaler();
92       res.scale = new double[scale.length];
93       res.a = new double[scale.length+1];
94       res.b = new double[scale.length+1];
95       res.pieces = pieces;
96       for (int i=0;i<=pieces;i++) {
97         if (i<pieces) res.scale[i]=scale[i];
98         res.a[i]=a[i];
99         res.b[i]=b[i];
100      }
101      return((Object)res);
102    }
103  /*  static double BIGSIZE = 1000000.0;
104  
105    public Scaler(CartoFrame owner) {
106      JPanel pane = new JPanel();
107      TextField minscalef = PrefEditor.addDouble(pane,0,"Min scale");
108      TextField minsizef = PrefEditor.addDouble(pane,0,"Min size");
109      TextField scaledf = PrefEditor.addDouble(pane,1,"Scaled size");
110      TextField minscalef = PrefEditor.addDouble(pane,BIGSIZE,"Max size");
111      int res = JOptionPane.
112    }
113  */
114  
115    public String toString() {
116      StringBuffer buf = new StringBuffer();
117      int i;
118      buf.append(a[0]);
119      buf.append(' ');
120      buf.append(b[0]);
121      buf.append(' ');
122      for (i=0;i<=pieces;i++) {
123        buf.append(scale[i]);
124        buf.append(' ');
125        buf.append(a[i+1]);
126        buf.append(' ');
127        buf.append(b[i+1]);
128        buf.append(' ');
129      }
130      return(buf.toString());
131    }
132  
133    transient double oldscale = 0;
134    transient double oldsize = 0;;
135  
136    public double getSize(double nscale) {
137      if (oldscale!=nscale) {
138        oldscale=nscale;
139        int i;
140        for (i=0;i<pieces && nscale>scale[i];i++);
141        oldsize = a[i]*Math.pow(nscale,b[i]);
142      }
143      return(oldsize);
144    }
145  
146  /*  public void edit() {
147      JPanel pane = new JPanel();
148      
149      pane.setLayout(new Gridlayout(3,scale.length+1));
150      for (int i=0;i<scale.length;i++) {
151        
152      
153      
154    }
155  */
156  }
157