CommandConstraintOnLinks.java
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2024, Arnaud Roques
*
* Project Info: https://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
*
* 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.command.note;
import java.util.List;
import net.atmp.CucaDiagram;
import net.sourceforge.plantuml.abel.Link;
import net.sourceforge.plantuml.annotation.Explain;
import net.sourceforge.plantuml.command.CommandExecutionResult;
import net.sourceforge.plantuml.command.ParserPass;
import net.sourceforge.plantuml.command.SingleLineCommand2;
import net.sourceforge.plantuml.klimt.color.ColorParser;
import net.sourceforge.plantuml.klimt.color.ColorType;
import net.sourceforge.plantuml.klimt.color.NoSuchColorException;
import net.sourceforge.plantuml.regex.IRegex;
import net.sourceforge.plantuml.regex.RegexConcat;
import net.sourceforge.plantuml.regex.RegexLeaf;
import net.sourceforge.plantuml.regex.RegexResult;
import net.sourceforge.plantuml.utils.BlocLines;
import net.sourceforge.plantuml.utils.LineLocation;
public final class CommandConstraintOnLinks extends SingleLineCommand2<CucaDiagram> {
public CommandConstraintOnLinks() {
super(getRegexConcat());
}
private static IRegex getRegexConcat() {
return RegexConcat.build(CommandConstraintOnLinks.class.getName(), RegexLeaf.start(), //
new RegexLeaf("constraint"), //
RegexLeaf.spaceZeroOrMore(), //
new RegexLeaf("on"), //
RegexLeaf.spaceOneOrMore(), //
new RegexLeaf("links"), //
RegexLeaf.spaceZeroOrMore(), //
color().getRegex(), //
RegexLeaf.spaceZeroOrMore(), //
new RegexLeaf(":"), //
RegexLeaf.spaceZeroOrMore(), //
new RegexLeaf(1, "NOTE", "(.*)"), RegexLeaf.end());
}
private static ColorParser color() {
return ColorParser.simpleColor(ColorType.BACK);
}
@Override
@Explain
protected String explainArg(LineLocation location, RegexResult arg) {
final StringBuilder sb = new StringBuilder();
// 'constraint on links : {xor}' draws a constraint between the two
// last links of the diagram, like the UML xor notation between two
// associations; executeArg fails when there are fewer than two links.
sb.append("Drawing the constraint \"").append(arg.get("NOTE", 0))
.append("\" between the two last links");
// The color is parsed but never read by executeArg.
if (arg.get("COLOR", 0) != null)
sb.append(" (the color ").append(arg.get("COLOR", 0)).append(" is currently ignored)");
return sb.toString();
}
@Override
protected CommandExecutionResult executeArg(CucaDiagram diagram, LineLocation location, RegexResult arg, ParserPass currentPass)
throws NoSuchColorException {
final List<Link> links = diagram.getTwoLastLinks();
if (links == null) {
return CommandExecutionResult.error("Cannot put constraint on two last links");
}
final BlocLines note = BlocLines.getWithNewlines(arg.get("NOTE", 0));
return diagram.constraintOnLinks(links.get(0), links.get(1), note.toDisplay());
}
}