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.hslf.model;
019:
020: import org.apache.poi.ddf.*;
021: import org.apache.poi.util.LittleEndian;
022: import org.apache.poi.hslf.record.ColorSchemeAtom;
023:
024: import java.awt.*;
025:
026: /**
027: * An abstract simple (non-group) shape.
028: * This is the parent class for all primitive shapes like Line, Rectangle, etc.
029: *
030: * @author Yegor Kozlov
031: */
032: public class SimpleShape extends Shape {
033:
034: /**
035: * Create a SimpleShape object and initialize it from the supplied Record container.
036: *
037: * @param escherRecord <code>EscherSpContainer</code> container which holds information about this shape
038: * @param parent the parent of the shape
039: */
040: protected SimpleShape(EscherContainerRecord escherRecord,
041: Shape parent) {
042: super (escherRecord, parent);
043: }
044:
045: /**
046: * Create a new Shape
047: *
048: * @param isChild <code>true</code> if the Line is inside a group, <code>false</code> otherwise
049: * @return the record container which holds this shape
050: */
051: protected EscherContainerRecord createSpContainer(boolean isChild) {
052: EscherContainerRecord spContainer = new EscherContainerRecord();
053: spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
054: spContainer.setOptions((short) 15);
055:
056: EscherSpRecord sp = new EscherSpRecord();
057: int flags = EscherSpRecord.FLAG_HAVEANCHOR
058: | EscherSpRecord.FLAG_HASSHAPETYPE;
059: if (isChild)
060: flags |= EscherSpRecord.FLAG_CHILD;
061: sp.setFlags(flags);
062: spContainer.addChildRecord(sp);
063:
064: EscherOptRecord opt = new EscherOptRecord();
065: opt.setRecordId(EscherOptRecord.RECORD_ID);
066: spContainer.addChildRecord(opt);
067:
068: EscherRecord anchor;
069: if (isChild)
070: anchor = new EscherChildAnchorRecord();
071: else {
072: anchor = new EscherClientAnchorRecord();
073:
074: //hack. internal variable EscherClientAnchorRecord.shortRecord can be
075: //initialized only in fillFields(). We need to set shortRecord=false;
076: byte[] header = new byte[16];
077: LittleEndian.putUShort(header, 0, 0);
078: LittleEndian.putUShort(header, 2, 0);
079: LittleEndian.putInt(header, 4, 8);
080: anchor.fillFields(header, 0, null);
081: }
082: spContainer.addChildRecord(anchor);
083:
084: return spContainer;
085: }
086:
087: /**
088: * Returns width of the line in in points
089: */
090: public double getLineWidth() {
091: EscherOptRecord opt = (EscherOptRecord) getEscherChild(
092: _escherContainer, EscherOptRecord.RECORD_ID);
093: EscherSimpleProperty prop = (EscherSimpleProperty) getEscherProperty(
094: opt, EscherProperties.LINESTYLE__LINEWIDTH);
095: return prop == null ? 0 : (double) prop.getPropertyValue()
096: / EMU_PER_POINT;
097: }
098:
099: /**
100: * Sets the width of line in in points
101: * @param width the width of line in in points
102: */
103: public void setLineWidth(double width) {
104: EscherOptRecord opt = (EscherOptRecord) getEscherChild(
105: _escherContainer, EscherOptRecord.RECORD_ID);
106: setEscherProperty(opt, EscherProperties.LINESTYLE__LINEWIDTH,
107: (int) (width * EMU_PER_POINT));
108: }
109:
110: /**
111: * Sets the color of line
112: *
113: * @param color new color of the line
114: */
115: public void setLineColor(Color color) {
116: EscherOptRecord opt = (EscherOptRecord) getEscherChild(
117: _escherContainer, EscherOptRecord.RECORD_ID);
118: int rgb = new Color(color.getBlue(), color.getGreen(), color
119: .getRed(), 0).getRGB();
120: setEscherProperty(opt, EscherProperties.LINESTYLE__COLOR, rgb);
121: setEscherProperty(opt,
122: EscherProperties.LINESTYLE__NOLINEDRAWDASH,
123: color == null ? 0x180010 : 0x180018);
124: }
125:
126: /**
127: * @return color of the line. If color is not set returns <code>java.awt.Color.black</code>
128: */
129: public Color getLineColor() {
130: EscherOptRecord opt = (EscherOptRecord) getEscherChild(
131: _escherContainer, EscherOptRecord.RECORD_ID);
132:
133: EscherSimpleProperty p1 = (EscherSimpleProperty) getEscherProperty(
134: opt, EscherProperties.LINESTYLE__COLOR);
135: EscherSimpleProperty p2 = (EscherSimpleProperty) getEscherProperty(
136: opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH);
137: int p2val = p2 == null ? 0 : p2.getPropertyValue();
138: Color clr = null;
139: if (p1 != null && (p2val & 0x8) != 0) {
140: int rgb = p1.getPropertyValue();
141: if (rgb >= 0x8000000) {
142: int idx = rgb % 0x8000000;
143: if (getSheet() != null) {
144: ColorSchemeAtom ca = getSheet().getColorScheme();
145: if (idx >= 0 && idx <= 7)
146: rgb = ca.getColor(idx);
147: }
148: }
149: Color tmp = new Color(rgb, true);
150: clr = new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed());
151: }
152: return clr;
153: }
154:
155: /**
156: * Gets line dashing. One of the PEN_* constants defined in this class.
157: *
158: * @return dashing of the line.
159: */
160: public int getLineDashing() {
161: EscherOptRecord opt = (EscherOptRecord) getEscherChild(
162: _escherContainer, EscherOptRecord.RECORD_ID);
163:
164: EscherSimpleProperty prop = (EscherSimpleProperty) getEscherProperty(
165: opt, EscherProperties.LINESTYLE__LINEDASHING);
166: return prop == null ? Line.PEN_SOLID : prop.getPropertyValue();
167: }
168:
169: /**
170: * Sets line dashing. One of the PEN_* constants defined in this class.
171: *
172: * @param pen new style of the line.
173: */
174: public void setLineDashing(int pen) {
175: EscherOptRecord opt = (EscherOptRecord) getEscherChild(
176: _escherContainer, EscherOptRecord.RECORD_ID);
177:
178: setEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING,
179: pen == Line.PEN_SOLID ? -1 : pen);
180: }
181:
182: /**
183: * Sets line style. One of the constants defined in this class.
184: *
185: * @param style new style of the line.
186: */
187: public void setLineStyle(int style) {
188: EscherOptRecord opt = (EscherOptRecord) getEscherChild(
189: _escherContainer, EscherOptRecord.RECORD_ID);
190: setEscherProperty(opt, EscherProperties.LINESTYLE__LINESTYLE,
191: style == Line.LINE_SIMPLE ? -1 : style);
192: }
193:
194: /**
195: * Returns line style. One of the constants defined in this class.
196: *
197: * @return style of the line.
198: */
199: public int getLineStyle() {
200: EscherOptRecord opt = (EscherOptRecord) getEscherChild(
201: _escherContainer, EscherOptRecord.RECORD_ID);
202: EscherSimpleProperty prop = (EscherSimpleProperty) getEscherProperty(
203: opt, EscherProperties.LINESTYLE__LINESTYLE);
204: return prop == null ? Line.LINE_SIMPLE : prop
205: .getPropertyValue();
206: }
207:
208: /**
209: * The color used to fill this shape.
210: */
211: public Color getFillColor() {
212: EscherOptRecord opt = (EscherOptRecord) getEscherChild(
213: _escherContainer, EscherOptRecord.RECORD_ID);
214: EscherSimpleProperty p1 = (EscherSimpleProperty) getEscherProperty(
215: opt, EscherProperties.FILL__FILLCOLOR);
216: EscherSimpleProperty p2 = (EscherSimpleProperty) getEscherProperty(
217: opt, EscherProperties.FILL__NOFILLHITTEST);
218:
219: int p2val = p2 == null ? 0 : p2.getPropertyValue();
220:
221: Color clr = null;
222: if (p1 != null && (p2val & 0x10) != 0) {
223: int rgb = p1.getPropertyValue();
224: if (rgb >= 0x8000000) {
225: int idx = rgb % 0x8000000;
226: if (getSheet() != null) {
227: ColorSchemeAtom ca = getSheet().getColorScheme();
228: rgb = ca.getColor(idx);
229: }
230: }
231: Color tmp = new Color(rgb, true);
232: clr = new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed());
233: }
234: return clr;
235: }
236:
237: /**
238: * The color used to fill this shape.
239: *
240: * @param color the background color
241: */
242: public void setFillColor(Color color) {
243: EscherOptRecord opt = (EscherOptRecord) getEscherChild(
244: _escherContainer, EscherOptRecord.RECORD_ID);
245: int rgb = new Color(color.getBlue(), color.getGreen(), color
246: .getRed(), 0).getRGB();
247: setEscherProperty(opt, EscherProperties.FILL__FILLCOLOR, rgb);
248: setEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST,
249: color == null ? 0x150010 : 0x150011);
250: }
251:
252: }
|