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: Maction.java,v 2986a8eeaebc 2009/09/24 12:53:08 max $ */
018    
019    package net.sourceforge.jeuclid.elements.presentation.enlivening;
020    
021    import java.util.Collections;
022    import java.util.List;
023    
024    import net.sourceforge.jeuclid.elements.AbstractElementWithDelegates;
025    import net.sourceforge.jeuclid.layout.LayoutableNode;
026    
027    import org.apache.batik.dom.AbstractDocument;
028    import org.w3c.dom.Node;
029    import org.w3c.dom.mathml.MathMLActionElement;
030    
031    /**
032     * Represents an maction element.
033     * <p>
034     * TODO: This element does not actually implement any action.
035     * 
036     * @version $Revision: 2986a8eeaebc $
037     */
038    public final class Maction extends AbstractElementWithDelegates implements
039            MathMLActionElement {
040    
041        /**
042         * The XML element from this class.
043         */
044        public static final String ELEMENT = "maction";
045    
046        private static final String ATTR_ACTIONTYPE = "actiontype";
047    
048        private static final String ATTR_SELECTION = "selection";
049    
050        private static final long serialVersionUID = 1L;
051    
052        /**
053         * Default constructor. Sets MathML Namespace.
054         * 
055         * @param qname
056         *            Qualified name.
057         * @param odoc
058         *            Owner Document.
059         */
060        public Maction(final String qname, final AbstractDocument odoc) {
061            super(qname, odoc);
062            this.setDefaultMathAttribute(Maction.ATTR_SELECTION, "1");
063        }
064    
065        /** {@inheritDoc} */
066        @Override
067        protected Node newNode() {
068            return new Maction(this.nodeName, this.ownerDocument);
069        }
070    
071        /** {@inheritDoc} */
072        public String getActiontype() {
073            return this.getMathAttribute(Maction.ATTR_ACTIONTYPE);
074        }
075    
076        /** {@inheritDoc} */
077        public String getSelection() {
078            return this.getMathAttribute(Maction.ATTR_SELECTION);
079        }
080    
081        /** {@inheritDoc} */
082        public void setActiontype(final String actiontype) {
083            this.setAttribute(Maction.ATTR_ACTIONTYPE, actiontype);
084        }
085    
086        /** {@inheritDoc} */
087        public void setSelection(final String selection) {
088            this.setAttribute(Maction.ATTR_SELECTION, selection);
089        }
090    
091        /** {@inheritDoc} */
092        @Override
093        protected List<LayoutableNode> createDelegates() {
094            LayoutableNode selectedElement;
095            try {
096                final int selected = Integer.parseInt(this.getSelection());
097                selectedElement = this.getMathElement(selected - 1);
098            } catch (final NumberFormatException nfe) {
099                selectedElement = null;
100            }
101    
102            if (selectedElement == null) {
103                return Collections.emptyList();
104            } else {
105                return Collections.singletonList(selectedElement);
106            }
107        }
108    
109    }