001package com.hfg.xml.msofficexml.xlsx.spreadsheetml;
002
003
004
005import com.hfg.xml.XMLTag;
006import com.hfg.xml.msofficexml.xlsx.CellRange;
007import com.hfg.xml.msofficexml.xlsx.Xlsx;
008import com.hfg.xml.msofficexml.xlsx.part.TablePart;
009
010//------------------------------------------------------------------------------
011/**
012 Represents an Office Open XML table definition (<ssml:table>) tag.
013 This is the root tag of a table part.
014
015 @author J. Alex Taylor, hairyfatguy.com
016 */
017//------------------------------------------------------------------------------
018// com.hfg XML/HTML Coding Library
019//
020// This library is free software; you can redistribute it and/or
021// modify it under the terms of the GNU Lesser General Public
022// License as published by the Free Software Foundation; either
023// version 2.1 of the License, or (at your option) any later version.
024//
025// This library is distributed in the hope that it will be useful,
026// but WITHOUT ANY WARRANTY; without even the implied warranty of
027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
028// Lesser General Public License for more details.
029//
030// You should have received a copy of the GNU Lesser General Public
031// License along with this library; if not, write to the Free Software
032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
033//
034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
035// jataylor@hairyfatguy.com
036//------------------------------------------------------------------------------
037public class SsmlTable extends SsmlXMLTag
038{
039   //###########################################################################
040   // PRIVATE FIELDS
041   //###########################################################################
042
043   private TablePart mParentTablePart;
044   private XMLTag    mTableColumnsTag;
045   private XMLTag    mAutoFilterTag;
046
047   private CellRange mCellRange;
048   private int       mColumnIndex = 1;
049
050   //###########################################################################
051   // CONSTRUCTORS
052   //###########################################################################
053
054   //---------------------------------------------------------------------------
055   public SsmlTable(TablePart inParentTablePart)
056   {
057      super(SsmlXML.TABLE, (Xlsx) inParentTablePart.getParentDoc());
058
059      mParentTablePart = inParentTablePart;
060
061      // Set some default attributes
062
063   }
064
065   //###########################################################################
066   // PUBLIC METHODS
067   //###########################################################################
068
069   //---------------------------------------------------------------------------
070   public SsmlTable setId(int inValue)
071   {
072      setAttribute(SsmlXML.ID_ATT, inValue);
073      return this;
074   }
075
076   //---------------------------------------------------------------------------
077   public TablePart getParentTablePart()
078   {
079      return mParentTablePart;
080   }
081
082   //---------------------------------------------------------------------------
083   public SsmlTable setName(String inValue)
084   {
085      String existingTableName = getAttributeValue(SsmlXML.NAME_ATT);
086      if (existingTableName != null)
087      {
088         getParentTablePart().getParentWorksheetPart().getRootNode().renameTable(existingTableName, inValue);
089      }
090
091      setAttribute(SsmlXML.NAME_ATT, inValue);
092      setAttribute(SsmlXML.DISPLAY_NAME_ATT, inValue);
093      return this;
094   }
095
096   //---------------------------------------------------------------------------
097   public String getName()
098   {
099      return getAttributeValue(SsmlXML.NAME_ATT);
100   }
101
102   //---------------------------------------------------------------------------
103   public SsmlTable setCellRange(CellRange inValue)
104   {
105      mCellRange = inValue;
106
107      setAttribute(SsmlXML.REF_RANGE_ATT, mCellRange);
108
109      // Add an autoFilter tag with the same ref range
110      if (null == mAutoFilterTag)
111      {
112         mAutoFilterTag = new XMLTag(SsmlXML.AUTO_FILTER);
113         addSubtag(mAutoFilterTag);
114      }
115      mAutoFilterTag.setAttribute(SsmlXML.REF_RANGE_ATT, mCellRange);
116
117      return this;
118   }
119
120   //---------------------------------------------------------------------------
121   public SsmlTable addColumn(String inName)
122   {
123      if (null == mTableColumnsTag)
124      {
125         mTableColumnsTag = new XMLTag(SsmlXML.TABLE_COLUMNS);
126         addSubtag(mTableColumnsTag);
127      }
128
129      XMLTag columnTag = new XMLTag(SsmlXML.TABLE_COLUMN);
130      columnTag.setAttribute(SsmlXML.ID_ATT, mColumnIndex++);
131      columnTag.setAttribute(SsmlXML.NAME_ATT, inName);
132      mTableColumnsTag.addSubtag(columnTag);
133
134      mTableColumnsTag.setAttribute(SsmlXML.COUNT_ATT, mColumnIndex - 1);
135
136      return this;
137   }
138}