com.hfg.svg.SvgSwitch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.svg;
import com.hfg.css.CssUtil;
import com.hfg.graphics.units.GfxSize;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLizable;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
//------------------------------------------------------------------------------
/**
* Object representation of an SVG (Scalable Vector Graphics) 'switch' 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 SvgSwitch extends AbstractSvgNode implements SvgNode
{
//**************************************************************************
// CONSTRUCTORS
//**************************************************************************
//---------------------------------------------------------------------------
public SvgSwitch()
{
super(SVG.switch_);
}
//---------------------------------------------------------------------------
public SvgSwitch(XMLTag inXMLTag)
{
this();
initFromXMLTag(inXMLTag);
}
//**************************************************************************
// PUBLIC METHODS
//**************************************************************************
//---------------------------------------------------------------------------
@Override
public SvgSwitch setClass(String inValue)
{
return (SvgSwitch) super.setClass(inValue);
}
//---------------------------------------------------------------------------
/**
Sets both the 'font-family' and 'font-size' attributes based on the Font data.
*/
public SvgSwitch setFont(Font inFont)
{
setFontFamily(inFont.getName());
setFontSize(inFont.getSize() + "pt;");
return this;
}
//---------------------------------------------------------------------------
public SvgSwitch setFontFamily(String inValue)
{
addStyle(SvgAttr.fontFamily, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgSwitch setFontWeight(String inValue)
{
addStyle(SvgAttr.fontWeight, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgSwitch setFontSize(String inValue)
{
setAttribute(SvgAttr.fontSize, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgSwitch 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 SvgSwitch setOpacity(float inOpacity)
{
rangeCheckOpacityValue(inOpacity);
setAttribute(SvgAttr.opacity, inOpacity);
return this;
}
//---------------------------------------------------------------------------
@Override
public SvgSwitch setStyle(String inValue)
{
setAttribute(SvgAttr.style, inValue);
return this;
}
//--------------------------------------------------------------------------
@Override
public SvgSwitch addStyle(String inValue)
{
CssUtil.addStyle(this, inValue);
return this;
}
//--------------------------------------------------------------------------
public SvgSwitch addStyle(String inName, String inValue)
{
CssUtil.addStyle(this, inName + ":" + inValue);
return this;
}
//---------------------------------------------------------------------------
@Override
public SvgSwitch setTransform(String inValue)
{
return (SvgSwitch) super.setTransform(inValue);
}
//---------------------------------------------------------------------------
@Override
public SvgSwitch setFilter(String inValue)
{
return (SvgSwitch) super.setFilter(inValue);
}
//---------------------------------------------------------------------------
public SvgSwitch setId(String inValue)
{
setAttribute(SvgAttr.id, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgGroup addGroup()
{
SvgGroup newGroup = new SvgGroup();
addSubtag(newGroup);
return newGroup;
}
//---------------------------------------------------------------------------
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(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;
}
//---------------------------------------------------------------------------
@Override
public Rectangle getBoundsBox()
{
Integer minX = null;
Integer minY = null;
Integer maxX = null;
Integer maxY = null;
for (XMLizable node : getSubtags())
{
if (node instanceof SvgNode)
{
Rectangle rect = ((SvgNode)node).getBoundsBox();
if (rect != null)
{
if (null == minX || rect.getX() < minX) minX = (int) rect.getX();
if (null == minY || rect.getY() < minY) minY = (int) rect.getY();
if (null == maxX || rect.getMaxX() > maxX) maxX = (int) rect.getMaxX();
if (null == maxY || rect.getMaxY() > maxY) maxY = (int) rect.getMaxY();
}
}
}
Rectangle boundsBox = null;
if (minX != null
&& minY != null
&& maxX != null
&& maxY != null)
{
boundsBox = new Rectangle(minX, minY, maxX - minX, maxY - minY);
adjustBoundsForTransform(boundsBox);
}
return boundsBox;
}
}