001package com.hfg.xml.msoffice2003.spreadsheetml;
002
003import com.hfg.xml.XMLTag;
004import com.hfg.graphics.ColorUtil;
005
006import java.awt.*;
007
008
009//------------------------------------------------------------------------------
010/**
011 Excel style.
012
013 @author J. Alex Taylor, hairyfatguy.com
014 */
015//------------------------------------------------------------------------------
016// com.hfg 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 class ExcelStyle extends XMLTag
037{
038   private XMLTag mBordersTag;
039   private XMLTag mAlignmentTag;
040
041   private static int sId = 1;
042
043   //---------------------------------------------------------------------------
044   public ExcelStyle()
045   {
046      this("s" + sId++);
047   }
048
049   //---------------------------------------------------------------------------
050   public ExcelStyle(String inId)
051   {
052      super(SpreadsheetML.STYLE);
053      setAttribute(SpreadsheetML.ID_ATT, inId);
054   }
055
056   //---------------------------------------------------------------------------
057   public String getId()
058   {
059      return getAttributeValue(SpreadsheetML.ID_ATT.getLocalName());
060   }
061
062
063   //---------------------------------------------------------------------------
064   public com.hfg.xml.msoffice2003.spreadsheetml.ExcelFont addFont()
065   {
066      com.hfg.xml.msoffice2003.spreadsheetml.ExcelFont font = new com.hfg.xml.msoffice2003.spreadsheetml.ExcelFont();
067      addSubtag(font);
068
069      return font;
070   }
071
072   //---------------------------------------------------------------------------
073   public ExcelBorder addBorder()
074   {
075      ExcelBorder border = new ExcelBorder();
076      getBordersTag().addSubtag(border);
077
078      return border;
079   }
080
081   //---------------------------------------------------------------------------
082   public ExcelStyle addBorder(ExcelBorder inValue)
083   {
084      getBordersTag().addSubtag(inValue);
085
086      return this;
087   }
088
089   //---------------------------------------------------------------------------
090   public ExcelStyle setParentStyleId(String inId)
091   {
092      setAttribute(SpreadsheetML.PARENT_ATT, inId);
093      return this;
094   }
095
096   //---------------------------------------------------------------------------
097   public ExcelStyle setBackgroundColor(Color inValue)
098   {
099      removeSubtagsByName(SpreadsheetML.INTERIOR.getLocalName());
100      XMLTag tag = new XMLTag(SpreadsheetML.INTERIOR);
101      tag.setAttribute(SpreadsheetML.COLOR_ATT, "#" + ColorUtil.colorToHex(inValue));
102      tag.setAttribute(SpreadsheetML.PATTERN_ATT, "Solid");
103      addSubtag(tag);
104      return this;
105   }
106
107   //---------------------------------------------------------------------------
108   public ExcelStyle setWrapText()
109   {
110      getAlignmentTag().setAttribute(SpreadsheetML.WRAP_TEXT_ATT, "1");
111
112      return this;
113   }
114
115   //---------------------------------------------------------------------------
116   public ExcelStyle setVerticalAlignment(ExcelVerticalAlign inValue)
117   {
118      getAlignmentTag().setAttribute(SpreadsheetML.VERTICAL_ATT, inValue.name());
119
120      return this;
121   }
122
123   //---------------------------------------------------------------------------
124   /**
125    Set the text rotation (-90 to 90).
126    */
127   public ExcelStyle setTextRotation(int inValue)
128   {
129      getAlignmentTag().setAttribute(SpreadsheetML.ROTATE_ATT, inValue);
130
131      return this;
132   }
133
134   //---------------------------------------------------------------------------
135   public ExcelStyle setHorizontalAlignment(ExcelHorizontalAlign inValue)
136   {
137      getAlignmentTag().setAttribute(SpreadsheetML.HORIZONTAL_ATT, inValue.name());
138
139      return this;
140   }
141
142   //---------------------------------------------------------------------------
143   public ExcelStyle setNumberFormat(ExcelNumberFormat inValue)
144   {
145      removeSubtagsByName(SpreadsheetML.NUMBER_FORMAT.getLocalName());
146      XMLTag tag = new XMLTag(SpreadsheetML.NUMBER_FORMAT);
147      tag.setAttribute(SpreadsheetML.FORMAT_ATT, inValue.name());
148      addSubtag(tag);
149      return this;
150   }
151
152   //---------------------------------------------------------------------------
153   private XMLTag getBordersTag()
154   {
155      if (null == mBordersTag)
156      {
157         mBordersTag = new XMLTag(SpreadsheetML.BORDERS);
158         addSubtag(mBordersTag);
159      }
160
161      return mBordersTag;
162   }
163
164   //---------------------------------------------------------------------------
165   private XMLTag getAlignmentTag()
166   {
167      if (null == mAlignmentTag)
168      {
169         mAlignmentTag = new XMLTag(SpreadsheetML.ALIGNMENT);
170         addSubtag(mAlignmentTag);
171      }
172
173      return mAlignmentTag;
174   }
175
176}