1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.gene;
13 import java.util.Map;
14 import org.eclipse.draw2d.Graphics;
15 import org.eclipse.draw2d.geometry.Dimension;
16 import org.eclipse.draw2d.geometry.Rectangle;
17 import sk.baka.xml.gene.CoordinatorInputKey;
18 import sk.uniba.euromath.document.XMLAccess;
19 import sk.uniba.euromath.editor.EditorException;
20 import sk.uniba.euromath.editor.IRenderer;
21 import sk.uniba.euromath.gene.GeneDataProvider.RendererSite;
22 /***
23 * The context object for renderers and editors. Processes requests for
24 * rendering with child renderers, etc. Intended to be used by renderers and
25 * editors.
26 * @author Martin Vysny
27 */
28 public final class RendererContext {
29 /***
30 * XMLAccess instance.
31 */
32 public final XMLAccess xmlAccess;
33 /***
34 * <p>
35 * Constructor.
36 * </p>
37 * <p>
38 * Receives instances of two live maps - modified outside this object, in
39 * the {@link GeneDataProvider} class.
40 * </p>
41 * @param geneIdMapping Maps GENE-generated ids to the coordinator input key
42 * instance. Used by renderer context to map from GENE id (provided to the
43 * renderer) to child renderer instance.
44 * @param rendererKeys maps input pipe ID to a renderer instance.
45 * @param xmlAccess the source document instance.
46 */
47 RendererContext(final Map<String, CoordinatorInputKey> geneIdMapping,
48 Map<CoordinatorInputKey, RendererSite> rendererKeys,
49 XMLAccess xmlAccess) {
50 super();
51 this.xmlAccess = xmlAccess;
52 this.geneIdMapping = geneIdMapping;
53 this.rendererKeys = rendererKeys;
54 }
55 /***
56 * Returns canvas size of a renderer.
57 * @param id the identifier from appropriate <code>emp:mark</code>
58 * element.
59 * @return the canvas size in pixels.
60 * @throws EditorException if error occurs.
61 */
62 public Dimension getCanvasSize(final String id) throws EditorException {
63 final IRenderer renderer = getInfo(id).renderer;
64 return renderer.getCanvasSize();
65 }
66 /***
67 * <p>
68 * Asks renderer to paint its contents on given graphics into given
69 * rectangle.
70 * </p>
71 * <p>
72 * The function automatically adjusts the graphics coordinate system: rect's
73 * upper-left corner becomes the starting (0, 0) point, and a clipping area
74 * is set to the rectangle. When the renderer finishes, graphics coordinate
75 * system and clip rectangle is returned to its previous state.
76 * </p>
77 * @param id calls renderer with this ID.
78 * @param g graphics where to paint.
79 * @param rect here the renderer shall paint.
80 * @throws EditorException if error happens.
81 * @deprecated renderers should not attempt to render child nametrees. They
82 * should leave enough free space instead and leave the rendering to the
83 * editor framework.
84 */
85 @Deprecated
86 public void render(final String id, final Graphics g, final Rectangle rect)
87 throws EditorException {
88 final RendererSite info = getInfo(id);
89 g.pushState();
90 g.setClip(rect);
91
92 final IRenderer renderer = info.renderer;
93
94 renderer.render(g);
95 g.popState();
96 }
97 /***
98 * <p>
99 * Maps GENE-generated ids to the coordinator input key instance. Used by
100 * renderer context to map from GENE id (provided to the renderer) to child
101 * renderer instance.
102 * </p>
103 */
104 private final Map<String, CoordinatorInputKey> geneIdMapping;
105 /***
106 * Maps input pipe ID to a renderer instance.
107 */
108 private final Map<CoordinatorInputKey, RendererSite> rendererKeys;
109 /***
110 * Returns renderer info object.
111 * @param id id of the nametree.
112 * @return renderer info, never <code>null</code>.
113 * @throws EditorException if id is not known.
114 */
115 private RendererSite getInfo(final String id) throws EditorException {
116 final CoordinatorInputKey key = geneIdMapping.get(id);
117 if (key == null)
118 throw new EditorException("Nametree with id " + id
119 + " is not known.");
120 final RendererSite site = rendererKeys.get(key);
121 assert site != null;
122 return site;
123 }
124 }