001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.model;
019:
020: import org.apache.poi.ddf.*;
021: import org.apache.poi.hssf.record.*;
022: import org.apache.poi.hssf.usermodel.*;
023:
024: /**
025: * Represents an textbox shape and converts between the highlevel records
026: * and lowlevel records for an oval.
027: *
028: * @author Glen Stampoultzis (glens at apache.org)
029: */
030: public class TextboxShape extends AbstractShape {
031: private EscherContainerRecord spContainer;
032: private TextObjectRecord textObjectRecord;
033: private ObjRecord objRecord;
034: private EscherTextboxRecord escherTextbox;
035:
036: /**
037: * Creates the low evel records for an textbox.
038: *
039: * @param hssfShape The highlevel shape.
040: * @param shapeId The shape id to use for this shape.
041: */
042: TextboxShape(HSSFTextbox hssfShape, int shapeId) {
043: spContainer = createSpContainer(hssfShape, shapeId);
044: objRecord = createObjRecord(hssfShape, shapeId);
045: textObjectRecord = createTextObjectRecord(hssfShape, shapeId);
046: }
047:
048: /**
049: * Creates the low level OBJ record for this shape.
050: */
051: private ObjRecord createObjRecord(HSSFTextbox hssfShape, int shapeId) {
052: HSSFShape shape = hssfShape;
053:
054: ObjRecord obj = new ObjRecord();
055: CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
056: c.setObjectType((short) ((HSSFSimpleShape) shape)
057: .getShapeType());
058: c.setObjectId((short) (shapeId));
059: c.setLocked(true);
060: c.setPrintable(true);
061: c.setAutofill(true);
062: c.setAutoline(true);
063: EndSubRecord e = new EndSubRecord();
064:
065: obj.addSubRecord(c);
066: obj.addSubRecord(e);
067:
068: return obj;
069: }
070:
071: /**
072: * Generates the escher shape records for this shape.
073: *
074: * @param hssfShape
075: * @param shapeId
076: */
077: private EscherContainerRecord createSpContainer(
078: HSSFTextbox hssfShape, int shapeId) {
079: HSSFTextbox shape = hssfShape;
080:
081: EscherContainerRecord spContainer = new EscherContainerRecord();
082: EscherSpRecord sp = new EscherSpRecord();
083: EscherOptRecord opt = new EscherOptRecord();
084: EscherRecord anchor = new EscherClientAnchorRecord();
085: EscherClientDataRecord clientData = new EscherClientDataRecord();
086: escherTextbox = new EscherTextboxRecord();
087:
088: spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
089: spContainer.setOptions((short) 0x000F);
090: sp.setRecordId(EscherSpRecord.RECORD_ID);
091: sp
092: .setOptions((short) ((EscherAggregate.ST_TEXTBOX << 4) | 0x2));
093:
094: sp.setShapeId(shapeId);
095: sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR
096: | EscherSpRecord.FLAG_HASSHAPETYPE);
097: opt.setRecordId(EscherOptRecord.RECORD_ID);
098: // opt.addEscherProperty( new EscherBoolProperty( EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 262144 ) );
099: opt.addEscherProperty(new EscherSimpleProperty(
100: EscherProperties.TEXT__TEXTID, 0));
101: opt
102: .addEscherProperty(new EscherSimpleProperty(
103: EscherProperties.TEXT__TEXTLEFT, shape
104: .getMarginLeft()));
105: opt.addEscherProperty(new EscherSimpleProperty(
106: EscherProperties.TEXT__TEXTRIGHT, shape
107: .getMarginRight()));
108: opt.addEscherProperty(new EscherSimpleProperty(
109: EscherProperties.TEXT__TEXTBOTTOM, shape
110: .getMarginBottom()));
111: opt.addEscherProperty(new EscherSimpleProperty(
112: EscherProperties.TEXT__TEXTTOP, shape.getMarginTop()));
113: addStandardOptions(shape, opt);
114: HSSFAnchor userAnchor = shape.getAnchor();
115: // if (userAnchor.isHorizontallyFlipped())
116: // sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPHORIZ);
117: // if (userAnchor.isVerticallyFlipped())
118: // sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPVERT);
119: anchor = createAnchor(userAnchor);
120: clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
121: clientData.setOptions((short) 0x0000);
122: escherTextbox.setRecordId(EscherTextboxRecord.RECORD_ID);
123: escherTextbox.setOptions((short) 0x0000);
124:
125: spContainer.addChildRecord(sp);
126: spContainer.addChildRecord(opt);
127: spContainer.addChildRecord(anchor);
128: spContainer.addChildRecord(clientData);
129: spContainer.addChildRecord(escherTextbox);
130:
131: return spContainer;
132: }
133:
134: /**
135: * Textboxes also have an extra TXO record associated with them that most
136: * other shapes dont have.
137: */
138: private TextObjectRecord createTextObjectRecord(
139: HSSFTextbox hssfShape, int shapeId) {
140: HSSFTextbox shape = hssfShape;
141:
142: TextObjectRecord obj = new TextObjectRecord();
143: obj
144: .setHorizontalTextAlignment(TextObjectRecord.HORIZONTAL_TEXT_ALIGNMENT_LEFT_ALIGNED);
145: obj
146: .setVerticalTextAlignment(TextObjectRecord.VERTICAL_TEXT_ALIGNMENT_TOP);
147: obj.setTextLocked(true);
148: obj.setTextOrientation(TextObjectRecord.TEXT_ORIENTATION_NONE);
149: int frLength = (shape.getString().numFormattingRuns() + 1) * 8;
150: obj.setFormattingRunLength((short) frLength);
151: obj.setTextLength((short) shape.getString().length());
152: obj.setStr(shape.getString());
153: obj.setReserved7(0);
154:
155: return obj;
156: }
157:
158: public EscherContainerRecord getSpContainer() {
159: return spContainer;
160: }
161:
162: public ObjRecord getObjRecord() {
163: return objRecord;
164: }
165:
166: public TextObjectRecord getTextObjectRecord() {
167: return textObjectRecord;
168: }
169:
170: public EscherRecord getEscherTextbox() {
171: return escherTextbox;
172: }
173: }
|