001package com.hfg.svg;
002
003import java.awt.Font;
004import java.awt.Point;
005import java.awt.geom.Point2D;
006import java.awt.geom.Rectangle2D;
007
008import com.hfg.css.CssUtil;
009import com.hfg.graphics.units.GfxSize;
010import com.hfg.util.StringUtil;
011import com.hfg.xml.XMLTag;
012import com.hfg.xml.XMLizable;
013
014//------------------------------------------------------------------------------
015/**
016 * Object representation of an SVG (Scalable Vector Graphics) 'g' 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 SvgGroup extends AbstractSvgNode implements SvgNode
042{
043
044   //**************************************************************************
045   // CONSTRUCTORS
046   //**************************************************************************
047
048   //---------------------------------------------------------------------------
049   public SvgGroup()
050   {
051      super(SVG.group);
052   }
053
054   //---------------------------------------------------------------------------
055   public SvgGroup(XMLTag inXMLTag)
056   {
057      this();
058      initFromXMLTag(inXMLTag);
059   }
060
061   //**************************************************************************
062   // PUBLIC METHODS
063   //**************************************************************************
064
065   //---------------------------------------------------------------------------
066   public String toString()
067   {
068      String id = getId();
069      return "<" + getTagName() + (id != null ? " id=" + StringUtil.quote(id) : "") +  ">";
070   }
071
072   //---------------------------------------------------------------------------
073   @Override
074   public SvgGroup setOnMouseDown(String inValue)
075   {
076      return (SvgGroup) super.setOnMouseDown(inValue);
077   }
078
079   //---------------------------------------------------------------------------
080   @Override
081   public SvgGroup setOnMouseUp(String inValue)
082   {
083      return (SvgGroup) super.setOnMouseUp(inValue);
084   }
085
086   //---------------------------------------------------------------------------
087   @Override
088   public SvgGroup setOnClick(String inValue)
089   {
090      return (SvgGroup) super.setOnClick(inValue);
091   }
092
093   //---------------------------------------------------------------------------
094   @Override
095   public SvgGroup setClass(String inValue)
096   {
097      return (SvgGroup) super.setClass(inValue);
098   }
099
100   //---------------------------------------------------------------------------
101   /**
102    Sets both the 'font-family' and 'font-size' attributes based on the Font data.
103    */
104   public SvgGroup setFont(Font inFont)
105   {
106      setFontFamily(inFont.getName());
107      setFontSize(inFont.getSize() + "pt;");
108      return this;
109   }
110
111   //---------------------------------------------------------------------------
112   public SvgGroup setFontFamily(String inValue)
113   {
114      addStyle(SvgAttr.fontFamily, inValue);
115      return this;
116   }
117
118   //---------------------------------------------------------------------------
119   public SvgGroup setFontWeight(String inValue)
120   {
121      addStyle(SvgAttr.fontWeight, inValue);
122      return this;
123   }
124
125   //---------------------------------------------------------------------------
126   public SvgGroup setFontSize(String inValue)
127   {
128      setAttribute(SvgAttr.fontSize, inValue);
129      return this;
130   }
131
132   //---------------------------------------------------------------------------
133   public SvgGroup setFontSize(GfxSize inValue)
134   {
135      setAttribute(SvgAttr.fontSize, inValue);
136      return this;
137   }
138
139   //---------------------------------------------------------------------------
140   /**
141    * Specifies a value for the opacity of children of this group (fill, stroke, and text).
142    * @param inOpacity A numeric value between 0.0 (completely transparent) and 1.0 (completely opaque).
143    * @return this object to allow for method chaining
144    */
145   public SvgGroup setOpacity(float inOpacity)
146   {
147      rangeCheckOpacityValue(inOpacity);
148
149      setAttribute(SvgAttr.opacity, inOpacity);
150      return this;
151   }
152
153   //---------------------------------------------------------------------------
154   @Override
155   public SvgGroup setStyle(String inValue)
156   {
157      setAttribute(SvgAttr.style, inValue);
158      return this;
159   }
160
161   //--------------------------------------------------------------------------
162   @Override
163   public SvgGroup addStyle(String inValue)
164   {
165      CssUtil.addStyle(this, inValue);
166      return this;
167   }
168
169   //--------------------------------------------------------------------------
170   public SvgGroup addStyle(String inName, String inValue)
171   {
172      CssUtil.addStyle(this, inName + ":" + inValue);
173      return this;
174   }
175
176   //---------------------------------------------------------------------------
177   @Override
178   public SvgGroup setTransform(String inValue)
179   {
180      return (SvgGroup) super.setTransform(inValue);
181   }
182
183   //---------------------------------------------------------------------------
184   @Override
185   public SvgGroup setFilter(String inValue)
186   {
187      return (SvgGroup) super.setFilter(inValue);
188   }
189
190   //---------------------------------------------------------------------------
191   public SvgGroup setId(String inValue)
192   {
193      setAttribute(SvgAttr.id, inValue);
194      return this;
195   }
196
197   //---------------------------------------------------------------------------
198   public String getId()
199   {
200      return getAttributeValue(SvgAttr.id);
201   }
202
203   //---------------------------------------------------------------------------
204   public SvgGroup addGroup()
205   {
206      SvgGroup newGroup = new SvgGroup();
207      addSubtag(newGroup);
208      return newGroup;
209   }
210
211   //---------------------------------------------------------------------------
212   public SvgDefs addDefs()
213   {
214      SvgDefs defs = new SvgDefs();
215      addSubtag(defs);
216      return defs;
217   }
218
219   //---------------------------------------------------------------------------
220   public SvgLink addLink(CharSequence inURL)
221   {
222      SvgLink newLink = new SvgLink(inURL);
223      addSubtag(newLink);
224      return newLink;
225   }
226
227   //---------------------------------------------------------------------------
228   public SvgLine addLine(Point inStart, Point inEnd)
229   {
230      SvgLine line = new SvgLine(inStart, inEnd);
231      addSubtag(line);
232      return line;
233   }
234
235   //---------------------------------------------------------------------------
236   public SvgLine addLine(Point2D inStart, Point2D inEnd)
237   {
238      SvgLine line = new SvgLine(inStart, inEnd);
239      addSubtag(line);
240      return line;
241   }
242
243   //---------------------------------------------------------------------------
244   public SvgRect addRect(Rectangle2D inRect)
245   {
246      SvgRect rect = new SvgRect(inRect);
247      addSubtag(rect);
248      return rect;
249   }
250
251   //---------------------------------------------------------------------------
252   public SvgText addText(String inText)
253   {
254      SvgText text = new SvgText(inText);
255      addSubtag(text);
256      return text;
257   }
258
259   //---------------------------------------------------------------------------
260   public SvgText addText(String inText, Font inFont, Point2D inLocation)
261   {
262      SvgText text = new SvgText(inText, inFont, inLocation);
263      addSubtag(text);
264      return text;
265   }
266
267   //---------------------------------------------------------------------------
268   public SvgCircle addCircle()
269   {
270      SvgCircle circle = new SvgCircle();
271      addSubtag(circle);
272      return circle;
273   }
274
275   //---------------------------------------------------------------------------
276   public SvgCircle addCircle(Point2D inCenter, int inRadius)
277   {
278      SvgCircle circle = new SvgCircle(inCenter, inRadius);
279      addSubtag(circle);
280      return circle;
281   }
282
283   //---------------------------------------------------------------------------
284   public SvgPath addPath()
285   {
286      SvgPath path = new SvgPath();
287      addSubtag(path);
288      return path;
289   }
290
291   //---------------------------------------------------------------------------
292   public SvgPath addPath(String inPathData)
293   {
294      SvgPath path = new SvgPath(inPathData);
295      addSubtag(path);
296      return path;
297   }
298
299   //---------------------------------------------------------------------------
300   public SvgEllipse addEllipse()
301   {
302      SvgEllipse ellipse = new SvgEllipse();
303      addSubtag(ellipse);
304      return ellipse;
305   }
306
307   //---------------------------------------------------------------------------
308   public SvgPolygon addPolygon()
309   {
310      SvgPolygon polygon = new SvgPolygon();
311      addSubtag(polygon);
312      return polygon;
313   }
314
315   //---------------------------------------------------------------------------
316   public SvgUse addUse(String inURL)
317   {
318      SvgUse use = new SvgUse(inURL);
319      addSubtag(use);
320      return use;
321   }
322
323   //---------------------------------------------------------------------------
324   @Override
325   public Rectangle2D getBoundsBox()
326   {
327      Double minX = null;
328      Double minY = null;
329      Double maxX = null;
330      Double maxY = null;
331
332      for (XMLizable node : getSubtags())
333      {
334         if (node instanceof SvgNode)
335         {
336            Rectangle2D rect = ((SvgNode)node).getBoundsBox();
337            if (rect != null)
338            {
339               if (null == minX || rect.getX() < minX) minX = rect.getX();
340               if (null == minY || rect.getY() < minY) minY = rect.getY();
341               if (null == maxX || rect.getMaxX() > maxX) maxX = rect.getMaxX();
342               if (null == maxY || rect.getMaxY() > maxY) maxY = rect.getMaxY();
343            }
344         }
345      }
346
347      Rectangle2D boundsBox = null;
348      if (minX != null
349            && minY != null
350            && maxX != null
351            && maxY != null)
352      {
353         boundsBox = new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY);
354         adjustBoundsForTransform(boundsBox);
355      }
356
357      return boundsBox;
358   }
359
360}