Coverage Report - net.sourceforge.jeuclid.elements.support.ElementListSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
ElementListSupport
89%
53/59
78%
11/14
2,167
 
 1  
 /*
 2  
  * Copyright 2007 - 2007 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: ElementListSupport.java,v d02878fc1d01 2009/09/08 12:35:02 max $ */
 18  
 
 19  
 package net.sourceforge.jeuclid.elements.support;
 20  
 
 21  
 import java.awt.Color;
 22  
 import java.awt.geom.Dimension2D;
 23  
 import java.util.ArrayList;
 24  
 import java.util.List;
 25  
 
 26  
 import net.sourceforge.jeuclid.layout.FillRectObject;
 27  
 import net.sourceforge.jeuclid.layout.GraphicsObject;
 28  
 import net.sourceforge.jeuclid.layout.LayoutInfo;
 29  
 import net.sourceforge.jeuclid.layout.LayoutStage;
 30  
 import net.sourceforge.jeuclid.layout.LayoutView;
 31  
 import net.sourceforge.jeuclid.layout.LayoutableNode;
 32  
 
 33  
 import org.w3c.dom.Node;
 34  
 
 35  
 /**
 36  
  * Class to support Lists of MathElements.
 37  
  * <p>
 38  
  * This class can be used by all elements that have some kind of a list of
 39  
  * children that they need to handle in a row-like manner.
 40  
  * 
 41  
  * @version $Revision: d02878fc1d01 $
 42  
  */
 43  
 public final class ElementListSupport {
 44  
 
 45  0
     private ElementListSupport() {
 46  
         // Utility class.
 47  0
     }
 48  
 
 49  
     /**
 50  
      * Creates a list of children for the given Element.
 51  
      * 
 52  
      * @param parent
 53  
      *            the parent element.
 54  
      * @return list of Children.
 55  
      */
 56  
     public static List<Node> createListOfChildren(final Node parent) {
 57  7185382
         final org.w3c.dom.NodeList childList = parent.getChildNodes();
 58  7185382
         final int len = childList.getLength();
 59  7185382
         final List<Node> children = new ArrayList<Node>(len);
 60  47013438
         for (int i = 0; i < len; i++) {
 61  39828056
             final Node child = childList.item(i);
 62  39828056
             children.add(child);
 63  
         }
 64  7185382
         return children;
 65  
 
 66  
     }
 67  
 
 68  
     /**
 69  
      * Creates a list of layoutable children for the given Element.
 70  
      * 
 71  
      * @param parent
 72  
      *            the parent element.
 73  
      * @return list of Children.
 74  
      */
 75  
     public static List<LayoutableNode> createListOfLayoutChildren(
 76  
             final Node parent) {
 77  204860
         final org.w3c.dom.NodeList childList = parent.getChildNodes();
 78  204860
         final int len = childList.getLength();
 79  204860
         final List<LayoutableNode> children = new ArrayList<LayoutableNode>(len);
 80  874110
         for (int i = 0; i < len; i++) {
 81  669250
             final Node child = childList.item(i);
 82  669250
             if (child instanceof LayoutableNode) {
 83  313950
                 children.add((LayoutableNode) child);
 84  
             }
 85  
         }
 86  204860
         return children;
 87  
 
 88  
     }
 89  
 
 90  
     /**
 91  
      * @param view
 92  
      *            View Object
 93  
      * @param info
 94  
      *            Info to fill
 95  
      * @param parent
 96  
      *            Current Node
 97  
      * @param stage
 98  
      *            Stage to load Info From
 99  
      * @param borderLeftTop
 100  
      *            border around element.
 101  
      * @param borderRightBottom
 102  
      *            border around element.
 103  
      */
 104  
     public static void fillInfoFromChildren(final LayoutView view,
 105  
             final LayoutInfo info, final Node parent, final LayoutStage stage,
 106  
             final Dimension2D borderLeftTop, final Dimension2D borderRightBottom) {
 107  14421
         float ascentHeight = (float) borderLeftTop.getHeight();
 108  14421
         float descentHeight = (float) borderRightBottom.getHeight();
 109  14421
         final float startX = (float) borderLeftTop.getWidth();
 110  14421
         float width = startX;
 111  14421
         for (final LayoutableNode child : ElementListSupport
 112  
                 .createListOfLayoutChildren(parent)) {
 113  33858
             final LayoutInfo childInfo = view.getInfo(child);
 114  33858
             ascentHeight = Math.max(ascentHeight, -childInfo.getPosY(stage)
 115  
                     + childInfo.getAscentHeight(stage));
 116  33858
             descentHeight = Math.max(descentHeight, childInfo.getPosY(stage)
 117  
                     + childInfo.getDescentHeight(stage));
 118  33858
             width = Math.max(width, childInfo.getPosX(stage)
 119  
                     + childInfo.getWidth(stage));
 120  33858
         }
 121  14421
         info.setAscentHeight(ascentHeight + (float) borderLeftTop.getHeight(),
 122  
                 stage);
 123  14421
         info.setDescentHeight(descentHeight
 124  
                 + (float) borderRightBottom.getHeight(), stage);
 125  14421
         info.setHorizontalCenterOffset((width + startX) / 2.0f, stage);
 126  14421
         info.setWidth(width + (float) borderRightBottom.getWidth(), stage);
 127  14421
     }
 128  
 
 129  
     /**
 130  
      * @param view
 131  
      *            View Object
 132  
      * @param info
 133  
      *            Info to fill
 134  
      * @param children
 135  
      *            Children to layout
 136  
      * @param stage
 137  
      *            Stage to load Info From
 138  
      */
 139  
     public static void layoutSequential(final LayoutView view,
 140  
             final LayoutInfo info, final List<LayoutableNode> children,
 141  
             final LayoutStage stage) {
 142  48090
         float ascentHeight = 0.0f;
 143  48090
         float descentHeight = 0.0f;
 144  48090
         float posX = 0.0f;
 145  48090
         float stretchAscent = 0.0f;
 146  48090
         float stretchDescent = 0.0f;
 147  
 
 148  48090
         for (final LayoutableNode child : children) {
 149  84243
             final LayoutInfo childInfo = view.getInfo(child);
 150  84243
             ascentHeight = Math.max(ascentHeight, childInfo
 151  
                     .getAscentHeight(stage));
 152  84243
             descentHeight = Math.max(descentHeight, childInfo
 153  
                     .getDescentHeight(stage));
 154  84243
             stretchAscent = Math.max(stretchAscent, childInfo
 155  
                     .getStretchAscent());
 156  84243
             stretchDescent = Math.max(stretchDescent, childInfo
 157  
                     .getStretchDescent());
 158  84243
             childInfo.moveTo(posX, 0.0f, stage);
 159  84243
             posX += childInfo.getWidth(stage);
 160  84243
         }
 161  48090
         info.setAscentHeight(ascentHeight, stage);
 162  48090
         info.setDescentHeight(descentHeight, stage);
 163  48090
         info.setStretchAscent(stretchAscent);
 164  48090
         info.setStretchDescent(stretchDescent);
 165  48090
         info.setHorizontalCenterOffset(posX / 2.0f, stage);
 166  48090
         info.setWidth(posX, stage);
 167  48090
     }
 168  
 
 169  
     /**
 170  
      * Add a background Rectangle for the given background color.
 171  
      * 
 172  
      * @param backgroundColor
 173  
      *            background color (may be null)
 174  
      * @param info
 175  
      *            LayoutInfo object to add to. Must already be completely
 176  
      *            rendered (stage 2)
 177  
      * @param useCeil
 178  
      *            if true, the {@link Math#ceil(double)} will be used to avoid
 179  
      *            anti-aliasing artifacts.
 180  
      */
 181  
     public static void addBackground(final Color backgroundColor,
 182  
             final LayoutInfo info, final boolean useCeil) {
 183  21736
         if (backgroundColor != null) {
 184  
             final GraphicsObject fillObject;
 185  0
             if (useCeil) {
 186  0
                 fillObject = new FillRectObject(backgroundColor, (float) Math
 187  
                         .ceil(info.getAscentHeight(LayoutStage.STAGE2)),
 188  
                         (float) Math.ceil(info
 189  
                                 .getDescentHeight(LayoutStage.STAGE2)),
 190  
                         (float) Math.ceil(info.getWidth(LayoutStage.STAGE2)));
 191  
             } else {
 192  0
                 fillObject = new FillRectObject(backgroundColor, info
 193  
                         .getAscentHeight(LayoutStage.STAGE2), info
 194  
                         .getDescentHeight(LayoutStage.STAGE2), info
 195  
                         .getWidth(LayoutStage.STAGE2));
 196  
 
 197  
             }
 198  
 
 199  0
             info.getGraphicObjects().add(0, fillObject);
 200  
         }
 201  
 
 202  21736
     }
 203  
 }