1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-08 03:10:53 +00:00
plantuml/src/net/sourceforge/plantuml/TitledDiagram.java

314 lines
9.4 KiB
Java
Raw Normal View History

2019-04-21 20:40:01 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2019-04-21 20:40:01 +00:00
*
* Project Info: http://plantuml.com
2022-08-17 17:34:24 +00:00
*
2019-04-21 20:40:01 +00:00
* If you like this project or if you find it useful, you can support us at:
2022-08-17 17:34:24 +00:00
*
2019-04-21 20:40:01 +00:00
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
2022-08-17 17:34:24 +00:00
*
2019-04-21 20:40:01 +00:00
* 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 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.
*
*
* Original Author: Arnaud Roques
*
*
*/
package net.sourceforge.plantuml;
2020-08-25 17:24:17 +00:00
import java.io.IOException;
2022-02-01 20:21:45 +00:00
import java.io.InputStream;
2022-09-20 20:35:41 +00:00
import java.util.Map;
2020-08-25 17:24:17 +00:00
import net.sourceforge.plantuml.anim.Animation;
import net.sourceforge.plantuml.anim.AnimationDecoder;
2021-11-21 11:09:15 +00:00
import net.sourceforge.plantuml.api.ApiStable;
2020-08-25 17:24:17 +00:00
import net.sourceforge.plantuml.command.CommandExecutionResult;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.core.Diagram;
2021-05-23 15:35:13 +00:00
import net.sourceforge.plantuml.core.UmlSource;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.cucadiagram.Display;
2021-11-09 17:47:19 +00:00
import net.sourceforge.plantuml.cucadiagram.DisplayPositioned;
import net.sourceforge.plantuml.cucadiagram.DisplayPositionned;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.cucadiagram.DisplaySection;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
import net.sourceforge.plantuml.graphic.VerticalAlignment;
2020-08-25 17:24:17 +00:00
import net.sourceforge.plantuml.sprite.Sprite;
import net.sourceforge.plantuml.style.ClockwiseTopRightBottomLeft;
2021-05-06 21:23:05 +00:00
import net.sourceforge.plantuml.style.PName;
import net.sourceforge.plantuml.style.SName;
import net.sourceforge.plantuml.style.Style;
2020-09-30 20:57:58 +00:00
import net.sourceforge.plantuml.style.StyleBuilder;
2022-02-01 20:21:45 +00:00
import net.sourceforge.plantuml.style.StyleLoader;
2022-03-01 18:11:51 +00:00
import net.sourceforge.plantuml.style.StyleSignatureBasic;
import net.sourceforge.plantuml.ugraphic.ImageBuilder;
2022-09-20 20:35:41 +00:00
import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
import net.sourceforge.plantuml.ugraphic.color.ColorOrder;
2021-05-06 21:23:05 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2022-08-19 16:34:21 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColors;
2019-04-21 20:40:01 +00:00
public abstract class TitledDiagram extends AbstractPSystem implements Diagram, Annotated {
2021-03-23 17:14:35 +00:00
public static boolean FORCE_SMETANA = false;
2021-04-20 20:19:49 +00:00
public static boolean FORCE_ELK = false;
2020-11-21 17:33:24 +00:00
2021-11-09 17:47:19 +00:00
private DisplayPositioned title = DisplayPositioned.none(HorizontalAlignment.CENTER, VerticalAlignment.TOP);
2019-04-21 20:40:01 +00:00
2021-11-09 17:47:19 +00:00
private DisplayPositioned caption = DisplayPositioned.none(HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
private DisplayPositioned legend = DisplayPositioned.none(HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
2019-04-21 20:40:01 +00:00
private final DisplaySection header = DisplaySection.none();
private final DisplaySection footer = DisplaySection.none();
private Display mainFrame;
2021-01-10 20:52:19 +00:00
private final UmlDiagramType type;
2019-04-21 20:40:01 +00:00
2020-08-25 17:24:17 +00:00
private final SkinParam skinParam;
// ::comment when WASM
private Animation animation;
// ::done
2020-09-30 20:57:58 +00:00
private final Pragma pragma = new Pragma();
public Pragma getPragma() {
return pragma;
}
2022-09-20 20:35:41 +00:00
public TitledDiagram(UmlSource source, UmlDiagramType type, Map<String, String> orig) {
2021-05-23 15:35:13 +00:00
super(source);
2021-01-10 20:52:19 +00:00
this.type = type;
2022-09-18 17:08:06 +00:00
this.skinParam = SkinParam.create(type);
2022-03-19 12:48:23 +00:00
if (orig != null)
this.skinParam.copyAllFrom(orig);
2020-08-25 17:24:17 +00:00
}
2020-09-30 20:57:58 +00:00
public final StyleBuilder getCurrentStyleBuilder() {
return skinParam.getCurrentStyleBuilder();
}
2021-01-10 20:52:19 +00:00
final public UmlDiagramType getUmlDiagramType() {
return type;
}
2020-08-25 17:24:17 +00:00
public final ISkinParam getSkinParam() {
return skinParam;
}
public void setParam(String key, String value) {
skinParam.setParam(StringUtils.goLowerCase(key), value);
}
public void addSprite(String name, Sprite sprite) {
skinParam.addSprite(name, sprite);
}
public CommandExecutionResult loadSkin(String newSkin) throws IOException {
2022-02-01 20:21:45 +00:00
final String filename = newSkin + ".skin";
final InputStream is = StyleLoader.getInputStreamForStyle(filename);
if (is == null)
return CommandExecutionResult.error("Cannot find style " + newSkin);
is.close();
getSkinParam().setDefaultSkin(filename);
2020-08-25 17:24:17 +00:00
return CommandExecutionResult.ok();
// final String res = "/skin/" + filename + ".skin";
// final InputStream internalIs = UmlDiagram.class.getResourceAsStream(res);
// if (internalIs != null) {
// final BlocLines lines2 = BlocLines.load(internalIs, new
// LineLocationImpl(filename, null));
// return loadSkinInternal(lines2);
// }
// if (OptionFlags.ALLOW_INCLUDE == false) {
// return CommandExecutionResult.ok();
// }
// final File f = FileSystem.getInstance().getFile(filename + ".skin");
// if (f == null || f.exists() == false || f.canRead() == false) {
// return CommandExecutionResult.error("Cannot load skin from " + filename);
// }
// final BlocLines lines = BlocLines.load(f, new LineLocationImpl(f.getName(),
// null));
// return loadSkinInternal(lines);
}
// private CommandExecutionResult loadSkinInternal(final BlocLines lines) {
// final CommandSkinParam cmd1 = new CommandSkinParam();
// final CommandSkinParamMultilines cmd2 = new CommandSkinParamMultilines();
// for (int i = 0; i < lines.size(); i++) {
// final BlocLines ext1 = lines.subList(i, i + 1);
// if (cmd1.isValid(ext1) == CommandControl.OK) {
// cmd1.execute(this, ext1);
// } else if (cmd2.isValid(ext1) == CommandControl.OK_PARTIAL) {
// i = tryMultilines(cmd2, i, lines);
// }
// }
// return CommandExecutionResult.ok();
// }
// private int tryMultilines(CommandSkinParamMultilines cmd2, int i, BlocLines lines) {
// for (int j = i + 1; j <= lines.size(); j++) {
// final BlocLines ext1 = lines.subList(i, j);
// if (cmd2.isValid(ext1) == CommandControl.OK) {
// cmd2.execute(this, ext1);
// return j;
// } else if (cmd2.isValid(ext1) == CommandControl.NOT_OK) {
// return j;
// }
// }
// return i;
// }
2021-11-09 17:47:19 +00:00
final public void setTitle(DisplayPositioned title) {
2021-11-21 11:09:15 +00:00
if (title.isNull() || title.getDisplay().isWhite())
2019-04-21 20:40:01 +00:00
return;
this.title = title;
}
@Override
final public DisplayPositionned getTitle() {
2019-04-21 20:40:01 +00:00
return title;
}
2020-08-25 17:24:17 +00:00
2021-11-21 11:09:15 +00:00
@Override
@ApiStable
final public Display getTitleDisplay() {
if (title == null)
return null;
return title.getDisplay();
}
2019-04-21 20:40:01 +00:00
final public void setMainFrame(Display mainFrame) {
this.mainFrame = mainFrame;
}
2021-11-09 17:47:19 +00:00
final public void setCaption(DisplayPositioned caption) {
2019-04-21 20:40:01 +00:00
this.caption = caption;
}
2021-11-09 17:47:19 +00:00
final public DisplayPositioned getCaption() {
2019-04-21 20:40:01 +00:00
return caption;
}
2020-08-25 17:24:17 +00:00
2019-04-21 20:40:01 +00:00
final public DisplaySection getHeader() {
return header;
}
final public DisplaySection getFooter() {
return footer;
}
2020-08-25 17:24:17 +00:00
2021-11-09 17:47:19 +00:00
final public DisplayPositioned getLegend() {
2019-04-21 20:40:01 +00:00
return legend;
}
2021-11-09 17:47:19 +00:00
public void setLegend(DisplayPositioned legend) {
2019-04-21 20:40:01 +00:00
this.legend = legend;
}
final public Display getMainFrame() {
return mainFrame;
}
2020-12-21 19:05:24 +00:00
private boolean useSmetana;
private boolean useElk;
2020-09-30 20:57:58 +00:00
2020-12-21 19:05:24 +00:00
public void setUseSmetana(boolean useSmetana) {
this.useSmetana = useSmetana;
2020-09-30 20:57:58 +00:00
}
public void setUseElk(boolean useElk) {
this.useElk = useElk;
}
public boolean isUseElk() {
2021-04-20 20:19:49 +00:00
if (FORCE_ELK)
return true;
return this.useElk;
}
2020-12-21 19:05:24 +00:00
public boolean isUseSmetana() {
if (FORCE_SMETANA)
2020-09-30 20:57:58 +00:00
return true;
2020-12-21 19:05:24 +00:00
return useSmetana;
2020-09-30 20:57:58 +00:00
}
@Override
public ClockwiseTopRightBottomLeft getDefaultMargins() {
return ClockwiseTopRightBottomLeft.same(10);
}
// ::comment when WASM
final public void setAnimation(Iterable<CharSequence> animationData) {
2021-05-14 08:42:57 +00:00
// try {
final AnimationDecoder animationDecoder = new AnimationDecoder(animationData);
this.animation = Animation.create(animationDecoder.decode());
// } catch (ScriptException e) {
2022-08-17 17:34:24 +00:00
// Logme.error(e);
2021-05-14 08:42:57 +00:00
// }
}
final public Animation getAnimation() {
return animation;
}
// ::done
@Override
public ImageBuilder createImageBuilder(FileFormatOption fileFormatOption) throws IOException {
return super.createImageBuilder(fileFormatOption).styled(this);
}
2021-05-06 21:23:05 +00:00
public HColor calculateBackColor() {
2022-04-10 19:24:55 +00:00
final Style style = StyleSignatureBasic.of(SName.root, SName.document, this.getUmlDiagramType().getStyleName())
.getMergedStyle(this.getSkinParam().getCurrentStyleBuilder());
2022-09-18 17:08:06 +00:00
HColor backgroundColor = style.value(PName.BackGroundColor).asColor(this.getSkinParam().getIHtmlColorSet());
2022-04-10 19:24:55 +00:00
if (backgroundColor == null)
2022-08-19 16:34:21 +00:00
backgroundColor = HColors.transparent();
2022-04-10 19:24:55 +00:00
return backgroundColor;
2021-05-06 21:23:05 +00:00
}
2022-09-20 20:35:41 +00:00
@Override
protected ColorMapper muteColorMapper(ColorMapper init) {
if ("dark".equalsIgnoreCase(getSkinParam().getValue("mode")))
2022-10-18 20:57:44 +00:00
return ColorMapper.DARK_MODE;
2022-09-20 20:35:41 +00:00
final String monochrome = getSkinParam().getValue("monochrome");
if ("true".equals(monochrome))
return ColorMapper.MONOCHROME;
if ("reverse".equals(monochrome))
return ColorMapper.MONOCHROME_REVERSE;
2022-09-23 16:53:33 +00:00
final String reversecolor = getSkinParam().getValue("reversecolor");
if (reversecolor == null)
2022-09-20 20:35:41 +00:00
return init;
2022-09-23 16:53:33 +00:00
if ("dark".equalsIgnoreCase(reversecolor))
2022-09-20 20:35:41 +00:00
return ColorMapper.LIGTHNESS_INVERSE;
2022-09-23 16:53:33 +00:00
final ColorOrder order = ColorOrder.fromString(reversecolor);
2022-09-20 20:35:41 +00:00
if (order == null)
return init;
return ColorMapper.reverse(order);
}
2019-04-21 20:40:01 +00:00
}