001package com.hfg.xml.msofficexml.docx.wordprocessingml.style;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import com.hfg.graphics.units.Points;
007import com.hfg.util.collection.CollectionUtil;
008import com.hfg.xml.msofficexml.docx.Docx;
009import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlTableProperties;
010import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXML;
011
012//------------------------------------------------------------------------------
013/**
014 Table style for Docx's Office Open XML documents.
015
016 @author J. Alex Taylor, hairyfatguy.com
017 */
018//------------------------------------------------------------------------------
019// com.hfg XML/HTML Coding Library
020//
021// This library is free software; you can redistribute it and/or
022// modify it under the terms of the GNU Lesser General Public
023// License as published by the Free Software Foundation; either
024// version 2.1 of the License, or (at your option) any later version.
025//
026// This library is distributed in the hope that it will be useful,
027// but WITHOUT ANY WARRANTY; without even the implied warranty of
028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
029// Lesser General Public License for more details.
030//
031// You should have received a copy of the GNU Lesser General Public
032// License along with this library; if not, write to the Free Software
033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
034//
035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
036// jataylor@hairyfatguy.com
037//------------------------------------------------------------------------------
038
039public class WmlTableStyle extends WmlStyle
040{
041   public static final String TABLE_NORMAL_STYLE_ID = "TableNormal";
042
043   private WmlTableProperties mTableProperties;
044
045   //##########################################################################
046   // CONSTRUCTORS
047   //##########################################################################
048
049   //---------------------------------------------------------------------------
050   public WmlTableStyle(Docx inDocx)
051   {
052      super(WmlStyleType.table, inDocx);
053   }
054
055   //---------------------------------------------------------------------------
056   public WmlTableStyle(String inId, Docx inDocx)
057   {
058      super(WmlStyleType.table, inId, inDocx);
059   }
060
061   //##########################################################################
062   // PUBLIC METHODS
063   //##########################################################################
064
065   //---------------------------------------------------------------------------
066   /**
067    * Returns the table properties tag if one exists or else instantiates a new one.
068    * @return the table properties for this table
069    */
070   public WmlTableProperties getTableProperties()
071   {
072      if (null == mTableProperties)
073      {
074         // Check it it has been added via addSubtag()...
075         mTableProperties = getOptionalSubtagByName(WmlXML.TABLE_PROPS);
076         if (null == mTableProperties)
077         {
078            mTableProperties = new WmlTableProperties();
079            addSubtag(mTableProperties);
080         }
081      }
082
083      return mTableProperties;
084   }
085
086   //---------------------------------------------------------------------------
087   /**
088    * Returns the table style properties tag if one exists or else instantiates a new one.
089    * @return the table style properties for this table
090    */
091   public WmlTableStyleProperties getProperties(WmlTableStyleProperties.Type inType)
092   {
093      WmlTableStyleProperties requestedTableStyleProperties = null;
094
095      List<WmlTableStyleProperties> tableStylePropertiesTags = getSubtagsByName(WmlXML.TABLE_STYLE_PROPS);
096      if (CollectionUtil.hasValues(tableStylePropertiesTags))
097      {
098         for (WmlTableStyleProperties tableStyleProperties : tableStylePropertiesTags)
099         {
100            if (inType.toString().equals(tableStyleProperties.getAttributeValue(WmlXML.TYPE_ATT)))
101            {
102               requestedTableStyleProperties = tableStyleProperties;
103               break;
104            }
105         }
106      }
107
108      if (null == requestedTableStyleProperties)
109      {
110         requestedTableStyleProperties = new WmlTableStyleProperties(inType, getParentDoc());
111         addSubtag(requestedTableStyleProperties);
112      }
113
114      return requestedTableStyleProperties;
115   }
116
117   //---------------------------------------------------------------------------
118   public static List<WmlStyle> generateDefaultStyles(Docx inParentDoc)
119   {
120      List<WmlStyle> defaultStyles = new ArrayList<>(5);
121
122
123      WmlTableStyle normalTableStyle = (WmlTableStyle)  new WmlTableStyle(TABLE_NORMAL_STYLE_ID, inParentDoc)
124            .setName("Normal Table")
125            .setIsDefault(true)
126            .setSemiHidden()
127            .setUnhideWhenUsed();
128      normalTableStyle.getTableProperties().setTableIndent(new Points(0));
129      normalTableStyle.getParagraphProperties().getSpacing().setAfter(new Points(0));
130      WmlTableCellMargins tableCellMargins = normalTableStyle.getTableProperties().getTableCellMargins();
131      tableCellMargins.addMargin(new WmlTableCellMargin().setPosition(WmlTableCellMargin.Position.top).setWidth(new Points(0)))
132            .addMargin(new WmlTableCellMargin().setPosition(WmlTableCellMargin.Position.left).setWidth(new Points(5)))
133            .addMargin(new WmlTableCellMargin().setPosition(WmlTableCellMargin.Position.bottom).setWidth(new Points(0)))
134            .addMargin(new WmlTableCellMargin().setPosition(WmlTableCellMargin.Position.right).setWidth(new Points(5)));
135      defaultStyles.add(normalTableStyle);
136
137      return defaultStyles;
138   }
139}