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.awt.geom.Rectangle2D;
15  import java.util.List;
16  import java.util.Vector;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.fop.area.Area;
20  import org.apache.fop.area.RegionViewport;
21  import org.apache.fop.area.Trait;
22  import org.apache.fop.datatypes.ColorType;
23  import org.eclipse.draw2d.geometry.Point;
24  import org.eclipse.draw2d.geometry.Rectangle;
25  import org.eclipse.swt.graphics.Color;
26  import org.eclipse.swt.graphics.RGB;
27  
28  import sk.uniba.euromath.foRenderer.Draw2dRenderer;
29  import sk.uniba.euromath.foRenderer.FORenderer;
30  import sk.uniba.euromath.foRenderer.IRenderingStateProvider;
31  
32  // ----------------------------------------------------------------------------//
33  // ----------------------------------------------------------------------------//
34  
35  // Each descendant of this class has to have a constructor with no parameters.
36  
37  public class FOPFigureBuilder {
38  
39          private static final List EMPTY_LIST = new Vector();
40  
41          private FOPFigureFactory factory = null;
42  
43          // private FOP2ScreenConverter converter = null;
44          private IRenderingStateProvider renderingInfo = null;
45  
46          protected Area currentArea = null;
47  
48          protected FOPFigure currentFigure = null;
49  
50          protected Rectangle currentPos = null;
51  
52          // internal config data:
53          protected boolean paintBounding = true;
54  
55          protected boolean paintSizeAndPos = true;
56  
57          public FOPFigureBuilder() {
58          }
59  
60          /***
61           * Structure defined below should be sufficient for most cases.
62           * Inherited classes should implement only <code>canStartBuilding</code>,
63           * <code>createNewFigure</code> and <code>customizeFigure</code>
64           * methods.
65           * 
66           * @param area
67           *                Area from FO AreaTree
68           * @return created FOPFigure
69           */
70          public FOPFigure build(Object area) {
71                  setCurrentArea((Area) area);
72                  if (canStartBuilding()) {
73                          currentFigure = createNewFigure();
74                          customizeFigure(currentFigure);
75                  } else {
76                          throw new IllegalArgumentException();
77                  }
78                  // currentFigure.setID(retrieveID(currentArea));
79                  currentPos = null;
80                  return currentFigure;
81          }
82  
83          public FOPFigure build(Object area, int x, int y, int w, int h) {
84                  storePosition(x, y, w, h);
85                  return build(area);
86          }
87  
88          /***
89           * Checks builder's state and input prior to the process of new Figure
90           * creation.
91           * 
92           * <p>
93           * All significant data should be accesible through accessor methods or
94           * as internal data members.
95           * </p>
96           * 
97           * @return <code>true</code> if the builder is ready to create figure
98           */
99          protected boolean canStartBuilding() {
100                 return (currentArea != null);
101         }
102 
103         public void configure(Object key, Object value) {
104         }
105 
106         // just a shortcut, usable in all descendants
107         protected Rectangle convert(int x, int y, int w, int h) {
108                 return FOFigureUtils.mps2pxs(x, y, w, h);
109         }
110 
111         protected Point convert(int x, int y) {
112                 return FOFigureUtils.mps2pxs(x, y);
113         }
114 
115         // / !!! ///
116         /***
117          * This method creates new instance of a FOPFigure class.
118          * 
119          */
120         protected FOPFigure createNewFigure() {
121                 return new BasicFOPFigure(currentArea);
122         }
123 
124         /***
125          * This method is responsible for setting correct values to figures
126          * properties. Two basic cases are - location and font !!!
127          * 
128          * @param figure
129          *                a figure which properties has to be customized.
130          */
131         protected void customizeFigure(FOPFigure figure) {
132                 setBackground(figure);
133                 if (currentPos != null)
134                         relocAndResize(figure, convert(currentPos.x,
135                                         currentPos.y, currentPos.width,
136                                         currentPos.height));
137                 else
138                         relocAndResize(figure, getRenderingState());
139         }
140 
141         // / !!! ///
142 
143         public List getChildrenAreas() {
144                 return EMPTY_LIST;
145         }
146 
147         /*
148          * public FOP2ScreenConverter getConverter(){ return this.converter; }
149          */
150 
151         public FOPFigureFactory getFactory() {
152                 return factory;
153         }
154 
155         public Point getRegionOffset() {
156 
157                 RegionFigure region = (RegionFigure) getFactory()
158                                 .getLastFigure(RegionFigure.class);
159                 if (region != null) {
160                         Rectangle2D r = ((RegionViewport) region.getArea())
161                                         .getViewArea();
162                         return convert((int) r.getX(), (int) r.getY());
163                 }
164                 return new Point(0, 0);
165         }
166 
167         public IRenderingStateProvider getRenderingState() {
168                 return this.renderingInfo;
169         }
170 
171         protected void relocAndResize(FOPFigure figure, Rectangle r) {
172                 figure
173                                 .setLocation(r.getLocation().translate(
174                                                 getRegionOffset()));
175                 figure.setSize(r.getSize());
176                 figure.setPixelDimensions(r);
177         }
178 
179         protected void relocAndResize(FOPFigure figure,
180                         IRenderingStateProvider fopLoc) {
181         }
182 
183         public static String retrieveID(Area area) {
184                 final String id = (String) area.getTrait(Trait.PROD_ID);
185                 final String nullId = StringUtils.defaultIfEmpty(id, null);
186                 return nullId;
187         }
188 
189         protected void setBackground(FOPFigure figure) {
190                 Area a = figure.getArea();
191                 if (a == null)
192                         return;
193 
194                 Trait.Background tBack;
195                 ColorType ct;
196                 tBack = (Trait.Background) a.getTrait(Trait.BACKGROUND);
197                 if (tBack == null)
198                         return;
199 
200                 ct = tBack.getColor();
201                 if (ct != null) {
202                         figure.setBackgroundColor(new Color(null, new RGB(ct
203                                         .getAWTColor().getRed(), ct
204                                         .getAWTColor().getGreen(), ct
205                                         .getAWTColor().getBlue())));
206                         figure.setOpaque(true);
207                 }
208         }
209 
210         /*
211          * public void setConverter(FOP2ScreenConverter converter){
212          * this.converter = converter; }
213          */
214         protected void setCurrentArea(Area area) {
215                 this.currentArea = area;
216         }
217 
218         public void setFactory(FOPFigureFactory factory) {
219                 this.factory = factory;
220         }
221 
222         public void setRenderingState(IRenderingStateProvider provider) {
223                 this.renderingInfo = provider;
224         }
225 
226         protected void storePosition(int x, int y, int w, int h) {
227                 currentPos = new Rectangle(x, y, w, h);
228         }
229 
230         /***
231          * Getter for draw2d renderer.
232          */
233         public Draw2dRenderer getDraw2dRenderer() {
234                 return getFactory().getRenderer();
235         }
236 
237         /***
238          * Getter for FO renderer.
239          */
240         public FORenderer getFORenderer() {
241                 return getFactory().getRenderer().getFORenderer();
242         }
243 
244 }