KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > model > PolygonShape


1 /* ====================================================================
2    Copyright 2004 Apache Software Foundation
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
18 package org.apache.poi.hssf.model;
19
20 import org.apache.poi.ddf.*;
21 import org.apache.poi.hssf.record.ObjRecord;
22 import org.apache.poi.hssf.record.EscherAggregate;
23 import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
24 import org.apache.poi.hssf.record.EndSubRecord;
25 import org.apache.poi.hssf.usermodel.HSSFSimpleShape;
26 import org.apache.poi.hssf.usermodel.HSSFShape;
27 import org.apache.poi.hssf.usermodel.HSSFPolygon;
28 import org.apache.poi.util.LittleEndian;
29
30 public class PolygonShape
31         extends AbstractShape
32 {
33     public final static short OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING = 30;
34
35     private EscherContainerRecord spContainer;
36     private ObjRecord objRecord;
37
38     /**
39      * Creates the low evel records for an polygon.
40      *
41      * @param hssfShape The highlevel shape.
42      * @param shapeId The shape id to use for this shape.
43      */

44     PolygonShape( HSSFPolygon hssfShape, int shapeId )
45     {
46         spContainer = createSpContainer( hssfShape, shapeId );
47         objRecord = createObjRecord( hssfShape, shapeId );
48     }
49
50     /**
51      * Generates the shape records for this shape.
52      *
53      */

54     private EscherContainerRecord createSpContainer( HSSFPolygon hssfShape, int shapeId )
55     {
56         HSSFShape shape = hssfShape;
57
58         EscherContainerRecord spContainer = new EscherContainerRecord();
59         EscherSpRecord sp = new EscherSpRecord();
60         EscherOptRecord opt = new EscherOptRecord();
61         EscherClientDataRecord clientData = new EscherClientDataRecord();
62
63         spContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
64         spContainer.setOptions( (short) 0x000F );
65         sp.setRecordId( EscherSpRecord.RECORD_ID );
66         sp.setOptions( (short) ( ( EscherAggregate.ST_DONUT << 4 ) | 0x2 ) );
67         sp.setShapeId( shapeId );
68         if (hssfShape.getParent() == null)
69             sp.setFlags( EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE );
70         else
71             sp.setFlags( EscherSpRecord.FLAG_CHILD | EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE );
72         opt.setRecordId( EscherOptRecord.RECORD_ID );
73         opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TRANSFORM__ROTATION, false, false, 0));
74         opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__RIGHT, false, false, hssfShape.getDrawAreaWidth()));
75         opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__BOTTOM, false, false, hssfShape.getDrawAreaHeight()));
76         opt.addEscherProperty(new EscherShapePathProperty(EscherProperties.GEOMETRY__SHAPEPATH, EscherShapePathProperty.COMPLEX));
77         EscherArrayProperty verticesProp = new EscherArrayProperty(EscherProperties.GEOMETRY__VERTICES, false, new byte[0] );
78         verticesProp.setNumberOfElementsInArray(hssfShape.getXPoints().length+1);
79         verticesProp.setNumberOfElementsInMemory(hssfShape.getXPoints().length+1);
80         verticesProp.setSizeOfElements(0xFFF0);
81         for (int i = 0; i < hssfShape.getXPoints().length; i++)
82         {
83             byte[] data = new byte[4];
84             LittleEndian.putShort(data, 0, (short)hssfShape.getXPoints()[i]);
85             LittleEndian.putShort(data, 2, (short)hssfShape.getYPoints()[i]);
86             verticesProp.setElement(i, data);
87         }
88         int point = hssfShape.getXPoints().length;
89         byte[] data = new byte[4];
90         LittleEndian.putShort(data, 0, (short)hssfShape.getXPoints()[0]);
91         LittleEndian.putShort(data, 2, (short)hssfShape.getYPoints()[0]);
92         verticesProp.setElement(point, data);
93         opt.addEscherProperty(verticesProp);
94         EscherArrayProperty segmentsProp = new EscherArrayProperty(EscherProperties.GEOMETRY__SEGMENTINFO, false, null );
95         segmentsProp.setSizeOfElements(0x0002);
96         segmentsProp.setNumberOfElementsInArray(hssfShape.getXPoints().length * 2 + 4);
97         segmentsProp.setNumberOfElementsInMemory(hssfShape.getXPoints().length * 2 + 4);
98         segmentsProp.setElement(0, new byte[] { (byte)0x00, (byte)0x40 } );
99         segmentsProp.setElement(1, new byte[] { (byte)0x00, (byte)0xAC } );
100         for (int i = 0; i < hssfShape.getXPoints().length; i++)
101         {
102             segmentsProp.setElement(2 + i * 2, new byte[] { (byte)0x01, (byte)0x00 } );
103             segmentsProp.setElement(3 + i * 2, new byte[] { (byte)0x00, (byte)0xAC } );
104         }
105         segmentsProp.setElement(segmentsProp.getNumberOfElementsInArray() - 2, new byte[] { (byte)0x01, (byte)0x60 } );
106         segmentsProp.setElement(segmentsProp.getNumberOfElementsInArray() - 1, new byte[] { (byte)0x00, (byte)0x80 } );
107         opt.addEscherProperty(segmentsProp);
108         opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__FILLOK, false, false, 0x00010001));
109         opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINESTARTARROWHEAD, false, false, 0x0));
110         opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEENDARROWHEAD, false, false, 0x0));
111         opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEENDCAPSTYLE, false, false, 0x0));
112
113         addStandardOptions(shape, opt);
114
115         EscherRecord anchor = createAnchor( shape.getAnchor() );
116         clientData.setRecordId( EscherClientDataRecord.RECORD_ID );
117         clientData.setOptions( (short) 0x0000 );
118
119         spContainer.addChildRecord( sp );
120         spContainer.addChildRecord( opt );
121         spContainer.addChildRecord( anchor );
122         spContainer.addChildRecord( clientData );
123
124         return spContainer;
125     }
126
127     /**
128      * Creates the low level OBJ record for this shape.
129      */

130     private ObjRecord createObjRecord( HSSFShape hssfShape, int shapeId )
131     {
132         HSSFShape shape = hssfShape;
133
134         ObjRecord obj = new ObjRecord();
135         CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
136         c.setObjectType( OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING );
137         c.setObjectId( (short) ( shapeId ) );
138         c.setLocked( true );
139         c.setPrintable( true );
140         c.setAutofill( true );
141         c.setAutoline( true );
142         EndSubRecord e = new EndSubRecord();
143
144         obj.addSubRecord( c );
145         obj.addSubRecord( e );
146
147         return obj;
148     }
149
150     public EscherContainerRecord getSpContainer()
151     {
152         return spContainer;
153     }
154
155     public ObjRecord getObjRecord()
156     {
157         return objRecord;
158     }
159
160 }
161
Popular Tags