All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.msoffice2003.spreadsheetml.ExcelStyle Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.msoffice2003.spreadsheetml;

import com.hfg.xml.XMLTag;
import com.hfg.graphics.ColorUtil;

import java.awt.*;


//------------------------------------------------------------------------------
/**
 Excel style.

 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class ExcelStyle extends XMLTag
{
   private XMLTag mBordersTag;
   private XMLTag mAlignmentTag;

   private static int sId = 1;

   //---------------------------------------------------------------------------
   public ExcelStyle()
   {
      this("s" + sId++);
   }

   //---------------------------------------------------------------------------
   public ExcelStyle(String inId)
   {
      super(SpreadsheetML.STYLE);
      setAttribute(SpreadsheetML.ID_ATT, inId);
   }

   //---------------------------------------------------------------------------
   public String getId()
   {
      return getAttributeValue(SpreadsheetML.ID_ATT.getLocalName());
   }


   //---------------------------------------------------------------------------
   public com.hfg.xml.msoffice2003.spreadsheetml.ExcelFont addFont()
   {
      com.hfg.xml.msoffice2003.spreadsheetml.ExcelFont font = new com.hfg.xml.msoffice2003.spreadsheetml.ExcelFont();
      addSubtag(font);

      return font;
   }

   //---------------------------------------------------------------------------
   public ExcelBorder addBorder()
   {
      ExcelBorder border = new ExcelBorder();
      getBordersTag().addSubtag(border);

      return border;
   }

   //---------------------------------------------------------------------------
   public ExcelStyle addBorder(ExcelBorder inValue)
   {
      getBordersTag().addSubtag(inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public ExcelStyle setParentStyleId(String inId)
   {
      setAttribute(SpreadsheetML.PARENT_ATT, inId);
      return this;
   }

   //---------------------------------------------------------------------------
   public ExcelStyle setBackgroundColor(Color inValue)
   {
      removeSubtagsByName(SpreadsheetML.INTERIOR.getLocalName());
      XMLTag tag = new XMLTag(SpreadsheetML.INTERIOR);
      tag.setAttribute(SpreadsheetML.COLOR_ATT, "#" + ColorUtil.colorToHex(inValue));
      tag.setAttribute(SpreadsheetML.PATTERN_ATT, "Solid");
      addSubtag(tag);
      return this;
   }

   //---------------------------------------------------------------------------
   public ExcelStyle setWrapText()
   {
      getAlignmentTag().setAttribute(SpreadsheetML.WRAP_TEXT_ATT, "1");

      return this;
   }

   //---------------------------------------------------------------------------
   public ExcelStyle setVerticalAlignment(ExcelVerticalAlign inValue)
   {
      getAlignmentTag().setAttribute(SpreadsheetML.VERTICAL_ATT, inValue.name());

      return this;
   }

   //---------------------------------------------------------------------------
   /**
    Set the text rotation (-90 to 90).
    */
   public ExcelStyle setTextRotation(int inValue)
   {
      getAlignmentTag().setAttribute(SpreadsheetML.ROTATE_ATT, inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public ExcelStyle setHorizontalAlignment(ExcelHorizontalAlign inValue)
   {
      getAlignmentTag().setAttribute(SpreadsheetML.HORIZONTAL_ATT, inValue.name());

      return this;
   }

   //---------------------------------------------------------------------------
   public ExcelStyle setNumberFormat(ExcelNumberFormat inValue)
   {
      removeSubtagsByName(SpreadsheetML.NUMBER_FORMAT.getLocalName());
      XMLTag tag = new XMLTag(SpreadsheetML.NUMBER_FORMAT);
      tag.setAttribute(SpreadsheetML.FORMAT_ATT, inValue.name());
      addSubtag(tag);
      return this;
   }

   //---------------------------------------------------------------------------
   private XMLTag getBordersTag()
   {
      if (null == mBordersTag)
      {
         mBordersTag = new XMLTag(SpreadsheetML.BORDERS);
         addSubtag(mBordersTag);
      }

      return mBordersTag;
   }

   //---------------------------------------------------------------------------
   private XMLTag getAlignmentTag()
   {
      if (null == mAlignmentTag)
      {
         mAlignmentTag = new XMLTag(SpreadsheetML.ALIGNMENT);
         addSubtag(mAlignmentTag);
      }

      return mAlignmentTag;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy