1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-17 07:32:24 +00:00
plantuml/src/net/sourceforge/plantuml/activitydiagram3/InstructionSwitch.java

193 lines
5.7 KiB
Java
Raw Normal View History

2019-04-21 20:40:01 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2019-04-21 20:40:01 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2019-04-21 20:40:01 +00:00
*
* If you like this project or if you find it useful, you can support us at:
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
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.activitydiagram3;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
2021-05-09 21:14:40 +00:00
import java.util.Objects;
2019-04-21 20:40:01 +00:00
import java.util.Set;
import net.sourceforge.plantuml.activitydiagram3.ftile.Ftile;
import net.sourceforge.plantuml.activitydiagram3.ftile.FtileFactory;
import net.sourceforge.plantuml.activitydiagram3.ftile.Swimlane;
2021-11-09 17:47:19 +00:00
import net.sourceforge.plantuml.activitydiagram3.gtile.Gtile;
import net.sourceforge.plantuml.activitydiagram3.gtile.GtileIfHexagon;
2021-07-25 10:41:36 +00:00
import net.sourceforge.plantuml.command.CommandExecutionResult;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.color.Colors;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.color.HColor;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.creole.Display;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.font.StringBounder;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.geom.VerticalAlignment;
2021-06-27 16:50:40 +00:00
import net.sourceforge.plantuml.sequencediagram.NotePosition;
import net.sourceforge.plantuml.sequencediagram.NoteType;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.style.ISkinParam;
2019-04-21 20:40:01 +00:00
public class InstructionSwitch extends WithNote implements Instruction, InstructionCollection {
2023-12-11 17:34:23 +00:00
// ::remove folder when __HAXE__
2019-04-21 20:40:01 +00:00
2021-11-09 17:47:19 +00:00
private final List<Branch> switches = new ArrayList<>();
2019-04-21 20:40:01 +00:00
private final ISkinParam skinParam;
private final Instruction parent;
private Branch current;
private final LinkRendering topInlinkRendering;
private LinkRendering afterEndwhile = LinkRendering.none();
private final Display labelTest;
private final Swimlane swimlane;
2021-11-09 17:47:19 +00:00
@Override
2019-04-21 20:40:01 +00:00
public boolean containsBreak() {
2022-05-21 09:41:00 +00:00
for (Branch branch : switches)
if (branch.containsBreak())
2019-04-21 20:40:01 +00:00
return true;
2022-05-21 09:41:00 +00:00
2019-04-21 20:40:01 +00:00
return false;
}
public InstructionSwitch(Swimlane swimlane, Instruction parent, Display labelTest, LinkRendering inlinkRendering,
2020-03-18 10:50:02 +00:00
HColor color, ISkinParam skinParam) {
2021-05-09 21:14:40 +00:00
this.topInlinkRendering = Objects.requireNonNull(inlinkRendering);
2019-04-21 20:40:01 +00:00
this.parent = parent;
this.skinParam = skinParam;
this.labelTest = labelTest;
this.swimlane = swimlane;
}
2021-11-09 17:47:19 +00:00
@Override
2021-07-25 10:41:36 +00:00
public CommandExecutionResult add(Instruction ins) {
2022-05-21 09:41:00 +00:00
if (current == null)
2021-07-25 10:41:36 +00:00
return CommandExecutionResult.error("No 'case' in this switch");
2022-05-21 09:41:00 +00:00
2021-07-25 10:41:36 +00:00
return current.add(ins);
2019-04-21 20:40:01 +00:00
}
2023-02-28 21:22:51 +00:00
// ::comment when __CORE__
2021-11-09 17:47:19 +00:00
@Override
public Gtile createGtile(ISkinParam skinParam, StringBounder stringBounder) {
for (Branch branch : switches)
branch.updateGtile(skinParam, stringBounder);
final List<Gtile> gtiles = new ArrayList<>();
final List<Branch> branches = new ArrayList<>();
for (Branch branch : switches) {
gtiles.add(branch.getGtile());
branches.add(branch);
}
2021-12-14 18:03:11 +00:00
return GtileIfHexagon.build(swimlane, gtiles, switches);
2021-11-09 17:47:19 +00:00
}
2023-02-02 17:59:43 +00:00
// ::done
2021-12-14 18:03:11 +00:00
2019-04-21 20:40:01 +00:00
public Ftile createFtile(FtileFactory factory) {
2021-11-09 17:47:19 +00:00
for (Branch branch : switches)
2019-04-21 20:40:01 +00:00
branch.updateFtile(factory);
2021-12-14 18:03:11 +00:00
2022-05-21 09:41:00 +00:00
Ftile result = factory.createSwitch(swimlane, switches, afterEndwhile, topInlinkRendering, labelTest);
result = eventuallyAddNote(factory, result, getSwimlaneIn(), VerticalAlignment.TOP);
return result;
2019-04-21 20:40:01 +00:00
}
2021-11-09 17:47:19 +00:00
@Override
2019-04-21 20:40:01 +00:00
final public boolean kill() {
2021-06-27 16:50:40 +00:00
return current.kill();
2019-04-21 20:40:01 +00:00
}
2021-11-09 17:47:19 +00:00
@Override
2019-04-21 20:40:01 +00:00
public LinkRendering getInLinkRendering() {
return topInlinkRendering;
}
2021-11-09 17:47:19 +00:00
@Override
2019-04-21 20:40:01 +00:00
public Set<Swimlane> getSwimlanes() {
2021-05-14 08:42:57 +00:00
final Set<Swimlane> result = new HashSet<>();
2022-05-21 09:41:00 +00:00
if (swimlane != null)
2019-04-21 20:40:01 +00:00
result.add(swimlane);
2022-05-21 09:41:00 +00:00
for (Branch branch : switches)
2019-04-21 20:40:01 +00:00
result.addAll(branch.getSwimlanes());
2022-05-21 09:41:00 +00:00
2019-04-21 20:40:01 +00:00
return Collections.unmodifiableSet(result);
}
2021-11-09 17:47:19 +00:00
@Override
2019-04-21 20:40:01 +00:00
public Swimlane getSwimlaneIn() {
return swimlane;
}
2021-11-09 17:47:19 +00:00
@Override
2019-04-21 20:40:01 +00:00
public Swimlane getSwimlaneOut() {
return swimlane;
}
2021-11-09 17:47:19 +00:00
@Override
2019-04-21 20:40:01 +00:00
public Instruction getLast() {
2021-11-09 17:47:19 +00:00
return switches.get(switches.size() - 1).getLast();
2019-04-21 20:40:01 +00:00
}
public boolean switchCase(Display labelCase, LinkRendering nextLinkRenderer) {
2021-10-27 16:43:04 +00:00
if (this.current != null)
this.current.setSpecial(nextLinkRenderer);
2020-11-21 17:33:24 +00:00
this.current = new Branch(skinParam.getCurrentStyleBuilder(), swimlane,
LinkRendering.none().withDisplay(labelCase), labelCase, null,
2023-12-11 17:34:23 +00:00
LinkRendering.none().withDisplay(labelCase), null);
2021-11-09 17:47:19 +00:00
this.switches.add(this.current);
2019-04-21 20:40:01 +00:00
return true;
}
public Instruction getParent() {
return parent;
}
public void endSwitch(LinkRendering nextLinkRenderer) {
if (this.current != null)
this.current.setSpecial(nextLinkRenderer);
2019-04-21 20:40:01 +00:00
}
2021-07-25 10:41:36 +00:00
2021-06-27 16:50:40 +00:00
@Override
public boolean addNote(Display note, NotePosition position, NoteType type, Colors colors, Swimlane swimlaneNote) {
2022-05-21 09:41:00 +00:00
if (current == null || current.isEmpty())
2021-06-27 16:50:40 +00:00
return super.addNote(note, position, type, colors, swimlaneNote);
2022-05-21 09:41:00 +00:00
else
2021-06-27 16:50:40 +00:00
return current.addNote(note, position, type, colors, swimlaneNote);
2022-05-21 09:41:00 +00:00
2021-06-27 16:50:40 +00:00
}
2019-04-21 20:40:01 +00:00
}