001package com.hfg.xml.msofficexml.docx.wordprocessingml;
002
003
004import java.util.List;
005
006import com.hfg.xml.XMLTag;
007import com.hfg.xml.msofficexml.docx.Docx;
008import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlIndentation;
009import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlJustification;
010import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlParagraphBorder;
011import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlShading;
012import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlSpacing;
013
014//------------------------------------------------------------------------------
015/**
016 Represents an Office Open XML paragraph properties (<w:pPr>) tag.
017 <div>
018 From Ecma Office Open XML Part 1 - 17.3.1.26 pPr (Paragraph Properties):<br />
019 <span style='font-style:italic'>
020 "This element specifies a set of paragraph properties which shall be applied to the contents of the parent paragraph
021 after all style/numbering/table properties have been applied to the text. These properties are defined as direct
022 formatting, since they are directly applied to the paragraph and supersede any formatting from styles."
023 </span>
024 </div>
025 @author J. Alex Taylor, hairyfatguy.com
026 */
027//------------------------------------------------------------------------------
028// com.hfg XML/HTML Coding Library
029//
030// This library is free software; you can redistribute it and/or
031// modify it under the terms of the GNU Lesser General Public
032// License as published by the Free Software Foundation; either
033// version 2.1 of the License, or (at your option) any later version.
034//
035// This library is distributed in the hope that it will be useful,
036// but WITHOUT ANY WARRANTY; without even the implied warranty of
037// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
038// Lesser General Public License for more details.
039//
040// You should have received a copy of the GNU Lesser General Public
041// License along with this library; if not, write to the Free Software
042// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
043//
044// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
045// jataylor@hairyfatguy.com
046//------------------------------------------------------------------------------
047public class WmlParagraphProperties extends WmlXMLTag
048{
049   private XMLTag     mStyle;
050   private XMLTag     mJustificationTag;
051   private WmlIndentation mIndentationTag;
052   private WmlSpacing mSpacingTag;
053   private WmlShading mShadingTag;
054   private XMLTag     mKeepLinesTag;
055   private XMLTag     mKeepNextTag;
056   private XMLTag     mPageBreakBeforeTag;
057   private XMLTag     mContextualSpacingTag;
058   private XMLTag     mTabsTag;
059   private XMLTag     mOutlineLevelTag;
060   private WmlParagraphBorder mBorderTag;
061   private WmlTextRunProperties mRunProperties;
062   private WmlSectionProperties mSectionProperties;
063   private WmlNumberingProperties mNumberingProperties;
064
065   //##########################################################################
066   // CONSTRUCTORS
067   //##########################################################################
068
069   //---------------------------------------------------------------------------
070   public WmlParagraphProperties(Docx inDocx)
071   {
072      super(WmlXML.PARAGRAPH_PROPS, inDocx);
073   }
074
075   //##########################################################################
076   // PUBLIC METHODS
077   //##########################################################################
078
079   //---------------------------------------------------------------------------
080   public WmlParagraphProperties setStyle(String inStyleId)
081   {
082      if (null == mStyle)
083      {
084         mStyle = new XMLTag(WmlXML.PARAGRAPH_STYLE);
085         addSubtag(mStyle);
086      }
087
088      mStyle.setAttribute(WmlXML.VALUE_ATT, inStyleId);
089
090      return this;
091   }
092
093
094   //---------------------------------------------------------------------------
095   public WmlParagraphProperties setOutlineLevel(Integer inValue)
096   {
097      if (null == inValue)
098      {
099         mOutlineLevelTag = null;
100         removeSubtagsByName(WmlXML.OUTLINE_LEVEL);
101      }
102      else
103      {
104         if (null == mOutlineLevelTag)
105         {
106            // Check it it has been added via addSubtag()...
107            mOutlineLevelTag = getOptionalSubtagByName(WmlXML.OUTLINE_LEVEL);
108            if (null == mOutlineLevelTag)
109            {
110               mOutlineLevelTag = new XMLTag(WmlXML.OUTLINE_LEVEL);
111               addSubtag(mOutlineLevelTag);
112            }
113         }
114
115         mOutlineLevelTag.setAttribute(WmlXML.VALUE, inValue);
116      }
117
118      return this;
119   }
120
121
122   //---------------------------------------------------------------------------
123   public WmlParagraphBorder getBorders()
124   {
125      if (null == mBorderTag)
126      {
127         // Check it it has been added via addSubtag()...
128         mBorderTag = getOptionalSubtagByName(WmlXML.PARAGRAPH_BORDER);
129         if (null == mBorderTag)
130         {
131            mBorderTag = new WmlParagraphBorder(getParentDoc());
132            addSubtag(mBorderTag);
133         }
134      }
135
136      return mBorderTag;
137   }
138
139   //---------------------------------------------------------------------------
140   public WmlShading getShading()
141   {
142      if (null == mShadingTag)
143      {
144         // Check it it has been added via addSubtag()...
145         mShadingTag = getOptionalSubtagByName(WmlXML.SHADING);
146         if (null == mShadingTag)
147         {
148            mShadingTag = new WmlShading();
149            addSubtag(mShadingTag);
150         }
151      }
152
153      return mShadingTag;
154   }
155
156   //---------------------------------------------------------------------------
157   public WmlSpacing getSpacing()
158   {
159      if (null == mSpacingTag)
160      {
161         // Check it it has been added via addSubtag()...
162         mSpacingTag = getOptionalSubtagByName(WmlXML.SPACING);
163         if (null == mSpacingTag)
164         {
165            mSpacingTag = new WmlSpacing();
166            addSubtag(mSpacingTag);
167         }
168      }
169
170      return mSpacingTag;
171   }
172
173   //---------------------------------------------------------------------------
174   public WmlIndentation getIndentation()
175   {
176      if (null == mIndentationTag)
177      {
178         // Check it it has been added via addSubtag()...
179         mIndentationTag = getOptionalSubtagByName(WmlXML.INDENTATION);
180         if (null == mIndentationTag)
181         {
182            mIndentationTag = new WmlIndentation();
183            addSubtag(mIndentationTag);
184         }
185      }
186
187      return mIndentationTag;
188   }
189
190   //---------------------------------------------------------------------------
191   public WmlParagraphProperties setJustification(WmlJustification inValue)
192   {
193      if (null == mJustificationTag)
194      {
195         // Check it it has been added via addSubtag()...
196         mJustificationTag = getOptionalSubtagByName(WmlXML.JUSTIFICATION);
197         if (null == mJustificationTag)
198         {
199            mJustificationTag = new XMLTag(WmlXML.JUSTIFICATION);
200            addSubtag(mJustificationTag);
201         }
202      }
203
204      mJustificationTag.setAttribute(WmlXML.VALUE_ATT, inValue);
205
206      return this;
207   }
208
209   //---------------------------------------------------------------------------
210   /**
211    * Specifies that this paragraph should not be separated by a page break from the one that follows it.
212    */
213   public WmlParagraphProperties setKeepNext()
214   {
215      if (null == mKeepNextTag)
216      {
217         // Check it it has been added via addSubtag()...
218         mKeepNextTag = getOptionalSubtagByName(WmlXML.KEEP_NEXT);
219         if (null == mKeepNextTag)
220         {
221            mKeepNextTag = new XMLTag(WmlXML.KEEP_NEXT);
222            addSubtag(mKeepNextTag);
223         }
224      }
225
226      return this;
227   }
228
229   //---------------------------------------------------------------------------
230   /**
231    * Specifies that this paragraph's lines should be kept on one page.
232    */
233   public WmlParagraphProperties setKeepLines()
234   {
235      if (null == mKeepLinesTag)
236      {
237         // Check it it has been added via addSubtag()...
238         mKeepLinesTag = getOptionalSubtagByName(WmlXML.KEEP_LINES);
239         if (null == mKeepLinesTag)
240         {
241            mKeepLinesTag = new XMLTag(WmlXML.KEEP_LINES);
242            addSubtag(mKeepLinesTag);
243         }
244      }
245
246      return this;
247   }
248
249   //---------------------------------------------------------------------------
250   /**
251    * Specifies that this paragraph should use contextual spacing.
252    */
253   public WmlParagraphProperties setContextualSpacing(boolean inValue)
254   {
255      if (inValue)
256      {
257         if (null == mContextualSpacingTag)
258         {
259            // Check it it has been added via addSubtag()...
260            mContextualSpacingTag = getOptionalSubtagByName(WmlXML.CONTEXTUAL_SPACING);
261            if (null == mContextualSpacingTag)
262            {
263               mContextualSpacingTag = new XMLTag(WmlXML.CONTEXTUAL_SPACING);
264               addSubtag(mContextualSpacingTag);
265            }
266         }
267      }
268      else
269      {
270         removeSubtagsByName(WmlXML.CONTEXTUAL_SPACING);
271         mContextualSpacingTag = null;
272      }
273
274      return this;
275   }
276
277   //---------------------------------------------------------------------------
278   /**
279    * Specifies whether or not this paragraph should be preceded by a page break.
280    */
281   public WmlParagraphProperties setPageBreakBefore(boolean inValue)
282   {
283      if (null == mPageBreakBeforeTag)
284      {
285         // Check it it has been added via addSubtag()...
286         mPageBreakBeforeTag = getOptionalSubtagByName(WmlXML.PAGE_BREAK_BEFORE);
287         if (null == mPageBreakBeforeTag)
288         {
289            mPageBreakBeforeTag = new XMLTag(WmlXML.PAGE_BREAK_BEFORE);
290            addSubtag(mPageBreakBeforeTag);
291         }
292      }
293
294      mPageBreakBeforeTag.setAttribute(WmlXML.VALUE_ATT, inValue ? "true" : "false");
295
296      return this;
297   }
298
299
300   //---------------------------------------------------------------------------
301   /**
302    * Returns the section properties tag if one exists or else instantiates a new one.
303    * @return the section properties for this table
304    */
305   public WmlSectionProperties getSectionProperties()
306   {
307      if (null == mSectionProperties)
308      {
309         // Check it it has been added via addSubtag()...
310         mSectionProperties = getOptionalSubtagByName(WmlXML.SECT_PROPS);
311         if (null == mSectionProperties)
312         {
313            mSectionProperties = new WmlSectionProperties(getParentDoc());
314            addSubtag(0, mSectionProperties);
315         }
316      }
317
318      return mSectionProperties;
319   }
320
321   //---------------------------------------------------------------------------
322   /**
323    * Returns the numbering properties tag if one exists or else instantiates a new one.
324    * @return the numbering properties for this table
325    */
326   public WmlNumberingProperties getNumberingProperties()
327   {
328      if (null == mNumberingProperties)
329      {
330         // Check it it has been added via addSubtag()...
331         mNumberingProperties = getOptionalSubtagByName(WmlXML.NUMBERING_PROPERTIES);
332         if (null == mNumberingProperties)
333         {
334            mNumberingProperties = new WmlNumberingProperties(getParentDoc());
335            addSubtag(mNumberingProperties);
336         }
337      }
338
339      return mNumberingProperties;
340   }
341
342   //---------------------------------------------------------------------------
343   /**
344    * Returns the run properties tag if one exists or else instantiates a new one.
345    * @return the run properties for this table
346    */
347   public WmlTextRunProperties getRunProperties()
348   {
349      if (null == mRunProperties)
350      {
351         // Check it it has been added via addSubtag()...
352         mRunProperties = getOptionalSubtagByName(WmlXML.RUN_PROPS);
353         if (null == mRunProperties)
354         {
355            mRunProperties = new WmlTextRunProperties(getParentDoc());
356            addSubtag(mRunProperties);
357         }
358      }
359
360      return mRunProperties;
361   }
362
363
364   //---------------------------------------------------------------------------
365   /**
366    Adds a tab stop definition.
367    */
368   public WmlParagraphProperties addTab(WmlTab inValue)
369   {
370      if (null == mTabsTag)
371      {
372         // Check it it has been added via addSubtag()...
373         mTabsTag = getOptionalSubtagByName(WmlXML.TABS);
374         if (null == mTabsTag)
375         {
376            mTabsTag = new XMLTag(WmlXML.TABS);
377            addSubtag(mTabsTag);
378         }
379      }
380
381      mTabsTag.addSubtag(inValue);
382
383      return this;
384   }
385
386
387   //---------------------------------------------------------------------------
388   /**
389    Adds a tab stop definition.
390    */
391   public List<WmlTab> getTabs()
392   {
393      List<WmlTab> tabTags = null;
394
395      if (null == mTabsTag)
396      {
397         // Check it it has been added via addSubtag()...
398         mTabsTag = getOptionalSubtagByName(WmlXML.TABS);
399      }
400
401      if (mTabsTag != null)
402      {
403         tabTags = mTabsTag.getSubtagsByName(WmlXML.TAB);
404      }
405
406      return tabTags;
407   }
408
409}