View Javadoc

1   // Copyright 2003-2004, 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.feature.Scaleable;
13  import hep.wired.feature.Scaleable2D;
14  import hep.wired.feature.Scaleable3D;
15  
16  /***
17   * Scales a plot in 2D or 3D.
18   *
19   * @author Mark Donszelmann
20   * @version $Id: Scale.java 674 2005-03-11 04:53:14Z duns $
21   */
22  public class Scale extends AnimatedWiredEdit implements GraphicsPanelEdit {
23  
24      private static Logger logger = Logger.getLogger(Scale.class.getPackage().getName());
25      private double sx, sy, sz;
26  
27      /***
28       * Creates a scale edit with no scaling done.
29       */
30      public Scale() {
31          this(1);
32      }
33  
34      /***
35       * Creates a scale edit with uniform given scale factor.
36       */
37      public Scale(double s) {
38          this(s, s);
39      }
40  
41      /***
42       * Creates a scale edit with given scale factors.
43       */
44      public Scale(double sx, double sy) {
45          this(sx, sy, 1);
46      }
47  
48      /***
49       * Creates a scale edit with given scale factors.
50       */
51      public Scale(double sx, double sy, double sz) {
52          this(sx, sy, sz, null, 0);
53      }
54  
55      /***
56       * Creates a scale edit with given scale factor, animation shape and number of frames.
57       */
58      public Scale(double sx, double sy, double sz,
59                   Shape shape, int frames) {
60          super(shape, frames);
61          this.sx = sx;
62          this.sy = sy;
63          this.sz = sz;
64      }
65      
66      public WiredEdit copy(RecordPlot plot) {
67          WiredEdit copy = new Scale(sx, sy, sz, getShape(), getFrames());
68          copy.setRecordPlot(plot);
69          return copy;
70      }
71      
72      protected Shape[] getShapes(Shape shape, int steps) {
73          if ((shape == null) || (steps <= 1)) return null;
74      
75          Shape[] shapes = new Shape[steps];
76  
77          // calculate scale increments
78          double dsx = (sx - 1.0)/(steps-1);
79          double dsy = (sy - 1.0)/(steps-1);
80  
81          // calculate window offset
82          double w = getRecordPlot().getWidth()/2.0;
83          double h = getRecordPlot().getHeight()/2.0;
84  
85          // calculate steps
86          for (int i=0; i<steps; i++) {
87              AffineTransform transform = new AffineTransform();
88              // put in the center
89              transform.translate(w, h);
90              // add our increment
91              transform.scale(1 + (i*dsx), 1 + (i*dsy));
92              // put back
93              transform.translate(-w, -h);
94              // transform
95              shapes[i] = transform.createTransformedShape(shape);
96          }
97          return shapes;
98      }
99  
100     public Shape createTransformedShape(Component component, Shape shape) {
101         
102         double w = component.getWidth()/2.0;
103         double h = component.getHeight()/2.0;
104         AffineTransform transform = new AffineTransform();
105         transform.translate(w, h);
106         transform.scale(sx, sy);
107         transform.translate(-w, -h);
108         return transform.createTransformedShape(shape);
109     }
110 
111     public String getPresentationName() {
112         StringBuffer s = new StringBuffer();
113         s.append(toString());
114         s.append(" (");
115         s.append(format.format(sx));
116         if ((sy != 1) || (sz != 1)) {
117             s.append(", ");
118             s.append(format.format(sy));
119         }
120         if (sz != 1) {
121             s.append(", ");
122             s.append(format.format(sz));
123         }
124         s.append(")");
125         return s.toString();
126     }
127 
128     public String toString() {
129         return "Scale";
130     }
131 
132     public boolean addEdit(UndoableEdit edit) {
133         if (edit instanceof Scale) {
134             Scale scale = (Scale)edit;
135             sx *= scale.sx;
136             sy *= scale.sy;
137             sz *= scale.sz;
138             return true;
139         }
140         return false;
141     }
142 
143     public boolean isSupportedBy(GraphicsPanel p) {
144         return p.supports(Scaleable.class) || p.supports(Scaleable2D.class) || p.supports(Scaleable3D.class);
145     }
146 
147     protected void redoEdit() {
148         GraphicsPanel panel = getRecordPlot().getGraphicsPanel();
149         Scaleable3D scaleable3D = (Scaleable3D)panel.getFeature(Scaleable3D.class);
150         if (scaleable3D != null) {
151             scaleable3D.scale(sx, sy, sz);
152         } else {
153             Scaleable2D scaleable2D = (Scaleable2D)panel.getFeature(Scaleable2D.class);
154             if (scaleable2D != null) {
155                 scaleable2D.scale(sx, sy);
156             } else {
157                 Scaleable scaleable = (Scaleable)panel.getFeature(Scaleable.class);
158                 if (scaleable != null) {
159                     scaleable.scale(sx);
160                 } else {    
161                     System.err.println("Could not redo: "+this);
162                     return;
163                 }
164             }
165         }
166         getRecordPlot().repaint();
167         logger.finer(toString());
168     }
169 
170     protected void undoEdit() {
171         GraphicsPanel panel = getRecordPlot().getGraphicsPanel();
172         Scaleable3D scaleable3D = (Scaleable3D)panel.getFeature(Scaleable3D.class);
173         if (scaleable3D != null) {
174             scaleable3D.scale(1/sx, 1/sy, 1/sz);
175         } else {
176             Scaleable2D scaleable2D = (Scaleable2D)panel.getFeature(Scaleable2D.class);
177             if (scaleable2D != null) {
178                 scaleable2D.scale(1/sx, 1/sy);
179             } else {
180                 Scaleable scaleable = (Scaleable)panel.getFeature(Scaleable.class);
181                 if (scaleable != null) {
182                     scaleable.scale(1/sx);
183                 } else {    
184                     System.err.println("Could not undo: "+this);
185                     return;
186                 }
187             }
188         }
189         getRecordPlot().repaint();
190         logger.finer(toString());
191     }
192 }