All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.msofficexml.docx.wordprocessingml.WmlTableProperties Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.msofficexml.docx.wordprocessingml;


import com.hfg.graphics.units.GfxSize;
import com.hfg.graphics.units.GfxUnits;
import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlShading;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlTableBorders;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlTableCellMargins;

public class WmlTableProperties extends XMLTag
{
   private XMLTag              mStyle;
   private XMLTag              mWidthTag;
   private XMLTag              mTableCellSpacingTag;
   private WmlTableBorders     mBorders;
   private WmlTableCellMargins mTableCellMargins;
   private XMLTag              mTableIndentTag;
   private XMLTag              mTableLayoutTag;
   private XMLTag              mRowBandSizeTag;
   private WmlShading          mShadingTag;

   private enum Type {
      auto,
      dxa,   // twentieths of a point
      pct
   }

   private enum Layout {
      autofit,
      fixed
   }

   //---------------------------------------------------------------------------
   public WmlTableProperties()
   {
      super(WmlXML.TABLE_PROPS);

      setRowBandSize(1);
   }

   //---------------------------------------------------------------------------
   public WmlTableProperties useFixedTableLayout()
   {
      setTableLayout(Layout.fixed);
      return this;
   }

   //---------------------------------------------------------------------------
   public WmlTableProperties useAutoTableLayout()
   {
      setTableLayout(Layout.autofit);
      return this;
   }

   //---------------------------------------------------------------------------
   private void setTableLayout(Layout inLayout)
   {
      if (null == mTableLayoutTag)
      {
         mTableLayoutTag = new XMLTag(WmlXML.TABLE_LAYOUT);
         addSubtag(mTableLayoutTag);
      }

      mTableLayoutTag.setAttribute(WmlXML.TYPE_ATT, inLayout);
   }

   //---------------------------------------------------------------------------
   public WmlShading getShading()
   {
      if (null == mShadingTag)
      {
         // Check it it has been added via addSubtag()...
         mShadingTag = (WmlShading) getOptionalSubtagByName(WmlXML.SHADING);
         if (null == mShadingTag)
         {
            mShadingTag = new WmlShading();
            addSubtag(mShadingTag);
         }
      }

      return mShadingTag;
   }

   //---------------------------------------------------------------------------
   public WmlTableProperties setRowBandSize(int inValue)
   {
      if (null == mRowBandSizeTag)
      {
         // Check it it has been added via addSubtag()...
         mRowBandSizeTag = getOptionalSubtagByName(WmlXML.TABLE_STYLE_ROW_BAND_SIZE);
         if (null == mRowBandSizeTag)
         {
            mRowBandSizeTag = new XMLTag(WmlXML.TABLE_STYLE_ROW_BAND_SIZE);
            addSubtag(mRowBandSizeTag);
         }
      }

      mRowBandSizeTag.setAttribute(WmlXML.VALUE_ATT, inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   /**
    * Specifies the table indent from the leading margin.
    */
   public WmlTableProperties setTableIndent(GfxSize inValue)
   {
      if (null == mTableIndentTag)
      {
         mTableIndentTag = new XMLTag(WmlXML.TABLE_INDENT);
         addSubtag(mTableIndentTag);
      }

      mTableIndentTag.setAttribute(WmlXML.WIDTH_ATT, inValue.toInt(GfxUnits.dxa));
      mTableIndentTag.setAttribute(WmlXML.TYPE_ATT, Type.dxa.name());

      return this;
   }

   //---------------------------------------------------------------------------
   public WmlTableProperties setStyle(String inStyleId)
   {
      if (null == mStyle)
      {
         mStyle = new XMLTag(WmlXML.TABLE_STYLE);
         addSubtag(0, mStyle);
      }

      mStyle.setAttribute(WmlXML.VALUE_ATT, inStyleId);

      return this;
   }

   //---------------------------------------------------------------------------
   public WmlTableBorders getBorders()
   {
      if (null == mBorders)
      {
         mBorders = new WmlTableBorders();
         addSubtag(mBorders);
      }

      return mBorders;
   }


   //---------------------------------------------------------------------------
   /**
    * Specifies the spacing between adjacent cells and the edges of the table.
    * @param inValue
    * @return this table properties object to enable method chaining
    */
   public WmlTableProperties setTableCellSpacing(GfxSize inValue)
   {
      if (null == mTableCellSpacingTag)
      {
         mTableCellSpacingTag = new XMLTag(WmlXML.TABLE_CELL_SPACING);
         addSubtag(mTableCellSpacingTag);
      }

      mTableCellSpacingTag.setAttribute(WmlXML.WIDTH_ATT, inValue.toInt(GfxUnits.dxa));
      mTableCellSpacingTag.setAttribute(WmlXML.TYPE_ATT, Type.dxa.name());

      return this;
   }


   //---------------------------------------------------------------------------
   /**
    * The width of a table.
    * @param inValue table width expressed in some GfxSize units.
    * @return this table properties object to enable method chaining
    */
   public WmlTableProperties setWidth(GfxSize inValue)
   {
      if (null == mWidthTag)
      {
         // Check it it has been added via addSubtag()...
         mWidthTag = getOptionalSubtagByName(WmlXML.TABLE_WIDTH);
         if (null == mWidthTag)
         {
            mWidthTag = new XMLTag(WmlXML.TABLE_WIDTH);
            addSubtag(mWidthTag);
         }
      }

      mWidthTag.setAttribute(WmlXML.WIDTH_ATT, inValue.to(GfxUnits.dxa));
      mWidthTag.setAttribute(WmlXML.TYPE_ATT, Type.dxa.name());

      return this;
   }


   //---------------------------------------------------------------------------
   /**
    * The width of a table is expressed in fiftieth of a percent but to make things
    * simpler this method will take a regular percentage and perform the conversion for you.
    * @param inValue the width of the table expressed as a percent
    * @return this table properties object to enable method chaining
    */
   public WmlTableProperties setWidthPct(int inValue)
   {
      if (null == mWidthTag)
      {
         // Check it it has been added via addSubtag()...
         mWidthTag = getOptionalSubtagByName(WmlXML.TABLE_WIDTH);
         if (null == mWidthTag)
         {
            mWidthTag = new XMLTag(WmlXML.TABLE_WIDTH);
            addSubtag(mWidthTag);
         }
      }

      mWidthTag.setAttribute(WmlXML.WIDTH_ATT, inValue * 50);
      mWidthTag.setAttribute(WmlXML.TYPE_ATT, Type.pct.name());

      return this;
   }


   //---------------------------------------------------------------------------
   public WmlTableCellMargins getTableCellMargins()
   {
      if (null == mTableCellMargins)
      {
         // Check it it has been added via addSubtag()...
         mTableCellMargins = getOptionalSubtagByName(WmlXML.TABLE_CELL_MARGIN_DEFAULTS);
         if (null == mTableCellMargins)
         {
            mTableCellMargins = new WmlTableCellMargins(this);
            addSubtag(mTableCellMargins);
         }
      }

      return mTableCellMargins;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy