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: AbstractMathElementWithChildren.java 310 2007-05-18 20:26:36Z maxberger $ */
018    
019    package net.sourceforge.jeuclid.elements.presentation.general;
020    
021    import java.awt.Graphics2D;
022    import java.util.List;
023    
024    import net.sourceforge.jeuclid.MathBase;
025    import net.sourceforge.jeuclid.elements.JEuclidElement;
026    import net.sourceforge.jeuclid.elements.presentation.AbstractContainer;
027    import net.sourceforge.jeuclid.elements.support.ElementListSupport;
028    
029    import org.w3c.dom.mathml.MathMLPresentationContainer;
030    
031    /**
032     * Represents a Math element that is painted and defined through its children.
033     * 
034     * @author Max Berger
035     * @version $Revision: 310 $
036     */
037    public abstract class AbstractMathElementWithChildren extends
038            AbstractContainer implements MathMLPresentationContainer {
039        /**
040         * Default constructor.
041         * 
042         * @param base
043         *            MathBase to use.
044         */
045        public AbstractMathElementWithChildren(final MathBase base) {
046            super(base);
047        }
048    
049        private List<JEuclidElement> getChildrenAsList() {
050            return ElementListSupport.createListOfChildren(this);
051        }
052    
053        /**
054         * Paint all children. To be called from within a paint() method.
055         * 
056         * @param g
057         *            Graphics2D context
058         * @param posX
059         *            x-offset to start painting
060         * @param posY
061         *            y-offset to start painting
062         */
063        protected void paintChildren(final Graphics2D g, final float posX,
064                final float posY) {
065            super.paint(g, posX, posY);
066            ElementListSupport.paint(g, posX, posY, this.getChildrenAsList());
067        }
068    
069        /**
070         * Calculates the width of all contained children.
071         * 
072         * @param g
073         *            Graphics context to use.
074         * @return the width.
075         */
076        public float calculateChildrenWidth(final Graphics2D g) {
077            return ElementListSupport.getWidth(g, this.getChildrenAsList());
078        }
079    
080        /**
081         * Calculates the ascent height of all contained children children.
082         * 
083         * @param g
084         *            Graphics context to use.
085         * @return the ascent height.
086         */
087        public float calculateChildrenAscentHeight(final Graphics2D g) {
088            return ElementListSupport
089                    .getAscentHeight(g, this.getChildrenAsList());
090        }
091    
092        /**
093         * Calculates the descent height of all contained children children.
094         * 
095         * @param g
096         *            Graphics context to use.
097         * @return the ascent height.
098         */
099        public float calculateChildrenDescentHeight(final Graphics2D g) {
100            return ElementListSupport.getDescentHeight(g, this
101                    .getChildrenAsList());
102        }
103    
104    }