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: AbstractElementWithDelegates.java,v bc1d5fde7b73 2009/06/01 14:40:54 maxberger $ */
018    
019    package net.sourceforge.jeuclid.elements;
020    
021    import java.util.List;
022    
023    import net.sourceforge.jeuclid.elements.presentation.AbstractContainer;
024    import net.sourceforge.jeuclid.layout.LayoutableNode;
025    
026    import org.apache.batik.dom.AbstractDocument;
027    
028    /**
029     * Generic class for all mathobjects that can be represented using other Math
030     * objects. These math objects use a delegates for the actual display and
031     * calculations.
032     * <p>
033     * To use this class, overwrite {@link #createDelegates()} to create the
034     * delegate objects.
035     * 
036     * @version $Revision: bc1d5fde7b73 $
037     */
038    public abstract class AbstractElementWithDelegates extends AbstractContainer {
039    
040        // TODO: Re-Enable resetting delegates on changeHook!
041        private List<LayoutableNode> delegates;
042    
043        /**
044         * Default constructor. Sets MathML Namespace.
045         * 
046         * @param qname
047         *            Qualified name.
048         * @param odoc
049         *            Owner Document.
050         */
051        public AbstractElementWithDelegates(final String qname,
052                final AbstractDocument odoc) {
053            super(qname, odoc);
054        }
055    
056        /**
057         * Overwrite this function in your implementation.
058         * 
059         * @return a MathObject representing the real contents.
060         */
061        protected abstract List<LayoutableNode> createDelegates();
062    
063        private void prepareDelegates() {
064            if (this.delegates == null) {
065                this.delegates = this.createDelegates();
066                for (final LayoutableNode element : this.delegates) {
067                    ((JEuclidElement) element).setFakeParent(this);
068                }
069            }
070        }
071    
072        /** {@inheritDoc} */
073        @Override
074        public List<LayoutableNode> getChildrenToLayout() {
075            this.prepareDelegates();
076            return this.delegates;
077        }
078    
079        /** {@inheritDoc} */
080        @Override
081        public List<LayoutableNode> getChildrenToDraw() {
082            this.prepareDelegates();
083            return this.delegates;
084        }
085    
086    }