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

com.hfg.svg.SvgGroup Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.svg;

import java.awt.Font;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import com.hfg.css.CssUtil;
import com.hfg.graphics.units.GfxSize;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLizable;

//------------------------------------------------------------------------------
/**
 * Object representation of an SVG (Scalable Vector Graphics) 'g' tag.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding 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 SvgGroup extends AbstractSvgNode implements SvgNode
{

   //**************************************************************************
   // CONSTRUCTORS
   //**************************************************************************

   //---------------------------------------------------------------------------
   public SvgGroup()
   {
      super(SVG.group);
   }

   //---------------------------------------------------------------------------
   public SvgGroup(XMLTag inXMLTag)
   {
      this();
      initFromXMLTag(inXMLTag);
   }

   //**************************************************************************
   // PUBLIC METHODS
   //**************************************************************************

   //---------------------------------------------------------------------------
   public String toString()
   {
      String id = getId();
      return "<" + getTagName() + (id != null ? " id=" + StringUtil.quote(id) : "") +  ">";
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgGroup setOnMouseDown(String inValue)
   {
      return (SvgGroup) super.setOnMouseDown(inValue);
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgGroup setOnMouseUp(String inValue)
   {
      return (SvgGroup) super.setOnMouseUp(inValue);
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgGroup setOnClick(String inValue)
   {
      return (SvgGroup) super.setOnClick(inValue);
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgGroup setClass(String inValue)
   {
      return (SvgGroup) super.setClass(inValue);
   }

   //---------------------------------------------------------------------------
   /**
    Sets both the 'font-family' and 'font-size' attributes based on the Font data.
    */
   public SvgGroup setFont(Font inFont)
   {
      setFontFamily(inFont.getName());
      setFontSize(inFont.getSize() + "pt;");
      return this;
   }

   //---------------------------------------------------------------------------
   public SvgGroup setFontFamily(String inValue)
   {
      addStyle(SvgAttr.fontFamily, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public SvgGroup setFontWeight(String inValue)
   {
      addStyle(SvgAttr.fontWeight, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public SvgGroup setFontSize(String inValue)
   {
      setAttribute(SvgAttr.fontSize, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public SvgGroup setFontSize(GfxSize inValue)
   {
      setAttribute(SvgAttr.fontSize, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   /**
    * Specifies a value for the opacity of children of this group (fill, stroke, and text).
    * @param inOpacity A numeric value between 0.0 (completely transparent) and 1.0 (completely opaque).
    * @return this object to allow for method chaining
    */
   public SvgGroup setOpacity(float inOpacity)
   {
      rangeCheckOpacityValue(inOpacity);

      setAttribute(SvgAttr.opacity, inOpacity);
      return this;
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgGroup setStyle(CharSequence inValue)
   {
      setAttribute(SvgAttr.style, inValue);
      return this;
   }

   //--------------------------------------------------------------------------
   @Override
   public SvgGroup addStyle(CharSequence inValue)
   {
      return (SvgGroup) super.addStyle(inValue);
   }

   //--------------------------------------------------------------------------
   public SvgGroup addStyle(String inName, String inValue)
   {
      CssUtil.addStyle(this, inName + ":" + inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgGroup setTransform(String inValue)
   {
      return (SvgGroup) super.setTransform(inValue);
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgGroup setFilter(String inValue)
   {
      return (SvgGroup) super.setFilter(inValue);
   }

   //---------------------------------------------------------------------------
   public SvgGroup setId(String inValue)
   {
      setAttribute(SvgAttr.id, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public String getId()
   {
      return getAttributeValue(SvgAttr.id);
   }

   //---------------------------------------------------------------------------
   public SvgGroup addGroup()
   {
      SvgGroup newGroup = new SvgGroup();
      addSubtag(newGroup);
      return newGroup;
   }

   //---------------------------------------------------------------------------
   public SvgDefs addDefs()
   {
      SvgDefs defs = new SvgDefs();
      addSubtag(defs);
      return defs;
   }

   //---------------------------------------------------------------------------
   public SvgLink addLink(CharSequence inURL)
   {
      SvgLink newLink = new SvgLink(inURL);
      addSubtag(newLink);
      return newLink;
   }

   //---------------------------------------------------------------------------
   public SvgLine addLine(Point inStart, Point inEnd)
   {
      SvgLine line = new SvgLine(inStart, inEnd);
      addSubtag(line);
      return line;
   }

   //---------------------------------------------------------------------------
   public SvgLine addLine(Point2D inStart, Point2D inEnd)
   {
      SvgLine line = new SvgLine(inStart, inEnd);
      addSubtag(line);
      return line;
   }

   //---------------------------------------------------------------------------
   public SvgRect addRect(Rectangle2D inRect)
   {
      SvgRect rect = new SvgRect(inRect);
      addSubtag(rect);
      return rect;
   }

   //---------------------------------------------------------------------------
   public SvgText addText()
   {
      SvgText text = new SvgText();
      addSubtag(text);
      return text;
   }

   //---------------------------------------------------------------------------
   public SvgText addText(String inText)
   {
      SvgText text = new SvgText(inText);
      addSubtag(text);
      return text;
   }

   //---------------------------------------------------------------------------
   public SvgText addText(String inText, Font inFont, Point2D inLocation)
   {
      SvgText text = new SvgText(inText, inFont, inLocation);
      addSubtag(text);
      return text;
   }

   //---------------------------------------------------------------------------
   public SvgCircle addCircle()
   {
      SvgCircle circle = new SvgCircle();
      addSubtag(circle);
      return circle;
   }

   //---------------------------------------------------------------------------
   public SvgCircle addCircle(Point2D inCenter, int inRadius)
   {
      SvgCircle circle = new SvgCircle(inCenter, inRadius);
      addSubtag(circle);
      return circle;
   }

   //---------------------------------------------------------------------------
   public SvgPath addPath()
   {
      SvgPath path = new SvgPath();
      addSubtag(path);
      return path;
   }

   //---------------------------------------------------------------------------
   public SvgPath addPath(String inPathData)
   {
      SvgPath path = new SvgPath(inPathData);
      addSubtag(path);
      return path;
   }

   //---------------------------------------------------------------------------
   public SvgEllipse addEllipse()
   {
      SvgEllipse ellipse = new SvgEllipse();
      addSubtag(ellipse);
      return ellipse;
   }

   //---------------------------------------------------------------------------
   public SvgPolygon addPolygon()
   {
      SvgPolygon polygon = new SvgPolygon();
      addSubtag(polygon);
      return polygon;
   }

   //---------------------------------------------------------------------------
   public SvgUse addUse(String inURL)
   {
      SvgUse use = new SvgUse(inURL);
      addSubtag(use);
      return use;
   }

   //---------------------------------------------------------------------------
   @Override
   public Rectangle2D getBoundsBox()
   {
      Double minX = null;
      Double minY = null;
      Double maxX = null;
      Double maxY = null;

      for (XMLizable node : getSubtags())
      {
         if (node instanceof SvgNode)
         {
            Rectangle2D rect = ((SvgNode)node).getBoundsBox();
            if (rect != null)
            {
               if (null == minX || rect.getX() < minX) minX = rect.getX();
               if (null == minY || rect.getY() < minY) minY = rect.getY();
               if (null == maxX || rect.getMaxX() > maxX) maxX = rect.getMaxX();
               if (null == maxY || rect.getMaxY() > maxY) maxY = rect.getMaxY();
            }
         }
      }

      Rectangle2D boundsBox = null;
      if (minX != null
            && minY != null
            && maxX != null
            && maxY != null)
      {
         boundsBox = new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY);
         adjustBoundsForTransform(boundsBox);
      }

      return boundsBox;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy