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.ObjRecord;
022: import org.apache.poi.hssf.record.EscherAggregate;
023: import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
024: import org.apache.poi.hssf.record.EndSubRecord;
025: import org.apache.poi.hssf.usermodel.HSSFSimpleShape;
026: import org.apache.poi.hssf.usermodel.HSSFShape;
027:
028: public class SimpleFilledShape extends AbstractShape {
029: private EscherContainerRecord spContainer;
030: private ObjRecord objRecord;
031:
032: /**
033: * Creates the low evel records for an oval.
034: *
035: * @param hssfShape The highlevel shape.
036: * @param shapeId The shape id to use for this shape.
037: */
038: SimpleFilledShape(HSSFSimpleShape hssfShape, int shapeId) {
039: spContainer = createSpContainer(hssfShape, shapeId);
040: objRecord = createObjRecord(hssfShape, shapeId);
041: }
042:
043: /**
044: * Generates the shape records for this shape.
045: *
046: * @param hssfShape
047: * @param shapeId
048: */
049: private EscherContainerRecord createSpContainer(
050: HSSFSimpleShape hssfShape, int shapeId) {
051: HSSFShape shape = hssfShape;
052:
053: EscherContainerRecord spContainer = new EscherContainerRecord();
054: EscherSpRecord sp = new EscherSpRecord();
055: EscherOptRecord opt = new EscherOptRecord();
056: EscherClientDataRecord clientData = new EscherClientDataRecord();
057:
058: spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
059: spContainer.setOptions((short) 0x000F);
060: sp.setRecordId(EscherSpRecord.RECORD_ID);
061: short shapeType = objTypeToShapeType(hssfShape.getShapeType());
062: sp.setOptions((short) ((shapeType << 4) | 0x2));
063: sp.setShapeId(shapeId);
064: sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR
065: | EscherSpRecord.FLAG_HASSHAPETYPE);
066: opt.setRecordId(EscherOptRecord.RECORD_ID);
067: addStandardOptions(shape, opt);
068: EscherRecord anchor = createAnchor(shape.getAnchor());
069: clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
070: clientData.setOptions((short) 0x0000);
071:
072: spContainer.addChildRecord(sp);
073: spContainer.addChildRecord(opt);
074: spContainer.addChildRecord(anchor);
075: spContainer.addChildRecord(clientData);
076:
077: return spContainer;
078: }
079:
080: private short objTypeToShapeType(int objType) {
081: short shapeType;
082: if (objType == HSSFSimpleShape.OBJECT_TYPE_OVAL)
083: shapeType = EscherAggregate.ST_ELLIPSE;
084: else if (objType == HSSFSimpleShape.OBJECT_TYPE_RECTANGLE)
085: shapeType = EscherAggregate.ST_RECTANGLE;
086: else
087: throw new IllegalArgumentException(
088: "Unable to handle an object of this type");
089: return shapeType;
090: }
091:
092: /**
093: * Creates the low level OBJ record for this shape.
094: */
095: private ObjRecord createObjRecord(HSSFShape hssfShape, int shapeId) {
096: HSSFShape shape = hssfShape;
097:
098: ObjRecord obj = new ObjRecord();
099: CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
100: c.setObjectType((short) ((HSSFSimpleShape) shape)
101: .getShapeType());
102: c.setObjectId((short) (shapeId));
103: c.setLocked(true);
104: c.setPrintable(true);
105: c.setAutofill(true);
106: c.setAutoline(true);
107: EndSubRecord e = new EndSubRecord();
108:
109: obj.addSubRecord(c);
110: obj.addSubRecord(e);
111:
112: return obj;
113: }
114:
115: public EscherContainerRecord getSpContainer() {
116: return spContainer;
117: }
118:
119: public ObjRecord getObjRecord() {
120: return objRecord;
121: }
122:
123: }
|