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,v 03dc0884e86f 2008/06/21 10:53:35 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 026 import net.sourceforge.jeuclid.MutableLayoutContext; 027 import net.sourceforge.jeuclid.context.LayoutContextImpl; 028 import net.sourceforge.jeuclid.context.Parameter; 029 import net.sourceforge.jeuclid.layout.JEuclidView; 030 031 import org.w3c.dom.Document; 032 033 /** 034 * A class for displaying MathML content in a AWT Component. 035 * 036 * @see net.sourceforge.jeuclid.swing.JMathComponent 037 * @version $Revision: 03dc0884e86f $ 038 */ 039 public class MathComponent extends Component { 040 /** 041 * Logger for this class 042 */ 043 // currently unused 044 // private static final Logger LOGGER = 045 // Logger.getLogger(MathComponent.class 046 // .getName()); 047 /** 048 * 049 */ 050 private static final long serialVersionUID = 1L; 051 052 /** 053 * Reference to the MathBase class. 054 */ 055 private transient JEuclidView view; 056 057 private Document document; 058 059 private MutableLayoutContext parameters = new LayoutContextImpl( 060 LayoutContextImpl.getDefaultLayoutContext()); 061 062 /** 063 * Default constructor. 064 */ 065 public MathComponent() { 066 // do nothing. 067 } 068 069 /** 070 * Sets the rendering parameters. 071 * 072 * @param newParameters 073 * the set of parameters. 074 */ 075 public final void setParameters(final MutableLayoutContext newParameters) { 076 this.parameters = newParameters; 077 } 078 079 /** 080 * @return the document 081 */ 082 public Document getDocument() { 083 return this.document; 084 } 085 086 /** 087 * Gets the minimum size of this component. 088 * 089 * @return A dimension object indicating this component's minimum size. 090 */ 091 @Override 092 public Dimension getMinimumSize() { 093 if (this.view == null) { 094 return new Dimension(1, 1); 095 } else { 096 return new Dimension((int) Math.ceil(this.view.getWidth()), 097 (int) Math.ceil(this.view.getAscentHeight() 098 + (int) Math.ceil(this.view.getDescentHeight()))); 099 } 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 }