001package com.hfg.xml.msofficexml.docx.wordprocessingml.style;
002
003import com.hfg.graphics.ColorUtil;
004import com.hfg.graphics.units.GfxSize;
005import com.hfg.graphics.units.GfxUnits;
006import com.hfg.xml.XMLName;
007import com.hfg.xml.XMLTag;
008import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXML;
009
010
011import java.awt.Color;
012import java.util.ArrayList;
013import java.util.List;
014
015
016// Section 17.3.4 (pg. 351) of Ecma Office Open XML Part 1
017
018public class WmlTableBorder
019{
020   private Position     mPosition;
021   private WmlLineBorderStyle mStyle;
022   private GfxSize      mSize;
023   private Integer      mSpace;
024   private Color mColor;
025   private Boolean      mAutoColor;
026
027   public static enum Position {
028         top,
029         bottom,
030         left,
031         right,
032         insideH,
033         insideV
034   }
035
036   //---------------------------------------------------------------------------
037   public WmlTableBorder()
038   {
039
040   }
041
042   //---------------------------------------------------------------------------
043   /**
044    * If a position is not specified via this method, a complete border set (top, end, bottom, and start) will be generated.
045    * @param inValue the border position
046    */
047   public WmlTableBorder setPosition(Position inValue)
048   {
049      mPosition = inValue;
050      return this;
051   }
052
053   //---------------------------------------------------------------------------
054   public WmlTableBorder setSize(GfxSize inValue)
055   {
056      mSize = inValue;
057      return this;
058   }
059
060   //---------------------------------------------------------------------------
061   public WmlTableBorder setStyle(WmlLineBorderStyle inValue)
062   {
063      mStyle = inValue;
064      return this;
065   }
066
067   //---------------------------------------------------------------------------
068   public WmlTableBorder setColor(Color inValue)
069   {
070      mColor = inValue;
071      return this;
072   }
073
074   //---------------------------------------------------------------------------
075   public WmlTableBorder setAutoColor(boolean inValue)
076   {
077      mAutoColor = inValue;
078      return this;
079   }
080
081   //---------------------------------------------------------------------------
082   /**
083    * The padding (in points) between the text and the border.
084    * @param inValue padding (in points)
085    */
086   public WmlTableBorder setSpace(int inValue)
087   {
088      mSpace = inValue;
089      return this;
090   }
091
092   //---------------------------------------------------------------------------
093   public List<XMLTag> toXMLTags()
094   {
095      List<XMLTag> tags = new ArrayList<XMLTag>(4);
096
097      List<Position> positions =  new ArrayList<Position>(4);
098      if (mPosition != null)
099      {
100         positions.add(mPosition);
101      }
102      else
103      {
104         positions.add(Position.top);
105         positions.add(Position.right);
106         positions.add(Position.bottom);
107         positions.add(Position.left);
108         positions.add(Position.insideH);
109         positions.add(Position.insideV);
110      }
111
112      for (Position position : positions)
113      {
114         XMLTag tag = new XMLTag(new XMLName(position.name(), WmlXML.WORDPROCESSINGML_NAMESPACE));
115
116         if (mSize != null)
117         {
118            // Size is specified in eighths of a point
119            int size = (int) (mSize.to(GfxUnits.points) / 8f);
120            if (size < 2)
121            {
122               size = 2;
123            }
124            else if (size > 96)
125            {
126               size = 96;
127            }
128
129            tag.setAttribute(WmlXML.SIZE_ATT, size);
130         }
131
132         if (mStyle != null)
133         {
134            tag.setAttribute(WmlXML.VALUE_ATT, mStyle.name());
135         }
136
137         if (mSpace != null)
138         {
139            tag.setAttribute(WmlXML.SPACE_ATT, mSpace);
140         }
141
142         if (mColor != null)
143         {
144            tag.setAttribute(WmlXML.COLOR_ATT, ColorUtil.colorToHex(mColor));
145         }
146         else if (mAutoColor != null
147               && mAutoColor)
148         {
149            tag.setAttribute(WmlXML.COLOR_ATT, "auto");
150         }
151
152         tags.add(tag);
153      }
154
155      return tags;
156   }
157}