001package com.hfg.xml.msofficexml.xlsx.spreadsheetDrawing; 002 003 004 005import java.awt.*; 006 007import com.hfg.xml.msofficexml.docx.drawingml.DmlShapeType; 008import com.hfg.xml.msofficexml.docx.drawingml.DmlTransform; 009import com.hfg.xml.msofficexml.docx.drawingml.DmlXML; 010import com.hfg.xml.msofficexml.docx.drawingml.color.DmlSRGBColor; 011import com.hfg.xml.msofficexml.docx.drawingml.text.DmlTextAnchoringType; 012import com.hfg.xml.msofficexml.xlsx.Xlsx; 013import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXMLTag; 014 015//------------------------------------------------------------------------------ 016/** 017 Represents an Office Open XML worksheet shape (<xdr:sp>) tag. 018 <div> 019 @author J. Alex Taylor, hairyfatguy.com 020 </div> 021 */ 022//------------------------------------------------------------------------------ 023// com.hfg XML/HTML Coding Library 024// 025// This library is free software; you can redistribute it and/or 026// modify it under the terms of the GNU Lesser General Public 027// License as published by the Free Software Foundation; either 028// version 2.1 of the License, or (at your option) any later version. 029// 030// This library is distributed in the hope that it will be useful, 031// but WITHOUT ANY WARRANTY; without even the implied warranty of 032// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 033// Lesser General Public License for more details. 034// 035// You should have received a copy of the GNU Lesser General Public 036// License along with this library; if not, write to the Free Software 037// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 038// 039// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 040// jataylor@hairyfatguy.com 041//------------------------------------------------------------------------------ 042 043public class SsDrawShape extends SsmlXMLTag 044{ 045 private SsDrawNonVisualShapeDrawingProperties mNonVisualShapeDrawingProperties; 046 private SsDrawShapeProperties mShapeProperties; 047 private SsDrawShapeStyle mStyle; 048 private SsDrawTextBody mTextBody; 049 private DmlTransform mTransform; 050 051 //--------------------------------------------------------------------------- 052 public SsDrawShape(Xlsx inXlsx) 053 { 054 super(SsDrawXML.SHAPE, inXlsx); 055 init(); 056 } 057 058 //--------------------------------------------------------------------------- 059 public SsDrawShape(Xlsx inXlsx, DmlShapeType inShapeType) 060 { 061 this(inXlsx); 062 getShapeProperties().getOrCreatePresetGeometry().setShape(inShapeType); 063 } 064 065 //--------------------------------------------------------------------------- 066 private void init() 067 { 068 // Initialize required subtags 069 getNonVisualShapeDrawingProperties(); 070 getShapeProperties(); 071 } 072 073 //--------------------------------------------------------------------------- 074 /** 075 A shortcut for setting the name of a shape. 076 @param inValue the name to be applied to the shape 077 @return this shape object to facilitate method chaining 078 */ 079 public SsDrawShape setName(String inValue) 080 { 081 getNonVisualShapeDrawingProperties().getNonVisualDrawingProperties().setName(inValue); 082 return this; 083 } 084 085 //--------------------------------------------------------------------------- 086 /** 087 * Returns the non-visual shape drawing properties (<xdr:nvSpPr>) tag if one exists or else instantiates a new one. 088 * @return the non-visual shape drawing properties (<xdr:nvSpPr>) for this shape tag 089 */ 090 public SsDrawNonVisualShapeDrawingProperties getNonVisualShapeDrawingProperties() 091 { 092 if (null == mNonVisualShapeDrawingProperties) 093 { 094 // Check if it has been added via addSubtag()... 095 mNonVisualShapeDrawingProperties = getOptionalSubtagByName(SsDrawXML.NON_VISUAL_SHAPE_DRAWING_PROPS); 096 if (null == mNonVisualShapeDrawingProperties) 097 { 098 mNonVisualShapeDrawingProperties = new SsDrawNonVisualShapeDrawingProperties(getParentDoc()); 099 addSubtag(mNonVisualShapeDrawingProperties); 100 } 101 } 102 103 return mNonVisualShapeDrawingProperties; 104 } 105 106 //--------------------------------------------------------------------------- 107 /** 108 * Returns the shape properties (<xdr:spPr>) tag if one exists or else instantiates a new one. 109 * @return the shape properties (<xdr:spPr>) for this shape tag 110 */ 111 public SsDrawShapeProperties getShapeProperties() 112 { 113 if (null == mShapeProperties) 114 { 115 // Check if it has been added via addSubtag()... 116 mShapeProperties = getOptionalSubtagByName(SsDrawXML.SHAPE_PROPS); 117 if (null == mShapeProperties) 118 { 119 mShapeProperties = new SsDrawShapeProperties(getParentDoc()); 120 addSubtag(mShapeProperties); 121 } 122 } 123 124 return mShapeProperties; 125 } 126 127 //--------------------------------------------------------------------------- 128 /** 129 * Returns the style (<xdr:style>) tag if one exists or else instantiates a new one. 130 * @return the style properties (<xdr:style>) for this shape tag 131 */ 132 public SsDrawShapeStyle getOrCreateStyle() 133 { 134 if (null == mStyle) 135 { 136 // Check if it has been added via addSubtag()... 137 mStyle = getOptionalSubtagByName(SsDrawXML.STYLE); 138 if (null == mStyle) 139 { 140 mStyle = new SsDrawShapeStyle(getParentDoc()); 141 addSubtag(mStyle); 142 } 143 } 144 145 return mStyle; 146 } 147 148 //--------------------------------------------------------------------------- 149 /** 150 * Returns the transform (<a:xfrm>) tag if one exists or else instantiates a new one. 151 * @return the transform (<a:xfrm>) for this shape tag 152 */ 153 public DmlTransform getOrCreateTransform() 154 { 155 if (null == mTransform) 156 { 157 // Check if it has been added via addSubtag()... 158 mTransform = getOptionalSubtagByName(DmlXML.TRANSFORM_2D); 159 if (null == mTransform) 160 { 161 mTransform = new DmlTransform(); 162 addSubtag(mTransform); 163 } 164 } 165 166 return mTransform; 167 } 168 169 //--------------------------------------------------------------------------- 170 /** 171 * Returns the text body (<xdr:txBody>) tag if one exists or else instantiates a new one. 172 * @return the text body (<xdr:txBody>) for this shape tag 173 */ 174 public SsDrawTextBody getOrCreateTextBody() 175 { 176 if (null == mTextBody) 177 { 178 // Check if it has been added via addSubtag()... 179 mTextBody = getOptionalSubtagByName(SsDrawXML.TEXT_BODY); 180 if (null == mTextBody) 181 { 182 mTextBody = new SsDrawTextBody(getParentDoc()); 183 addSubtag(mTextBody); 184 } 185 } 186 187 return mTextBody; 188 } 189 190 //--------------------------------------------------------------------------- 191 public void setText(String inText) 192 { 193 SsDrawTextBody textBody = getOrCreateTextBody(); 194 textBody.getProperties().setAnchor(DmlTextAnchoringType.ctr); // Default to centered text 195 textBody.addParagraph(inText); 196 } 197 198 199}