001package com.hfg.xml.msofficexml.xlsx.spreadsheetml.style;
002
003
004
005import com.hfg.html.attribute.Align;
006import com.hfg.util.CompareUtil;
007import com.hfg.xml.XMLNamespaceSet;
008import com.hfg.xml.msofficexml.xlsx.Xlsx;
009import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXML;
010import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXMLTag;
011
012import java.io.Writer;
013
014//------------------------------------------------------------------------------
015/**
016 Represents an Office Open XML spreadsheetml (<ssml:alignment>) tag.
017
018 @author J. Alex Taylor, hairyfatguy.com
019 */
020//------------------------------------------------------------------------------
021// com.hfg XML/HTML Coding Library
022//
023// This library is free software; you can redistribute it and/or
024// modify it under the terms of the GNU Lesser General Public
025// License as published by the Free Software Foundation; either
026// version 2.1 of the License, or (at your option) any later version.
027//
028// This library is distributed in the hope that it will be useful,
029// but WITHOUT ANY WARRANTY; without even the implied warranty of
030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
031// Lesser General Public License for more details.
032//
033// You should have received a copy of the GNU Lesser General Public
034// License along with this library; if not, write to the Free Software
035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
036//
037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
038// jataylor@hairyfatguy.com
039//------------------------------------------------------------------------------
040
041public class SsmlAlignment extends SsmlXMLTag implements Comparable
042{
043
044   //##########################################################################
045   // CONSTRUCTORS
046   //##########################################################################
047
048   //---------------------------------------------------------------------------
049   public SsmlAlignment(Xlsx inXlsx)
050   {
051      super(SsmlXML.ALIGNMENT, inXlsx);
052   }
053
054   //---------------------------------------------------------------------------
055   public SsmlAlignment(SsmlXMLTag inSsmlXMLTag)
056   {
057      this(inSsmlXMLTag.getParentDoc());
058   }
059
060   //##########################################################################
061   // PUBLIC METHODS
062   //##########################################################################
063
064   //---------------------------------------------------------------------------
065   // Attempt to prevent outputting an unconfigured alignment tag like '<alignment/>'
066   // because Excel might choke on it.
067   @Override
068   public synchronized void toXML(Writer inWriter, XMLNamespaceSet inDeclaredNamespaces)
069   {
070      if (hasAttributes()
071          || hasContentOrSubtags())
072      {
073         super.toXML(inWriter, inDeclaredNamespaces);
074      }
075   }
076
077   //---------------------------------------------------------------------------
078   // Attempt to prevent outputting an unconfigured alignment tag like '<alignment/>'
079   // because Excel might choke on it.
080   @Override
081   public void toIndentedXML(Writer inWriter, int inInitialIndentSize, int inIndentSize,
082                             XMLNamespaceSet inDeclaredNamespaces)
083   {
084      if (hasAttributes()
085          || hasContentOrSubtags())
086      {
087         super.toIndentedXML(inWriter, inInitialIndentSize, inIndentSize, inDeclaredNamespaces);
088      }
089   }
090
091   //---------------------------------------------------------------------------
092   @Override
093   public SsmlAlignment clone()
094   {
095      return (SsmlAlignment) super.clone();
096   }
097
098   //--------------------------------------------------------------------------
099   @Override
100   public int hashCode()
101   {
102      int value = 0;
103
104      if (hasAttribute(SsmlXML.HORIZONTAL_ATT))
105      {
106         value += getAttributeValue(SsmlXML.HORIZONTAL_ATT).hashCode();
107      }
108
109      if (hasAttribute(SsmlXML.VERTICAL_ATT))
110      {
111         value += getAttributeValue(SsmlXML.VERTICAL_ATT).hashCode();
112      }
113
114      if (hasAttribute(SsmlXML.TEXT_ROTATION_ATT))
115      {
116         value += getAttributeValue(SsmlXML.TEXT_ROTATION_ATT).hashCode();
117      }
118
119      if (hasAttribute(SsmlXML.WRAP_TEXT_ATT))
120      {
121         value += getAttributeValue(SsmlXML.WRAP_TEXT_ATT).hashCode();
122      }
123
124      // TODO: indent, relativeIndent, justifyLastLine, shrinkToFit, readingOrder
125
126      return value;
127   }
128
129   //--------------------------------------------------------------------------
130   @Override
131   public boolean equals(Object inObj2)
132   {
133      return (0 == compareTo(inObj2));
134   }
135
136   //--------------------------------------------------------------------------
137   @Override
138   public int compareTo(Object inObj2)
139   {
140      int result = -1;
141
142      if (inObj2 instanceof SsmlAlignment)
143      {
144         SsmlAlignment alignment2 = (SsmlAlignment) inObj2;
145
146         result = CompareUtil.compare(getAttributeValue(SsmlXML.HORIZONTAL_ATT), alignment2.getAttributeValue(SsmlXML.HORIZONTAL_ATT));
147
148         if (0 == result)
149         {
150            result = CompareUtil.compare(getAttributeValue(SsmlXML.VERTICAL_ATT), alignment2.getAttributeValue(SsmlXML.VERTICAL_ATT));
151
152            if (0 == result)
153            {
154               result = CompareUtil.compare(getAttributeValue(SsmlXML.TEXT_ROTATION_ATT), alignment2.getAttributeValue(SsmlXML.TEXT_ROTATION_ATT));
155
156               if (0 == result)
157               {
158                  result = CompareUtil.compare(getAttributeValue(SsmlXML.WRAP_TEXT_ATT), alignment2.getAttributeValue(SsmlXML.WRAP_TEXT_ATT));
159
160                  if (0 == result)
161                  {
162                     // TODO: indent, relativeIndent, justifyLastLine, shrinkToFit, readingOrder
163                  }
164               }
165            }
166         }
167      }
168
169      return result;
170   }
171
172   //---------------------------------------------------------------------------
173   public SsmlAlignment setHorizontal(Align inValue)
174   {
175      return setHorizontal(SsmlHorizontalAlign.valueOf(inValue));
176   }
177
178   //---------------------------------------------------------------------------
179   public SsmlAlignment setHorizontal(SsmlHorizontalAlign inValue)
180   {
181      if (inValue != null)
182      {
183         setAttribute(SsmlXML.HORIZONTAL_ATT, inValue);
184      }
185      else
186      {
187         removeAttribute(SsmlXML.HORIZONTAL_ATT);
188      }
189
190      return this;
191   }
192
193   //---------------------------------------------------------------------------
194   public SsmlAlignment setVertical(SsmlVerticalAlign inValue)
195   {
196      if (inValue != null)
197      {
198         setAttribute(SsmlXML.VERTICAL_ATT, inValue);
199      }
200      else
201      {
202         removeAttribute(SsmlXML.VERTICAL_ATT);
203      }
204
205      return this;
206   }
207
208   //---------------------------------------------------------------------------
209   public SsmlAlignment setTextRotation(Integer inValue)
210   {
211      if (inValue != null)
212      {
213         setAttribute(SsmlXML.TEXT_ROTATION_ATT, inValue);
214      }
215      else
216      {
217         removeAttribute(SsmlXML.TEXT_ROTATION_ATT);
218      }
219
220      return this;
221   }
222
223   //---------------------------------------------------------------------------
224   public SsmlAlignment setWrapText(boolean inValue)
225   {
226      setAttribute(SsmlXML.WRAP_TEXT_ATT, inValue);
227
228      return this;
229   }
230
231   // TODO: indent, relativeIndent, justifyLastLine, shrinkToFit, readingOrder
232}