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.geom.*;
7    import java.awt.Container;
8    import java.util.Set;
9    import java.awt.Graphics2D;
10   
11   public class Offset extends Box {
12   
13     public Box viewport;
14     public boolean portselected;
15     public Stacking stack;
16     public boolean usestack;
17     
18     public Offset(Point2D where,View view,Object arg) {
19       super(where,view,arg);
20     }
21   
22     void init(Point2D where,View view) {
23       super.init(where,view);
24       viewport = new Box(where,view,box);
25       viewport.init(where,view);
26     }
27   
28     public int getLevel() {return(super.getLevel()+2);}
29   
30     transient javax.swing.JCheckBox usestackbox = null;
31   
32     public void getPropertyEdit(Object[] edits,int slot,Set sub, Symbol parent) {
33       Container sbox = new javax.swing.Box(javax.swing.BoxLayout.Y_AXIS);
34   
35       usestackbox = new javax.swing.JCheckBox("Overide default stacking",usestack);
36       sbox.add(usestackbox);
37       
38       if (stack==null) stack = new Stacking();
39       Container res = stack.getPane((Comp)parent);
40       sbox.add(res);
41   
42       edits[slot] = sbox;
43       sbox.setName("Stacking");
44   
45       super.getPropertyEdit(edits,slot-1,sub,parent);
46     }
47   
48     public void acceptPropertyEdit() {
49       super.acceptPropertyEdit();
50   
51       usestack = usestackbox.isSelected();
52       usestackbox = null;
53   
54       stack.save();
55       stack.clear();
56     }
57   
58     public void abandonPropertyEdit() {
59       usestackbox = null;
60   
61       stack.clear();
62       super.abandonPropertyEdit();
63     }
64   
65     public boolean selectProbe(Point2D pos,View view){
66       if (super.selectProbe(pos,view)) {
67         portselected = false;
68         return(true);
69       }
70       if (viewport.selectProbe(pos,view)) {
71         portselected = true;
72         return(true);
73       }
74       return(false);
75     }
76   
77     public void startDrag(int command,Point2D where,View view){
78       if (portselected) viewport.startDrag(command,where,view);
79       else super.startDrag(command,where,view);
80     }
81   
82     public void drag(int command,Point2D where,View view){
83       if (portselected) viewport.drag(command,where,view);
84       else super.drag(command,where,view);
85     }
86   
87     public void endDrag(int command,Point2D where,View view){
88       if (portselected) {
89         viewport.endDrag(command,where,view);
90   //      subaspect = viewport.subaspect;
91   //      anchorpoint.position.setScale(viewport.anchorpoint.position.scale);
92   //      anchorpoint.position.setRotation(viewport.anchorpoint.position.angle);
93   //      located = false;
94       }
95       else {
96         super.endDrag(command,where,view);
97         viewport.subaspect = subaspect;
98         viewport.anchorpoint.position.setScale(anchorpoint.position.scale);
99         viewport.anchorpoint.position.setRotation(anchorpoint.position.angle);
100        viewport.located = false;
101        viewport.box = (SavableRectangle2D) box.clone();
102      }
103    }
104  
105    public void showSelected(View view) {
106      super.showSelected(view);
107      viewport.showSelected(view);
108    }
109  
110  /*
111    public reshape() {
112      super.reshape();
113      viewport.reshape();
114    }
115  */
116  
117    public void paint(View view) {
118      if (view.offsets.contains(this)) return;
119  
120      view.offsets.add(this);
121  
122      super.paint(view);
123      viewport.locate();
124  
125      java.awt.Shape clip = view.getClip();
126  
127      view.clip(view.trans.createTransformedShape(pos.createTransformedShape(box)));
128  
129      AffineTransform trans = view.trans;
130      AffineTransform newtrans = (AffineTransform)trans.clone();
131      
132      newtrans.translate(pos.getTranslateX()-viewport.pos.getTranslateX(),
133          	       pos.getTranslateY()-viewport.pos.getTranslateY());
134  
135  
136      view.trans = newtrans;
137  
138      Stacking oldstack = view.stacking;
139      if (usestack) view.stacking = stack;
140  
141      view.composite.paint(view);
142  
143      view.stacking = stack;
144  
145      view.trans = trans;
146      view.setClip(clip);
147      view.offsets.remove(this);
148    }
149  
150  }
151