View Javadoc

1   // Copyright 2004-2005, FreeHEP.
2   package hep.wired.edit;
3   
4   import java.awt.*;
5   import java.awt.geom.*;
6   import java.util.logging.*;
7   import javax.swing.*;
8   import javax.swing.undo.*;
9   
10  import hep.wired.services.RecordPlot;
11  import hep.wired.services.GraphicsPanel;
12  import hep.wired.services.ViewPort;
13  import hep.wired.feature.Translateable;
14  import hep.wired.feature.Translateable2D;
15  import hep.wired.feature.Translateable3D;
16  import hep.wired.util.UVWindices;
17  
18  /***
19   * Translates a plot in 2D or 3D.
20   *
21   * @author Mark Donszelmann
22   * @version $Id: Translate.java 2083 2005-07-20 17:16:52Z duns $
23   */
24  
25  public class Translate extends AnimatedWiredEdit implements GraphicsPanelEdit, UVWindices {
26  
27      private static Logger logger = Logger.getLogger(Translate.class.getPackage().getName());
28      private double tx, ty, tz;
29  
30      /***    
31       * Creates a translate edit with no translation. 
32       */    
33      public Translate() {
34          this(0, 0, 0);
35      }
36  
37      /***    
38       * Creates a translate edit with given translation in model units. 
39       */    
40      public Translate(double tx, double ty, double tz) {
41          this(tx, ty, tz, null, 0);
42      }
43  
44      /***    
45       * Creates a translate edit with given translation in model units, animation shape and 
46       * number of frames. 
47       */    
48      public Translate(double tx, double ty, double tz,
49                       Shape shape, int frames) {
50          super(shape, frames);
51          this.tx = tx;
52          this.ty = ty;
53          this.tz = tz;
54      }
55  
56      public WiredEdit copy(RecordPlot plot) {
57          WiredEdit copy = new Translate(tx, ty, tz, getShape(), getFrames());
58          copy.setRecordPlot(plot);
59          return copy;
60      }
61      
62      protected Shape[] getShapes(Shape shape, int steps) {
63          if ((shape == null) || (steps <= 1)) return null;
64          
65          Shape[] shapes = new Shape[steps];
66  
67          // calculate translate increments
68          GraphicsPanel panel = getRecordPlot().getGraphicsPanel();
69          Translateable translateable = (Translateable)panel.getFeature(Translateable.class);
70          double[] uvw = translateable.getScreenTranslation(new double[] { tx/(steps-1), ty/(steps-1), tz/(steps-1) }, 
71                                                           panel.getViewPort());
72          AffineTransform transform = new AffineTransform();
73  
74          // calculate steps
75          for (int i=0; i<steps; i++) {
76              // transform
77              shapes[i] = transform.createTransformedShape(shape);
78              // add our increment
79              transform.translate(uvw[U], -uvw[V]);
80          }
81          return shapes;
82      }
83  
84      public Shape createTransformedShape(Component component, Shape shape) {
85          RecordPlot plot = (RecordPlot)component;
86          GraphicsPanel panel = plot.getGraphicsPanel();
87          Translateable translateable = (Translateable)panel.getFeature(Translateable.class);
88          double[] uvw = translateable.getScreenTranslation(new double[] { tx, ty, tz }, panel.getViewPort());
89          return AffineTransform.getTranslateInstance(uvw[U], -uvw[V]).createTransformedShape(shape);
90      }
91  
92      public String getPresentationName() {
93          StringBuffer s = new StringBuffer();
94          s.append(toString());
95          s.append(" (");
96          s.append(format.format(tx));
97          s.append(", ");
98          s.append(format.format(ty));
99          if (tz != 0) {
100             s.append(", ");
101             s.append(format.format(tz));
102         }
103         s.append(")");
104         return s.toString();
105     }
106 
107     public String toString() {
108         return "Translate";
109     }
110 
111     public boolean addEdit(UndoableEdit edit) {
112         if (edit instanceof Translate) {
113             Translate translate = (Translate)edit;
114             tx += translate.tx;
115             ty += translate.ty;
116             tz += translate.tz;
117             return true;
118         }
119         return false;
120     }
121 
122     public boolean isSupportedBy(GraphicsPanel p) {
123         return p.supports(Translateable2D.class) || p.supports(Translateable3D.class);
124     }
125 
126     protected void redoEdit() {
127         GraphicsPanel panel = getRecordPlot().getGraphicsPanel();
128         Translateable3D translateable3D = (Translateable3D)panel.getFeature(Translateable3D.class);
129         if (translateable3D != null) {
130             translateable3D.translate(tx, ty, tz);
131         } else {
132             Translateable2D translateable2D = (Translateable2D)panel.getFeature(Translateable2D.class);
133             if (translateable2D == null) {
134                 System.err.println("Could not redo: "+this);
135                 return;
136             }
137             translateable2D.translate(tx, ty);
138         }
139         getRecordPlot().repaint();
140         logger.finer(toString());
141     }
142 
143     protected void undoEdit() {
144         GraphicsPanel panel = getRecordPlot().getGraphicsPanel();
145         Translateable3D translateable3D = (Translateable3D)panel.getFeature(Translateable3D.class);
146         if (translateable3D != null) {
147             translateable3D.translate(-tx, -ty, -tz);
148         } else {
149             Translateable2D translateable2D = (Translateable2D)panel.getFeature(Translateable2D.class);
150             if (translateable2D == null) {
151                 System.err.println("Could not undo: "+this);
152                 return;
153             }
154             translateable2D.translate(-tx, -ty);
155         }
156         getRecordPlot().repaint();
157         logger.finer(toString());
158     }
159 }