001package com.hfg.xml.msofficexml.docx.wordprocessingml;
002
003
004import com.hfg.graphics.units.GfxSize;
005import com.hfg.graphics.units.GfxUnits;
006import com.hfg.xml.XMLTag;
007import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlShading;
008import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlTableBorders;
009import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlTableCellMargins;
010
011public class WmlTableProperties extends XMLTag
012{
013   private XMLTag              mStyle;
014   private XMLTag              mWidthTag;
015   private XMLTag              mTableCellSpacingTag;
016   private WmlTableBorders     mBorders;
017   private WmlTableCellMargins mTableCellMargins;
018   private XMLTag              mTableIndentTag;
019   private XMLTag              mTableLayoutTag;
020   private XMLTag              mRowBandSizeTag;
021   private WmlShading          mShadingTag;
022
023   private enum Type {
024      auto,
025      dxa,   // twentieths of a point
026      pct
027   }
028
029   private enum Layout {
030      autofit,
031      fixed
032   }
033
034   //---------------------------------------------------------------------------
035   public WmlTableProperties()
036   {
037      super(WmlXML.TABLE_PROPS);
038
039      setRowBandSize(1);
040   }
041
042   //---------------------------------------------------------------------------
043   public WmlTableProperties useFixedTableLayout()
044   {
045      setTableLayout(Layout.fixed);
046      return this;
047   }
048
049   //---------------------------------------------------------------------------
050   public WmlTableProperties useAutoTableLayout()
051   {
052      setTableLayout(Layout.autofit);
053      return this;
054   }
055
056   //---------------------------------------------------------------------------
057   private void setTableLayout(Layout inLayout)
058   {
059      if (null == mTableLayoutTag)
060      {
061         mTableLayoutTag = new XMLTag(WmlXML.TABLE_LAYOUT);
062         addSubtag(mTableLayoutTag);
063      }
064
065      mTableLayoutTag.setAttribute(WmlXML.TYPE_ATT, inLayout);
066   }
067
068   //---------------------------------------------------------------------------
069   public WmlShading getShading()
070   {
071      if (null == mShadingTag)
072      {
073         // Check it it has been added via addSubtag()...
074         mShadingTag = (WmlShading) getOptionalSubtagByName(WmlXML.SHADING);
075         if (null == mShadingTag)
076         {
077            mShadingTag = new WmlShading();
078            addSubtag(mShadingTag);
079         }
080      }
081
082      return mShadingTag;
083   }
084
085   //---------------------------------------------------------------------------
086   public WmlTableProperties setRowBandSize(int inValue)
087   {
088      if (null == mRowBandSizeTag)
089      {
090         // Check it it has been added via addSubtag()...
091         mRowBandSizeTag = getOptionalSubtagByName(WmlXML.TABLE_STYLE_ROW_BAND_SIZE);
092         if (null == mRowBandSizeTag)
093         {
094            mRowBandSizeTag = new XMLTag(WmlXML.TABLE_STYLE_ROW_BAND_SIZE);
095            addSubtag(mRowBandSizeTag);
096         }
097      }
098
099      mRowBandSizeTag.setAttribute(WmlXML.VALUE_ATT, inValue);
100
101      return this;
102   }
103
104   //---------------------------------------------------------------------------
105   /**
106    * Specifies the table indent from the leading margin.
107    */
108   public WmlTableProperties setTableIndent(GfxSize inValue)
109   {
110      if (null == mTableIndentTag)
111      {
112         mTableIndentTag = new XMLTag(WmlXML.TABLE_INDENT);
113         addSubtag(mTableIndentTag);
114      }
115
116      mTableIndentTag.setAttribute(WmlXML.WIDTH_ATT, inValue.toInt(GfxUnits.dxa));
117      mTableIndentTag.setAttribute(WmlXML.TYPE_ATT, Type.dxa.name());
118
119      return this;
120   }
121
122   //---------------------------------------------------------------------------
123   public WmlTableProperties setStyle(String inStyleId)
124   {
125      if (null == mStyle)
126      {
127         mStyle = new XMLTag(WmlXML.TABLE_STYLE);
128         addSubtag(0, mStyle);
129      }
130
131      mStyle.setAttribute(WmlXML.VALUE_ATT, inStyleId);
132
133      return this;
134   }
135
136   //---------------------------------------------------------------------------
137   public WmlTableBorders getBorders()
138   {
139      if (null == mBorders)
140      {
141         mBorders = new WmlTableBorders();
142         addSubtag(mBorders);
143      }
144
145      return mBorders;
146   }
147
148
149   //---------------------------------------------------------------------------
150   /**
151    * Specifies the spacing between adjacent cells and the edges of the table.
152    * @param inValue
153    * @return this table properties object to enable method chaining
154    */
155   public WmlTableProperties setTableCellSpacing(GfxSize inValue)
156   {
157      if (null == mTableCellSpacingTag)
158      {
159         mTableCellSpacingTag = new XMLTag(WmlXML.TABLE_CELL_SPACING);
160         addSubtag(mTableCellSpacingTag);
161      }
162
163      mTableCellSpacingTag.setAttribute(WmlXML.WIDTH_ATT, inValue.toInt(GfxUnits.dxa));
164      mTableCellSpacingTag.setAttribute(WmlXML.TYPE_ATT, Type.dxa.name());
165
166      return this;
167   }
168
169
170   //---------------------------------------------------------------------------
171   /**
172    * The width of a table.
173    * @param inValue table width expressed in some GfxSize units.
174    * @return this table properties object to enable method chaining
175    */
176   public WmlTableProperties setWidth(GfxSize inValue)
177   {
178      if (null == mWidthTag)
179      {
180         // Check it it has been added via addSubtag()...
181         mWidthTag = getOptionalSubtagByName(WmlXML.TABLE_WIDTH);
182         if (null == mWidthTag)
183         {
184            mWidthTag = new XMLTag(WmlXML.TABLE_WIDTH);
185            addSubtag(mWidthTag);
186         }
187      }
188
189      mWidthTag.setAttribute(WmlXML.WIDTH_ATT, inValue.to(GfxUnits.dxa));
190      mWidthTag.setAttribute(WmlXML.TYPE_ATT, Type.dxa.name());
191
192      return this;
193   }
194
195
196   //---------------------------------------------------------------------------
197   /**
198    * The width of a table is expressed in fiftieth of a percent but to make things
199    * simpler this method will take a regular percentage and perform the conversion for you.
200    * @param inValue the width of the table expressed as a percent
201    * @return this table properties object to enable method chaining
202    */
203   public WmlTableProperties setWidthPct(int inValue)
204   {
205      if (null == mWidthTag)
206      {
207         // Check it it has been added via addSubtag()...
208         mWidthTag = getOptionalSubtagByName(WmlXML.TABLE_WIDTH);
209         if (null == mWidthTag)
210         {
211            mWidthTag = new XMLTag(WmlXML.TABLE_WIDTH);
212            addSubtag(mWidthTag);
213         }
214      }
215
216      mWidthTag.setAttribute(WmlXML.WIDTH_ATT, inValue * 50);
217      mWidthTag.setAttribute(WmlXML.TYPE_ATT, Type.pct.name());
218
219      return this;
220   }
221
222
223   //---------------------------------------------------------------------------
224   public WmlTableCellMargins getTableCellMargins()
225   {
226      if (null == mTableCellMargins)
227      {
228         // Check it it has been added via addSubtag()...
229         mTableCellMargins = getOptionalSubtagByName(WmlXML.TABLE_CELL_MARGIN_DEFAULTS);
230         if (null == mTableCellMargins)
231         {
232            mTableCellMargins = new WmlTableCellMargins(this);
233            addSubtag(mTableCellMargins);
234         }
235      }
236
237      return mTableCellMargins;
238   }
239}