1 /*
2 * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /* $Id: MathComponent.java,v 03dc0884e86f 2008/06/21 10:53:35 maxberger $ */
18
19 package net.sourceforge.jeuclid.awt;
20
21 import java.awt.Component;
22 import java.awt.Dimension;
23 import java.awt.Graphics;
24 import java.awt.Graphics2D;
25
26 import net.sourceforge.jeuclid.MutableLayoutContext;
27 import net.sourceforge.jeuclid.context.LayoutContextImpl;
28 import net.sourceforge.jeuclid.context.Parameter;
29 import net.sourceforge.jeuclid.layout.JEuclidView;
30
31 import org.w3c.dom.Document;
32
33 /**
34 * A class for displaying MathML content in a AWT Component.
35 *
36 * @see net.sourceforge.jeuclid.swing.JMathComponent
37 * @version $Revision: 03dc0884e86f $
38 */
39 public class MathComponent extends Component {
40 /**
41 * Logger for this class
42 */
43 // currently unused
44 // private static final Logger LOGGER =
45 // Logger.getLogger(MathComponent.class
46 // .getName());
47 /**
48 *
49 */
50 private static final long serialVersionUID = 1L;
51
52 /**
53 * Reference to the MathBase class.
54 */
55 private transient JEuclidView view;
56
57 private Document document;
58
59 private MutableLayoutContext parameters = new LayoutContextImpl(
60 LayoutContextImpl.getDefaultLayoutContext());
61
62 /**
63 * Default constructor.
64 */
65 public MathComponent() {
66 // do nothing.
67 }
68
69 /**
70 * Sets the rendering parameters.
71 *
72 * @param newParameters
73 * the set of parameters.
74 */
75 public final void setParameters(final MutableLayoutContext newParameters) {
76 this.parameters = newParameters;
77 }
78
79 /**
80 * @return the document
81 */
82 public Document getDocument() {
83 return this.document;
84 }
85
86 /**
87 * Gets the minimum size of this component.
88 *
89 * @return A dimension object indicating this component's minimum size.
90 */
91 @Override
92 public Dimension getMinimumSize() {
93 if (this.view == null) {
94 return new Dimension(1, 1);
95 } else {
96 return new Dimension((int) Math.ceil(this.view.getWidth()),
97 (int) Math.ceil(this.view.getAscentHeight()
98 + (int) Math.ceil(this.view.getDescentHeight())));
99 }
100 }
101
102 /**
103 * Gets the preferred size of this component.
104 *
105 * @return A dimension object indicating this component's preferred size.
106 */
107 @Override
108 public Dimension getPreferredSize() {
109 return this.getMinimumSize();
110 }
111
112 /**
113 * Paints this component.
114 *
115 * @param g
116 * The graphics context to use for painting.
117 */
118 @Override
119 public void paint(final Graphics g) {
120 super.paint(g);
121 if (this.view != null) {
122 this.view.draw((Graphics2D) g, 0, (int) Math.ceil(this.view
123 .getAscentHeight()));
124 }
125 }
126
127 private void redo() {
128 final Graphics2D g2d = (Graphics2D) this.getGraphics();
129 if ((this.document == null) || (g2d == null)) {
130 this.view = null;
131 } else {
132 this.view = new JEuclidView(this.document, this.parameters, g2d);
133 }
134 this.repaint();
135 }
136
137 /**
138 * Enables, or disables the debug mode.
139 *
140 * @param debugMode
141 * Debug mode.
142 */
143 public void setDebug(final boolean debugMode) {
144 this.parameters.setParameter(Parameter.DEBUG, debugMode);
145 this.redo();
146 }
147
148 /**
149 * @param newDocument
150 * the document to set
151 */
152 public void setDocument(final Document newDocument) {
153 this.document = newDocument;
154 this.redo();
155 }
156
157 }