com.mxgraph.view.mxStylesheet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jgraphx Show documentation
Show all versions of jgraphx Show documentation
JGraphX Swing Component - Java Graph Visualization Library
This is a binary & source redistribution of the original, unmodified JGraphX library originating from:
"https://github.com/jgraph/jgraphx/archive/v3.4.1.3.zip".
The purpose of this redistribution is to make the library available to other Maven projects.
/**
* $Id: mxStylesheet.java,v 1.27 2010/03/26 10:24:58 gaudenz Exp $
* Copyright (c) 2007, Gaudenz Alder
*/
package com.mxgraph.view;
import java.util.Hashtable;
import java.util.Map;
import com.mxgraph.util.mxConstants;
/**
* Defines the appearance of the cells in a graph. The following example
* changes the font size for all vertices by changing the default vertex
* style in-place:
*
* getDefaultVertexStyle().put(mxConstants.STYLE_FONTSIZE, 16);
*
*
* To change the default font size for all cells, set
* mxConstants.DEFAULT_FONTSIZE.
*/
public class mxStylesheet
{
/**
* Shared immutable empty hashtable (for undefined cell styles).
*/
public static final Map EMPTY_STYLE = new Hashtable();
/**
* Maps from names to styles.
*/
protected Map> styles = new Hashtable>();
/**
* Constructs a new stylesheet and assigns default styles.
*/
public mxStylesheet()
{
setDefaultVertexStyle(createDefaultVertexStyle());
setDefaultEdgeStyle(createDefaultEdgeStyle());
}
/**
* Returns all styles as map of name, hashtable pairs.
*
* @return All styles in this stylesheet.
*/
public Map> getStyles()
{
return styles;
}
/**
* Sets all styles in the stylesheet.
*/
public void setStyles(Map> styles)
{
this.styles = styles;
}
/**
* Creates and returns the default vertex style.
*
* @return Returns the default vertex style.
*/
protected Map createDefaultVertexStyle()
{
Map style = new Hashtable();
style.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_RECTANGLE);
style.put(mxConstants.STYLE_PERIMETER, mxPerimeter.RectanglePerimeter);
style.put(mxConstants.STYLE_VERTICAL_ALIGN, mxConstants.ALIGN_MIDDLE);
style.put(mxConstants.STYLE_ALIGN, mxConstants.ALIGN_CENTER);
style.put(mxConstants.STYLE_FILLCOLOR, "#C3D9FF");
style.put(mxConstants.STYLE_STROKECOLOR, "#6482B9");
style.put(mxConstants.STYLE_FONTCOLOR, "#774400");
return style;
}
/**
* Creates and returns the default edge style.
*
* @return Returns the default edge style.
*/
protected Map createDefaultEdgeStyle()
{
Map style = new Hashtable();
style.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_CONNECTOR);
style.put(mxConstants.STYLE_ENDARROW, mxConstants.ARROW_CLASSIC);
style.put(mxConstants.STYLE_VERTICAL_ALIGN, mxConstants.ALIGN_MIDDLE);
style.put(mxConstants.STYLE_ALIGN, mxConstants.ALIGN_CENTER);
style.put(mxConstants.STYLE_STROKECOLOR, "#6482B9");
style.put(mxConstants.STYLE_FONTCOLOR, "#446299");
return style;
}
/**
* Returns the default style for vertices.
*
* @return Returns the default vertex style.
*/
public Map getDefaultVertexStyle()
{
return styles.get("defaultVertex");
}
/**
* Sets the default style for vertices.
*
* @param value Style to be used for vertices.
*/
public void setDefaultVertexStyle(Map value)
{
putCellStyle("defaultVertex", value);
}
/**
* Returns the default style for edges.
*
* @return Returns the default edge style.
*/
public Map getDefaultEdgeStyle()
{
return styles.get("defaultEdge");
}
/**
* Sets the default style for edges.
*
* @param value Style to be used for edges.
*/
public void setDefaultEdgeStyle(Map value)
{
putCellStyle("defaultEdge", value);
}
/**
* Stores the specified style under the given name.
*
* @param name Name for the style to be stored.
* @param style Key, value pairs that define the style.
*/
public void putCellStyle(String name, Map style)
{
styles.put(name, style);
}
/**
* Returns the cell style for the specified cell or the given defaultStyle
* if no style can be found for the given stylename.
*
* @param name String of the form [(stylename|key=value);] that represents the
* style.
* @param defaultStyle Default style to be returned if no style can be found.
* @return Returns the style for the given formatted cell style.
*/
public Map getCellStyle(String name,
Map defaultStyle)
{
Map style = defaultStyle;
if (name != null && name.length() > 0)
{
String[] pairs = name.split(";");
if (style != null && !name.startsWith(";"))
{
style = new Hashtable(style);
}
else
{
style = new Hashtable();
}
for (int i = 0; i < pairs.length; i++)
{
String tmp = pairs[i];
int c = tmp.indexOf('=');
if (c >= 0)
{
String key = tmp.substring(0, c);
String value = tmp.substring(c + 1);
if (value.equals(mxConstants.NONE))
{
style.remove(key);
}
else
{
style.put(key, value);
}
}
else
{
Map tmpStyle = styles.get(tmp);
if (tmpStyle != null)
{
style.putAll(tmpStyle);
}
}
}
}
return style;
}
}