Save This Page
Home » poi-src-3.2-FINAL-20081019 » org.apache » poi » hssf » usermodel » [javadoc | source]
    1   /* ====================================================================
    2      Licensed to the Apache Software Foundation (ASF) under one or more
    3      contributor license agreements.  See the NOTICE file distributed with
    4      this work for additional information regarding copyright ownership.
    5      The ASF licenses this file to You under the Apache License, Version 2.0
    6      (the "License"); you may not use this file except in compliance with
    7      the License.  You may obtain a copy of the License at
    8   
    9          http://www.apache.org/licenses/LICENSE-2.0
   10   
   11      Unless required by applicable law or agreed to in writing, software
   12      distributed under the License is distributed on an "AS IS" BASIS,
   13      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14      See the License for the specific language governing permissions and
   15      limitations under the License.
   16   ==================================================================== */
   17   
   18   package org.apache.poi.hssf.usermodel;
   19   
   20   import java.util.ArrayList;
   21   import java.util.Iterator;
   22   import java.util.List;
   23   
   24   import org.apache.poi.ddf.EscherComplexProperty;
   25   import org.apache.poi.ddf.EscherOptRecord;
   26   import org.apache.poi.ddf.EscherProperty;
   27   import org.apache.poi.hssf.record.EscherAggregate;
   28   import org.apache.poi.util.StringUtil;
   29   
   30   /**
   31    * The patriarch is the toplevel container for shapes in a sheet.  It does
   32    * little other than act as a container for other shapes and groups.
   33    *
   34    * @author Glen Stampoultzis (glens at apache.org)
   35    */
   36   public final class HSSFPatriarch implements HSSFShapeContainer
   37   {
   38       List shapes = new ArrayList();
   39       HSSFSheet sheet;
   40       int x1 = 0;
   41       int y1  = 0 ;
   42       int x2 = 1023;
   43       int y2 = 255;
   44   
   45       /**
   46        * The EscherAggregate we have been bound to.
   47        * (This will handle writing us out into records,
   48        *  and building up our shapes from the records)
   49        */
   50       private EscherAggregate boundAggregate;
   51   
   52       /**
   53        * Creates the patriarch.
   54        *
   55        * @param sheet the sheet this patriarch is stored in.
   56        */
   57       HSSFPatriarch(HSSFSheet sheet, EscherAggregate boundAggregate)
   58       {
   59           this.boundAggregate = boundAggregate;
   60           this.sheet = sheet;
   61       }
   62   
   63       /**
   64        * Creates a new group record stored under this patriarch.
   65        *
   66        * @param anchor    the client anchor describes how this group is attached
   67        *                  to the sheet.
   68        * @return  the newly created group.
   69        */
   70       public HSSFShapeGroup createGroup(HSSFClientAnchor anchor)
   71       {
   72           HSSFShapeGroup group = new HSSFShapeGroup(null, anchor);
   73           group.anchor = anchor;
   74           shapes.add(group);
   75           return group;
   76       }
   77   
   78       /**
   79        * Creates a simple shape.  This includes such shapes as lines, rectangles,
   80        * and ovals.
   81        *
   82        * @param anchor    the client anchor describes how this group is attached
   83        *                  to the sheet.
   84        * @return  the newly created shape.
   85        */
   86       public HSSFSimpleShape createSimpleShape(HSSFClientAnchor anchor)
   87       {
   88           HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
   89           shape.anchor = anchor;
   90           shapes.add(shape);
   91           return shape;
   92       }
   93   
   94       /**
   95        * Creates a picture.
   96        *
   97        * @param anchor    the client anchor describes how this group is attached
   98        *                  to the sheet.
   99        * @return  the newly created shape.
  100        */
  101       public HSSFPicture createPicture(HSSFClientAnchor anchor, int pictureIndex)
  102       {
  103           HSSFPicture shape = new HSSFPicture(null, anchor);
  104           shape.setPictureIndex( pictureIndex );
  105           shape.anchor = anchor;
  106           shape.patriarch = this;
  107           shapes.add(shape);
  108           return shape;
  109       }
  110   
  111   
  112       /**
  113        * Creates a polygon
  114        *
  115        * @param anchor    the client anchor describes how this group is attached
  116        *                  to the sheet.
  117        * @return  the newly created shape.
  118        */
  119       public HSSFPolygon createPolygon(HSSFClientAnchor anchor)
  120       {
  121           HSSFPolygon shape = new HSSFPolygon(null, anchor);
  122           shape.anchor = anchor;
  123           shapes.add(shape);
  124           return shape;
  125       }
  126   
  127       /**
  128        * Constructs a textbox under the patriarch.
  129        *
  130        * @param anchor    the client anchor describes how this group is attached
  131        *                  to the sheet.
  132        * @return      the newly created textbox.
  133        */
  134       public HSSFTextbox createTextbox(HSSFClientAnchor anchor)
  135       {
  136           HSSFTextbox shape = new HSSFTextbox(null, anchor);
  137           shape.anchor = anchor;
  138           shapes.add(shape);
  139           return shape;
  140       }
  141   
  142       /**
  143        * Constructs a cell comment.
  144        *
  145        * @param anchor    the client anchor describes how this comment is attached
  146        *                  to the sheet.
  147        * @return      the newly created comment.
  148        */
  149      public HSSFComment createComment(HSSFAnchor anchor)
  150       {
  151           HSSFComment shape = new HSSFComment(null, anchor);
  152           shape.anchor = anchor;
  153           shapes.add(shape);
  154           return shape;
  155       }
  156   
  157       /**
  158        * Returns a list of all shapes contained by the patriarch.
  159        */
  160       public List getChildren()
  161       {
  162           return shapes;
  163       }
  164   
  165       /**
  166        * Total count of all children and their children's children.
  167        */
  168       public int countOfAllChildren()
  169       {
  170           int count = shapes.size();
  171           for ( Iterator iterator = shapes.iterator(); iterator.hasNext(); )
  172           {
  173               HSSFShape shape = (HSSFShape) iterator.next();
  174               count += shape.countOfAllChildren();
  175           }
  176           return count;
  177       }
  178       /**
  179        * Sets the coordinate space of this group.  All children are contrained
  180        * to these coordinates.
  181        */
  182       public void setCoordinates( int x1, int y1, int x2, int y2 )
  183       {
  184           this.x1 = x1;
  185           this.y1 = y1;
  186           this.x2 = x2;
  187           this.y2 = y2;
  188       }
  189       
  190       /**
  191        * Does this HSSFPatriarch contain a chart?
  192        * (Technically a reference to a chart, since they
  193        *  get stored in a different block of records)
  194        * FIXME - detect chart in all cases (only seems
  195        *  to work on some charts so far)
  196        */
  197       public boolean containsChart() {
  198           // TODO - support charts properly in usermodel
  199           
  200           // We're looking for a EscherOptRecord
  201           EscherOptRecord optRecord = (EscherOptRecord)
  202               boundAggregate.findFirstWithId(EscherOptRecord.RECORD_ID);
  203           if(optRecord == null) {
  204               // No opt record, can't have chart
  205               return false;
  206           }
  207           
  208           for(Iterator it = optRecord.getEscherProperties().iterator(); it.hasNext();) {
  209               EscherProperty prop = (EscherProperty)it.next();
  210               if(prop.getPropertyNumber() == 896 && prop.isComplex()) {
  211                   EscherComplexProperty cp = (EscherComplexProperty)prop;
  212                   String str = StringUtil.getFromUnicodeLE(cp.getComplexData());
  213                   //System.err.println(str);
  214                   if(str.equals("Chart 1\0")) {
  215                       return true;
  216                   }
  217               }
  218           }
  219   
  220           return false;
  221       }
  222   
  223       /**
  224        * The top left x coordinate of this group.
  225        */
  226       public int getX1()
  227       {
  228           return x1;
  229       }
  230   
  231       /**
  232        * The top left y coordinate of this group.
  233        */
  234       public int getY1()
  235       {
  236           return y1;
  237       }
  238   
  239       /**
  240        * The bottom right x coordinate of this group.
  241        */
  242       public int getX2()
  243       {
  244           return x2;
  245       }
  246   
  247       /**
  248        * The bottom right y coordinate of this group.
  249        */
  250       public int getY2()
  251       {
  252           return y2;
  253       }
  254   
  255       /**
  256        * Returns the aggregate escher record we're bound to 
  257        */
  258       protected EscherAggregate _getBoundAggregate() {
  259           return boundAggregate;
  260       }
  261   }

Save This Page
Home » poi-src-3.2-FINAL-20081019 » org.apache » poi » hssf » usermodel » [javadoc | source]