001package com.hfg.xml.msofficexml.docx.drawingml.fill;
002
003
004import com.hfg.math.Percent;
005import com.hfg.math.units.Angle;
006import com.hfg.math.units.AngleUnits;
007import com.hfg.xml.XMLTag;
008import com.hfg.xml.msofficexml.docx.drawingml.DmlXML;
009import com.hfg.xml.msofficexml.docx.drawingml.color.DmlColor;
010
011//------------------------------------------------------------------------------
012/**
013 Represents a gradient fill (<a:gradFill>) tag in drawing markup language (DML) from Office Open XML.
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//------------------------------------------------------------------------------
037
038/*
039  Ex:
040  <a:gradFill>
041    <a:gsLst>
042      <a:gs pos="39999">
043        <a:srgbClr val="85C2FF"/>
044      </a:gs>
045      <a:gs pos="70000">
046        <a:srgbClr val="C4D6EB"/>
047      </a:gs>
048    </a:gsLst>
049    <a:lin ang="16200000" scaled="1"/>
050  </a:gradFill>
051 */
052
053public class DmlGradientFill extends DmlFill
054{
055   private XMLTag   mGradientStopListTag;
056   private XMLTag   mLinearGradientFillTag;
057
058   public enum TileRectFlip
059   {
060      none,
061      x,
062      xy,
063      y
064   }
065
066   //###########################################################################
067   // CONSTRUCTORS
068   //###########################################################################
069
070   //---------------------------------------------------------------------------
071   public DmlGradientFill()
072   {
073      super(DmlXML.SOLID_FILL);
074
075      // TODO: Are defaults needed to keep Excel from barfing?
076   }
077
078   //###########################################################################
079   // PUBLIC METHODS
080   //###########################################################################
081
082   //---------------------------------------------------------------------------
083   /**
084    Specifies the direction (clockwise) for the gradient.
085    @param inValue gradient angle in arbitrary units. (The value in the xml will be converted to 60000ths of a degree)
086    @return this DmlGradientFill object to enable method chaining
087    */
088   public DmlGradientFill setAngle(Angle inValue)
089   {
090      getOrCreateLinearGradientFillTag().setAttribute(DmlXML.ANGLE_ATT, inValue.to(AngleUnits.degrees) * 60000);
091
092      return this;
093   }
094
095   //---------------------------------------------------------------------------
096   /**
097    Specifies whether the angle scales with the fill region.
098    @param inValue whether the angle scales with the fill region
099    @return this DmlGradientFill object to enable method chaining
100    */
101   public DmlGradientFill setScaled(boolean inValue)
102   {
103      getOrCreateLinearGradientFillTag().setAttribute(DmlXML.SCALED_ATT, inValue ? "1" : "0");
104
105      return this;
106   }
107
108   //---------------------------------------------------------------------------
109   public DmlGradientFill addGradientStop(Percent inPosition, DmlColor inColorModel)
110   {
111      if (null == mGradientStopListTag)
112      {
113         mGradientStopListTag = new XMLTag(DmlXML.GRADIENT_STOP_LIST);
114         addSubtag(mGradientStopListTag);
115      }
116
117      DmlGradientStop gradientStopTag = new DmlGradientStop().setPosition(inPosition).setColorModel(inColorModel);
118      mGradientStopListTag.addSubtag(gradientStopTag);
119
120      return this;
121   }
122
123   //---------------------------------------------------------------------------
124   /**
125    Specifies the direction in which to flip the gradient while tiling.
126    This is only relevant when a tileRect is specified so that the gradient must
127    be tiles to fill the shape. Possible values are none, x (horizontal), xy
128    (horizontal and vertical), and y (vertical).
129    @param inValue the direction to flip the gradient fill
130    @return this DmlGradientFill object to enable method chaining
131    */
132   public DmlGradientFill setFlip(TileRectFlip inValue)
133   {
134      setAttribute(DmlXML.FLIP_ATT, inValue);
135      return this;
136   }
137
138   //---------------------------------------------------------------------------
139   /**
140    Specifies if the gradient fill rotates with the shape when the shape is rotated.
141    @param inValue whether to rotate the gradient fill when the shape is rotated
142    @return this DmlGradientFill object to enable method chaining
143    */
144   public DmlGradientFill setRotateWithShape(boolean inValue)
145   {
146      setAttribute(DmlXML.ROTATE_WITH_SHAPE_ATT, inValue);
147      return this;
148   }
149
150
151   //###########################################################################
152   // PRIVATE METHODS
153   //###########################################################################
154
155   //---------------------------------------------------------------------------
156   private XMLTag getOrCreateLinearGradientFillTag()
157   {
158      if (null == mLinearGradientFillTag)
159      {
160         mLinearGradientFillTag = new XMLTag(DmlXML.LINEAR_GRAD_FILL);
161         addSubtag(mLinearGradientFillTag);
162      }
163
164      return mLinearGradientFillTag;
165   }
166
167}