001package com.hfg.xml.msofficexml.docx.wordprocessingml;
002
003import java.awt.Font;
004
005import com.hfg.util.StringUtil;
006import com.hfg.xml.XMLTag;
007import com.hfg.xml.msofficexml.docx.Docx;
008import com.hfg.xml.msofficexml.docx.DocxException;
009import com.hfg.xml.msofficexml.docx.drawingml.DmlGraphicData;
010import com.hfg.xml.msofficexml.docx.wordprocessingml.field.WmlComplexField;
011import com.hfg.xml.msofficexml.docx.wordprocessingml.field.WmlComplexFieldCharType;
012import com.hfg.xml.msofficexml.part.MediaPart;
013
014//------------------------------------------------------------------------------
015/**
016 Represents an Office Open XML text run (<w:r>) tag.
017
018 @author J. Alex Taylor, hairyfatguy.com
019 */
020//------------------------------------------------------------------------------
021// com.hfg XML/HTML Coding Library
022//
023// This library is free software; you can redistribute it and/or
024// modify it under the terms of the GNU Lesser General Public
025// License as published by the Free Software Foundation; either
026// version 2.1 of the License, or (at your option) any later version.
027//
028// This library is distributed in the hope that it will be useful,
029// but WITHOUT ANY WARRANTY; without even the implied warranty of
030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
031// Lesser General Public License for more details.
032//
033// You should have received a copy of the GNU Lesser General Public
034// License along with this library; if not, write to the Free Software
035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
036//
037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
038// jataylor@hairyfatguy.com
039//------------------------------------------------------------------------------
040
041public class WmlTextRun extends WmlXMLTag
042{
043   private WmlTextRunProperties mProperties;
044
045   //###########################################################################
046   // CONSTRUCTORS
047   //###########################################################################
048
049   //---------------------------------------------------------------------------
050   public WmlTextRun(Docx inDocx)
051   {
052      super(WmlXML.R, inDocx);
053   }
054
055   //---------------------------------------------------------------------------
056   public WmlTextRun(Docx inDocx, String inContent)
057   {
058      this(inDocx);
059      addText(inContent);
060   }
061
062
063   //###########################################################################
064   // PUBLIC METHODS
065   //###########################################################################
066
067   //---------------------------------------------------------------------------
068   /**
069    * Returns whether or not this text run has a run properties subtag.
070    */
071   public boolean hasProperties()
072   {
073      return (getOptionalSubtagByName(WmlXML.RUN_PROPS) != null);
074   }
075
076   //---------------------------------------------------------------------------
077   public void setProperties(WmlTextRunProperties inValue)
078   {
079      mProperties = getOptionalSubtagByName(WmlXML.RUN_PROPS);
080      if (mProperties != null)
081      {
082         removeSubtag(mProperties);
083      }
084
085      mProperties = inValue;
086      if (inValue != null)
087      {
088         addSubtag(inValue);
089      }
090   }
091
092   //---------------------------------------------------------------------------
093   /**
094    * Returns the run properties tag if one exists or else instantiates a new one.
095    * @return the run properties for this table
096    */
097   public WmlTextRunProperties getProperties()
098   {
099      if (null == mProperties)
100      {
101         // Check if it has been added via addSubtag()...
102         mProperties = getOptionalSubtagByName(WmlXML.RUN_PROPS);
103         if (null == mProperties)
104         {
105            mProperties = new WmlTextRunProperties(getParentDoc());
106            addSubtag(0, mProperties);
107         }
108      }
109
110      return mProperties;
111   }
112
113   //---------------------------------------------------------------------------
114   public WmlTextRun addText(String inText)
115   {
116      if (inText != null)
117      {
118         // Any tabs present in the content need to be handled 
119         String[] pieces = inText.split("\t");
120
121         for (int i = 0; i < pieces.length; i++)
122         {
123            String piece = pieces[i];
124            if (piece.length() > 0)
125            {
126               addSubtag(new WmlText().setContent(piece));
127            }
128
129            if (i < pieces.length - 1)
130            {
131               addSubtag(new WmlTab(getParentDoc()));
132            }
133         }
134      }
135
136      return this;
137   }
138
139
140   //---------------------------------------------------------------------------
141   public void addSymbol(Font inFont, String inHexCode)
142   {
143      addSymbol(inFont.getName(), inHexCode);
144   }
145
146   //---------------------------------------------------------------------------
147   public void addSymbol(String inFontName, String inHexCode)
148   {
149      XMLTag symbolTag = new XMLTag(WmlXML.SYMBOL);
150      symbolTag.setAttribute(WmlXML.FONT_ATT, inFontName);
151      symbolTag.setAttribute(WmlXML.CHAR_ATT, inHexCode);
152      addSubtag(symbolTag);
153   }
154
155/* TODO
156   //---------------------------------------------------------------------------
157   public WmlTextRun addImage(OutputStream inStream)
158   {
159      addSubtag(new WmlText().setContent(inText));
160
161      return this;
162   }
163*/
164
165   //---------------------------------------------------------------------------
166   public void addMedia(MediaPart inMediaPart)
167   {
168      Docx docx = getParentDoc();
169      if (null == docx)
170      {
171         throw new DocxException("No reference to the parent doc available!?");
172      }
173
174      WmlInline inline = addDrawing().getInline();
175      inline.getDrawingObjNonVisualProperties().setName(inMediaPart.getFile().getName());
176      if (inMediaPart.getDimensions() != null)
177      {
178         inline.setWidth(inMediaPart.getDimensions().getWidth());
179         inline.setHeight(inMediaPart.getDimensions().getHeight());
180      }
181
182      //TODO: Temp
183      inline.getEffectExtent();
184      inline.getCommonDrawingMLNonVisualProperties().getGraphicFrameLocks().setDisallowAspectRatioChange(true);
185
186      DmlGraphicData graphicData = inline.getGraphic().getGraphicData();
187      graphicData.addMedia(inMediaPart);
188   }
189
190   //---------------------------------------------------------------------------
191   public WmlDrawing addDrawing()
192   {
193      WmlDrawing drawingTag = new WmlDrawing(getParentDoc());
194      addSubtag(drawingTag);
195
196      return drawingTag;
197   }
198
199   //---------------------------------------------------------------------------
200   public WmlTextRun br()
201   {
202      return br(1);
203   }
204
205   //---------------------------------------------------------------------------
206   public WmlTextRun br(int inCount)
207   {
208      for (int i = 0; i < inCount; i++)
209      {
210         addSubtag(new XMLTag(WmlXML.BR));
211      }
212
213      return this;
214   }
215
216   //---------------------------------------------------------------------------
217   public WmlTextRun addPageBreak()
218   {
219      XMLTag br = new XMLTag(WmlXML.BR);
220      br.setAttribute(WmlXML.TYPE_ATT, "page");
221      addSubtag(br);
222
223      return this;
224   }
225
226   //---------------------------------------------------------------------------
227   // The comment itself is placed in the CommentsPart and a reference is retained.
228   public void addComment(WmlParagraph inValue)
229   {
230      Docx docx = getParentDoc();
231      if (null == docx)
232      {
233         throw new DocxException("No reference to the parent doc available!?");
234      }
235
236      int commentId = docx.getCommentsPart().addComment(inValue);
237
238      XMLTag commentRefTag = new XMLTag(WmlXML.COMMENT_REF);
239      commentRefTag.setAttribute(WmlXML.ID_ATT, commentId);
240
241      addSubtag(commentRefTag);
242   }
243
244   //---------------------------------------------------------------------------
245   public WmlComplexField addComplexField(WmlComplexFieldCharType inType)
246   {
247      WmlComplexField field = new WmlComplexField(getParentDoc(), inType);
248      addSubtag(field);
249
250      return field;
251   }
252
253   //---------------------------------------------------------------------------
254   public void addInstructionText(CharSequence inValue)
255   {
256      XMLTag tag = new XMLTag(WmlXML.INSTRUCTION_TEXT);
257      tag.setAttribute(WmlXML.SPACE_ATT, "preserve");
258      tag.setContent(inValue);
259      addSubtag(tag);
260   }
261
262}