You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HSSFSimpleShape.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.usermodel;
  16. import org.apache.poi.ddf.*;
  17. import org.apache.poi.hssf.record.*;
  18. import org.apache.poi.ss.usermodel.RichTextString;
  19. /**
  20. * Represents a simple shape such as a line, rectangle or oval.
  21. *
  22. * @author Glen Stampoultzis (glens at apache.org)
  23. */
  24. public class HSSFSimpleShape extends HSSFShape
  25. {
  26. // The commented out ones haven't been tested yet or aren't supported
  27. // by HSSFSimpleShape.
  28. public final static short OBJECT_TYPE_LINE = HSSFShapeTypes.Line;
  29. public final static short OBJECT_TYPE_RECTANGLE = HSSFShapeTypes.Rectangle;
  30. public final static short OBJECT_TYPE_OVAL = HSSFShapeTypes.Ellipse;
  31. public final static short OBJECT_TYPE_ARC = HSSFShapeTypes.Arc;
  32. // public final static short OBJECT_TYPE_CHART = 5;
  33. // public final static short OBJECT_TYPE_TEXT = 6;
  34. // public final static short OBJECT_TYPE_BUTTON = 7;
  35. public final static short OBJECT_TYPE_PICTURE = HSSFShapeTypes.PictureFrame;
  36. // public final static short OBJECT_TYPE_POLYGON = 9;
  37. // public final static short OBJECT_TYPE_CHECKBOX = 11;
  38. // public final static short OBJECT_TYPE_OPTION_BUTTON = 12;
  39. // public final static short OBJECT_TYPE_EDIT_BOX = 13;
  40. // public final static short OBJECT_TYPE_LABEL = 14;
  41. // public final static short OBJECT_TYPE_DIALOG_BOX = 15;
  42. // public final static short OBJECT_TYPE_SPINNER = 16;
  43. // public final static short OBJECT_TYPE_SCROLL_BAR = 17;
  44. // public final static short OBJECT_TYPE_LIST_BOX = 18;
  45. // public final static short OBJECT_TYPE_GROUP_BOX = 19;
  46. public final static short OBJECT_TYPE_COMBO_BOX = HSSFShapeTypes.HostControl;
  47. public final static short OBJECT_TYPE_COMMENT = HSSFShapeTypes.TextBox;
  48. public final static short OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING = 30;
  49. public final static int WRAP_SQUARE = 0;
  50. public final static int WRAP_BY_POINTS = 1;
  51. public final static int WRAP_NONE = 2;
  52. private TextObjectRecord _textObjectRecord;
  53. public HSSFSimpleShape(EscherContainerRecord spContainer, ObjRecord objRecord, TextObjectRecord textObjectRecord) {
  54. super(spContainer, objRecord);
  55. this._textObjectRecord = textObjectRecord;
  56. }
  57. public HSSFSimpleShape(EscherContainerRecord spContainer, ObjRecord objRecord) {
  58. super(spContainer, objRecord);
  59. }
  60. public HSSFSimpleShape( HSSFShape parent, HSSFAnchor anchor)
  61. {
  62. super( parent, anchor );
  63. _textObjectRecord = createTextObjRecord();
  64. }
  65. protected TextObjectRecord getTextObjectRecord() {
  66. return _textObjectRecord;
  67. }
  68. protected TextObjectRecord createTextObjRecord(){
  69. TextObjectRecord obj = new TextObjectRecord();
  70. obj.setHorizontalTextAlignment(2);
  71. obj.setVerticalTextAlignment(2);
  72. obj.setTextLocked(true);
  73. obj.setTextOrientation(TextObjectRecord.TEXT_ORIENTATION_NONE);
  74. obj.setStr(new HSSFRichTextString(""));
  75. return obj;
  76. }
  77. @Override
  78. protected EscherContainerRecord createSpContainer() {
  79. EscherContainerRecord spContainer = new EscherContainerRecord();
  80. spContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
  81. spContainer.setOptions( (short) 0x000F );
  82. EscherSpRecord sp = new EscherSpRecord();
  83. sp.setRecordId( EscherSpRecord.RECORD_ID );
  84. sp.setFlags( EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE );
  85. sp.setVersion((short) 0x2);
  86. EscherClientDataRecord clientData = new EscherClientDataRecord();
  87. clientData.setRecordId( EscherClientDataRecord.RECORD_ID );
  88. clientData.setOptions( (short) (0x0000) );
  89. EscherOptRecord optRecord = new EscherOptRecord();
  90. optRecord.setEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEDASHING, LINESTYLE_SOLID));
  91. optRecord.setEscherProperty( new EscherBoolProperty( EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x00080008));
  92. // optRecord.setEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEWIDTH, LINEWIDTH_DEFAULT));
  93. optRecord.setEscherProperty(new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, FILL__FILLCOLOR_DEFAULT));
  94. optRecord.setEscherProperty(new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, LINESTYLE__COLOR_DEFAULT));
  95. optRecord.setEscherProperty(new EscherBoolProperty(EscherProperties.FILL__NOFILLHITTEST, NO_FILLHITTEST_FALSE));
  96. optRecord.setEscherProperty( new EscherBoolProperty( EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x00080008));
  97. optRecord.setEscherProperty( new EscherShapePathProperty( EscherProperties.GEOMETRY__SHAPEPATH, EscherShapePathProperty.COMPLEX ) );
  98. optRecord.setEscherProperty(new EscherBoolProperty( EscherProperties.GROUPSHAPE__PRINT, 0x080000));
  99. optRecord.setRecordId( EscherOptRecord.RECORD_ID );
  100. EscherTextboxRecord escherTextbox = new EscherTextboxRecord();
  101. escherTextbox.setRecordId(EscherTextboxRecord.RECORD_ID);
  102. escherTextbox.setOptions((short) 0x0000);
  103. spContainer.addChildRecord(sp);
  104. spContainer.addChildRecord(optRecord);
  105. spContainer.addChildRecord(getAnchor().getEscherAnchor());
  106. spContainer.addChildRecord(clientData);
  107. spContainer.addChildRecord(escherTextbox);
  108. return spContainer;
  109. }
  110. @Override
  111. protected ObjRecord createObjRecord() {
  112. ObjRecord obj = new ObjRecord();
  113. CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
  114. c.setLocked(true);
  115. c.setPrintable(true);
  116. c.setAutofill(true);
  117. c.setAutoline(true);
  118. EndSubRecord e = new EndSubRecord();
  119. obj.addSubRecord(c);
  120. obj.addSubRecord(e);
  121. return obj;
  122. }
  123. @Override
  124. protected void afterRemove(HSSFPatriarch patriarch) {
  125. patriarch._getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID));
  126. if (null != getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID)){
  127. patriarch._getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));
  128. }
  129. }
  130. /**
  131. * @return the rich text string for this textbox.
  132. */
  133. public HSSFRichTextString getString() {
  134. return _textObjectRecord.getStr();
  135. }
  136. /**
  137. * @param string Sets the rich text string used by this object.
  138. */
  139. public void setString(RichTextString string) {
  140. //TODO add other shape types which can not contain text
  141. if (getShapeType() == 0 || getShapeType() == OBJECT_TYPE_LINE){
  142. throw new IllegalStateException("Cannot set text for shape type: "+getShapeType());
  143. }
  144. HSSFRichTextString rtr = (HSSFRichTextString) string;
  145. // If font is not set we must set the default one
  146. if (rtr.numFormattingRuns() == 0) rtr.applyFont((short) 0);
  147. TextObjectRecord txo = getOrCreateTextObjRecord();
  148. txo.setStr(rtr);
  149. if (string.getString() != null){
  150. setPropertyValue(new EscherSimpleProperty(EscherProperties.TEXT__TEXTID, string.getString().hashCode()));
  151. }
  152. }
  153. @Override
  154. void afterInsert(HSSFPatriarch patriarch){
  155. EscherAggregate agg = patriarch._getBoundAggregate();
  156. agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
  157. if (null != getTextObjectRecord()){
  158. agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID), getTextObjectRecord());
  159. }
  160. }
  161. @Override
  162. protected HSSFShape cloneShape() {
  163. TextObjectRecord txo = null;
  164. EscherContainerRecord spContainer = new EscherContainerRecord();
  165. byte [] inSp = getEscherContainer().serialize();
  166. spContainer.fillFields(inSp, 0, new DefaultEscherRecordFactory());
  167. ObjRecord obj = (ObjRecord) getObjRecord().cloneViaReserialise();
  168. if (getTextObjectRecord() != null && getString() != null && null != getString().getString()){
  169. txo = (TextObjectRecord) getTextObjectRecord().cloneViaReserialise();
  170. }
  171. return new HSSFSimpleShape(spContainer, obj, txo);
  172. }
  173. /**
  174. * Gets the shape type.
  175. * @return One of the OBJECT_TYPE_* constants.
  176. *
  177. * @see #OBJECT_TYPE_LINE
  178. * @see #OBJECT_TYPE_OVAL
  179. * @see #OBJECT_TYPE_RECTANGLE
  180. * @see #OBJECT_TYPE_PICTURE
  181. * @see #OBJECT_TYPE_COMMENT
  182. */
  183. public int getShapeType() {
  184. EscherSpRecord spRecord = getEscherContainer().getChildById(EscherSpRecord.RECORD_ID);
  185. return spRecord.getShapeType();
  186. }
  187. public int getWrapText(){
  188. EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.TEXT__WRAPTEXT);
  189. return null == property ? WRAP_SQUARE : property.getPropertyValue();
  190. }
  191. public void setWrapText(int value){
  192. setPropertyValue(new EscherSimpleProperty(EscherProperties.TEXT__WRAPTEXT, false, false, value));
  193. }
  194. /**
  195. * @see HSSFShapeTypes
  196. * @param value - shapeType
  197. */
  198. public void setShapeType(int value){
  199. CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) getObjRecord().getSubRecords().get(0);
  200. cod.setObjectType(OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING);
  201. EscherSpRecord spRecord = getEscherContainer().getChildById(EscherSpRecord.RECORD_ID);
  202. spRecord.setShapeType((short) value);
  203. }
  204. private TextObjectRecord getOrCreateTextObjRecord(){
  205. if (getTextObjectRecord() == null){
  206. _textObjectRecord = createTextObjRecord();
  207. }
  208. EscherTextboxRecord escherTextbox = getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID);
  209. if (null == escherTextbox){
  210. escherTextbox = new EscherTextboxRecord();
  211. escherTextbox.setRecordId(EscherTextboxRecord.RECORD_ID);
  212. escherTextbox.setOptions((short) 0x0000);
  213. getEscherContainer().addChildRecord(escherTextbox);
  214. getPatriarch()._getBoundAggregate().associateShapeToObjRecord(escherTextbox, _textObjectRecord);
  215. }
  216. return _textObjectRecord;
  217. }
  218. }