KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2    Copyright 2002-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.*;
22 import org.apache.poi.hssf.usermodel.*;
23
24 /**
25  * Represents an textbox shape and converts between the highlevel records
26  * and lowlevel records for an oval.
27  *
28  * @author Glen Stampoultzis (glens at apache.org)
29  */

30 public class TextboxShape
31         extends AbstractShape
32 {
33     private EscherContainerRecord spContainer;
34     private TextObjectRecord textObjectRecord;
35     private ObjRecord objRecord;
36     private EscherTextboxRecord escherTextbox;
37
38     /**
39      * Creates the low evel records for an textbox.
40      *
41      * @param hssfShape The highlevel shape.
42      * @param shapeId The shape id to use for this shape.
43      */

44     TextboxShape( HSSFTextbox hssfShape, int shapeId )
45     {
46         spContainer = createSpContainer( hssfShape, shapeId );
47         objRecord = createObjRecord( hssfShape, shapeId );
48         textObjectRecord = createTextObjectRecord( hssfShape, shapeId );
49     }
50
51     /**
52      * Creates the low level OBJ record for this shape.
53      */

54     private ObjRecord createObjRecord( HSSFTextbox hssfShape, int shapeId )
55     {
56         HSSFShape shape = hssfShape;
57
58         ObjRecord obj = new ObjRecord();
59         CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
60         c.setObjectType( (short) ( (HSSFSimpleShape) shape ).getShapeType() );
61         c.setObjectId( (short) ( shapeId ) );
62         c.setLocked( true );
63         c.setPrintable( true );
64         c.setAutofill( true );
65         c.setAutoline( true );
66         EndSubRecord e = new EndSubRecord();
67
68         obj.addSubRecord( c );
69         obj.addSubRecord( e );
70
71         return obj;
72     }
73
74     /**
75      * Generates the escher shape records for this shape.
76      *
77      * @param hssfShape
78      * @param shapeId
79      * @return
80      */

81     private EscherContainerRecord createSpContainer( HSSFTextbox hssfShape, int shapeId )
82     {
83         HSSFTextbox shape = hssfShape;
84
85         EscherContainerRecord spContainer = new EscherContainerRecord();
86         EscherSpRecord sp = new EscherSpRecord();
87         EscherOptRecord opt = new EscherOptRecord();
88         EscherRecord anchor = new EscherClientAnchorRecord();
89         EscherClientDataRecord clientData = new EscherClientDataRecord();
90         escherTextbox = new EscherTextboxRecord();
91
92         spContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
93         spContainer.setOptions( (short) 0x000F );
94         sp.setRecordId( EscherSpRecord.RECORD_ID );
95         sp.setOptions( (short) ( ( EscherAggregate.ST_TEXTBOX << 4 ) | 0x2 ) );
96
97         sp.setShapeId( shapeId );
98         sp.setFlags( EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE );
99         opt.setRecordId( EscherOptRecord.RECORD_ID );
100         // opt.addEscherProperty( new EscherBoolProperty( EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 262144 ) );
101
opt.addEscherProperty( new EscherSimpleProperty( EscherProperties.TEXT__TEXTID, 0 ) );
102         opt.addEscherProperty( new EscherSimpleProperty( EscherProperties.TEXT__TEXTLEFT, shape.getMarginLeft() ) );
103         opt.addEscherProperty( new EscherSimpleProperty( EscherProperties.TEXT__TEXTRIGHT, shape.getMarginRight() ) );
104         opt.addEscherProperty( new EscherSimpleProperty( EscherProperties.TEXT__TEXTBOTTOM, shape.getMarginBottom() ) );
105         opt.addEscherProperty( new EscherSimpleProperty( EscherProperties.TEXT__TEXTTOP, shape.getMarginTop() ) );
106         addStandardOptions( shape, opt );
107         HSSFAnchor userAnchor = shape.getAnchor();
108         // if (userAnchor.isHorizontallyFlipped())
109
// sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPHORIZ);
110
// if (userAnchor.isVerticallyFlipped())
111
// sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPVERT);
112
anchor = createAnchor( userAnchor );
113         clientData.setRecordId( EscherClientDataRecord.RECORD_ID );
114         clientData.setOptions( (short) 0x0000 );
115         escherTextbox.setRecordId( EscherTextboxRecord.RECORD_ID );
116         escherTextbox.setOptions( (short) 0x0000 );
117
118         spContainer.addChildRecord( sp );
119         spContainer.addChildRecord( opt );
120         spContainer.addChildRecord( anchor );
121         spContainer.addChildRecord( clientData );
122         spContainer.addChildRecord( escherTextbox );
123
124         return spContainer;
125     }
126
127     /**
128      * Textboxes also have an extra TXO record associated with them that most
129      * other shapes dont have.
130      */

131     private TextObjectRecord createTextObjectRecord( HSSFTextbox hssfShape, int shapeId )
132     {
133         HSSFTextbox shape = hssfShape;
134
135         TextObjectRecord obj = new TextObjectRecord();
136         obj.setHorizontalTextAlignment( TextObjectRecord.HORIZONTAL_TEXT_ALIGNMENT_LEFT_ALIGNED );
137         obj.setVerticalTextAlignment( TextObjectRecord.VERTICAL_TEXT_ALIGNMENT_TOP );
138         obj.setTextLocked( true );
139         obj.setTextOrientation( TextObjectRecord.TEXT_ORIENTATION_NONE );
140         int frLength = ( shape.getString().numFormattingRuns() + 1 ) * 8;
141         obj.setFormattingRunLength( (short) frLength );
142         obj.setTextLength( (short) shape.getString().length() );
143         obj.setStr( shape.getString() );
144         obj.setReserved7( 0 );
145
146         return obj;
147     }
148
149     public EscherContainerRecord getSpContainer()
150     {
151         return spContainer;
152     }
153
154     public ObjRecord getObjRecord()
155     {
156         return objRecord;
157     }
158
159     public TextObjectRecord getTextObjectRecord()
160     {
161         return textObjectRecord;
162     }
163
164     public EscherRecord getEscherTextbox()
165     {
166         return escherTextbox;
167     }
168 }
169
Popular Tags