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 org.apache.fop.area.Area;
15  import org.apache.fop.area.RegionViewport;
16  import org.apache.fop.fo.Constants;
17  import org.eclipse.draw2d.ColorConstants;
18  // import org.eclipse.draw2d.FigureUtilities;
19  import org.eclipse.draw2d.Graphics;
20  import org.eclipse.draw2d.IFigure;
21  import org.eclipse.draw2d.Layer;
22  import org.eclipse.draw2d.ScalableLayeredPane;
23  import org.eclipse.draw2d.geometry.Dimension;
24  import org.eclipse.draw2d.geometry.Insets;
25  import org.eclipse.draw2d.geometry.Point;
26  import org.eclipse.draw2d.geometry.Rectangle;
27  import org.eclipse.swt.graphics.Color;
28  // import org.eclipse.swt.graphics.RGB;
29  
30  import sk.uniba.euromath.editor.figures.IEMFigure;
31  // import sk.uniba.euromath.foRenderer.config.FOViewConsts;
32  import sk.uniba.euromath.foRenderer.config.GlobalFOPFigureCfg;
33  
34  /***
35   * @author TV Created on 19.1.2004
36   * 
37   */
38  
39  // Layers: Paper, WaterMark, Writing, Decoration, User-ACTION-layer
40  public class PageFigure extends ScalableLayeredPane implements FOPFigure,
41                  IEMFigure {
42  
43          /***
44           * Margin color
45           */
46          private static Color marginColor = ColorConstants.white;
47  
48          /***
49           * Paper layer
50           */
51          public static final String LAYER_PAPER = "Paper Layer";
52  
53          /***
54           * Background logo, ...
55           */
56          public static final String LAYER_WATERMARK = "WaterMark Layer";
57  
58          /***
59           * Writing layer functions as basic destination for all textual
60           * information belonging to the data being rendered. Printable Layer
61           */
62          public static final String LAYER_WRITING = "Writing Layer";
63  
64          /*** for rulers, figure borders, etc. */
65          public static final String LAYER_DECORATION = "Decoration Layer";
66  
67          /*** handles??? */
68          public static final String LAYER_ACTION = "User Action Layer";
69  
70          private static final String[] LAYER_LIST = { LAYER_PAPER,
71                          LAYER_WATERMARK, LAYER_WRITING, LAYER_DECORATION,
72                          LAYER_ACTION };
73  
74          // Regions of the page (as defined in XSL-FO): before, start, body, end,
75          // after
76          private RegionFigure regionBefore = null;
77  
78          private RegionFigure regionAfter = null;
79  
80          private RegionFigure regionStart = null;
81  
82          private RegionFigure regionEnd = null;
83  
84          private RegionFigure regionBody = null;
85  
86          private int pageWidth = 0;
87  
88          private int pageHeight = 0;
89  
90          private Insets margins;
91  
92          /*** Constructor */
93          public PageFigure() {
94                  super();
95                  setOpaque(true);
96                  setBackgroundColor(ColorConstants.white);
97  
98                  createLayers();
99                  // setBorder(new LineRuleBorder());
100                 setLayoutManager(new PageRegionLayoutManager(this));
101         }
102 
103         /***
104          * Constructor
105          * 
106          * @param pageSize
107          *                size of page
108          * @param margins
109          * @see Insets
110          */
111         public PageFigure(Dimension pageSize, Insets margins) {
112                 this();
113                 this.pageWidth = pageSize.width;
114                 this.pageHeight = pageSize.height;
115                 setPreferredSize(pageSize.expand(2, 2));
116                 this.margins = margins;
117         }
118 
119         /***
120          * Adds the given layer figure, identifiable with the given key, at the
121          * specified index. While adding the layer, it informs the surrounding
122          * layers of the addition.
123          * 
124          * @param figure
125          *                the layer
126          * @param layerKey
127          *                the layer's key
128          * @param index
129          *                the index where the layer should be added
130          */
131         public void add(IFigure figure, Object layerKey, int index) {
132                 if (figure instanceof RegionFigure) {
133                         RegionViewport region = (RegionViewport) ((RegionFigure) figure)
134                                         .getArea();
135 
136                         if (region.getRegionReference().getRegionClass() == Constants.FO_REGION_BEFORE) {
137                                 regionBefore = (RegionFigure) figure;
138                         } else if (region.getRegionReference().getRegionClass() == Constants.FO_REGION_AFTER) {
139                                 regionAfter = (RegionFigure) figure;
140                         } else if (region.getRegionReference().getRegionClass() == Constants.FO_REGION_START) {
141                                 regionStart = (RegionFigure) figure;
142                         } else if (region.getRegionReference().getRegionClass() == Constants.FO_REGION_END) {
143                                 regionEnd = (RegionFigure) figure;
144                         } else if (region.getRegionReference().getRegionClass() == Constants.FO_REGION_BODY) {
145                                 regionBody = (RegionFigure) figure;
146                         }
147                         super.add(figure, layerKey, index);
148                 }
149 
150         }
151 
152         protected void createLayers() {
153                 for (int i = 0; i < LAYER_LIST.length; i++) {
154                         add(new Layer(), LAYER_LIST[i], -1);
155                 }
156         }
157 
158         /***
159          * @return <code> null </code>
160          */
161         public Area getArea() {
162                 return null;
163         }
164 
165         /***
166          * @return configuration object
167          * @param key
168          */
169         public Object getConfig(Object key) {
170                 return GlobalFOPFigureCfg.getConfig().getData(key,
171                                 this.getClass());
172         }
173 
174         /***
175          * @return <code> null </code>
176          */
177         public Object getModel() {
178                 return null;
179         }
180 
181         /***
182          * @return <code> true </code>
183          */
184         public boolean needsEditPart() {
185                 return true;
186         }
187 
188         /***
189          * @return null
190          */
191         public String getID() {
192                 return null;
193         }
194 
195         /***
196          * @see org.eclipse.draw2d.IFigure#getMaximumSize()
197          */
198         public Dimension getMaximumSize() {
199                 return getPreferredSize();
200         }
201 
202         /***
203          * 
204          * @param code
205          *                of Region
206          * @return RegionFigure spicified with code
207          */
208         public RegionFigure getRegion(int code) {
209                 if (code == Constants.FO_REGION_AFTER) {
210                         return regionAfter;
211                 } else if (code == Constants.FO_REGION_BEFORE) {
212                         return regionBefore;
213                 } else if (code == Constants.FO_REGION_BODY) {
214                         return regionBody;
215                 } else if (code == Constants.FO_REGION_END) {
216                         return regionEnd;
217                 } else if (code == Constants.FO_REGION_START) {
218                         return regionStart;
219                 } else {
220                         return null;
221                 }
222         }
223 
224         /*
225          * protected String getCoordinatesString() { Rectangle r = getBounds();
226          * return "[" + r.x + "," + r.y + "]x[" + r.width + "," + r.height +
227          * "]"; }
228          * 
229          * 
230          * protected Color getMarkingColor() { RGB rgb = (RGB)
231          * getConfig(FOViewConsts.BOUNDING_RGB); if (rgb != null) { return new
232          * Color(null, rgb); } return new Color(null,
233          * ColorConstants.green.getRGB()); }
234          * 
235          * protected void paintBoundingBox(Graphics g) { Color old =
236          * g.getForegroundColor(); Color markClr = getMarkingColor();
237          * g.setForegroundColor(markClr);
238          * g.translate(getClientArea().getLocation()); g.drawRectangle(0, 0,
239          * getSize().width - getInsets().getWidth(), getSize().height -
240          * getInsets().getHeight()); markClr.dispose();
241          * g.translate(getClientArea().getLocation().getNegated());
242          * g.setForegroundColor(old); }
243          * 
244          * 
245          * protected void paintSizeData(Graphics g) { String str =
246          * BasicFOPFigure.getCoordinatesString(this, true);
247          * 
248          * Dimension size = FigureUtilities.getTextExtents(str, getFont());
249          * Rectangle start = getClientArea();
250          * 
251          * g.translate(start.getLocation()); g.drawText(str, start.width -
252          * size.width - 1, start.height - size.height - 1);
253          * g.translate(start.getLocation().getNegated()); }
254          * 
255          * protected void paintDebug(Graphics g) {
256          * 
257          * Object data; data = getConfig(FOViewConsts.BOUNDING_BOX); if ((data !=
258          * null) && (data.equals(FOViewConsts.ON))) { paintBoundingBox(g); }
259          * 
260          * data = getConfig(FOViewConsts.BOUNDING_TXT); if ((data != null) &&
261          * (data.equals(FOViewConsts.ON))) { paintSizeData(g); } }
262          */
263         protected void paintFigure(Graphics g) {
264                 g.fillRectangle(getBounds());
265 
266                 // paintDebug(g);
267                 paintMargins(g);
268 
269         }
270 
271         protected void paintMargins(Graphics g) {
272                 Rectangle area = getClientArea();
273 
274                 Color background = g.getBackgroundColor();
275                 g.translate(area.getLocation());
276                 g.setBackgroundColor(marginColor);
277 
278                 if (margins.left != 0) {
279                         g.fillRectangle(0, 0, margins.left - 1, pageHeight - 1);
280                 }
281 
282                 if (margins.top != 0) {
283                         g.fillRectangle(0, 0, pageWidth - 1, margins.top - 1);
284                 }
285 
286                 if (margins.right != 0) {
287                         g.fillRectangle(pageWidth - margins.right, 0,
288                                         margins.right - 1, pageHeight - 1);
289                 }
290 
291                 if (margins.bottom != 0) {
292                         g.fillRectangle(0, pageHeight - margins.bottom,
293                                         pageWidth - 1, margins.bottom - 1);
294                 }
295 
296                 g.setBackgroundColor(background);
297                 g.translate(area.getLocation().getNegated());
298         }
299 
300         /***
301          * @see sk.uniba.euromath.fo.figures.FOPFigure#setPixelDimensions(org.eclipse.draw2d.geometry.Rectangle)
302          * @deprecated
303          * @param r
304          *                size in pixels of Figures rectangle
305          */
306         public void setPixelDimensions(Rectangle r) {
307                 pageWidth = r.width;
308                 pageHeight = r.height;
309                 setSize(r.getSize());
310         }
311 
312         private class PageRegionLayoutManager extends BaseFOLayout {
313 
314                 private PageFigure page = null;
315 
316                 private int verticalExtent, horizontalExtent;
317 
318                 PageRegionLayoutManager(PageFigure page) {
319                         setPage(page);
320                 }
321 
322                 /***
323                  * Calculates the preferred size of the given figure, using
324                  * width and height hints.
325                  * 
326                  * @param parent
327                  *                The figure
328                  * @param wHint
329                  *                The width hint
330                  * @param hHint
331                  *                The height hint
332                  * @return The preferred size
333                  */
334                 protected Dimension calculatePreferredSize(IFigure parent,
335                                 int wHint, int hHint) {
336                         setPage(parent);
337 
338                         if (preferredSize == null) {
339                                 Insets i = parent.getInsets();
340                                 preferredSize = page.getSize().getCopy()
341                                                 .expand(i.getWidth(),
342                                                                 i.getHeight());
343                         }
344                         return preferredSize;
345                 }
346 
347                 private BodyRegionFigure getBody() {
348                         return (BodyRegionFigure) page
349                                         .getRegion(Constants.FO_REGION_BODY);
350                 }
351 
352                 /***
353                  * @see org.eclipse.draw2d.LayoutManager#layout(org.eclipse.draw2d.IFigure)
354                  */
355                 public void layout(IFigure parent) {
356                         setPage(parent);
357 
358                         Point origin = /* new Point(3, 3); */getOrigin(parent);
359                         RegionFigure region;
360 
361                         verticalExtent = 0;
362                         horizontalExtent = 0;
363 
364                         region = page.getRegion(Constants.FO_REGION_BEFORE);
365                         if (region != null) {
366                                 layoutBefore(region, origin);
367                         }
368 
369                         region = page.getRegion(Constants.FO_REGION_AFTER);
370                         if (region != null) {
371                                 layoutAfter(region, origin);
372                         }
373 
374                         region = page.getRegion(Constants.FO_REGION_START);
375                         if (region != null) {
376                                 layoutStart(region, origin);
377                         }
378 
379                         region = page.getRegion(Constants.FO_REGION_END);
380                         if (region != null) {
381                                 layoutEnd(region, origin);
382                         }
383 
384                         layoutBodyRegion(getBody(), origin);
385 
386                 }
387 
388                 protected void layoutBodyRegion(BodyRegionFigure body,
389                                 Point origin) {
390                         Rectangle data = body.getViewportRectangle();
391 
392                         // we need to change size & loc of body region if
393                         // before, after,
394                         // start or
395                         // end regions are present
396                         data.translate(origin);
397 
398                         // data.translate(offsetX, offsetY);
399                         // data.width -= horizontalExtent;
400                         // data.height -= verticalExtent;
401 
402                         body.setBounds(data);
403 
404                 }
405 
406                 private void layoutAfter(RegionFigure after, Point origin) {
407                         Rectangle data = after.getViewportRectangle().getCopy();
408                         verticalExtent += data.height;
409                         setRegionBounds(after, data.getLocation().translate(
410                                         origin), data.getSize());
411                 }
412 
413                 private void layoutBefore(RegionFigure before, Point origin) {
414                         Rectangle data = before.getViewportRectangle();
415                         verticalExtent += data.height;
416                         setRegionBounds(before, data.getLocation().translate(
417                                         origin), data.getSize());
418                 }
419 
420                 private void layoutEnd(RegionFigure before, Point origin) {
421                         Rectangle data = before.getViewportRectangle();
422                         horizontalExtent += data.width;
423                         setRegionBounds(before, data.getLocation().translate(
424                                         origin), data.getSize());
425                 }
426 
427                 private void layoutStart(RegionFigure before, Point origin) {
428                         Rectangle data = before.getViewportRectangle();
429                         horizontalExtent += data.width;
430                         setRegionBounds(before, data.getLocation().translate(
431                                         origin), data.getSize());
432                 }
433 
434                 protected void setRegionBounds(RegionFigure region, Point loc,
435                                 Dimension size) {
436                         if (region == null)
437                                 return;
438                         region.setBounds(new Rectangle(loc, size));
439                 }
440 
441                 protected void setPage(IFigure parent) {
442                         assert (parent != null);
443 
444                         if (parent instanceof PageFigure) {
445                                 page = (PageFigure) parent;
446                                 // assert(getBody() != null);
447                                 invalidate();
448                         }
449                 }
450 
451                 /***
452                  * @see org.eclipse.draw2d.AbstractLayout#getBorderPreferredSize(org.eclipse.draw2d.IFigure)
453                  */
454                 protected Dimension getBorderPreferredSize(IFigure container) {
455                         if (container != null) {
456                                 Insets i = container.getBorder().getInsets(
457                                                 container);
458                                 return new Dimension(i.getWidth(), i
459                                                 .getHeight());
460                         }
461                         return super.getBorderPreferredSize(container);
462                 }
463         }
464 
465 }