001    /*
002     * Copyright 2002 - 2009 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: DocumentElement.java,v 371548310efa 2010/08/09 21:15:33 max $ */
018    
019    package net.sourceforge.jeuclid.elements.generic;
020    
021    import java.awt.Color;
022    import java.util.List;
023    
024    import net.sourceforge.jeuclid.LayoutContext;
025    import net.sourceforge.jeuclid.context.LayoutContextImpl;
026    import net.sourceforge.jeuclid.context.Parameter;
027    import net.sourceforge.jeuclid.elements.JEuclidElementFactory;
028    import net.sourceforge.jeuclid.elements.JEuclidNode;
029    import net.sourceforge.jeuclid.elements.support.ElementListSupport;
030    import net.sourceforge.jeuclid.layout.JEuclidView;
031    import net.sourceforge.jeuclid.layout.LayoutInfo;
032    import net.sourceforge.jeuclid.layout.LayoutStage;
033    import net.sourceforge.jeuclid.layout.LayoutView;
034    import net.sourceforge.jeuclid.layout.LayoutableDocument;
035    import net.sourceforge.jeuclid.layout.LayoutableNode;
036    
037    import org.apache.batik.dom.GenericDocument;
038    import org.w3c.dom.DocumentType;
039    import org.w3c.dom.Element;
040    import org.w3c.dom.Node;
041    import org.w3c.dom.mathml.MathMLDocument;
042    import org.w3c.dom.views.DocumentView;
043    
044    /**
045     * Class for MathML Document Nodes.
046     * 
047     * @version $Revision: 371548310efa $
048     */
049    public final class DocumentElement extends GenericDocument implements
050            MathMLDocument, JEuclidNode, DocumentView, LayoutableDocument {
051    
052        private static final long serialVersionUID = 1L;
053    
054        /**
055         * Creates a math element.
056         * 
057         */
058        public DocumentElement() {
059            this(null);
060        }
061    
062        /**
063         * Creates a MathML compatible document with the given DocumentType.
064         * 
065         * @param doctype
066         *            DocumentType to use. This is currently ignored.
067         */
068        public DocumentElement(final DocumentType doctype) {
069            super(doctype, JEuclidDOMImplementation.getInstance());
070            super.setEventsEnabled(true);
071            this.ownerDocument = this;
072        }
073    
074        /** {@inheritDoc} */
075        public String getDomain() {
076            throw new UnsupportedOperationException();
077        }
078    
079        /** {@inheritDoc} */
080        public String getReferrer() {
081            throw new UnsupportedOperationException();
082        }
083    
084        /** {@inheritDoc} */
085        public String getURI() {
086            throw new UnsupportedOperationException();
087        }
088    
089        /** {@inheritDoc} */
090        public LayoutContext getChildLayoutContext(final int childNum,
091                final LayoutContext context) {
092            return context;
093        }
094    
095        /** {@inheritDoc} */
096        // CHECKSTYLE:OFF
097        public JEuclidView getDefaultView() {
098            // CHECKSTYLE:ON
099            return new JEuclidView(this,
100                    LayoutContextImpl.getDefaultLayoutContext(), null);
101        }
102    
103        /** {@inheritDoc} */
104        public List<LayoutableNode> getChildrenToLayout() {
105            return ElementListSupport.createListOfLayoutChildren(this);
106        }
107    
108        /** {@inheritDoc} */
109        public List<LayoutableNode> getChildrenToDraw() {
110            return ElementListSupport.createListOfLayoutChildren(this);
111        }
112    
113        /** {@inheritDoc} */
114        public void layoutStage1(final LayoutView view, final LayoutInfo info,
115                final LayoutStage childMinStage, final LayoutContext context) {
116            ElementListSupport.layoutSequential(view, info, this
117                    .getChildrenToLayout(), LayoutStage.STAGE1);
118            info.setLayoutStage(childMinStage);
119            // TODO: This should be done in a better way.
120            if (context.getParameter(Parameter.MATHBACKGROUND) == null) {
121                info.setLayoutStage(childMinStage);
122            } else {
123                info.setLayoutStage(LayoutStage.STAGE1);
124            }
125        }
126    
127        /** {@inheritDoc} */
128        public void layoutStage2(final LayoutView view, final LayoutInfo info,
129                final LayoutContext context) {
130            ElementListSupport.layoutSequential(view, info, this
131                    .getChildrenToLayout(), LayoutStage.STAGE2);
132            ElementListSupport.addBackground((Color) context
133                    .getParameter(Parameter.MATHBACKGROUND), info, true);
134            info.setLayoutStage(LayoutStage.STAGE2);
135        }
136    
137        /** {@inheritDoc} */
138        @Override
139        protected Node newNode() {
140            return new DocumentElement();
141        }
142    
143        /** {@inheritDoc} */
144        @Override
145        public Element createElement(final String tagName) {
146            return JEuclidElementFactory.elementFromName(null, tagName, this);
147        }
148    
149        /** {@inheritDoc} */
150        @Override
151        public Element createElementNS(final String namespaceURI,
152                final String qualifiedName) {
153            final String ns;
154            if (namespaceURI != null && namespaceURI.length() == 0) {
155                ns = null;
156            } else {
157                ns = namespaceURI;
158            }
159            return JEuclidElementFactory.elementFromName(ns, qualifiedName, this);
160        }
161    }