KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2    Copyright 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 package org.apache.poi.hssf.model;
18
19 import org.apache.poi.ddf.*;
20 import org.apache.poi.hssf.record.ObjRecord;
21 import org.apache.poi.hssf.usermodel.*;
22
23 /**
24  * An abstract shape is the lowlevel model for a shape.
25  *
26  * @author Glen Stampoultzis (glens at apache.org)
27  */

28 public abstract class AbstractShape
29 {
30     /**
31      * Create a new shape object used to create the escher records.
32      *
33      * @param hssfShape The simple shape this is based on.
34      */

35     public static AbstractShape createShape( HSSFShape hssfShape, int shapeId )
36     {
37         AbstractShape shape = null;
38         if (hssfShape instanceof HSSFTextbox)
39         {
40             shape = new TextboxShape( (HSSFTextbox)hssfShape, shapeId );
41         }
42         else if (hssfShape instanceof HSSFPolygon)
43         {
44             shape = new PolygonShape( (HSSFPolygon) hssfShape, shapeId );
45         }
46         else if (hssfShape instanceof HSSFSimpleShape)
47         {
48             HSSFSimpleShape simpleShape = (HSSFSimpleShape) hssfShape;
49             switch ( simpleShape.getShapeType() )
50             {
51                 case HSSFSimpleShape.OBJECT_TYPE_LINE:
52                     shape = new LineShape( simpleShape, shapeId );
53                     break;
54                 case HSSFSimpleShape.OBJECT_TYPE_OVAL:
55                 case HSSFSimpleShape.OBJECT_TYPE_RECTANGLE:
56                     shape = new SimpleFilledShape( simpleShape, shapeId );
57                     break;
58                 default:
59                     throw new IllegalArgumentException JavaDoc("Do not know how to handle this type of shape");
60             }
61         }
62         else
63         {
64             throw new IllegalArgumentException JavaDoc("Unknown shape type");
65         }
66         EscherSpRecord sp = shape.getSpContainer().getChildById(EscherSpRecord.RECORD_ID);
67         if (hssfShape.getParent() != null)
68             sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_CHILD);
69         return shape;
70     }
71
72     protected AbstractShape()
73     {
74     }
75
76     /**
77      * @return The shape container and it's children that can represent this
78      * shape.
79      */

80     public abstract EscherContainerRecord getSpContainer();
81
82     /**
83      * @return The object record that is associated with this shape.
84      */

85     public abstract ObjRecord getObjRecord();
86
87     /**
88      * Creates an escher anchor record from a HSSFAnchor.
89      *
90      * @param userAnchor The high level anchor to convert.
91      * @return An escher anchor record.
92      */

93     protected EscherRecord createAnchor( HSSFAnchor userAnchor )
94     {
95         return ConvertAnchor.createAnchor(userAnchor);
96     }
97
98     /**
99      * Add standard properties to the opt record. These properties effect
100      * all records.
101      *
102      * @param shape The user model shape.
103      * @param opt The opt record to add the properties to.
104      * @return The number of options added.
105      */

106     protected int addStandardOptions( HSSFShape shape, EscherOptRecord opt )
107     {
108         opt.addEscherProperty( new EscherBoolProperty( EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 0x080000 ) );
109 // opt.addEscherProperty( new EscherBoolProperty( EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 0x080008 ) );
110
if ( shape.isNoFill() )
111         {
112             // Wonderful... none of the spec's give any clue as to what these constants mean.
113
opt.addEscherProperty( new EscherBoolProperty( EscherProperties.FILL__NOFILLHITTEST, 0x00110000 ) );
114         }
115         else
116         {
117             opt.addEscherProperty( new EscherBoolProperty( EscherProperties.FILL__NOFILLHITTEST, 0x00010000 ) );
118         }
119         opt.addEscherProperty( new EscherRGBProperty( EscherProperties.FILL__FILLCOLOR, shape.getFillColor() ) );
120         opt.addEscherProperty( new EscherBoolProperty( EscherProperties.GROUPSHAPE__PRINT, 0x080000 ) );
121         opt.addEscherProperty( new EscherRGBProperty( EscherProperties.LINESTYLE__COLOR, shape.getLineStyleColor() ) );
122         int options = 5;
123         if (shape.getLineWidth() != HSSFShape.LINEWIDTH_DEFAULT)
124         {
125             opt.addEscherProperty( new EscherSimpleProperty( EscherProperties.LINESTYLE__LINEWIDTH, shape.getLineWidth()));
126             options++;
127         }
128         if (shape.getLineStyle() != HSSFShape.LINESTYLE_SOLID)
129         {
130             opt.addEscherProperty( new EscherSimpleProperty( EscherProperties.LINESTYLE__LINEDASHING, shape.getLineStyle()));
131             opt.addEscherProperty( new EscherSimpleProperty( EscherProperties.LINESTYLE__LINEENDCAPSTYLE, 0));
132             if (shape.getLineStyle() == HSSFShape.LINESTYLE_NONE)
133                 opt.addEscherProperty( new EscherBoolProperty( EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x00080000));
134             else
135                 opt.addEscherProperty( new EscherBoolProperty( EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x00080008));
136             options += 3;
137         }
138         opt.sortProperties();
139         return options; // # options added
140
}
141
142 }
143
Popular Tags