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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.CommonObjectDataSubRecord;
  18. import org.apache.poi.hssf.record.EndSubRecord;
  19. import org.apache.poi.hssf.record.EscherAggregate;
  20. import org.apache.poi.hssf.record.ObjRecord;
  21. import org.apache.poi.hssf.usermodel.drawing.HSSFShapeType;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. /**
  25. * Represents a simple shape such as a line, rectangle or oval.
  26. *
  27. * @author Glen Stampoultzis (glens at apache.org)
  28. */
  29. public class HSSFSimpleShape
  30. extends HSSFShape
  31. {
  32. // The commented out ones haven't been tested yet or aren't supported
  33. // by HSSFSimpleShape.
  34. public final static short OBJECT_TYPE_LINE = 1;
  35. public final static short OBJECT_TYPE_RECTANGLE = 2;
  36. public final static short OBJECT_TYPE_OVAL = 3;
  37. // public final static short OBJECT_TYPE_ARC = 4;
  38. // public final static short OBJECT_TYPE_CHART = 5;
  39. // public final static short OBJECT_TYPE_TEXT = 6;
  40. // public final static short OBJECT_TYPE_BUTTON = 7;
  41. public final static short OBJECT_TYPE_PICTURE = 8;
  42. // public final static short OBJECT_TYPE_POLYGON = 9;
  43. // public final static short OBJECT_TYPE_CHECKBOX = 11;
  44. // public final static short OBJECT_TYPE_OPTION_BUTTON = 12;
  45. // public final static short OBJECT_TYPE_EDIT_BOX = 13;
  46. // public final static short OBJECT_TYPE_LABEL = 14;
  47. // public final static short OBJECT_TYPE_DIALOG_BOX = 15;
  48. // public final static short OBJECT_TYPE_SPINNER = 16;
  49. // public final static short OBJECT_TYPE_SCROLL_BAR = 17;
  50. // public final static short OBJECT_TYPE_LIST_BOX = 18;
  51. // public final static short OBJECT_TYPE_GROUP_BOX = 19;
  52. public final static short OBJECT_TYPE_COMBO_BOX = 20;
  53. public final static short OBJECT_TYPE_COMMENT = 25;
  54. // public final static short OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING = 30;
  55. int shapeType = OBJECT_TYPE_LINE;
  56. private static final Map <Short, Short> objTypeToShapeType = new HashMap<Short, Short>();
  57. static {
  58. objTypeToShapeType.put(OBJECT_TYPE_RECTANGLE, HSSFShapeType.RECTANGLE.getType());
  59. objTypeToShapeType.put(OBJECT_TYPE_PICTURE, HSSFShapeType.PICTURE.getType());
  60. objTypeToShapeType.put(OBJECT_TYPE_LINE, HSSFShapeType.LINE.getType());
  61. }
  62. public HSSFSimpleShape(EscherContainerRecord spContainer, ObjRecord objRecord) {
  63. super(spContainer, objRecord);
  64. }
  65. public HSSFSimpleShape( HSSFShape parent, HSSFAnchor anchor)
  66. {
  67. super( parent, anchor );
  68. _escherContainer = createSpContainer();
  69. _objRecord = createObjRecord();
  70. setShapeType(OBJECT_TYPE_LINE);
  71. }
  72. @Override
  73. protected EscherContainerRecord createSpContainer() {
  74. EscherContainerRecord spContainer = new EscherContainerRecord();
  75. spContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
  76. spContainer.setOptions( (short) 0x000F );
  77. EscherSpRecord sp = new EscherSpRecord();
  78. sp.setRecordId( EscherSpRecord.RECORD_ID );
  79. sp.setFlags( EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE );
  80. EscherClientDataRecord clientData = new EscherClientDataRecord();
  81. clientData.setRecordId( EscherClientDataRecord.RECORD_ID );
  82. clientData.setOptions( (short) 0x0000 );
  83. spContainer.addChildRecord(sp);
  84. spContainer.addChildRecord(_optRecord);
  85. spContainer.addChildRecord(anchor.getEscherAnchor());
  86. spContainer.addChildRecord(clientData);
  87. return spContainer;
  88. }
  89. @Override
  90. protected ObjRecord createObjRecord() {
  91. ObjRecord obj = new ObjRecord();
  92. CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
  93. c.setLocked(true);
  94. c.setPrintable(true);
  95. c.setAutofill(true);
  96. c.setAutoline(true);
  97. EndSubRecord e = new EndSubRecord();
  98. obj.addSubRecord(c);
  99. obj.addSubRecord(e);
  100. return obj;
  101. }
  102. @Override
  103. void afterInsert(HSSFPatriarch patriarch){
  104. EscherAggregate agg = patriarch._getBoundAggregate();
  105. agg.associateShapeToObjRecord(_escherContainer.getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
  106. }
  107. /**
  108. * Gets the shape type.
  109. * @return One of the OBJECT_TYPE_* constants.
  110. *
  111. * @see #OBJECT_TYPE_LINE
  112. * @see #OBJECT_TYPE_OVAL
  113. * @see #OBJECT_TYPE_RECTANGLE
  114. * @see #OBJECT_TYPE_PICTURE
  115. * @see #OBJECT_TYPE_COMMENT
  116. */
  117. public int getShapeType() {
  118. CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) _objRecord.getSubRecords().get(0);
  119. return cod.getObjectType();
  120. }
  121. /**
  122. * Sets the shape types.
  123. *
  124. * @param shapeType One of the OBJECT_TYPE_* constants.
  125. *
  126. * @see #OBJECT_TYPE_LINE
  127. * @see #OBJECT_TYPE_OVAL
  128. * @see #OBJECT_TYPE_RECTANGLE
  129. * @see #OBJECT_TYPE_PICTURE
  130. * @see #OBJECT_TYPE_COMMENT
  131. */
  132. public void setShapeType( int shapeType ){
  133. CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) _objRecord.getSubRecords().get(0);
  134. cod.setObjectType((short) shapeType);
  135. EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID);
  136. if (null == objTypeToShapeType.get((short)shapeType)){
  137. System.out.println("Unknown shape type: "+shapeType);
  138. return;
  139. }
  140. spRecord.setShapeType(objTypeToShapeType.get((short)shapeType));
  141. }
  142. }