1    import javax.swing.*;
2    import java.awt.event.*;
3    import java.awt.*;
4    import java.awt.geom.*;
5    import java.util.*;
6    
7    public class PrefTrans extends PrefEditor implements java.io.Serializable,ActionListener {
8    
9      private static final long serialVersionUID = Version.getSUID();
10   
11     public static String ALIGN = "Align";
12     public static String UNALIGN = "UnAlign";
13     public static String ZERO = "Zero";
14   
15     public double x = 0;
16     public double y = 0;
17     public double scale = 1;
18     public double angle = 0;
19   
20     CompPane cpane = null;
21     Scale scalebar = null;
22     Rose rose = null;
23   
24     ViewTransform target = null;
25     double xoff=0,yoff=0;
26   
27     public PrefTrans() {
28       name = "Transform";
29     }
30   
31   /*  public PrefTrans(ViewTransform target,double xoff,double yoff) {
32       this.target = target;
33       this.xoff=xoff;
34       this.yoff=yoff;
35       Point2D.Double pos = new Point2D.Double(xoff,yoff);
36       target.transform(pos,pos);
37       pane=null;
38       x = pos.getX()-xoff;
39       y = pos.getY()-yoff;
40   
41       scale = target.scale;
42       angle = target.angle*180/Math.PI;
43       while (angle < 0) angle += 360;
44       while (angle > 360) angle -= 360;
45     }
46   */
47   
48     public PrefTrans(ViewTransform target) {
49       pane=null;
50       setTarget(target);
51     }
52   
53     void setTarget(ViewTransform target) {
54       this.target = target;
55       x = target.getTranslateX();
56       y = target.getTranslateY();
57       scale = target.scale;
58       angle = target.angle*180/Math.PI;
59   
60   //    System.out.println("old = "+x+" "+y+" "+scale+" "+angle);
61   
62       if (target.aligner!=null) {
63         x -= target.oldx;
64         y -= target.oldy;
65         scale /= target.oldscale;
66         angle -= target.oldangle*180/Math.PI;
67   //      System.out.println("rel = "+x+" "+y+" "+scale+" "+angle);
68       }
69   
70       while (angle < 0) angle += 360;
71       while (angle > 360) angle -= 360;
72     }
73   
74     public PrefTrans(Scale scalebar,Rose rose,CompPane cpane) {
75       target=null;
76       this.cpane = cpane;
77       Point2D pos = cpane.getCenter();
78       x = 0; //pos.getX();
79       y = 0; //pos.getY();
80       this.scalebar = scalebar;
81       scale = scalebar.fact;
82       this.rose = rose;
83       angle = rose.theta*180/Math.PI;
84   //    view = edit.scroll.getViewport().getViewPosition();
85     }
86     
87     JTextField xfield = null;
88     JTextField yfield = null;
89     JTextField scalefield = null;
90     JTextField rotfield = null;
91   
92     public void actionPerformed(ActionEvent e) {
93       String command = ((AbstractButton)e.getSource()).getActionCommand();
94       if (command == ALIGN) {}
95       else if (command == UNALIGN) {
96         if (target!=null && target.hasAlignment()) {
97           target.clearAlignment(); 
98           setTarget(target);
99           reset();
100        }
101      }
102      else if (command == ZERO) {
103  /*      if (target!=null && target.aligners!=null) {
104          if (target.aligners[0]!=null)// && !(target.aligners[0] instanceof FixedAligner))
105            xfield.setText("0.0");
106          if (target.aligners[1]!=null)// && !(target.aligners[1] instanceof FixedAligner))
107            yfield.setText("0.0");
108          if (target.aligners[2]!=null)// && !(target.aligners[2] instanceof FixedAligner))
109            scalefield.setText("1.0");
110          if (target.aligners[3]!=null)// && !(target.aligners[3] instanceof FixedAligner))
111            rotfield.setText("0.0");
112        } else {
113          xfield.setText("0.0");
114          yfield.setText("0.0");
115          scalefield.setText("1.0");
116          rotfield.setText("0.0");
117        }
118  */
119      }
120    }
121  
122    public void newPane() throws NoSuchFieldException {
123      setup();
124  
125      pane.setLayout(new javax.swing.BoxLayout(pane,javax.swing.BoxLayout.Y_AXIS));
126      JPanel sub = new JPanel();
127      sub.setLayout(new GridLayout(2,2));
128  /*
129      if (target!=null && target.aligners!=null) {
130        xfield = (JTextField)setParm(sub,"x","X ("+FixedAligner.getName(target.aligners[0])+")");
131        yfield = (JTextField)setParm(sub,"y","Y ("+FixedAligner.getName(target.aligners[1])+")");
132        scalefield = (JTextField)setParm(sub,"scale","Scale ("+FixedAligner.getName(target.aligners[2])+")");
133        rotfield = (JTextField)setParm(sub,"angle","Rotation ("+FixedAligner.getName(target.aligners[3])+")");
134        
135      }
136      else {
137        xfield = (JTextField)setParm(sub,"x","X");
138        yfield = (JTextField)setParm(sub,"y","Y");
139        scalefield = (JTextField)setParm(sub,"scale","Scale");
140        rotfield =(JTextField)setParm(sub,"angle","Rotation");
141        }*/
142      pane.add(sub);
143      javax.swing.JPanel controls = new javax.swing.JPanel();
144  
145      JButton but = new JButton(ZERO);
146      but.addActionListener(this);
147      controls.add(but);
148  
149      if (target!=null) {
150  //      but = new JButton(ALIGN);
151  //      but.addActionListener(this);
152  //      controls.add(but);
153        but = new JButton(UNALIGN);
154        but.addActionListener(this);
155        controls.add(but);
156      }
157  
158      pane.add(controls);
159    }
160  
161    public void save() {
162      if (pane==null) return;
163      super.save();
164      apply();
165    }
166  
167    public void apply() {
168      if (cpane!=null) {
169        rose.change(angle*Math.PI/180);
170        scalebar.change(scale);
171      }
172      if (target!=null) {
173        if (target.aligner!=null) {
174          x += target.oldx;
175          y += target.oldy;
176          scale *= target.oldscale;
177          angle += target.oldangle*180/Math.PI;
178        }
179        target.setTransform(scale,angle*Math.PI/180,x,y);
180      }
181    }
182  }
183