View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the License); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.foRenderer.figures;
13  
14  import java.util.List;
15  
16  import org.apache.fop.area.MainReference;
17  import org.eclipse.draw2d.Graphics;
18  import org.eclipse.draw2d.IFigure;
19  import org.eclipse.draw2d.geometry.Dimension;
20  import org.eclipse.draw2d.geometry.Point;
21  import org.eclipse.draw2d.geometry.Rectangle;
22  
23  /***
24   * @author TV
25   * Created on 8.5.2004
26   *
27   */
28  public class MainReferenceFigure extends BasicFOPFigure {
29    
30    private int columnGap = 0;
31    
32    public MainReferenceFigure(MainReference mainRef) {
33      super(mainRef);
34      setLayoutManager(new MainReferenceLayoutManager());
35    }
36  
37    public BodyRegionFigure getBodyRegion() {
38      IFigure parent = getParent();
39      if (parent instanceof BodyRegionFigure){
40        return (BodyRegionFigure)parent;
41      }
42      return null;
43    }
44    
45    public int getColumnGap() {
46      return this.columnGap;
47    }
48    
49    /* (non-Javadoc)
50     * @see org.eclipse.draw2d.Figure#paintChildren(org.eclipse.draw2d.Graphics)
51     */
52    protected void paintChildren(Graphics graphics) {
53      super.paintChildren(graphics);
54    }
55    
56    public void setColumnGap(int pixels) {
57      this.columnGap = pixels;
58    }
59    
60  //*****************************************************************************//
61  //                                                                            //
62  //                                                                            //
63  //                                                                            //
64  //*****************************************************************************//
65    
66    
67    public class MainReferenceLayoutManager extends BaseFOLayout {
68    
69      protected BodyRegionFigure body;
70      protected MainReferenceFigure mainRef;
71      
72      public MainReferenceLayoutManager() {
73        super();
74      }
75      
76      /***
77       * 
78       * MainReference's size is not dependant on size of its children, but
79       * on space provided by its parent <code>BodyRegionFigure</code> instance.
80       */
81      protected Dimension calculatePreferredSize(IFigure parent, int wHint,
82        int hHint) {
83        
84        setFigure(parent);
85        
86        // if calculation was already done, return its result:
87        if (preferredSize != null)
88          return preferredSize;
89        
90        // the calculation itself:
91        if ((body.getBeforeFloat() != null) || (body.getFootnote() != null)){
92      	  //TODO : Kollar - this is probably wrong
93      	  // ther should by some shifting or smth
94          preferredSize = body.getBounds().getSize();
95          preferredSize.width -= body.getMarginLeft();
96          preferredSize.width -= body.getMarginRight();
97          preferredSize.height -= body.getMarginTop();
98          preferredSize.height -= body.getMarginBottom();
99        } else {
100           preferredSize = body.getBounds().getSize();
101           preferredSize.width -= body.getMarginLeft();
102           preferredSize.width -= body.getMarginRight();
103           preferredSize.height -= body.getMarginTop();
104           preferredSize.height -= body.getMarginBottom();
105       }
106       
107       return preferredSize;
108     }
109     
110     /* (non-Javadoc)
111      * @see org.eclipse.draw2d.LayoutManager#layout(org.eclipse.draw2d.IFigure)
112      */
113     public void layout(IFigure parent) {
114       setFigure(parent);
115       
116       Point origin = getOrigin(parent);
117       
118       IFigure child;
119       Dimension size;
120       int offsetY = 0;
121       
122       List children = getChildren();
123       for (int i = 0; i < children.size(); i++) {
124         child = (IFigure)children.get(i);
125         size = child.getPreferredSize();
126         child.setBounds(new Rectangle(origin, size));
127         origin.y += size.height;
128       }
129     }
130     
131     /***
132      * Prepares layouter for work with given figure, if neccessary. After 
133      * checking given figure, <code>invalidate()</code> is called if needed.
134      * 
135      * @param fig Figure which has to be layed out.
136      */
137     protected void setFigure(IFigure fig) {
138       assert(fig != null);
139       
140       if (fig == mainRef)
141         return;
142       
143       if (fig instanceof MainReferenceFigure) {
144         invalidate();
145         mainRef = (MainReferenceFigure)fig;
146         body = mainRef.getBodyRegion();
147         assert(body != null);
148       } else {
149         throw new IllegalArgumentException();
150       }
151     }
152     
153     /* (non-Javadoc)
154      * @see org.eclipse.draw2d.LayoutManager#invalidate()
155      */
156     public void invalidate() {
157       super.invalidate();
158       mainRef = null;
159       body = null;
160     }
161     
162   }
163 }