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.

HSSFTextbox.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.DefaultEscherRecordFactory;
  17. import org.apache.poi.ddf.EscherBoolProperty;
  18. import org.apache.poi.ddf.EscherClientDataRecord;
  19. import org.apache.poi.ddf.EscherContainerRecord;
  20. import org.apache.poi.ddf.EscherOptRecord;
  21. import org.apache.poi.ddf.EscherPropertyTypes;
  22. import org.apache.poi.ddf.EscherRGBProperty;
  23. import org.apache.poi.ddf.EscherRecord;
  24. import org.apache.poi.ddf.EscherSimpleProperty;
  25. import org.apache.poi.ddf.EscherSpRecord;
  26. import org.apache.poi.ddf.EscherTextboxRecord;
  27. import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
  28. import org.apache.poi.hssf.record.EndSubRecord;
  29. import org.apache.poi.hssf.record.EscherAggregate;
  30. import org.apache.poi.hssf.record.ObjRecord;
  31. import org.apache.poi.hssf.record.TextObjectRecord;
  32. /**
  33. * A textbox is a shape that may hold a rich text string.
  34. */
  35. public class HSSFTextbox extends HSSFSimpleShape {
  36. public final static short OBJECT_TYPE_TEXT = 6;
  37. /**
  38. * How to align text horizontally
  39. */
  40. public final static short HORIZONTAL_ALIGNMENT_LEFT = 1;
  41. public final static short HORIZONTAL_ALIGNMENT_CENTERED = 2;
  42. public final static short HORIZONTAL_ALIGNMENT_RIGHT = 3;
  43. public final static short HORIZONTAL_ALIGNMENT_JUSTIFIED = 4;
  44. public final static short HORIZONTAL_ALIGNMENT_DISTRIBUTED = 7;
  45. /**
  46. * How to align text vertically
  47. */
  48. public final static short VERTICAL_ALIGNMENT_TOP = 1;
  49. public final static short VERTICAL_ALIGNMENT_CENTER = 2;
  50. public final static short VERTICAL_ALIGNMENT_BOTTOM = 3;
  51. public final static short VERTICAL_ALIGNMENT_JUSTIFY = 4;
  52. public final static short VERTICAL_ALIGNMENT_DISTRIBUTED = 7;
  53. public HSSFTextbox(EscherContainerRecord spContainer, ObjRecord objRecord, TextObjectRecord textObjectRecord) {
  54. super(spContainer, objRecord, textObjectRecord);
  55. }
  56. // Findbugs: URF_UNREAD_FIELD. Do not delete without understanding how this class works.
  57. //HSSFRichTextString string = new HSSFRichTextString("");
  58. /**
  59. * Construct a new textbox with the given parent and anchor.
  60. *
  61. * @param parent
  62. * @param anchor One of HSSFClientAnchor or HSSFChildAnchor
  63. */
  64. public HSSFTextbox(HSSFShape parent, HSSFAnchor anchor) {
  65. super(parent, anchor);
  66. setHorizontalAlignment(HORIZONTAL_ALIGNMENT_LEFT);
  67. setVerticalAlignment(VERTICAL_ALIGNMENT_TOP);
  68. setString(new HSSFRichTextString(""));
  69. }
  70. @Override
  71. protected ObjRecord createObjRecord() {
  72. ObjRecord obj = new ObjRecord();
  73. CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
  74. c.setObjectType(HSSFTextbox.OBJECT_TYPE_TEXT);
  75. c.setLocked(true);
  76. c.setPrintable(true);
  77. c.setAutofill(true);
  78. c.setAutoline(true);
  79. EndSubRecord e = new EndSubRecord();
  80. obj.addSubRecord(c);
  81. obj.addSubRecord(e);
  82. return obj;
  83. }
  84. @Override
  85. protected EscherContainerRecord createSpContainer() {
  86. EscherContainerRecord spContainer = new EscherContainerRecord();
  87. EscherSpRecord sp = new EscherSpRecord();
  88. EscherOptRecord opt = new EscherOptRecord();
  89. EscherClientDataRecord clientData = new EscherClientDataRecord();
  90. EscherTextboxRecord escherTextbox = new EscherTextboxRecord();
  91. spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
  92. spContainer.setOptions((short) 0x000F);
  93. sp.setRecordId(EscherSpRecord.RECORD_ID);
  94. sp.setOptions((short) ((EscherAggregate.ST_TEXTBOX << 4) | 0x2));
  95. sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE);
  96. opt.setRecordId(EscherOptRecord.RECORD_ID);
  97. opt.addEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.TEXT__TEXTID, 0));
  98. opt.addEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.TEXT__WRAPTEXT, 0));
  99. opt.addEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.TEXT__ANCHORTEXT, 0));
  100. opt.addEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.GROUPSHAPE__FLAGS, 0x00080000));
  101. opt.addEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.TEXT__TEXTLEFT, 0));
  102. opt.addEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.TEXT__TEXTRIGHT, 0));
  103. opt.addEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.TEXT__TEXTTOP, 0));
  104. opt.addEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.TEXT__TEXTBOTTOM, 0));
  105. opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.LINESTYLE__LINEDASHING, LINESTYLE_SOLID));
  106. opt.setEscherProperty(new EscherBoolProperty(EscherPropertyTypes.LINESTYLE__NOLINEDRAWDASH, 0x00080008));
  107. opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.LINESTYLE__LINEWIDTH, LINEWIDTH_DEFAULT));
  108. opt.setEscherProperty(new EscherRGBProperty(EscherPropertyTypes.FILL__FILLCOLOR, FILL__FILLCOLOR_DEFAULT));
  109. opt.setEscherProperty(new EscherRGBProperty(EscherPropertyTypes.LINESTYLE__COLOR, LINESTYLE__COLOR_DEFAULT));
  110. opt.setEscherProperty(new EscherBoolProperty(EscherPropertyTypes.FILL__NOFILLHITTEST, NO_FILLHITTEST_FALSE));
  111. opt.setEscherProperty(new EscherBoolProperty(EscherPropertyTypes.GROUPSHAPE__FLAGS, 0x080000));
  112. EscherRecord anchor = getAnchor().getEscherAnchor();
  113. clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
  114. clientData.setOptions((short) 0x0000);
  115. escherTextbox.setRecordId(EscherTextboxRecord.RECORD_ID);
  116. escherTextbox.setOptions((short) 0x0000);
  117. spContainer.addChildRecord(sp);
  118. spContainer.addChildRecord(opt);
  119. spContainer.addChildRecord(anchor);
  120. spContainer.addChildRecord(clientData);
  121. spContainer.addChildRecord(escherTextbox);
  122. return spContainer;
  123. }
  124. @Override
  125. void afterInsert(HSSFPatriarch patriarch) {
  126. EscherAggregate agg = patriarch.getBoundAggregate();
  127. agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
  128. if (getTextObjectRecord() != null){
  129. agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID), getTextObjectRecord());
  130. }
  131. }
  132. /**
  133. * @return Returns the left margin within the textbox.
  134. */
  135. public int getMarginLeft() {
  136. EscherSimpleProperty property = getOptRecord().lookup(EscherPropertyTypes.TEXT__TEXTLEFT);
  137. return property == null ? 0 : property.getPropertyValue();
  138. }
  139. /**
  140. * Sets the left margin within the textbox.
  141. */
  142. public void setMarginLeft(int marginLeft) {
  143. setPropertyValue(new EscherSimpleProperty(EscherPropertyTypes.TEXT__TEXTLEFT, marginLeft));
  144. }
  145. /**
  146. * @return returns the right margin within the textbox.
  147. */
  148. public int getMarginRight() {
  149. EscherSimpleProperty property = getOptRecord().lookup(EscherPropertyTypes.TEXT__TEXTRIGHT);
  150. return property == null ? 0 : property.getPropertyValue();
  151. }
  152. /**
  153. * Sets the right margin within the textbox.
  154. */
  155. public void setMarginRight(int marginRight) {
  156. setPropertyValue(new EscherSimpleProperty(EscherPropertyTypes.TEXT__TEXTRIGHT, marginRight));
  157. }
  158. /**
  159. * @return returns the top margin within the textbox.
  160. */
  161. public int getMarginTop() {
  162. EscherSimpleProperty property = getOptRecord().lookup(EscherPropertyTypes.TEXT__TEXTTOP);
  163. return property == null ? 0 : property.getPropertyValue();
  164. }
  165. /**
  166. * Sets the top margin within the textbox.
  167. */
  168. public void setMarginTop(int marginTop) {
  169. setPropertyValue(new EscherSimpleProperty(EscherPropertyTypes.TEXT__TEXTTOP, marginTop));
  170. }
  171. /**
  172. * Gets the bottom margin within the textbox.
  173. */
  174. public int getMarginBottom() {
  175. EscherSimpleProperty property = getOptRecord().lookup(EscherPropertyTypes.TEXT__TEXTBOTTOM);
  176. return property == null ? 0 : property.getPropertyValue();
  177. }
  178. /**
  179. * Sets the bottom margin within the textbox.
  180. */
  181. public void setMarginBottom(int marginBottom) {
  182. setPropertyValue(new EscherSimpleProperty(EscherPropertyTypes.TEXT__TEXTBOTTOM, marginBottom));
  183. }
  184. /**
  185. * Gets the horizontal alignment.
  186. */
  187. public short getHorizontalAlignment() {
  188. return (short) getTextObjectRecord().getHorizontalTextAlignment();
  189. }
  190. /**
  191. * Sets the horizontal alignment.
  192. */
  193. public void setHorizontalAlignment(short align) {
  194. getTextObjectRecord().setHorizontalTextAlignment(align);
  195. }
  196. /**
  197. * Gets the vertical alignment.
  198. */
  199. public short getVerticalAlignment() {
  200. return (short) getTextObjectRecord().getVerticalTextAlignment();
  201. }
  202. /**
  203. * Sets the vertical alignment.
  204. */
  205. public void setVerticalAlignment(short align) {
  206. getTextObjectRecord().setVerticalTextAlignment(align);
  207. }
  208. @Override
  209. public void setShapeType(int shapeType) {
  210. throw new IllegalStateException("Shape type can not be changed in " + this.getClass().getSimpleName());
  211. }
  212. @Override
  213. protected HSSFShape cloneShape() {
  214. TextObjectRecord txo = getTextObjectRecord() == null ? null : (TextObjectRecord) getTextObjectRecord().cloneViaReserialise();
  215. EscherContainerRecord spContainer = new EscherContainerRecord();
  216. byte[] inSp = getEscherContainer().serialize();
  217. spContainer.fillFields(inSp, 0, new DefaultEscherRecordFactory());
  218. ObjRecord obj = (ObjRecord) getObjRecord().cloneViaReserialise();
  219. return new HSSFTextbox(spContainer, obj, txo);
  220. }
  221. @Override
  222. protected void afterRemove(HSSFPatriarch patriarch) {
  223. patriarch.getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID));
  224. patriarch.getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));
  225. }
  226. }