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.awt.event.*;
7    import java.awt.*;
8    import java.util.*;
9    import java.awt.geom.*;
10   import javax.swing.*;
11   public class PointSym extends Symbol implements Aligner {
12   
13     private static final long serialVersionUID = Version.getSUID();
14   
15   //  transient Point2D pos;
16     boolean valid = false;
17     public ViewTransform position = new ViewTransform(ViewTransform.PREMULTIPLY);
18   
19     public PointSym(){
20     }
21   
22     public PointSym(Point2D pos,View view,Object arg) {
23       this.position = new ViewTransform(pos);
24       position.addAlignmentListener(this);
25     }
26   
27     public ViewTransform defaultPosition() {
28       return(null);
29     }
30   
31     public ViewTransform getAlignTransform() {
32       return(position);
33     }
34   
35     private void readObject(java.io.ObjectInputStream stream)
36         throws java.io.IOException,java.lang.ClassNotFoundException {
37       stream.defaultReadObject();
38       if (position == null) // up compat 2/19/01
39         position = new ViewTransform(stream.readDouble(),stream.readDouble());
40       position.addAlignmentListener(this);
41     }
42   
43     transient PrefTrans trans=null;
44   
45     public int getLevel() {return(super.getLevel()+1);}
46   
47     public void getPropertyEdit(Object[] edits,int slot,java.util.Set sub,Symbol parent) {
48       edits[slot] = pointPropertyEdit();
49   //  ((Component)edits[slot]).setName("Point");
50       super.getPropertyEdit(edits,slot-1,sub,parent);
51     }
52     
53     java.awt.Container pointPropertyEdit() {
54       trans = new PrefTrans(position);
55       java.awt.Container res = trans.getBarePane(null);
56       res.setName("point");
57       return(res);
58     }
59   
60     public void acceptPropertyEdit() {
61       if (trans!=null) {
62         trans.save();
63         trans = null;
64         position.notifyChange();
65       }
66       super.acceptPropertyEdit();
67     }
68   
69     public void abandonPropertyEdit() {
70       trans = null;
71       super.abandonPropertyEdit();
72     }
73   
74     public void alignmentChanged() {}
75   
76     public transient Segment morphsegment = null;
77   
78     transient Point2D morphpos = null;
79   
80   //  transient AffineTransform morphtrans = null;
81   //  static int upcount = 0;
82   
83     public void prepareForSurveyUpdate(java.util.List segments) {
84   //    System.out.println("pointsym prep");
85   //    if (true) return;
86       if (segments==null) return;
87   
88       Point2D pos = position.getTranslation();
89       Point2D.Double imagepos = new Point2D.Double();
90   
91   //    morphsegment = null;
92   
93       for (Iterator it = segments.iterator(); morphsegment==null && it.hasNext();) {
94         Segment seg = (Segment)it.next();
95   //      System.out.println("Position = "+position);
96   //      System.out.println("Pos = "+pos);
97         seg.map.btrans(pos,imagepos);
98   
99         if (seg.boundary.contains(imagepos)) {
100  
101          morphsegment = seg;
102          morphpos = imagepos;
103          imagepos = new Point2D.Double();
104          if (Curve.debug&& morphsegment.name.compareTo("d17")==0)
105            System.out.println("seg = "+seg.name+" old pos = "+pos+" morphpos = "+morphpos);
106  //        double[][] grad = new double[2][2];
107        
108  //        seg.map.getGrad(grad,pos.getX(),pos.getY());
109          
110  //      System.out.println("grad = [ "+grad[0][0]+" , "+grad[0][1]+" , "+grad[1][0]+" , "+grad[1][1]+" ]");
111  
112  //        morphtrans = new AffineTransform(grad[0][0],grad[1][0],grad[0][1],grad[1][1],0,0);
113  //        morphtrans.preConcatenate(position);
114  
115  //      System.out.println(" morphtrans = "+morphtrans);
116        
117        }
118      }
119  
120  //    upcount = 0;
121    }
122  
123    /**
124     *Called when the survey has changed.<br>
125     */
126    public void useSurveyUpdate() {
127  //    if (true) return;
128  
129      if (morphsegment == null) return;
130  
131      morphsegment.map.solve();
132  
133      Point2D.Double pos = new Point2D.Double();
134      morphsegment.map.ftrans(morphpos,pos);
135  
136      if (Curve.debug && morphsegment.name.compareTo("d17")==0)
137        System.out.println("seg = "+morphsegment.name+"morphpos = "+morphpos+" newpos = "+pos);
138  
139  /*
140      double[][] grad = new double[2][2];
141      
142      morphsegment.map.getGrad(grad,pos.getX(),pos.getY());
143  
144  //    System.out.println("grad = [ "+grad[0][0]+" , "+grad[0][1]+" , "+grad[1][0]+" , "+grad[1][1]+" ]");
145        
146      AffineTransform trans = new AffineTransform(grad[0][0],grad[1][0],grad[0][1],grad[1][1],0,0);
147  
148      try {
149        trans = trans.createInverse();
150      } catch (NoninvertibleTransformException ex) {
151        showUpdateError(morphsegment,"noninvertible matrix");
152        return;
153      } 
154  
155      morphtrans.concatenate(trans);
156      
157      if (!position.setTrans(morphtrans,pos))
158        showUpdateError(morphsegment,"couldn't orthogonalize");
159  */
160  
161      position.setLocation(pos.getX(),pos.getY());
162  //    System.out.println("new pos = "+position);
163  
164  //    morphtrans = null;
165      morphsegment = null;
166    }
167  
168  /*
169    public void showUpdateError(Segment seg,String str) {
170      if (upcount==0)
171        ErrorLog.log("Trouble tracking survey update\n"+
172          	   "Problem ="+str+
173          	   "anchor = "+this+" segment = "+seg+"\n"+
174          	   "The map symbol was not updated\n"+
175          	   "This warning will only appear once/update");
176      upcount++;
177    }
178  */
179    public boolean selectProbe(Point2D where,View view){
180      return(position.distance(where)<Size.handle.getSize(view.mapscale)/view.scale);
181    }
182    
183    public Rectangle2D getBounds(AffineTransform trans) {
184      Point2D tpos = position.getTranslation();
185      trans.transform(tpos,tpos);
186      return(new Rectangle2D.Double(tpos.getX(),tpos.getY(),0,0));
187    }
188  
189    public void showSelected(View view) {
190      int BOXSIZE = (int)Size.handle.getSize(view.mapscale);
191      Point2D tpos = position.getTranslation();
192      view.trans.transform(tpos,tpos);
193      view.draw.fillRect((int)tpos.getX()-BOXSIZE,(int)tpos.getY()-BOXSIZE,2*BOXSIZE,2*BOXSIZE);
194    }
195  
196    transient Point2D dragpoint = null;
197  
198    public void startDrag(int command,Point2D where,View view) {
199      dragpoint = where;//position.getTranslation();
200    }
201  
202    public void drag(int command,Point2D where,View view) {
203      showSelected(view);
204      position.setLocation(position.getTranslateX()+where.getX()-dragpoint.getX(),
205          		 position.getTranslateY()+where.getY()-dragpoint.getY());
206      dragpoint = where;
207      showSelected(view);
208    }
209  
210    public void paint(View view) {
211      if (!view.visible.isMember(this)) return;
212      int BOXSIZE = (int)Size.handle.getSize(view.mapscale);
213      Point2D tpos = position.getTranslation();
214      view.trans.transform(tpos,tpos);
215      view.draw.drawRect((int)tpos.getX()-BOXSIZE,(int)tpos.getY()-BOXSIZE,2*BOXSIZE,2*BOXSIZE);
216    }
217  
218    public void showAlignee(View view) {
219      position.showAlignee(view);
220    }
221  
222    public Point2D showAligner(View view) {
223      return(position.showAligner(view));
224    }
225  }
226