KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.poi.hssf.model;
18
19 import org.apache.poi.ddf.*;
20 import org.apache.poi.hssf.record.ObjRecord;
21 import org.apache.poi.hssf.record.EscherAggregate;
22 import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
23 import org.apache.poi.hssf.record.EndSubRecord;
24 import org.apache.poi.hssf.usermodel.HSSFSimpleShape;
25 import org.apache.poi.hssf.usermodel.HSSFShape;
26
27 public class SimpleFilledShape
28         extends AbstractShape
29 {
30     private EscherContainerRecord spContainer;
31     private ObjRecord objRecord;
32
33     /**
34      * Creates the low evel records for an oval.
35      *
36      * @param hssfShape The highlevel shape.
37      * @param shapeId The shape id to use for this shape.
38      */

39     SimpleFilledShape( HSSFSimpleShape hssfShape, int shapeId )
40     {
41         spContainer = createSpContainer( hssfShape, shapeId );
42         objRecord = createObjRecord( hssfShape, shapeId );
43     }
44
45     /**
46      * Generates the shape records for this shape.
47      *
48      * @param hssfShape
49      * @param shapeId
50      * @return
51      */

52     private EscherContainerRecord createSpContainer( HSSFSimpleShape hssfShape, int shapeId )
53     {
54         HSSFShape shape = hssfShape;
55
56         EscherContainerRecord spContainer = new EscherContainerRecord();
57         EscherSpRecord sp = new EscherSpRecord();
58         EscherOptRecord opt = new EscherOptRecord();
59         EscherClientDataRecord clientData = new EscherClientDataRecord();
60
61         spContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
62         spContainer.setOptions( (short) 0x000F );
63         sp.setRecordId( EscherSpRecord.RECORD_ID );
64         short shapeType = objTypeToShapeType( hssfShape.getShapeType() );
65         sp.setOptions( (short) ( ( shapeType << 4 ) | 0x2 ) );
66         sp.setShapeId( shapeId );
67         sp.setFlags( EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE );
68         opt.setRecordId( EscherOptRecord.RECORD_ID );
69         addStandardOptions(shape, opt);
70         EscherRecord anchor = createAnchor( shape.getAnchor() );
71         clientData.setRecordId( EscherClientDataRecord.RECORD_ID );
72         clientData.setOptions( (short) 0x0000 );
73
74         spContainer.addChildRecord( sp );
75         spContainer.addChildRecord( opt );
76         spContainer.addChildRecord( anchor );
77         spContainer.addChildRecord( clientData );
78
79         return spContainer;
80     }
81
82     private short objTypeToShapeType( int objType )
83     {
84         short shapeType;
85         if (objType == HSSFSimpleShape.OBJECT_TYPE_OVAL)
86             shapeType = EscherAggregate.ST_ELLIPSE;
87         else if (objType == HSSFSimpleShape.OBJECT_TYPE_RECTANGLE)
88             shapeType = EscherAggregate.ST_RECTANGLE;
89         else
90             throw new IllegalArgumentException JavaDoc("Unable to handle an object of this type");
91         return shapeType;
92     }
93
94     /**
95      * Creates the low level OBJ record for this shape.
96      */

97     private ObjRecord createObjRecord( HSSFShape hssfShape, int shapeId )
98     {
99         HSSFShape shape = hssfShape;
100
101         ObjRecord obj = new ObjRecord();
102         CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
103         c.setObjectType( (short) ( (HSSFSimpleShape) shape ).getShapeType() );
104         c.setObjectId( (short) ( shapeId ) );
105         c.setLocked( true );
106         c.setPrintable( true );
107         c.setAutofill( true );
108         c.setAutoline( true );
109         EndSubRecord e = new EndSubRecord();
110
111         obj.addSubRecord( c );
112         obj.addSubRecord( e );
113
114         return obj;
115     }
116
117     public EscherContainerRecord getSpContainer()
118     {
119         return spContainer;
120     }
121
122     public ObjRecord getObjRecord()
123     {
124         return objRecord;
125     }
126
127 }
128
Popular Tags