1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-05-31 23:50:49 +00:00
plantuml/src/net/sourceforge/plantuml/SkinParam.java

355 lines
10 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009, Arnaud Roques
*
* Project Info: http://plantuml.sourceforge.net
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML 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 General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* Original Author: Arnaud Roques
*
2011-09-07 20:41:58 +00:00
* Revision $Revision: 7180 $
2010-11-15 20:35:36 +00:00
*
*/
package net.sourceforge.plantuml;
import java.awt.Font;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
2011-01-05 18:23:06 +00:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2010-11-15 20:35:36 +00:00
2011-03-26 12:37:27 +00:00
import net.sourceforge.plantuml.cucadiagram.dot.DotSplines;
import net.sourceforge.plantuml.cucadiagram.dot.GraphvizLayoutStrategy;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.graphic.HorizontalAlignement;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.graphic.HtmlColor;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.ugraphic.ColorMapper;
import net.sourceforge.plantuml.ugraphic.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.ColorMapperMonochrome;
import net.sourceforge.plantuml.ugraphic.UFont;
2010-11-15 20:35:36 +00:00
public class SkinParam implements ISkinParam {
private final Map<String, String> params = new HashMap<String, String>();
public void setParam(String key, String value) {
2011-01-05 18:23:06 +00:00
params.put(cleanForKey(key), value.trim());
}
private static final String stereoPatternString = "\\<\\<(.*?)\\>\\>";
private static final Pattern stereoPattern = Pattern.compile(stereoPatternString);
2011-09-07 20:41:58 +00:00
private final UmlDiagramType type;
public SkinParam(UmlDiagramType type) {
this.type = type;
}
2011-01-05 18:23:06 +00:00
static String cleanForKey(String key) {
key = key.toLowerCase().trim();
key = key.replaceAll("_|\\.|\\s", "");
final Matcher m = stereoPattern.matcher(key);
if (m.find()) {
final String s = m.group(1);
key = key.replaceAll(stereoPatternString, "");
key += "<<" + s + ">>";
}
return key;
2010-11-15 20:35:36 +00:00
}
public HtmlColor getBackgroundColor() {
2011-01-05 18:23:06 +00:00
final HtmlColor result = getHtmlColor(ColorParam.background, null);
2010-11-15 20:35:36 +00:00
if (result == null) {
2011-08-08 17:48:29 +00:00
return HtmlColor.WHITE;
2010-11-15 20:35:36 +00:00
}
return result;
}
public String getValue(String key) {
2011-01-05 18:23:06 +00:00
return params.get(cleanForKey(key));
2010-11-15 20:35:36 +00:00
}
static String humanName(String key) {
final StringBuilder sb = new StringBuilder();
boolean upper = true;
for (int i = 0; i < key.length(); i++) {
final char c = key.charAt(i);
if (c == '_') {
upper = true;
} else {
sb.append(upper ? Character.toUpperCase(c) : Character.toLowerCase(c));
upper = false;
}
}
return sb.toString();
}
2011-01-05 18:23:06 +00:00
public HtmlColor getHtmlColor(ColorParam param, String stereotype) {
if (stereotype != null) {
checkStereotype(stereotype);
final String value2 = getValue(param.name() + "color" + stereotype);
if (value2 != null && HtmlColor.isValid(value2)) {
2011-01-23 19:36:52 +00:00
return HtmlColor.getColorIfValid(value2);
2011-01-05 18:23:06 +00:00
}
}
2010-11-15 20:35:36 +00:00
final String value = getValue(param.name() + "color");
if (value == null || HtmlColor.isValid(value) == false) {
return null;
}
2011-01-23 19:36:52 +00:00
return HtmlColor.getColorIfValid(value);
2010-11-15 20:35:36 +00:00
}
2011-01-05 18:23:06 +00:00
private void checkStereotype(String stereotype) {
if (stereotype.startsWith("<<") == false || stereotype.endsWith(">>") == false) {
throw new IllegalArgumentException();
}
}
2011-08-08 17:48:29 +00:00
private int getFontSize(FontParam param, String stereotype) {
2011-01-05 18:23:06 +00:00
if (stereotype != null) {
checkStereotype(stereotype);
final String value2 = getValue(param.name() + "fontsize" + stereotype);
if (value2 != null && value2.matches("\\d+")) {
return Integer.parseInt(value2);
}
}
2010-11-15 20:35:36 +00:00
String value = getValue(param.name() + "fontsize");
if (value == null || value.matches("\\d+") == false) {
value = getValue("defaultfontsize");
}
if (value == null || value.matches("\\d+") == false) {
2011-08-08 17:48:29 +00:00
return param.getDefaultSize(this);
2010-11-15 20:35:36 +00:00
}
return Integer.parseInt(value);
}
2011-08-08 17:48:29 +00:00
private String getFontFamily(FontParam param, String stereotype) {
2011-01-05 18:23:06 +00:00
if (stereotype != null) {
checkStereotype(stereotype);
final String value2 = getValue(param.name() + "fontname" + stereotype);
if (value2 != null) {
return StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(value2);
}
}
2010-11-15 20:35:36 +00:00
// Times, Helvetica, Courier or Symbol
String value = getValue(param.name() + "fontname");
if (value != null) {
2011-01-05 18:23:06 +00:00
return StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(value);
2010-11-15 20:35:36 +00:00
}
if (param != FontParam.CIRCLED_CHARACTER) {
value = getValue("defaultfontname");
if (value != null) {
2011-01-05 18:23:06 +00:00
return StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(value);
2010-11-15 20:35:36 +00:00
}
}
return param.getDefaultFamily();
}
2011-01-05 18:23:06 +00:00
public HtmlColor getFontHtmlColor(FontParam param, String stereotype) {
String value = null;
if (stereotype != null) {
checkStereotype(stereotype);
value = getValue(param.name() + "fontcolor" + stereotype);
}
if (value == null || HtmlColor.isValid(value) == false) {
value = getValue(param.name() + "fontcolor");
}
if (value == null || HtmlColor.isValid(value) == false) {
2010-11-15 20:35:36 +00:00
value = getValue("defaultfontcolor");
}
2011-01-05 18:23:06 +00:00
if (value == null || HtmlColor.isValid(value) == false) {
2010-11-15 20:35:36 +00:00
value = param.getDefaultColor();
}
2011-01-23 19:36:52 +00:00
return HtmlColor.getColorIfValid(value);
2010-11-15 20:35:36 +00:00
}
2011-08-08 17:48:29 +00:00
private int getFontStyle(FontParam param, String stereotype) {
2011-01-05 18:23:06 +00:00
String value = null;
if (stereotype != null) {
checkStereotype(stereotype);
value = getValue(param.name() + "fontstyle" + stereotype);
}
if (value == null) {
value = getValue(param.name() + "fontstyle");
}
2010-11-15 20:35:36 +00:00
if (value == null) {
value = getValue("defaultfontstyle");
}
if (value == null) {
2011-08-08 17:48:29 +00:00
return param.getDefaultFontStyle(this);
2010-11-15 20:35:36 +00:00
}
int result = Font.PLAIN;
if (value.toLowerCase().contains("bold")) {
result = result | Font.BOLD;
}
if (value.toLowerCase().contains("italic")) {
result = result | Font.ITALIC;
}
return result;
}
2011-08-08 17:48:29 +00:00
public UFont getFont(FontParam fontParam, String stereotype) {
2011-01-05 18:23:06 +00:00
if (stereotype != null) {
checkStereotype(stereotype);
}
2011-09-07 20:41:58 +00:00
return new UFont(getFontFamily(fontParam, stereotype), getFontStyle(fontParam, stereotype), getFontSize(
fontParam, stereotype));
2010-11-15 20:35:36 +00:00
}
public int getCircledCharacterRadius() {
final String value = getValue("circledcharacterradius");
if (value != null && value.matches("\\d+")) {
return Integer.parseInt(value);
}
// return 11;
// System.err.println("SIZE1="+getFontSize(FontParam.CIRCLED_CHARACTER));
// System.err.println("SIZE1="+getFontSize(FontParam.CIRCLED_CHARACTER)/3);
2011-01-05 18:23:06 +00:00
return getFontSize(FontParam.CIRCLED_CHARACTER, null) / 3 + 6;
2010-11-15 20:35:36 +00:00
}
public boolean isClassCollapse() {
return true;
}
public int classAttributeIconSize() {
final String value = getValue("classAttributeIconSize");
if (value != null && value.matches("\\d+")) {
return Integer.parseInt(value);
}
return 10;
}
2011-08-08 17:48:29 +00:00
private boolean isMonochrome() {
2010-11-15 20:35:36 +00:00
return "true".equals(getValue("monochrome"));
}
public static Collection<String> getPossibleValues() {
final Set<String> result = new TreeSet<String>();
result.add("Monochrome");
2011-04-19 16:50:40 +00:00
// result.add("BackgroundColor");
2010-11-15 20:35:36 +00:00
result.add("CircledCharacterRadius");
result.add("ClassAttributeIconSize");
result.add("DefaultFontName");
result.add("DefaultFontStyle");
result.add("DefaultFontSize");
result.add("DefaultFontColor");
for (FontParam p : EnumSet.allOf(FontParam.class)) {
final String h = humanName(p.name());
result.add(h + "FontStyle");
result.add(h + "FontName");
result.add(h + "FontSize");
result.add(h + "FontColor");
}
2011-04-19 16:50:40 +00:00
for (ColorParam p : EnumSet.allOf(ColorParam.class)) {
final String h = capitalize(p.name());
result.add(h + "Color");
}
2010-11-15 20:35:36 +00:00
return Collections.unmodifiableSet(result);
}
2011-04-19 16:50:40 +00:00
private static String capitalize(String name) {
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
2011-01-05 18:23:06 +00:00
public int getDpi() {
final String value = getValue("dpi");
if (value != null && value.matches("\\d+")) {
return Integer.parseInt(value);
}
return 96;
}
2011-02-23 21:14:39 +00:00
public boolean useOctagonForActivity() {
final String value = getValue("activityshape");
if ("roundedbox".equalsIgnoreCase(value)) {
return false;
}
2011-03-26 12:37:27 +00:00
if ("octagon".equalsIgnoreCase(value)) {
return true;
}
return false;
}
public DotSplines getDotSplines() {
final String value = getValue("linetype");
if ("polyline".equalsIgnoreCase(value)) {
return DotSplines.POLYLINE;
}
if ("ortho".equalsIgnoreCase(value)) {
return DotSplines.ORTHO;
}
return DotSplines.SPLINES;
2011-02-23 21:14:39 +00:00
}
2011-04-19 16:50:40 +00:00
2011-03-26 12:37:27 +00:00
public GraphvizLayoutStrategy getStrategy() {
final String value = getValue("layout");
if ("neato".equalsIgnoreCase(value)) {
return GraphvizLayoutStrategy.NEATO;
}
if ("circo".equalsIgnoreCase(value)) {
return GraphvizLayoutStrategy.CIRCO;
}
if ("fdp".equalsIgnoreCase(value)) {
return GraphvizLayoutStrategy.FDP;
}
if ("twopi".equalsIgnoreCase(value)) {
return GraphvizLayoutStrategy.TWOPI;
}
return GraphvizLayoutStrategy.DOT;
}
2011-08-08 17:48:29 +00:00
public HorizontalAlignement getHorizontalAlignement(AlignParam param) {
final String value = getValue(param.name());
final HorizontalAlignement result = HorizontalAlignement.fromString(value);
if (result == null) {
return param.getDefaultValue();
}
return result;
}
public ColorMapper getColorMapper() {
if (isMonochrome()) {
return new ColorMapperMonochrome();
}
return new ColorMapperIdentity();
}
2011-09-07 20:41:58 +00:00
public boolean isSvek() {
boolean defaultValue = false;
if (OptionFlags.SVEK && type == UmlDiagramType.CLASS) {
defaultValue = true;
}
final String value = getValue("svek");
if (value == null) {
return defaultValue;
}
return "true".equalsIgnoreCase(value);
}
2010-11-15 20:35:36 +00:00
}