001package com.hfg.xml.msofficexml.docx.wordprocessingml;
002
003import java.util.List;
004
005import com.hfg.xml.msofficexml.docx.Docx;
006import com.hfg.xml.msofficexml.docx.part.NumberingPart;
007import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlParagraphStyle;
008
009//------------------------------------------------------------------------------
010/**
011 Base class for an Office Open XML list.
012
013 @author J. Alex Taylor, hairyfatguy.com
014 */
015//------------------------------------------------------------------------------
016// com.hfg XML/HTML Coding Library
017//
018// This library is free software; you can redistribute it and/or
019// modify it under the terms of the GNU Lesser General Public
020// License as published by the Free Software Foundation; either
021// version 2.1 of the License, or (at your option) any later version.
022//
023// This library is distributed in the hope that it will be useful,
024// but WITHOUT ANY WARRANTY; without even the implied warranty of
025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
026// Lesser General Public License for more details.
027//
028// You should have received a copy of the GNU Lesser General Public
029// License along with this library; if not, write to the Free Software
030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031//
032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
033// jataylor@hairyfatguy.com
034//------------------------------------------------------------------------------
035
036public abstract class WmlList extends WmlXMLTag
037{
038
039   private Integer mNumberingId;
040
041   //##########################################################################
042   // CONSTRUCTORS
043   //##########################################################################
044
045   //---------------------------------------------------------------------------
046   protected WmlList(Docx inParentDoc)
047   {
048      super(null, inParentDoc);
049
050      // Ensure that the styles and numbering parts has been initialized.
051      inParentDoc.getStylesPart();
052      inParentDoc.getNumberingPart();
053   }
054
055   //##########################################################################
056   // PUBLIC METHODS
057   //##########################################################################
058
059   //---------------------------------------------------------------------------
060   public WmlList setNumberingId(int inValue)
061   {
062      mNumberingId = inValue;
063      return this;
064   }
065
066   //---------------------------------------------------------------------------
067   public Integer getNumberingId()
068   {
069      if (null == mNumberingId)
070      {
071         if (this instanceof WmlUnorderedList)
072         {
073            mNumberingId = NumberingPart.UNORDERED_LIST_ID;
074         }
075         else if (this instanceof WmlOrderedList)
076         {
077            mNumberingId = NumberingPart.ORDERED_LIST_ID;
078         }
079      }
080
081      return mNumberingId;
082   }
083
084   //---------------------------------------------------------------------------
085   public WmlList addItem(String inValue)
086   {
087      WmlParagraph p = new WmlParagraph(getParentDoc());
088      p.addTextRun(inValue);
089
090      return addItem(p, 0);
091   }
092
093   //---------------------------------------------------------------------------
094   public WmlList addItem(String inValue, int inLevel)
095   {
096      WmlParagraph p = new WmlParagraph(getParentDoc());
097      p.addTextRun(inValue);
098
099      return addItem(p, inLevel);
100   }
101
102   //---------------------------------------------------------------------------
103   public WmlList addItem(WmlParagraph inValue)
104   {
105      return addItem(inValue, 0);
106   }
107
108   //---------------------------------------------------------------------------
109   public WmlList addItem(WmlParagraph inValue, int inLevel)
110   {
111      inValue.getProperties().setStyle(WmlParagraphStyle.LIST_PARAGRAPH_STYLE_ID);
112
113      inValue.getProperties().getNumberingProperties()
114            .setLevel(inLevel)
115            .setNumberingId(getNumberingId());
116
117      addSubtag(inValue);
118
119      return this;
120   }
121
122   //---------------------------------------------------------------------------
123   public int getLastLevel()
124   {
125      int lastLevel = 0;
126      List<WmlParagraph> items = getItems();
127      if (items.size() > 0)
128      {
129         WmlParagraph lastItem = items.get(items.size() - 1);
130         lastLevel = lastItem.getProperties().getNumberingProperties().getLevel();
131      }
132
133      return lastLevel;
134   }
135
136   //---------------------------------------------------------------------------
137   public List<WmlParagraph> getItems()
138   {
139      return (List<WmlParagraph>) (Object) getSubtagsByName(WmlXML.P);
140   }
141}