001 /*
002 * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 /* $Id: MathComponent.java 310 2007-05-18 20:26:36Z maxberger $ */
018
019 package net.sourceforge.jeuclid.awt;
020
021 import java.awt.Component;
022 import java.awt.Dimension;
023 import java.awt.Graphics;
024 import java.awt.Graphics2D;
025 import java.util.Map;
026
027 import net.sourceforge.jeuclid.DOMBuilder;
028 import net.sourceforge.jeuclid.MathBase;
029 import net.sourceforge.jeuclid.ParameterKey;
030
031 import org.w3c.dom.Document;
032
033 /**
034 * A class for displaying MathML content in a AWT Component.
035 *
036 * @author Unknown, Max Berger
037 * @see net.sourceforge.jeuclid.swing.JMathComponent
038 * @version $Revision: 310 $
039 */
040 public class MathComponent extends Component {
041 /**
042 * Logger for this class
043 */
044 // currently unused
045 // private static final Logger LOGGER =
046 // Logger.getLogger(MathComponent.class
047 // .getName());
048 /**
049 *
050 */
051 private static final long serialVersionUID = 1L;
052
053 /**
054 * Reference to the MathBase class.
055 */
056 private MathBase base;
057
058 private boolean debug;
059
060 private Document document;
061
062 private Map<ParameterKey, String> parameters = MathBase
063 .getDefaultParameters();
064
065 /**
066 * Default constructor.
067 */
068 public MathComponent() {
069 // do nothing.
070 }
071
072 /**
073 * Sets the rendering parameters.
074 *
075 * @param newParameters
076 * the set of parameters.
077 */
078 public final void setParameters(
079 final Map<ParameterKey, String> newParameters) {
080 this.parameters = newParameters;
081 }
082
083 /**
084 * @return the document
085 */
086 public Document getDocument() {
087 return this.document;
088 }
089
090 /**
091 * Gets the mininimum size of this component.
092 *
093 * @return A dimension object indicating this component's minimum size.
094 */
095 @Override
096 public Dimension getMinimumSize() {
097 if (this.base == null) {
098 return new Dimension(1, 1);
099 } else {
100 final Graphics2D g2d = (Graphics2D) this.getGraphics();
101 return new Dimension((int) Math.ceil(this.base.getWidth(g2d)),
102 (int) Math.ceil(this.base.getHeight(g2d)));
103 }
104 }
105
106 /**
107 * Gets the preferred size of this component.
108 *
109 * @return A dimension object indicating this component's preferred size.
110 */
111 @Override
112 public Dimension getPreferredSize() {
113 return this.getMinimumSize();
114 }
115
116 /**
117 * Paints this component.
118 *
119 * @param g
120 * The graphics context to use for painting.
121 */
122 @Override
123 public void paint(final Graphics g) {
124 super.paint(g);
125 if (this.base != null) {
126 this.base.paint((Graphics2D) g);
127 }
128 }
129
130 private void redo() {
131 if (this.document != null) {
132 this.base = new MathBase(this.parameters);
133 new DOMBuilder(this.document, this.base);
134 this.base.setDebug(this.debug);
135 } else {
136 this.base = null;
137 }
138 this.repaint();
139 }
140
141 /**
142 * Enables, or disables the debug mode.
143 *
144 * @param debugMode
145 * Debug mode.
146 */
147 public void setDebug(final boolean debugMode) {
148 this.debug = debugMode;
149 this.redo();
150 }
151
152 /**
153 * @param newDocument
154 * the document to set
155 */
156 public void setDocument(final Document newDocument) {
157 this.document = newDocument;
158 this.redo();
159 }
160
161 }