View Javadoc

1   /*
2    * Copyright 2002 - 2009 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: DocumentElement.java,v 371548310efa 2010/08/09 21:15:33 max $ */
18  
19  package net.sourceforge.jeuclid.elements.generic;
20  
21  import java.awt.Color;
22  import java.util.List;
23  
24  import net.sourceforge.jeuclid.LayoutContext;
25  import net.sourceforge.jeuclid.context.LayoutContextImpl;
26  import net.sourceforge.jeuclid.context.Parameter;
27  import net.sourceforge.jeuclid.elements.JEuclidElementFactory;
28  import net.sourceforge.jeuclid.elements.JEuclidNode;
29  import net.sourceforge.jeuclid.elements.support.ElementListSupport;
30  import net.sourceforge.jeuclid.layout.JEuclidView;
31  import net.sourceforge.jeuclid.layout.LayoutInfo;
32  import net.sourceforge.jeuclid.layout.LayoutStage;
33  import net.sourceforge.jeuclid.layout.LayoutView;
34  import net.sourceforge.jeuclid.layout.LayoutableDocument;
35  import net.sourceforge.jeuclid.layout.LayoutableNode;
36  
37  import org.apache.batik.dom.GenericDocument;
38  import org.w3c.dom.DocumentType;
39  import org.w3c.dom.Element;
40  import org.w3c.dom.Node;
41  import org.w3c.dom.mathml.MathMLDocument;
42  import org.w3c.dom.views.DocumentView;
43  
44  /**
45   * Class for MathML Document Nodes.
46   * 
47   * @version $Revision: 371548310efa $
48   */
49  public final class DocumentElement extends GenericDocument implements
50          MathMLDocument, JEuclidNode, DocumentView, LayoutableDocument {
51  
52      private static final long serialVersionUID = 1L;
53  
54      /**
55       * Creates a math element.
56       * 
57       */
58      public DocumentElement() {
59          this(null);
60      }
61  
62      /**
63       * Creates a MathML compatible document with the given DocumentType.
64       * 
65       * @param doctype
66       *            DocumentType to use. This is currently ignored.
67       */
68      public DocumentElement(final DocumentType doctype) {
69          super(doctype, JEuclidDOMImplementation.getInstance());
70          super.setEventsEnabled(true);
71          this.ownerDocument = this;
72      }
73  
74      /** {@inheritDoc} */
75      public String getDomain() {
76          throw new UnsupportedOperationException();
77      }
78  
79      /** {@inheritDoc} */
80      public String getReferrer() {
81          throw new UnsupportedOperationException();
82      }
83  
84      /** {@inheritDoc} */
85      public String getURI() {
86          throw new UnsupportedOperationException();
87      }
88  
89      /** {@inheritDoc} */
90      public LayoutContext getChildLayoutContext(final int childNum,
91              final LayoutContext context) {
92          return context;
93      }
94  
95      /** {@inheritDoc} */
96      // CHECKSTYLE:OFF
97      public JEuclidView getDefaultView() {
98          // CHECKSTYLE:ON
99          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 }