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.RecordPlot;
13  import hep.wired.feature.Translateable;
14  import hep.wired.feature.Rotateable3D;
15  import hep.wired.feature.HasBoundingBox;
16  
17  /***
18   * Fits plot to window by translation and scaling. If rotation is available a fit (translate and scale)
19   * will also be done perpendicular to the visible plane.
20   *
21   * @author Mark Donszelmann
22   * @version $Id: FitToWindow.java 652 2005-03-01 01:12:05Z duns $
23   */
24  public class FitToWindow extends WiredCompoundEdit implements GraphicsPanelEdit {
25  
26      private static Logger logger = Logger.getLogger(FitToWindow.class.getPackage().getName());
27  
28      /***
29       * Creates a Fit to Window edit.
30       */
31      public FitToWindow() {
32          super("FitToWindow");
33      }
34      
35      public WiredCompoundEdit copy(RecordPlot plot) {
36          WiredCompoundEdit copy = new FitToWindow();
37          copy.setRecordPlot(plot);
38  
39          GraphicsPanel panel = plot.getGraphicsPanel();
40  
41          Rectangle2D bbUV = ((HasBoundingBox)panel.getFeature(HasBoundingBox.class)).getBoundingBoxForPlot();
42          
43          // bail out if no bounding box, or no size on plot, but keep edit.
44          if ((bbUV == null) || (plot.getWidth() == 0) || (plot.getHeight() == 0)) {
45              copy.addEdit(new NoOperation().copy(plot));
46              copy.end();
47              return copy;
48          }
49          double du = bbUV.getX()+bbUV.getWidth()/2 -plot.getWidth()/2;
50          double dv = bbUV.getY()+bbUV.getHeight()/2-plot.getHeight()/2;
51          double dz = 0;
52  //System.out.println(new Rectangle((int)bbUV.getX(), (int)bbUV.getY(), (int)bbUV.getWidth(), (int)bbUV.getHeight())+" "+du+" "+dv);
53                          
54          // make a copy, rotate and calculate dz if possible
55          if (panel.supports(Rotateable3D.class)) {
56  // FIXME: does NOT work correctly, seems to keep old information, see WIRED-225 and WIRED-227
57  //            RecordPlot wvPlot = plot.copy("tmp-copy", plot.getRecord(), false);
58              RecordPlot wvPlot = plot;
59              wvPlot.postEdit(new Rotate(Math.PI/2, 0, 1, 0));
60              Rectangle2D bbWV = ((HasBoundingBox)wvPlot.getGraphicsPanel().getFeature(HasBoundingBox.class)).getBoundingBoxForPlot();
61              // use plot here since wvPlot is not visible (width = 0).
62              dz = (bbWV.getX()+bbWV.getWidth()/2 -plot.getWidth()/2);            
63  //System.out.println(new Rectangle((int)bbWV.getX(), (int)bbWV.getY(), (int)bbWV.getWidth(), (int)bbWV.getHeight())+" "+dz);
64              wvPlot.postEdit(new Rotate(-Math.PI/2, 0, 1, 0));
65          }
66  
67          // center
68          Translateable translateable = (Translateable)plot.getGraphicsPanel().getFeature(Translateable.class);
69          double t[] = translateable.getModelTranslation(new double[] { -du, dv, -dz }, plot.getGraphicsPanel().getViewPort());             
70  //System.out.println("Plot: "+plot.getWidth()+" "+plot.getHeight());
71  //System.out.println(t[0]+" "+t[1]+" "+t[2]/*+tz[0]+" "+tz[1]*/);
72          Translate translate = new Translate(t[0], t[1], t[2], bbUV, 5);
73          Shape shape = translate.createTransformedShape((Component)plot, bbUV);
74          copy.addEdit(translate.copy(plot));
75  
76          // zoom
77          double sx = plot.getWidth() / (bbUV.getWidth() + 1);
78          double sy = plot.getHeight() / (bbUV.getHeight() + 1);
79          double s = Math.min(sx, sy);
80          copy.addEdit(new Scale(s, s, s, shape, 5).copy(plot));
81          copy.end();
82          return copy;
83      }
84      
85      public boolean isSupportedBy(GraphicsPanel p) {
86          Translate t = new Translate();
87          Scale s = new Scale();
88          return t.isSupportedBy(p) && s.isSupportedBy(p);
89      }
90  
91      public boolean addEdit(UndoableEdit edit) {
92          if (edit instanceof FitToWindow) {
93              // absorb
94              return true;
95          }
96          return super.addEdit(edit);
97      }
98  }