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 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. * A textbox is a shape that may hold a rich text string.
  21. *
  22. * @author Glen Stampoultzis (glens at apache.org)
  23. */
  24. public class HSSFTextbox extends HSSFSimpleShape {
  25. public final static short OBJECT_TYPE_TEXT = 6;
  26. /**
  27. * How to align text horizontally
  28. */
  29. public final static short HORIZONTAL_ALIGNMENT_LEFT = 1;
  30. public final static short HORIZONTAL_ALIGNMENT_CENTERED = 2;
  31. public final static short HORIZONTAL_ALIGNMENT_RIGHT = 3;
  32. public final static short HORIZONTAL_ALIGNMENT_JUSTIFIED = 4;
  33. public final static short HORIZONTAL_ALIGNMENT_DISTRIBUTED = 7;
  34. /**
  35. * How to align text vertically
  36. */
  37. public final static short VERTICAL_ALIGNMENT_TOP = 1;
  38. public final static short VERTICAL_ALIGNMENT_CENTER = 2;
  39. public final static short VERTICAL_ALIGNMENT_BOTTOM = 3;
  40. public final static short VERTICAL_ALIGNMENT_JUSTIFY = 4;
  41. public final static short VERTICAL_ALIGNMENT_DISTRIBUTED = 7;
  42. int marginLeft, marginRight, marginTop, marginBottom;
  43. short halign, valign;
  44. private TextObjectRecord _textObjectRecord;
  45. public HSSFTextbox(EscherContainerRecord spContainer, ObjRecord objRecord, TextObjectRecord textObjectRecord) {
  46. super(spContainer, objRecord);
  47. this._textObjectRecord = textObjectRecord;
  48. }
  49. HSSFRichTextString string = new HSSFRichTextString("");
  50. /**
  51. * Construct a new textbox with the given parent and anchor.
  52. *
  53. * @param parent
  54. * @param anchor One of HSSFClientAnchor or HSSFChildAnchor
  55. */
  56. public HSSFTextbox(HSSFShape parent, HSSFAnchor anchor) {
  57. super(parent, anchor);
  58. _textObjectRecord = createTextObjRecord();
  59. setHorizontalAlignment(HORIZONTAL_ALIGNMENT_LEFT);
  60. setVerticalAlignment(VERTICAL_ALIGNMENT_TOP);
  61. setString(new HSSFRichTextString(""));
  62. }
  63. protected TextObjectRecord createTextObjRecord(){
  64. TextObjectRecord obj = new TextObjectRecord();
  65. obj.setTextLocked(true);
  66. obj.setTextOrientation(TextObjectRecord.TEXT_ORIENTATION_NONE);
  67. return obj;
  68. }
  69. @Override
  70. protected ObjRecord createObjRecord() {
  71. ObjRecord obj = new ObjRecord();
  72. CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
  73. c.setObjectType(OBJECT_TYPE_TEXT);
  74. c.setLocked( true );
  75. c.setPrintable( true );
  76. c.setAutofill( true );
  77. c.setAutoline( true );
  78. EndSubRecord e = new EndSubRecord();
  79. obj.addSubRecord( c );
  80. obj.addSubRecord( e );
  81. return obj;
  82. }
  83. @Override
  84. protected EscherContainerRecord createSpContainer() {
  85. EscherContainerRecord spContainer = new EscherContainerRecord();
  86. EscherSpRecord sp = new EscherSpRecord();
  87. EscherOptRecord opt = new EscherOptRecord();
  88. EscherClientDataRecord clientData = new EscherClientDataRecord();
  89. EscherTextboxRecord escherTextbox = new EscherTextboxRecord();
  90. spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
  91. spContainer.setOptions((short) 0x000F);
  92. sp.setRecordId(EscherSpRecord.RECORD_ID);
  93. sp.setOptions((short) ((EscherAggregate.ST_TEXTBOX << 4) | 0x2));
  94. sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE);
  95. opt.setRecordId(EscherOptRecord.RECORD_ID);
  96. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTID, 0));
  97. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTLEFT, getMarginLeft()));
  98. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTRIGHT, getMarginRight()));
  99. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTBOTTOM, getMarginBottom()));
  100. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTTOP, getMarginTop()));
  101. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__WRAPTEXT, 0));
  102. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__ANCHORTEXT, 0));
  103. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GROUPSHAPE__PRINT, 0x00080000));
  104. EscherRecord anchor = getAnchor().getEscherAnchor();
  105. clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
  106. clientData.setOptions((short) 0x0000);
  107. escherTextbox.setRecordId(EscherTextboxRecord.RECORD_ID);
  108. escherTextbox.setOptions((short) 0x0000);
  109. spContainer.addChildRecord(sp);
  110. spContainer.addChildRecord(opt);
  111. spContainer.addChildRecord(anchor);
  112. spContainer.addChildRecord(clientData);
  113. spContainer.addChildRecord(escherTextbox);
  114. return spContainer;
  115. }
  116. @Override
  117. void afterInsert(HSSFPatriarch patriarch){
  118. EscherAggregate agg = patriarch._getBoundAggregate();
  119. agg.associateShapeToObjRecord(_escherContainer.getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
  120. agg.associateShapeToObjRecord(_escherContainer.getChildById(EscherTextboxRecord.RECORD_ID), getTextObjectRecord());
  121. }
  122. /**
  123. * @return the rich text string for this textbox.
  124. */
  125. public HSSFRichTextString getString() {
  126. return _textObjectRecord.getStr();
  127. }
  128. /**
  129. * @param string Sets the rich text string used by this object.
  130. */
  131. public void setString(RichTextString string) {
  132. HSSFRichTextString rtr = (HSSFRichTextString) string;
  133. // If font is not set we must set the default one
  134. if (rtr.numFormattingRuns() == 0) rtr.applyFont((short) 0);
  135. _textObjectRecord.setStr(rtr);
  136. }
  137. /**
  138. * @return Returns the left margin within the textbox.
  139. */
  140. public int getMarginLeft() {
  141. EscherSimpleProperty property = _optRecord.lookup(EscherProperties.TEXT__TEXTLEFT);
  142. return property == null ? 0: property.getPropertyValue();
  143. }
  144. /**
  145. * Sets the left margin within the textbox.
  146. */
  147. public void setMarginLeft(int marginLeft) {
  148. setPropertyValue(new EscherSimpleProperty(EscherProperties.TEXT__TEXTLEFT, marginLeft));
  149. }
  150. /**
  151. * @return returns the right margin within the textbox.
  152. */
  153. public int getMarginRight() {
  154. EscherSimpleProperty property = _optRecord.lookup(EscherProperties.TEXT__TEXTRIGHT);
  155. return property == null ? 0: property.getPropertyValue();
  156. }
  157. /**
  158. * Sets the right margin within the textbox.
  159. */
  160. public void setMarginRight(int marginRight) {
  161. setPropertyValue(new EscherSimpleProperty(EscherProperties.TEXT__TEXTRIGHT, marginRight));
  162. }
  163. /**
  164. * @return returns the top margin within the textbox.
  165. */
  166. public int getMarginTop() {
  167. EscherSimpleProperty property = _optRecord.lookup(EscherProperties.TEXT__TEXTTOP);
  168. return property == null ? 0: property.getPropertyValue();
  169. }
  170. /**
  171. * Sets the top margin within the textbox.
  172. */
  173. public void setMarginTop(int marginTop) {
  174. setPropertyValue(new EscherSimpleProperty(EscherProperties.TEXT__TEXTTOP, marginTop));
  175. }
  176. /**
  177. * Gets the bottom margin within the textbox.
  178. */
  179. public int getMarginBottom() {
  180. EscherSimpleProperty property = _optRecord.lookup(EscherProperties.TEXT__TEXTBOTTOM);
  181. return property == null ? 0: property.getPropertyValue();
  182. }
  183. /**
  184. * Sets the bottom margin within the textbox.
  185. */
  186. public void setMarginBottom(int marginBottom) {
  187. setPropertyValue(new EscherSimpleProperty(EscherProperties.TEXT__TEXTBOTTOM, marginBottom));
  188. }
  189. /**
  190. * Gets the horizontal alignment.
  191. */
  192. public short getHorizontalAlignment() {
  193. return (short) _textObjectRecord.getHorizontalTextAlignment();
  194. }
  195. /**
  196. * Sets the horizontal alignment.
  197. */
  198. public void setHorizontalAlignment(short align) {
  199. _textObjectRecord.setHorizontalTextAlignment(align);
  200. }
  201. /**
  202. * Gets the vertical alignment.
  203. */
  204. public short getVerticalAlignment() {
  205. return (short) _textObjectRecord.getVerticalTextAlignment();
  206. }
  207. /**
  208. * Sets the vertical alignment.
  209. */
  210. public void setVerticalAlignment(short align) {
  211. _textObjectRecord.setVerticalTextAlignment(align);
  212. }
  213. public TextObjectRecord getTextObjectRecord() {
  214. return _textObjectRecord;
  215. }
  216. @Override
  217. public void setShapeType(int shapeType) {/**DO NOTHING**/}
  218. }