View Javadoc

1   /*
2    * Created on Jul 25, 2005. 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.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  		// get renderer instance
92  		final IRenderer renderer = info.renderer;
93  		// render
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 //$NON-NLS-1$
119 					+ " is not known."); //$NON-NLS-1$
120 		final RendererSite site = rendererKeys.get(key);
121 		assert site != null;
122 		return site;
123 	}
124 }