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.Docx;
008import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlParagraphStyle;
009import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlShading;
010import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlTableCellBorders;
011import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlTableCellMargins;
012import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlTextDirection;
013import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlVerticalJustification;
014
015public class WmlTableCellProperties extends WmlXMLTag
016{
017   private XMLTag                    mStyle;
018   private XMLTag                    mWidthTag;
019   private XMLTag                    mGridSpanTag;
020   private XMLTag                    mHideMarkTag;
021   private XMLTag                    mTextDirectionTag;
022   private XMLTag                    mVAlignTag;
023   private XMLTag                    mVMergeTag;
024   private WmlShading mShadingTag;
025   private WmlTableCellBorders mBorders;
026   private WmlTableCellMargins mTableCellMargins;
027
028   private enum Type {
029      auto,
030      dxa,   // twentieths of a point
031      pct
032   }
033
034   //---------------------------------------------------------------------------
035   public WmlTableCellProperties(Docx inDocx)
036   {
037      super(WmlXML.TABLE_CELL_PROPS, inDocx);
038   }
039
040   //---------------------------------------------------------------------------
041   public WmlTableCellProperties add(WmlTableCellProperties inProperties)
042   {
043      if (inProperties.mStyle != null)
044      {
045         mStyle = inProperties.mStyle;
046      }
047
048      if (inProperties.mWidthTag != null)
049      {
050         mWidthTag = inProperties.mWidthTag;
051      }
052
053      if (inProperties.mGridSpanTag != null)
054      {
055         mGridSpanTag = inProperties.mGridSpanTag;
056      }
057
058      if (inProperties.mHideMarkTag != null)
059      {
060         mHideMarkTag = inProperties.mHideMarkTag;
061      }
062
063      if (inProperties.mTextDirectionTag != null)
064      {
065         mTextDirectionTag = inProperties.mTextDirectionTag;
066      }
067
068      if (inProperties.mVAlignTag != null)
069      {
070         mVAlignTag = inProperties.mVAlignTag;
071      }
072
073      if (inProperties.mVMergeTag != null)
074      {
075         mVMergeTag = inProperties.mVMergeTag;
076      }
077
078      if (inProperties.mShadingTag != null)
079      {
080         mShadingTag = inProperties.mShadingTag;
081      }
082
083      if (inProperties.mBorders != null)
084      {
085         mBorders = inProperties.mBorders;
086      }
087
088      if (inProperties.mTableCellMargins != null)
089      {
090         mTableCellMargins = inProperties.mTableCellMargins;
091      }
092
093      return this;
094   }
095
096   //---------------------------------------------------------------------------
097   public WmlTableCellProperties setStyle(String inStyleId)
098   {
099      if (null == mStyle)
100      {
101         mStyle = new WmlParagraphStyle(getParentDoc());
102         addSubtag(mStyle);
103      }
104
105      mStyle.setAttribute(WmlXML.STYLE_ID_ATT, inStyleId);
106
107      return this;
108   }
109
110   //---------------------------------------------------------------------------
111   public WmlTableCellProperties setWidth(GfxSize inValue)
112   {
113      if (null == mWidthTag)
114      {
115         // Check if it has been added via addSubtag()...
116         mWidthTag = getOptionalSubtagByName(WmlXML.TABLE_CELL_WIDTH);
117         if (null == mWidthTag)
118         {
119            mWidthTag = new XMLTag(WmlXML.TABLE_CELL_WIDTH);
120            addSubtag(mWidthTag);
121         }
122      }
123
124      mWidthTag.setAttribute(WmlXML.TYPE_ATT, "dxa");
125      mWidthTag.setAttribute(WmlXML.VALUE_ATT, inValue.toInt(GfxUnits.dxa));
126
127      return this;
128   }
129
130   //---------------------------------------------------------------------------
131   // From http://officeopenxml.com/WPtableCellProperties-Width.php :
132   // "Note: The 2006 version of the OOXML standard specified that the value was to be a decimal.
133   // When type="pct", the value was interpreted as fifths of a percent, so 4975=99.5%, and no % symbol
134   // was included in the attribute. In the 2011 version the value can be either a decimal or a percent,
135   // so a % symbol should be included when type="pct"."
136   public WmlTableCellProperties setWidthPct(float inValue)
137   {
138      if (null == mWidthTag)
139      {
140         // Check it it has been added via addSubtag()...
141         mWidthTag = getOptionalSubtagByName(WmlXML.TABLE_CELL_WIDTH);
142         if (null == mWidthTag)
143         {
144            mWidthTag = new XMLTag(WmlXML.TABLE_CELL_WIDTH);
145            addSubtag(mWidthTag);
146         }
147      }
148
149      // This was causing an error for older versions of Word that prevented the file from opening
150//      mWidthTag.setAttribute(WmlXML.WIDTH_ATT, inValue + "%");
151      mWidthTag.setAttribute(WmlXML.WIDTH_ATT, (int) inValue * 50);
152
153      mWidthTag.setAttribute(WmlXML.TYPE_ATT, Type.pct.name());
154
155      return this;
156   }
157
158   //---------------------------------------------------------------------------
159   public WmlTableCellProperties setGridSpan(Integer inValue)
160   {
161      if (null == mGridSpanTag)
162      {
163         // Check if it has been added via addSubtag()...
164         mGridSpanTag = getOptionalSubtagByName(WmlXML.GRID_SPAN);
165         if (null == mGridSpanTag)
166         {
167            mGridSpanTag = new XMLTag(WmlXML.GRID_SPAN);
168            addSubtag(mGridSpanTag);
169         }
170      }
171
172      mGridSpanTag.setAttribute(WmlXML.VALUE_ATT, inValue);
173
174      return this;
175   }
176
177   //---------------------------------------------------------------------------
178   /**
179    Ignore End Of Cell Marker In Row Height Calculation.
180    */
181   public WmlTableCellProperties setHideMark(boolean inValue)
182   {
183      if (null == mHideMarkTag)
184      {
185         // Check if it has been added via addSubtag()...
186         mHideMarkTag = getOptionalSubtagByName(WmlXML.HIDE_MARK);
187         if (null == mHideMarkTag)
188         {
189            mHideMarkTag = new XMLTag(WmlXML.HIDE_MARK);
190            addSubtag(mHideMarkTag);
191         }
192      }
193
194      mHideMarkTag.setAttribute(WmlXML.VALUE_ATT, inValue);
195
196      return this;
197   }
198
199   //---------------------------------------------------------------------------
200   public WmlTableCellProperties setTextDirection(WmlTextDirection inValue)
201   {
202      if (null == mTextDirectionTag)
203      {
204         // Check if it has been added via addSubtag()...
205         mTextDirectionTag = getOptionalSubtagByName(WmlXML.TEXT_DIRECTION);
206         if (inValue != null
207             && null == mTextDirectionTag)
208         {
209            mTextDirectionTag = new XMLTag(WmlXML.TEXT_DIRECTION);
210            addSubtag(mTextDirectionTag);
211         }
212         else if (null == inValue
213                  && mTextDirectionTag != null)
214         {
215            removeSubtag(mTextDirectionTag);
216            mTextDirectionTag = null;
217         }
218      }
219
220      if (inValue != null)
221      {
222         mTextDirectionTag.setAttribute(WmlXML.VALUE_ATT, inValue.name());
223      }
224
225      return this;
226   }
227
228   //---------------------------------------------------------------------------
229   public WmlTableCellProperties setVerticalJustification(WmlVerticalJustification inValue)
230   {
231      if (null == mVAlignTag)
232      {
233         // Check if it has been added via addSubtag()...
234         mVAlignTag = getOptionalSubtagByName(WmlXML.VALIGN);
235         if (null == mVAlignTag)
236         {
237            mVAlignTag = new XMLTag(WmlXML.VALIGN);
238            addSubtag(mVAlignTag);
239         }
240      }
241
242      mVAlignTag.setAttribute(WmlXML.VALUE_ATT, inValue);
243
244      return this;
245   }
246
247   //---------------------------------------------------------------------------
248   public WmlTableCellProperties setVerticalMerge(WmlVerticalMerge inValue)
249   {
250      if (null == mVMergeTag)
251      {
252         // Check if it has been added via addSubtag()...
253         mVMergeTag = getOptionalSubtagByName(WmlXML.VMERGE);
254         if (null == mVMergeTag)
255         {
256            mVMergeTag = new XMLTag(WmlXML.VMERGE);
257            addSubtag(mVMergeTag);
258         }
259      }
260
261      mVMergeTag.setAttribute(WmlXML.VALUE_ATT, inValue);
262
263      return this;
264   }
265
266   //---------------------------------------------------------------------------
267   public WmlShading getShading()
268   {
269      if (null == mShadingTag)
270      {
271         // Check it it has been added via addSubtag()...
272         mShadingTag = getOptionalSubtagByName(WmlXML.SHADING);
273         if (null == mShadingTag)
274         {
275            mShadingTag = new WmlShading();
276            addSubtag(mShadingTag);
277         }
278      }
279
280      return mShadingTag;
281   }
282
283   //---------------------------------------------------------------------------
284   public WmlTableCellBorders getBorders()
285   {
286      if (null == mBorders)
287      {
288         mBorders = new WmlTableCellBorders();
289         addSubtag(mBorders);
290      }
291
292      return mBorders;
293   }
294
295
296   //---------------------------------------------------------------------------
297   public WmlTableCellMargins getTableCellMargins()
298   {
299      if (null == mTableCellMargins)
300      {
301         // Check it it has been added via addSubtag()...
302         mTableCellMargins = getOptionalSubtagByName(WmlXML.TABLE_CELL_MARGIN);
303         if (null == mTableCellMargins)
304         {
305            mTableCellMargins = new WmlTableCellMargins(this);
306            addSubtag(mTableCellMargins);
307         }
308      }
309
310      return mTableCellMargins;
311   }
312}
313