1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-11 04:32:26 +00:00
plantuml/src/net/sourceforge/plantuml/classdiagram/command/CommandHideShowByGender.java

221 lines
8.6 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2010-11-15 20:35:36 +00:00
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2010-11-15 20:35:36 +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
2013-12-10 19:36:50 +00:00
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
2010-11-15 20:35:36 +00:00
* 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.classdiagram.command;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.LineLocation;
2020-01-12 22:13:17 +00:00
import net.sourceforge.plantuml.OptionFlags;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.StringUtils;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.UmlDiagram;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.command.CommandExecutionResult;
2011-03-20 21:40:07 +00:00
import net.sourceforge.plantuml.command.SingleLineCommand2;
2019-06-26 19:24:49 +00:00
import net.sourceforge.plantuml.command.regex.IRegex;
2011-03-20 21:40:07 +00:00
import net.sourceforge.plantuml.command.regex.RegexConcat;
import net.sourceforge.plantuml.command.regex.RegexLeaf;
2019-06-26 19:24:49 +00:00
import net.sourceforge.plantuml.command.regex.RegexOptional;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.command.regex.RegexResult;
import net.sourceforge.plantuml.cucadiagram.Code;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.cucadiagram.EntityGender;
import net.sourceforge.plantuml.cucadiagram.EntityGenderUtils;
import net.sourceforge.plantuml.cucadiagram.EntityPortion;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.EntityUtils;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.cucadiagram.IEntity;
2020-01-12 22:13:17 +00:00
import net.sourceforge.plantuml.cucadiagram.Ident;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.LeafType;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.descdiagram.DescriptionDiagram;
2016-11-04 21:39:29 +00:00
import net.sourceforge.plantuml.objectdiagram.AbstractClassOrObjectDiagram;
2017-06-05 11:27:21 +00:00
import net.sourceforge.plantuml.sequencediagram.SequenceDiagram;
2010-11-15 20:35:36 +00:00
2016-11-04 21:39:29 +00:00
public class CommandHideShowByGender extends SingleLineCommand2<UmlDiagram> {
2010-11-15 20:35:36 +00:00
2016-11-04 21:39:29 +00:00
public CommandHideShowByGender() {
2013-12-10 19:36:50 +00:00
super(getRegexConcat());
2011-03-20 21:40:07 +00:00
}
2019-06-26 19:24:49 +00:00
static IRegex getRegexConcat() {
2019-09-14 18:12:04 +00:00
return RegexConcat
.build(CommandHideShowByGender.class.getName(),
RegexLeaf.start(), //
new RegexLeaf("COMMAND", "(hide|show)"), //
RegexLeaf.spaceOneOrMore(), //
new RegexLeaf("GENDER",
"(?:(class|object|interface|enum|annotation|abstract|[\\p{L}0-9_.]+|[%g][^%g]+[%g]|\\<\\<.*\\>\\>)[%s]+)*?"), //
new RegexOptional( //
new RegexConcat( //
new RegexLeaf("EMPTY", "(empty)"), //
RegexLeaf.spaceOneOrMore()) //
), //
new RegexLeaf("PORTION",
"(members?|attributes?|fields?|methods?|circles?|circled?|stereotypes?)"), //
RegexLeaf.end());
2010-11-15 20:35:36 +00:00
}
2019-09-14 18:12:04 +00:00
private final EntityGender emptyByGender(EntityPortion portion) {
if (portion == EntityPortion.METHOD) {
2010-11-15 20:35:36 +00:00
return EntityGenderUtils.emptyMethods();
}
2019-09-14 18:12:04 +00:00
if (portion == EntityPortion.FIELD) {
2010-11-15 20:35:36 +00:00
return EntityGenderUtils.emptyFields();
}
2019-09-14 18:12:04 +00:00
if (portion == EntityPortion.MEMBER) {
2017-07-03 17:59:53 +00:00
throw new IllegalArgumentException();
// return EntityGenderUtils.emptyMembers();
2010-11-15 20:35:36 +00:00
}
return EntityGenderUtils.all();
}
@Override
2019-03-29 22:14:07 +00:00
protected CommandExecutionResult executeArg(UmlDiagram diagram, LineLocation location, RegexResult arg) {
2017-06-05 11:27:21 +00:00
if (diagram instanceof AbstractClassOrObjectDiagram) {
return executeClassDiagram((AbstractClassOrObjectDiagram) diagram, arg);
2015-04-07 18:18:37 +00:00
}
2017-06-05 11:27:21 +00:00
if (diagram instanceof DescriptionDiagram) {
return executeDescriptionDiagram((DescriptionDiagram) diagram, arg);
}
if (diagram instanceof SequenceDiagram) {
return executeSequenceDiagram((SequenceDiagram) diagram, arg);
2013-12-10 19:36:50 +00:00
}
// Just ignored
return CommandExecutionResult.ok();
}
2017-06-05 11:27:21 +00:00
private CommandExecutionResult executeSequenceDiagram(SequenceDiagram diagram, RegexResult arg) {
2019-09-14 18:12:04 +00:00
final EntityPortion portion = getEntityPortion(arg.get("PORTION", 0));
diagram.hideOrShow(portion.asSet(), arg.get("COMMAND", 0).equalsIgnoreCase("show"));
2017-06-05 11:27:21 +00:00
return CommandExecutionResult.ok();
}
2015-04-07 18:18:37 +00:00
private CommandExecutionResult executeDescriptionDiagram(DescriptionDiagram diagram, RegexResult arg) {
2019-09-14 18:12:04 +00:00
final EntityPortion portion = getEntityPortion(arg.get("PORTION", 0));
2015-04-07 18:18:37 +00:00
final EntityGender gender;
final String arg1 = arg.get("GENDER", 0);
if (arg1 == null) {
gender = EntityGenderUtils.all();
} else if (arg1.equalsIgnoreCase("class")) {
gender = EntityGenderUtils.byEntityType(LeafType.CLASS);
2016-11-04 21:39:29 +00:00
} else if (arg1.equalsIgnoreCase("object")) {
gender = EntityGenderUtils.byEntityType(LeafType.OBJECT);
2015-04-07 18:18:37 +00:00
} else if (arg1.equalsIgnoreCase("interface")) {
gender = EntityGenderUtils.byEntityType(LeafType.INTERFACE);
} else if (arg1.equalsIgnoreCase("enum")) {
gender = EntityGenderUtils.byEntityType(LeafType.ENUM);
} else if (arg1.equalsIgnoreCase("abstract")) {
gender = EntityGenderUtils.byEntityType(LeafType.ABSTRACT_CLASS);
} else if (arg1.equalsIgnoreCase("annotation")) {
gender = EntityGenderUtils.byEntityType(LeafType.ANNOTATION);
} else if (arg1.startsWith("<<")) {
gender = EntityGenderUtils.byStereotype(arg1);
} else {
2020-01-12 22:13:17 +00:00
final IEntity entity = diagram.getOrCreateLeaf(diagram.buildLeafIdent(arg1), diagram.buildCode(arg1), null,
null);
2015-04-07 18:18:37 +00:00
gender = EntityGenderUtils.byEntityAlone(entity);
}
diagram.hideOrShow(gender, portion, arg.get("COMMAND", 0).equalsIgnoreCase("show"));
return CommandExecutionResult.ok();
}
2019-12-10 21:45:49 +00:00
private CommandExecutionResult executeClassDiagram(AbstractClassOrObjectDiagram diagram, RegexResult arg) {
2011-03-20 21:40:07 +00:00
2019-09-14 18:12:04 +00:00
final EntityPortion portion = getEntityPortion(arg.get("PORTION", 0));
2010-11-15 20:35:36 +00:00
2015-04-07 18:18:37 +00:00
EntityGender gender = null;
2013-12-10 19:36:50 +00:00
final String arg1 = arg.get("GENDER", 0);
2011-03-20 21:40:07 +00:00
if (arg1 == null) {
2010-11-15 20:35:36 +00:00
gender = EntityGenderUtils.all();
2011-03-20 21:40:07 +00:00
} else if (arg1.equalsIgnoreCase("class")) {
2013-12-10 19:36:50 +00:00
gender = EntityGenderUtils.byEntityType(LeafType.CLASS);
2016-11-04 21:39:29 +00:00
} else if (arg1.equalsIgnoreCase("object")) {
gender = EntityGenderUtils.byEntityType(LeafType.OBJECT);
2011-03-20 21:40:07 +00:00
} else if (arg1.equalsIgnoreCase("interface")) {
2013-12-10 19:36:50 +00:00
gender = EntityGenderUtils.byEntityType(LeafType.INTERFACE);
2011-03-20 21:40:07 +00:00
} else if (arg1.equalsIgnoreCase("enum")) {
2013-12-10 19:36:50 +00:00
gender = EntityGenderUtils.byEntityType(LeafType.ENUM);
2011-03-20 21:40:07 +00:00
} else if (arg1.equalsIgnoreCase("abstract")) {
2013-12-10 19:36:50 +00:00
gender = EntityGenderUtils.byEntityType(LeafType.ABSTRACT_CLASS);
} else if (arg1.equalsIgnoreCase("annotation")) {
gender = EntityGenderUtils.byEntityType(LeafType.ANNOTATION);
2011-03-20 21:40:07 +00:00
} else if (arg1.startsWith("<<")) {
gender = EntityGenderUtils.byStereotype(arg1);
2010-11-15 20:35:36 +00:00
} else {
2020-01-12 22:13:17 +00:00
final Ident ident = diagram.buildLeafIdent(arg1);
final Code code = diagram.V1972() ? ident : diagram.buildCode(arg1);
final IEntity entity = diagram.getOrCreateLeaf(ident, code, null, null);
2010-11-15 20:35:36 +00:00
gender = EntityGenderUtils.byEntityAlone(entity);
}
if (gender != null) {
2013-12-10 19:36:50 +00:00
final boolean empty = arg.get("EMPTY", 0) != null;
2019-09-14 18:12:04 +00:00
final boolean emptyMembers = empty && portion == EntityPortion.MEMBER;
2017-07-03 17:59:53 +00:00
if (empty == true && emptyMembers == false) {
2010-11-15 20:35:36 +00:00
gender = EntityGenderUtils.and(gender, emptyByGender(portion));
}
2019-12-10 21:45:49 +00:00
if (EntityUtils.groupRoot(diagram.getCurrentGroup()) == false) {
gender = EntityGenderUtils.and(gender, EntityGenderUtils.byPackage(diagram.getCurrentGroup()));
2010-11-15 20:35:36 +00:00
}
2017-07-03 17:59:53 +00:00
if (emptyMembers) {
2019-12-10 21:45:49 +00:00
diagram.hideOrShow(EntityGenderUtils.and(gender, emptyByGender(EntityPortion.FIELD)),
EntityPortion.FIELD, arg.get("COMMAND", 0).equalsIgnoreCase("show"));
diagram.hideOrShow(EntityGenderUtils.and(gender, emptyByGender(EntityPortion.METHOD)),
EntityPortion.METHOD, arg.get("COMMAND", 0).equalsIgnoreCase("show"));
2017-07-03 17:59:53 +00:00
} else {
2019-12-10 21:45:49 +00:00
diagram.hideOrShow(gender, portion, arg.get("COMMAND", 0).equalsIgnoreCase("show"));
2017-07-03 17:59:53 +00:00
}
2010-11-15 20:35:36 +00:00
}
return CommandExecutionResult.ok();
}
2019-09-14 18:12:04 +00:00
private EntityPortion getEntityPortion(String s) {
2015-04-07 18:18:37 +00:00
final String sub = StringUtils.goLowerCase(s.substring(0, 3));
2010-11-15 20:35:36 +00:00
if (sub.equals("met")) {
2019-09-14 18:12:04 +00:00
return EntityPortion.METHOD;
2010-11-15 20:35:36 +00:00
}
if (sub.equals("mem")) {
2019-09-14 18:12:04 +00:00
return EntityPortion.MEMBER;
2010-11-15 20:35:36 +00:00
}
if (sub.equals("att") || sub.equals("fie")) {
2019-09-14 18:12:04 +00:00
return EntityPortion.FIELD;
2010-11-15 20:35:36 +00:00
}
if (sub.equals("cir")) {
2019-09-14 18:12:04 +00:00
return EntityPortion.CIRCLED_CHARACTER;
2010-11-15 20:35:36 +00:00
}
if (sub.equals("ste")) {
2019-09-14 18:12:04 +00:00
return EntityPortion.STEREOTYPE;
2010-11-15 20:35:36 +00:00
}
throw new IllegalArgumentException();
}
2011-03-20 21:40:07 +00:00
2010-11-15 20:35:36 +00:00
}