001package com.hfg.xml.msofficexml.docx.drawingml;
002
003
004import com.hfg.util.StringUtil;
005import com.hfg.xml.msofficexml.docx.Docx;
006import com.hfg.xml.msofficexml.docx.DocxException;
007import com.hfg.xml.msofficexml.docx.RelationshipXML;
008import com.hfg.xml.msofficexml.docx.drawingml.fill.DmlNoFill;
009import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXMLTag;
010import com.hfg.xml.msofficexml.part.MediaPart;
011import com.hfg.xml.msofficexml.part.OfficeXMLContentPart;
012
013//------------------------------------------------------------------------------
014/**
015 Represents graphic data (<graphicData:r>) tag in drawing markup language (DML) from Office Open XML.
016
017 @author J. Alex Taylor, hairyfatguy.com
018 */
019//------------------------------------------------------------------------------
020// com.hfg XML/HTML Coding Library
021//
022// This library is free software; you can redistribute it and/or
023// modify it under the terms of the GNU Lesser General Public
024// License as published by the Free Software Foundation; either
025// version 2.1 of the License, or (at your option) any later version.
026//
027// This library is distributed in the hope that it will be useful,
028// but WITHOUT ANY WARRANTY; without even the implied warranty of
029// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
030// Lesser General Public License for more details.
031//
032// You should have received a copy of the GNU Lesser General Public
033// License along with this library; if not, write to the Free Software
034// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
035//
036// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
037// jataylor@hairyfatguy.com
038//------------------------------------------------------------------------------
039
040public class DmlGraphicData extends WmlXMLTag
041{
042   private DmlPicture mPicture;
043
044   //---------------------------------------------------------------------------
045   public DmlGraphicData(Docx inDocx)
046   {
047      super(DmlXML.GRAPHIC_DATA, inDocx);
048   }
049
050
051   //---------------------------------------------------------------------------
052   public void addMedia(MediaPart inMediaPart)
053   {
054      Docx docx = getParentDoc();
055      if (null == docx)
056      {
057         throw new DocxException("No reference to the parent doc available!?");
058      }
059
060      // TODO: The URI should change based on the media type
061      setAttribute("uri", DmlXML.PIC_NAMESPACE.getURI());
062
063//      String id = docx.getMainDocPartRelationshipPart().addMedia(inMediaPart);
064      OfficeXMLContentPart parentPart = getParentOfficeXMLPart();
065      String id = parentPart.getRelationshipPart().addMedia(inMediaPart);
066
067      DmlPicture picture = getPic();
068
069      DmlNonVisualDrawingProperties drawingProperties = picture.getNonVisualPictureProperties().getNonVisualDrawingProperties();
070      drawingProperties.setName(inMediaPart.getFile().getName());
071
072      if (StringUtil.isSet(inMediaPart.getDescription()))
073      {
074         drawingProperties.setDescription(inMediaPart.getDescription());
075      }
076
077      if (StringUtil.isSet(inMediaPart.getTitle()))
078      {
079         drawingProperties.setDescription(inMediaPart.getTitle());
080      }
081
082      picture.getNonVisualPictureProperties().getNonVisualPictureDrawingProperties().getPictureLocks().setDisallowAspectRatioChange(true).setDisallowArrowheadChanges(true);
083
084      DmlBlip blip = picture.getBlipFill().getBlip();
085      blip.setAttribute(RelationshipXML.EMBED_ATT, id);
086      picture.getBlipFill().getStretch().fillRect();
087
088      if (inMediaPart.getDimensions() != null)
089      {
090         picture.getShapeProperties().getTransform().setWidth(inMediaPart.getDimensions().getWidth())
091               .setHeight(inMediaPart.getDimensions().getHeight());
092      }
093      picture.getShapeProperties().getPresetGeometry().setShape(DmlShapeType.rect);
094      picture.getShapeProperties().setFill(new DmlNoFill());
095      
096      // Put a line around the image
097      // picture.getShapeProperties().getLine().setWidth(new EMUs(9525)).setFill(new DmlNoFill()).setLineJoin(new DmlMiterLineJoin().setLimit(new EMUs(800000)));
098   }
099
100
101   //---------------------------------------------------------------------------
102   /**
103    * Returns the pic tag if one exists or else instantiates a new one.
104    * @return the pic tag for this graphic data
105    */
106   public DmlPicture getPic()
107   {
108      if (null == mPicture)
109      {
110         // Check if it has been added via addSubtag()...
111         mPicture = getOptionalSubtagByName(DmlXML.PIC);
112         if (null == mPicture)
113         {
114            mPicture = new DmlPicture(getParentDoc());
115            addSubtag(mPicture);
116         }
117      }
118
119      return mPicture;
120   }
121
122   //---------------------------------------------------------------------------
123   /**
124    * Creates a new shape object and adds it as a subtag of this object.
125    * @return the new shape object
126    */
127   public DmlShape addShape()
128   {
129      DmlShape shape = new DmlShape(getParentDoc());
130      addSubtag(shape);
131
132      return shape;
133   }
134
135}