Coverage Report - net.sourceforge.jeuclid.awt.MathComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
MathComponent
0%
0/26
0%
0/8
1,556
 
 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  0
     private MutableLayoutContext parameters = new LayoutContextImpl(
 60  
             LayoutContextImpl.getDefaultLayoutContext());
 61  
 
 62  
     /**
 63  
      * Default constructor.
 64  
      */
 65  0
     public MathComponent() {
 66  
         // do nothing.
 67  0
     }
 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  0
         this.parameters = newParameters;
 77  0
     }
 78  
 
 79  
     /**
 80  
      * @return the document
 81  
      */
 82  
     public Document getDocument() {
 83  0
         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  0
         if (this.view == null) {
 94  0
             return new Dimension(1, 1);
 95  
         } else {
 96  0
             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  0
         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  0
         super.paint(g);
 121  0
         if (this.view != null) {
 122  0
             this.view.draw((Graphics2D) g, 0, (int) Math.ceil(this.view
 123  
                     .getAscentHeight()));
 124  
         }
 125  0
     }
 126  
 
 127  
     private void redo() {
 128  0
         final Graphics2D g2d = (Graphics2D) this.getGraphics();
 129  0
         if ((this.document == null) || (g2d == null)) {
 130  0
             this.view = null;
 131  
         } else {
 132  0
             this.view = new JEuclidView(this.document, this.parameters, g2d);
 133  
         }
 134  0
         this.repaint();
 135  0
     }
 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  0
         this.parameters.setParameter(Parameter.DEBUG, debugMode);
 145  0
         this.redo();
 146  0
     }
 147  
 
 148  
     /**
 149  
      * @param newDocument
 150  
      *            the document to set
 151  
      */
 152  
     public void setDocument(final Document newDocument) {
 153  0
         this.document = newDocument;
 154  0
         this.redo();
 155  0
     }
 156  
 
 157  
 }