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.usermodel.*;
023:
024: /**
025: * An abstract shape is the lowlevel model for a shape.
026: *
027: * @author Glen Stampoultzis (glens at apache.org)
028: */
029: public abstract class AbstractShape {
030: /**
031: * Create a new shape object used to create the escher records.
032: *
033: * @param hssfShape The simple shape this is based on.
034: */
035: public static AbstractShape createShape(HSSFShape hssfShape,
036: int shapeId) {
037: AbstractShape shape;
038: if (hssfShape instanceof HSSFComment) {
039: shape = new CommentShape((HSSFComment) hssfShape, shapeId);
040: } else if (hssfShape instanceof HSSFTextbox) {
041: shape = new TextboxShape((HSSFTextbox) hssfShape, shapeId);
042: } else if (hssfShape instanceof HSSFPolygon) {
043: shape = new PolygonShape((HSSFPolygon) hssfShape, shapeId);
044: } else if (hssfShape instanceof HSSFSimpleShape) {
045: HSSFSimpleShape simpleShape = (HSSFSimpleShape) hssfShape;
046: switch (simpleShape.getShapeType()) {
047: case HSSFSimpleShape.OBJECT_TYPE_PICTURE:
048: shape = new PictureShape(simpleShape, shapeId);
049: break;
050: case HSSFSimpleShape.OBJECT_TYPE_LINE:
051: shape = new LineShape(simpleShape, shapeId);
052: break;
053: case HSSFSimpleShape.OBJECT_TYPE_OVAL:
054: case HSSFSimpleShape.OBJECT_TYPE_RECTANGLE:
055: shape = new SimpleFilledShape(simpleShape, shapeId);
056: break;
057: default:
058: throw new IllegalArgumentException(
059: "Do not know how to handle this type of shape");
060: }
061: } else {
062: throw new IllegalArgumentException("Unknown shape type");
063: }
064: EscherSpRecord sp = shape.getSpContainer().getChildById(
065: EscherSpRecord.RECORD_ID);
066: if (hssfShape.getParent() != null)
067: sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_CHILD);
068: return shape;
069: }
070:
071: protected AbstractShape() {
072: }
073:
074: /**
075: * @return The shape container and it's children that can represent this
076: * shape.
077: */
078: public abstract EscherContainerRecord getSpContainer();
079:
080: /**
081: * @return The object record that is associated with this shape.
082: */
083: public abstract ObjRecord getObjRecord();
084:
085: /**
086: * Creates an escher anchor record from a HSSFAnchor.
087: *
088: * @param userAnchor The high level anchor to convert.
089: * @return An escher anchor record.
090: */
091: protected EscherRecord createAnchor(HSSFAnchor userAnchor) {
092: return ConvertAnchor.createAnchor(userAnchor);
093: }
094:
095: /**
096: * Add standard properties to the opt record. These properties effect
097: * all records.
098: *
099: * @param shape The user model shape.
100: * @param opt The opt record to add the properties to.
101: * @return The number of options added.
102: */
103: protected int addStandardOptions(HSSFShape shape,
104: EscherOptRecord opt) {
105: opt
106: .addEscherProperty(new EscherBoolProperty(
107: EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE,
108: 0x080000));
109: // opt.addEscherProperty( new EscherBoolProperty( EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 0x080008 ) );
110: if (shape.isNoFill()) {
111: // Wonderful... none of the spec's give any clue as to what these constants mean.
112: opt.addEscherProperty(new EscherBoolProperty(
113: EscherProperties.FILL__NOFILLHITTEST, 0x00110000));
114: } else {
115: opt.addEscherProperty(new EscherBoolProperty(
116: EscherProperties.FILL__NOFILLHITTEST, 0x00010000));
117: }
118: opt
119: .addEscherProperty(new EscherRGBProperty(
120: EscherProperties.FILL__FILLCOLOR, shape
121: .getFillColor()));
122: opt.addEscherProperty(new EscherBoolProperty(
123: EscherProperties.GROUPSHAPE__PRINT, 0x080000));
124: opt.addEscherProperty(new EscherRGBProperty(
125: EscherProperties.LINESTYLE__COLOR, shape
126: .getLineStyleColor()));
127: int options = 5;
128: if (shape.getLineWidth() != HSSFShape.LINEWIDTH_DEFAULT) {
129: opt.addEscherProperty(new EscherSimpleProperty(
130: EscherProperties.LINESTYLE__LINEWIDTH, shape
131: .getLineWidth()));
132: options++;
133: }
134: if (shape.getLineStyle() != HSSFShape.LINESTYLE_SOLID) {
135: opt.addEscherProperty(new EscherSimpleProperty(
136: EscherProperties.LINESTYLE__LINEDASHING, shape
137: .getLineStyle()));
138: opt.addEscherProperty(new EscherSimpleProperty(
139: EscherProperties.LINESTYLE__LINEENDCAPSTYLE, 0));
140: if (shape.getLineStyle() == HSSFShape.LINESTYLE_NONE)
141: opt.addEscherProperty(new EscherBoolProperty(
142: EscherProperties.LINESTYLE__NOLINEDRAWDASH,
143: 0x00080000));
144: else
145: opt.addEscherProperty(new EscherBoolProperty(
146: EscherProperties.LINESTYLE__NOLINEDRAWDASH,
147: 0x00080008));
148: options += 3;
149: }
150: opt.sortProperties();
151: return options; // # options added
152: }
153:
154: }
|