plantuml/src/net/sourceforge/plantuml/project/GanttDiagram.java

862 lines
26 KiB
Java
Raw Normal View History

2017-02-01 18:55:51 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2017-02-01 18:55:51 +00:00
*
* Project Info: http://plantuml.com
2022-08-17 17:34:24 +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:
2022-08-17 17:34:24 +00:00
*
2017-03-15 19:13:31 +00:00
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
2022-08-17 17:34:24 +00:00
*
2017-02-01 18:55:51 +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
2022-08-17 17:34:24 +00:00
*
2017-02-01 18:55:51 +00:00
*
*/
2020-02-18 21:24:31 +00:00
package net.sourceforge.plantuml.project;
2017-02-01 18:55:51 +00:00
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
2018-04-06 20:36:30 +00:00
import java.util.Collection;
2020-11-21 17:33:24 +00:00
import java.util.Collections;
2017-02-26 16:26:11 +00:00
import java.util.HashMap;
2022-08-18 16:55:09 +00:00
import java.util.HashSet;
2017-02-01 18:55:51 +00:00
import java.util.LinkedHashMap;
import java.util.List;
2021-06-27 16:50:40 +00:00
import java.util.Locale;
2017-02-01 18:55:51 +00:00
import java.util.Map;
2021-05-09 21:14:40 +00:00
import java.util.Objects;
2022-08-18 16:55:09 +00:00
import java.util.Set;
2019-02-09 21:56:24 +00:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.FileFormatOption;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.TitledDiagram;
import net.sourceforge.plantuml.UmlDiagramType;
2020-09-30 20:57:58 +00:00
import net.sourceforge.plantuml.WithSprite;
2022-09-12 20:08:34 +00:00
import net.sourceforge.plantuml.awt.geom.XDimension2D;
import net.sourceforge.plantuml.awt.geom.XRectangle2D;
2019-02-09 21:56:24 +00:00
import net.sourceforge.plantuml.command.CommandExecutionResult;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.core.DiagramDescription;
import net.sourceforge.plantuml.core.ImageData;
2021-05-23 15:35:13 +00:00
import net.sourceforge.plantuml.core.UmlSource;
2020-08-25 17:24:17 +00:00
import net.sourceforge.plantuml.cucadiagram.Display;
2021-05-06 21:23:05 +00:00
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.graphic.InnerStrategy;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.UDrawable;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.log.Logme;
2020-02-18 21:24:31 +00:00
import net.sourceforge.plantuml.project.core.Moment;
import net.sourceforge.plantuml.project.core.MomentImpl;
import net.sourceforge.plantuml.project.core.PrintScale;
import net.sourceforge.plantuml.project.core.Resource;
import net.sourceforge.plantuml.project.core.Task;
import net.sourceforge.plantuml.project.core.TaskAttribute;
import net.sourceforge.plantuml.project.core.TaskCode;
2022-08-18 16:55:09 +00:00
import net.sourceforge.plantuml.project.core.TaskGroup;
2020-02-18 21:24:31 +00:00
import net.sourceforge.plantuml.project.core.TaskImpl;
import net.sourceforge.plantuml.project.core.TaskInstant;
import net.sourceforge.plantuml.project.core.TaskSeparator;
2020-09-19 15:43:24 +00:00
import net.sourceforge.plantuml.project.draw.FingerPrint;
2020-02-18 21:24:31 +00:00
import net.sourceforge.plantuml.project.draw.ResourceDraw;
import net.sourceforge.plantuml.project.draw.TaskDraw;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.project.draw.TaskDrawDiamond;
2022-08-18 16:55:09 +00:00
import net.sourceforge.plantuml.project.draw.TaskDrawGroup;
2020-02-18 21:24:31 +00:00
import net.sourceforge.plantuml.project.draw.TaskDrawRegular;
import net.sourceforge.plantuml.project.draw.TaskDrawSeparator;
import net.sourceforge.plantuml.project.draw.TimeHeader;
import net.sourceforge.plantuml.project.draw.TimeHeaderDaily;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.project.draw.TimeHeaderMonthly;
import net.sourceforge.plantuml.project.draw.TimeHeaderQuarterly;
2020-02-18 21:24:31 +00:00
import net.sourceforge.plantuml.project.draw.TimeHeaderSimple;
import net.sourceforge.plantuml.project.draw.TimeHeaderWeekly;
import net.sourceforge.plantuml.project.draw.TimeHeaderYearly;
2020-08-25 17:24:17 +00:00
import net.sourceforge.plantuml.project.lang.CenterBorderColor;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.project.time.Day;
import net.sourceforge.plantuml.project.time.DayOfWeek;
2021-04-07 18:02:23 +00:00
import net.sourceforge.plantuml.project.time.WeekNumberStrategy;
2020-02-18 21:24:31 +00:00
import net.sourceforge.plantuml.project.timescale.TimeScale;
2021-06-27 16:50:40 +00:00
import net.sourceforge.plantuml.real.Real;
import net.sourceforge.plantuml.real.RealOrigin;
import net.sourceforge.plantuml.real.RealUtils;
import net.sourceforge.plantuml.style.ClockwiseTopRightBottomLeft;
2021-01-08 01:56:09 +00:00
import net.sourceforge.plantuml.style.PName;
import net.sourceforge.plantuml.style.SName;
import net.sourceforge.plantuml.style.Style;
2022-03-01 18:11:51 +00:00
import net.sourceforge.plantuml.style.StyleSignatureBasic;
import net.sourceforge.plantuml.svek.GraphvizCrash;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.svek.TextBlockBackcolored;
import net.sourceforge.plantuml.ugraphic.MinMax;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
2021-04-07 18:02:23 +00:00
import net.sourceforge.plantuml.ugraphic.URectangle;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.ugraphic.UTranslate;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
import net.sourceforge.plantuml.ugraphic.color.HColorSet;
2022-08-19 16:34:21 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColors;
2017-02-01 18:55:51 +00:00
2020-09-30 20:57:58 +00:00
public class GanttDiagram extends TitledDiagram implements ToTaskDraw, WithSprite {
2017-02-01 18:55:51 +00:00
2020-11-21 17:33:24 +00:00
private final Map<Task, TaskDraw> draws = new LinkedHashMap<Task, TaskDraw>();
2017-02-01 18:55:51 +00:00
private final Map<TaskCode, Task> tasks = new LinkedHashMap<TaskCode, Task>();
2017-02-26 16:26:11 +00:00
private final Map<String, Task> byShortName = new HashMap<String, Task>();
2021-05-14 08:42:57 +00:00
private final List<GanttConstraint> constraints = new ArrayList<>();
2020-03-18 10:50:02 +00:00
private final HColorSet colorSet = HColorSet.instance();
2020-02-18 21:24:31 +00:00
2020-11-21 17:33:24 +00:00
private final OpenClose openClose = new OpenClose();
2020-02-18 21:24:31 +00:00
private final Map<String, Resource> resources = new LinkedHashMap<String, Resource>();
2021-05-06 21:23:05 +00:00
private final Map<Day, HColor> colorDaysToday = new HashMap<Day, HColor>();
private final Map<Day, HColor> colorDaysInternal = new HashMap<Day, HColor>();
private final Map<DayOfWeek, HColor> colorDaysOfWeek = new HashMap<DayOfWeek, HColor>();
2020-03-18 10:50:02 +00:00
private final Map<Day, String> nameDays = new HashMap<Day, String>();
2021-05-06 21:23:05 +00:00
private LabelStrategy labelStrategy = new LabelStrategy(LabelPosition.LEGACY, HorizontalAlignment.LEFT);
2020-02-18 21:24:31 +00:00
2021-04-07 18:02:23 +00:00
// Let's follow ISO-8601 rules
private WeekNumberStrategy weekNumberStrategy = new WeekNumberStrategy(DayOfWeek.MONDAY, 4);
2020-02-18 21:24:31 +00:00
private PrintScale printScale = PrintScale.DAILY;
2021-04-07 18:02:23 +00:00
private double factorScale = 1.0;
2021-06-27 16:50:40 +00:00
private Locale locale = Locale.ENGLISH;
2021-04-07 18:02:23 +00:00
2020-03-18 10:50:02 +00:00
private Day today;
2020-11-21 17:33:24 +00:00
private double totalHeightWithoutFooter;
2020-09-30 20:57:58 +00:00
private Day min = Day.create(0);
private Day max;
2017-02-26 16:26:11 +00:00
2020-03-18 10:50:02 +00:00
private Day printStart;
private Day printEnd;
2017-02-01 18:55:51 +00:00
2021-06-27 16:50:40 +00:00
private final RealOrigin origin = RealUtils.createOrigin();
public CommandExecutionResult changeLanguage(String lang) {
this.locale = new Locale(lang);
return CommandExecutionResult.ok();
}
2017-02-01 18:55:51 +00:00
public DiagramDescription getDescription() {
2017-03-12 17:22:02 +00:00
return new DiagramDescription("(Project)");
2017-02-01 18:55:51 +00:00
}
2021-04-07 18:02:23 +00:00
public void setWeekNumberStrategy(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek) {
this.weekNumberStrategy = new WeekNumberStrategy(firstDayOfWeek, minimalDaysInFirstWeek);
}
2022-09-18 17:08:06 +00:00
public GanttDiagram(UmlSource source) {
super(source, UmlDiagramType.GANTT, null);
2020-08-25 17:24:17 +00:00
}
2018-04-06 20:36:30 +00:00
public final int getDpi(FileFormatOption fileFormatOption) {
return 96;
}
2017-02-01 18:55:51 +00:00
@Override
2021-03-23 13:06:33 +00:00
protected ImageData exportDiagramNow(OutputStream os, int index, FileFormatOption fileFormatOption)
2017-02-01 18:55:51 +00:00
throws IOException {
2020-12-06 21:43:09 +00:00
final StringBounder stringBounder = fileFormatOption.getDefaultStringBounder(getSkinParam());
2021-04-07 18:02:23 +00:00
return createImageBuilder(fileFormatOption).drawable(getTextBlock(stringBounder)).write(os);
2017-02-01 18:55:51 +00:00
}
2020-02-18 21:24:31 +00:00
public void setPrintScale(PrintScale printScale) {
this.printScale = printScale;
}
2021-04-07 18:02:23 +00:00
public void setFactorScale(double factorScale) {
this.factorScale = factorScale;
}
2021-04-04 10:31:53 +00:00
2021-04-07 18:02:23 +00:00
private double getFactorScale() {
return this.printScale.getDefaultScale() * this.factorScale;
}
2021-04-04 10:31:53 +00:00
2020-03-18 10:50:02 +00:00
private boolean isHidden(Task task) {
2022-03-07 19:33:46 +00:00
if (printStart == null || task instanceof TaskSeparator)
2020-03-18 10:50:02 +00:00
return false;
2022-03-07 19:33:46 +00:00
if (task.getEnd().compareTo(min) < 0)
2020-03-18 10:50:02 +00:00
return true;
2022-03-07 19:33:46 +00:00
if (task.getStart().compareTo(max) > 0)
2020-03-18 10:50:02 +00:00
return true;
2022-03-07 19:33:46 +00:00
2020-03-18 10:50:02 +00:00
return false;
}
2020-09-19 15:43:24 +00:00
private TextBlockBackcolored getTextBlock(StringBounder stringBounder) {
2020-02-18 21:24:31 +00:00
if (printStart == null) {
initMinMax();
} else {
2020-09-30 20:57:58 +00:00
this.min = printStart;
this.max = printEnd;
2020-02-18 21:24:31 +00:00
}
2020-11-21 17:33:24 +00:00
final TimeHeader timeHeader = getTimeHeader();
2020-09-19 15:43:24 +00:00
initTaskAndResourceDraws(timeHeader.getTimeScale(), timeHeader.getFullHeaderHeight(), stringBounder);
2019-04-21 20:40:01 +00:00
return new TextBlockBackcolored() {
2017-02-01 18:55:51 +00:00
public void drawU(UGraphic ug) {
try {
final UGraphic ugOrig = ug;
2021-05-06 21:23:05 +00:00
if (labelStrategy.titleInFirstColumn())
ug = ug.apply(UTranslate.dx(getTitlesColumnWidth(ug.getStringBounder())));
2022-03-01 18:11:51 +00:00
final Style timelineStyle = StyleSignatureBasic
.of(SName.root, SName.element, SName.ganttDiagram, SName.timeline)
.getMergedStyle(getCurrentStyleBuilder());
2021-05-06 21:23:05 +00:00
final HColor back = timelineStyle.value(PName.BackGroundColor)
2022-09-18 17:08:06 +00:00
.asColor(getIHtmlColorSet());
2022-09-15 17:24:26 +00:00
if (back.isTransparent() == false) {
final URectangle rect1 = new URectangle(calculateDimension(ug.getStringBounder()).getWidth(),
timeHeader.getTimeHeaderHeight());
ug.apply(back.bg()).draw(rect1);
2021-06-27 16:50:40 +00:00
if (showFootbox) {
final URectangle rect2 = new URectangle(
calculateDimension(ug.getStringBounder()).getWidth(),
timeHeader.getTimeFooterHeight());
ug.apply(back.bg()).apply(UTranslate.dy(totalHeightWithoutFooter)).draw(rect2);
}
}
timeHeader.drawTimeHeader(ug, totalHeightWithoutFooter);
drawConstraints(ug, timeHeader.getTimeScale());
drawTasksRect(ug);
2021-05-06 21:23:05 +00:00
drawTasksTitle(ugOrig, getTitlesColumnWidth(ug.getStringBounder()), getBarsColumnWidth(timeHeader));
2022-01-03 17:35:17 +00:00
2022-10-05 20:32:57 +00:00
if (hideResourceFoobox == false)
2022-01-03 17:35:17 +00:00
drawResources(ug);
if (showFootbox)
timeHeader.drawTimeFooter(ug.apply(UTranslate.dy(totalHeightWithoutFooter)));
2022-01-03 17:35:17 +00:00
} catch (Throwable t) {
2022-08-17 17:34:24 +00:00
Logme.error(t);
final UDrawable crash = new GraphvizCrash(getSource().getPlainString(), false, t);
crash.drawU(ug);
2021-04-07 18:02:23 +00:00
2020-11-21 17:33:24 +00:00
}
2017-02-01 18:55:51 +00:00
}
2019-04-21 20:40:01 +00:00
2021-05-06 21:23:05 +00:00
private double getTitlesColumnWidth(StringBounder stringBounder) {
2022-03-07 19:33:46 +00:00
if (labelStrategy.titleInside())
return 0;
2022-03-07 19:33:46 +00:00
2021-05-06 21:23:05 +00:00
double width = 0;
for (Task task : tasks.values()) {
2022-03-07 19:33:46 +00:00
if (isHidden(task))
2021-05-06 21:23:05 +00:00
continue;
2022-03-07 19:33:46 +00:00
2021-05-06 21:23:05 +00:00
width = Math.max(width, draws.get(task).getTitleWidth(stringBounder));
}
return width;
}
2022-09-12 20:08:34 +00:00
public XRectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
2019-04-21 20:40:01 +00:00
return null;
}
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
return new XDimension2D(getTitlesColumnWidth(stringBounder) + getBarsColumnWidth(timeHeader),
2021-05-06 21:23:05 +00:00
getTotalHeight(timeHeader));
}
private double getBarsColumnWidth(final TimeHeader timeHeader) {
2020-02-18 21:24:31 +00:00
final double xmin = timeHeader.getTimeScale().getStartingPosition(min);
final double xmax = timeHeader.getTimeScale().getEndingPosition(max);
2021-05-06 21:23:05 +00:00
return xmax - xmin;
2019-04-21 20:40:01 +00:00
}
public MinMax getMinMax(StringBounder stringBounder) {
throw new UnsupportedOperationException();
}
2020-03-18 10:50:02 +00:00
public HColor getBackcolor() {
2019-04-21 20:40:01 +00:00
return null;
}
2017-02-01 18:55:51 +00:00
};
}
2020-11-21 17:33:24 +00:00
private TimeHeader getTimeHeader() {
2022-08-17 17:34:24 +00:00
if (openClose.getStartingDay() == null)
2022-08-18 16:55:09 +00:00
return new TimeHeaderSimple(thParam(), printScale);
2022-03-07 19:33:46 +00:00
else if (printScale == PrintScale.DAILY)
2022-08-18 16:55:09 +00:00
return new TimeHeaderDaily(thParam(), nameDays, printStart, printEnd);
2022-03-07 19:33:46 +00:00
else if (printScale == PrintScale.WEEKLY)
2022-08-18 16:55:09 +00:00
return new TimeHeaderWeekly(thParam(), weekNumberStrategy, withCalendarDate);
2022-03-07 19:33:46 +00:00
else if (printScale == PrintScale.MONTHLY)
2022-08-18 16:55:09 +00:00
return new TimeHeaderMonthly(thParam());
2022-03-07 19:33:46 +00:00
else if (printScale == PrintScale.QUARTERLY)
2022-08-18 16:55:09 +00:00
return new TimeHeaderQuarterly(thParam());
2022-03-07 19:33:46 +00:00
else if (printScale == PrintScale.YEARLY)
2022-08-18 16:55:09 +00:00
return new TimeHeaderYearly(thParam());
2022-03-07 19:33:46 +00:00
else
2021-05-06 21:23:05 +00:00
throw new IllegalStateException();
2022-03-07 19:33:46 +00:00
2020-11-21 17:33:24 +00:00
}
2022-08-18 16:55:09 +00:00
private TimeHeaderParameters thParam() {
2022-09-18 17:08:06 +00:00
return new TimeHeaderParameters(colorDays(), getFactorScale(), min, max, getIHtmlColorSet(),
getTimelineStyle(), getClosedStyle(), locale, openClose, colorDaysOfWeek, verticalSeparatorBefore);
2022-08-18 16:55:09 +00:00
}
2021-05-06 21:23:05 +00:00
private Map<Day, HColor> colorDays() {
colorDaysInternal.putAll(colorDaysToday);
return Collections.unmodifiableMap(colorDaysInternal);
}
2021-04-25 20:59:17 +00:00
private Style getClosedStyle() {
2022-03-01 18:11:51 +00:00
return StyleSignatureBasic.of(SName.root, SName.element, SName.ganttDiagram, SName.closed)
2021-04-25 20:59:17 +00:00
.getMergedStyle(getCurrentStyleBuilder());
}
2021-06-27 16:50:40 +00:00
private Style getTimelineStyle() {
2022-03-01 18:11:51 +00:00
return StyleSignatureBasic.of(SName.root, SName.element, SName.ganttDiagram, SName.timeline)
2021-06-27 16:50:40 +00:00
.getMergedStyle(getCurrentStyleBuilder());
}
2020-11-21 17:33:24 +00:00
private double getTotalHeight(TimeHeader timeHeader) {
2022-03-07 19:33:46 +00:00
if (showFootbox)
2020-11-21 17:33:24 +00:00
return totalHeightWithoutFooter + timeHeader.getTimeFooterHeight();
2022-03-07 19:33:46 +00:00
2020-11-21 17:33:24 +00:00
return totalHeightWithoutFooter;
}
2020-02-18 21:24:31 +00:00
private void drawTasksRect(UGraphic ug) {
for (Task task : tasks.values()) {
2022-03-07 19:33:46 +00:00
if (isHidden(task))
2020-11-21 17:33:24 +00:00
continue;
2022-03-07 19:33:46 +00:00
2020-03-18 10:50:02 +00:00
final TaskDraw draw = draws.get(task);
2021-06-27 16:50:40 +00:00
final UTranslate move = UTranslate.dy(draw.getY(ug.getStringBounder()).getCurrentValue());
2020-02-18 21:24:31 +00:00
draw.drawU(ug.apply(move));
}
}
private void drawConstraints(final UGraphic ug, TimeScale timeScale) {
for (GanttConstraint constraint : constraints) {
2022-03-07 19:33:46 +00:00
if (printStart != null && constraint.isHidden(min, max))
2020-03-18 10:50:02 +00:00
continue;
2022-03-07 19:33:46 +00:00
2021-04-07 18:02:23 +00:00
constraint.getUDrawable(timeScale, this).drawU(ug);
2018-04-06 20:36:30 +00:00
}
2021-01-10 20:52:19 +00:00
2017-03-12 17:22:02 +00:00
}
2022-03-01 18:11:51 +00:00
public StyleSignatureBasic getDefaultStyleDefinitionArrow() {
return StyleSignatureBasic.of(SName.root, SName.element, SName.ganttDiagram, SName.arrow);
2021-01-08 01:56:09 +00:00
}
2021-05-06 21:23:05 +00:00
private void drawTasksTitle(UGraphic ug, double colTitles, double colBars) {
2020-02-18 21:24:31 +00:00
for (Task task : tasks.values()) {
2022-03-07 19:33:46 +00:00
if (isHidden(task))
2020-03-18 10:50:02 +00:00
continue;
2022-03-07 19:33:46 +00:00
2020-03-18 10:50:02 +00:00
final TaskDraw draw = draws.get(task);
2021-06-27 16:50:40 +00:00
final UTranslate move = UTranslate.dy(draw.getY(ug.getStringBounder()).getCurrentValue());
2021-05-06 21:23:05 +00:00
draw.drawTitle(ug.apply(move), labelStrategy, colTitles, colBars);
2020-02-18 21:24:31 +00:00
}
}
private void drawResources(UGraphic ug) {
for (Resource res : resources.values()) {
final ResourceDraw draw = res.getResourceDraw();
2020-03-18 10:50:02 +00:00
final UTranslate move = UTranslate.dy(draw.getY());
2020-02-18 21:24:31 +00:00
draw.drawU(ug.apply(move));
}
2018-04-06 20:36:30 +00:00
}
2022-08-19 16:34:21 +00:00
public void closeDayOfWeek(DayOfWeek day, String task) {
2020-11-21 17:33:24 +00:00
openClose.close(day);
2018-12-22 11:11:40 +00:00
}
2022-08-19 16:34:21 +00:00
public void openDayOfWeek(DayOfWeek day, String task) {
if (task.length() == 0)
openClose.open(day);
else
getOpenCloseForTask(task).open(day);
}
2022-08-17 17:34:24 +00:00
public void closeDayAsDate(Day day, String task) {
if (task.length() == 0)
openClose.close(day);
else
getOpenCloseForTask(task).close(day);
}
public void openDayAsDate(Day day, String task) {
if (task.length() == 0)
openClose.open(day);
else
getOpenCloseForTask(task).open(day);
2018-12-22 11:11:40 +00:00
}
2022-08-17 17:34:24 +00:00
private OpenClose getOpenCloseForTask(String task) {
OpenClose except = openCloseForTask.get(task);
if (except == null) {
except = new OpenClose();
openCloseForTask.put(task, except);
}
return except;
2018-12-22 11:11:40 +00:00
}
2022-08-17 17:34:24 +00:00
private final Map<String, OpenClose> openCloseForTask = new HashMap<>();
2020-09-19 15:43:24 +00:00
private void initTaskAndResourceDraws(TimeScale timeScale, double headerHeight, StringBounder stringBounder) {
2021-06-27 16:50:40 +00:00
Real y = origin.addFixed(headerHeight);
2017-02-26 16:26:11 +00:00
for (Task task : tasks.values()) {
2018-04-06 20:36:30 +00:00
final TaskDraw draw;
if (task instanceof TaskSeparator) {
2022-08-18 16:55:09 +00:00
final TaskSeparator taskSeparator = (TaskSeparator) task;
draw = new TaskDrawSeparator(taskSeparator.getName(), timeScale, y, min, max, task.getStyleBuilder(),
getSkinParam().getIHtmlColorSet());
} else if (task instanceof TaskGroup) {
final TaskGroup taskGroup = (TaskGroup) task;
draw = new TaskDrawGroup(timeScale, y, taskGroup.getCode().getSimpleDisplay(), getStart(taskGroup),
getEnd(taskGroup), getSkinParam(), task, this, task.getStyleBuilder());
2018-04-06 20:36:30 +00:00
} else {
2020-03-18 10:50:02 +00:00
final TaskImpl tmp = (TaskImpl) task;
2022-10-05 20:32:57 +00:00
final String disp = hideResourceName ? tmp.getCode().getSimpleDisplay() : tmp.getPrettyDisplay();
2020-03-18 10:50:02 +00:00
if (tmp.isDiamond()) {
2022-01-03 13:38:18 +00:00
draw = new TaskDrawDiamond(timeScale, y, disp, getStart(tmp), getSkinParam(), task, this,
2022-08-18 16:55:09 +00:00
task.getStyleBuilder());
2020-03-18 10:50:02 +00:00
} else {
final boolean oddStart = printStart != null && min.compareTo(getStart(tmp)) == 0;
final boolean oddEnd = printStart != null && max.compareTo(getEnd(tmp)) == 0;
2022-01-03 13:38:18 +00:00
draw = new TaskDrawRegular(timeScale, y, disp, getStart(tmp), getEnd(tmp), oddStart, oddEnd,
2022-08-18 16:55:09 +00:00
getSkinParam(), task, this, getConstraints(task), task.getStyleBuilder());
2020-03-18 10:50:02 +00:00
}
2020-08-25 17:24:17 +00:00
draw.setColorsAndCompletion(tmp.getColors(), tmp.getCompletion(), tmp.getUrl(), tmp.getNote());
}
2022-08-18 16:55:09 +00:00
if (task.getRow() == null)
2021-06-27 16:50:40 +00:00
y = y.addAtLeast(draw.getFullHeightTask(stringBounder));
2022-08-18 16:55:09 +00:00
2020-03-18 10:50:02 +00:00
draws.put(task, draw);
2018-04-06 20:36:30 +00:00
}
2021-06-27 16:50:40 +00:00
origin.compileNow();
magicPush(stringBounder);
double yy = lastY(stringBounder);
if (yy == 0) {
yy = headerHeight;
2022-10-05 20:32:57 +00:00
} else if (this.hideResourceFoobox == false)
2020-09-30 20:57:58 +00:00
for (Resource res : resources.values()) {
2021-06-27 16:50:40 +00:00
final ResourceDraw draw = new ResourceDraw(this, res, timeScale, yy, min, max);
2020-09-30 20:57:58 +00:00
res.setTaskDraw(draw);
2021-06-27 16:50:40 +00:00
yy += draw.getHeight();
2020-09-30 20:57:58 +00:00
}
2022-01-03 17:35:17 +00:00
2021-06-27 16:50:40 +00:00
this.totalHeightWithoutFooter = yy;
2020-11-21 17:33:24 +00:00
}
private Collection<GanttConstraint> getConstraints(Task task) {
2021-05-14 08:42:57 +00:00
final List<GanttConstraint> result = new ArrayList<>();
2020-11-21 17:33:24 +00:00
for (GanttConstraint constraint : constraints) {
2022-03-07 19:33:46 +00:00
if (constraint.isOn(task))
2020-11-21 17:33:24 +00:00
result.add(constraint);
2022-03-07 19:33:46 +00:00
2020-11-21 17:33:24 +00:00
}
return Collections.unmodifiableCollection(result);
2020-09-19 15:43:24 +00:00
}
2018-04-06 20:36:30 +00:00
2020-09-30 20:57:58 +00:00
private double lastY(StringBounder stringBounder) {
2020-11-21 17:33:24 +00:00
double result = 0;
2022-03-07 19:33:46 +00:00
for (TaskDraw td : draws.values())
2021-06-27 16:50:40 +00:00
result = Math.max(result, td.getY(stringBounder).getCurrentValue() + td.getHeightMax(stringBounder));
2022-03-07 19:33:46 +00:00
2020-11-21 17:33:24 +00:00
return result;
2020-09-19 15:43:24 +00:00
}
2021-06-27 16:50:40 +00:00
private void magicPush(StringBounder stringBounder) {
final List<TaskDraw> notes = new ArrayList<>();
2020-09-19 15:43:24 +00:00
for (TaskDraw td : draws.values()) {
2021-04-07 18:02:23 +00:00
final FingerPrint taskPrint = td.getFingerPrint(stringBounder);
2021-06-27 16:50:40 +00:00
final FingerPrint fingerPrintNote = td.getFingerPrintNote(stringBounder);
if (td.getTrueRow() == null)
for (TaskDraw note : notes) {
final FingerPrint otherNote = note.getFingerPrintNote(stringBounder);
final double deltaY = otherNote.overlap(taskPrint);
if (deltaY > 0) {
final Real bottom = note.getY(stringBounder).addAtLeast(note.getHeightMax(stringBounder));
td.getY(stringBounder).ensureBiggerThan(bottom);
origin.compileNow();
}
2020-09-19 15:43:24 +00:00
}
2022-03-07 19:33:46 +00:00
if (fingerPrintNote != null)
2021-06-27 16:50:40 +00:00
notes.add(td);
2022-03-07 19:33:46 +00:00
2020-09-19 15:43:24 +00:00
}
2017-02-26 16:26:11 +00:00
}
2022-08-18 16:55:09 +00:00
private Day getStart(final Task tmp) {
if (printStart == null)
2020-03-18 10:50:02 +00:00
return tmp.getStart();
2022-08-18 16:55:09 +00:00
2020-09-30 20:57:58 +00:00
return Day.max(min, tmp.getStart());
2020-03-18 10:50:02 +00:00
}
2022-08-18 16:55:09 +00:00
private Day getEnd(final Task tmp) {
if (printStart == null)
2020-03-18 10:50:02 +00:00
return tmp.getEnd();
2022-08-18 16:55:09 +00:00
2020-09-30 20:57:58 +00:00
return Day.min(max, tmp.getEnd());
2020-03-18 10:50:02 +00:00
}
2017-02-26 16:26:11 +00:00
private void initMinMax() {
2018-11-26 18:46:22 +00:00
if (tasks.size() == 0) {
max = min.increment();
} else {
2019-03-29 22:14:07 +00:00
max = null;
2018-11-26 18:46:22 +00:00
for (Task task : tasks.values()) {
2022-08-18 16:55:09 +00:00
if (task instanceof TaskSeparator || task instanceof TaskGroup)
2018-11-26 18:46:22 +00:00
continue;
2022-03-07 19:33:46 +00:00
2020-09-30 20:57:58 +00:00
final Day start = task.getStart();
final Day end = task.getEnd();
2018-11-26 18:46:22 +00:00
// if (min.compareTo(start) > 0) {
// min = start;
// }
2022-03-07 19:33:46 +00:00
if (max == null || max.compareTo(end) < 0)
2018-11-26 18:46:22 +00:00
max = end;
2022-03-07 19:33:46 +00:00
2018-11-26 18:46:22 +00:00
}
}
2022-08-17 17:34:24 +00:00
if (openClose.getStartingDay() != null) {
2022-03-07 19:33:46 +00:00
for (Day d : colorDays().keySet())
if (d.compareTo(max) > 0)
2020-09-30 20:57:58 +00:00
max = d;
2022-03-07 19:33:46 +00:00
for (Day d : nameDays.keySet())
if (d.compareTo(max) > 0)
2020-09-30 20:57:58 +00:00
max = d;
2022-03-07 19:33:46 +00:00
2017-02-26 16:26:11 +00:00
}
}
2020-03-18 10:50:02 +00:00
public Day getThenDate() {
Day result = getStartingDate();
2022-03-07 19:33:46 +00:00
for (Day d : colorDays().keySet())
if (d.compareTo(result) > 0)
2018-11-26 18:46:22 +00:00
result = d;
2022-03-07 19:33:46 +00:00
for (Day d : nameDays.keySet())
if (d.compareTo(result) > 0)
2018-11-26 18:46:22 +00:00
result = d;
2022-03-07 19:33:46 +00:00
2018-11-26 18:46:22 +00:00
return result;
}
2017-02-26 16:26:11 +00:00
public Task getExistingTask(String id) {
2021-05-14 08:42:57 +00:00
final Task result = byShortName.get(Objects.requireNonNull(id));
2022-03-07 19:33:46 +00:00
if (result != null)
2017-02-26 16:26:11 +00:00
return result;
2022-03-07 19:33:46 +00:00
2017-02-26 16:26:11 +00:00
final TaskCode code = new TaskCode(id);
return tasks.get(code);
}
2020-08-25 17:24:17 +00:00
public GanttConstraint forceTaskOrder(Task task1, Task task2) {
2018-04-06 20:36:30 +00:00
final TaskInstant end1 = new TaskInstant(task1, TaskAttribute.END);
task2.setStart(end1.getInstantPrecise());
2021-04-07 18:02:23 +00:00
final GanttConstraint result = new GanttConstraint(this.getIHtmlColorSet(),
getSkinParam().getCurrentStyleBuilder(), end1, new TaskInstant(task2, TaskAttribute.START));
2020-08-25 17:24:17 +00:00
addContraint(result);
return result;
2018-04-06 20:36:30 +00:00
}
public Task getOrCreateTask(String codeOrShortName, String shortName, boolean linkedToPrevious) {
2021-05-09 21:14:40 +00:00
Objects.requireNonNull(codeOrShortName);
2017-02-26 16:26:11 +00:00
Task result = shortName == null ? null : byShortName.get(shortName);
2022-03-07 19:33:46 +00:00
if (result != null)
2017-02-26 16:26:11 +00:00
return result;
2022-03-07 19:33:46 +00:00
2017-02-26 16:26:11 +00:00
result = byShortName.get(codeOrShortName);
2022-03-07 19:33:46 +00:00
if (result != null)
2017-02-26 16:26:11 +00:00
return result;
2022-03-07 19:33:46 +00:00
2017-02-26 16:26:11 +00:00
final TaskCode code = new TaskCode(codeOrShortName);
result = tasks.get(code);
2017-02-01 18:55:51 +00:00
if (result == null) {
2018-04-06 20:36:30 +00:00
Task previous = null;
2022-03-07 19:33:46 +00:00
if (linkedToPrevious)
2018-04-06 20:36:30 +00:00
previous = getLastCreatedTask();
2022-03-07 19:33:46 +00:00
2022-08-17 17:34:24 +00:00
final OpenClose except = this.openCloseForTask.get(codeOrShortName);
result = new TaskImpl(getSkinParam().getCurrentStyleBuilder(), code, openClose.mutateMe(except),
openClose.getStartingDay());
2022-08-18 16:55:09 +00:00
if (currentGroup != null)
currentGroup.addTask(result);
2017-02-01 18:55:51 +00:00
tasks.put(code, result);
2022-03-07 19:33:46 +00:00
if (byShortName != null)
2017-02-26 16:26:11 +00:00
byShortName.put(shortName, result);
2022-03-07 19:33:46 +00:00
if (previous != null)
2020-08-25 17:24:17 +00:00
forceTaskOrder(previous, result);
2022-03-07 19:33:46 +00:00
2017-02-01 18:55:51 +00:00
}
return result;
}
2018-04-06 20:36:30 +00:00
private Task getLastCreatedTask() {
2021-05-14 08:42:57 +00:00
final List<Task> all = new ArrayList<>(tasks.values());
2022-03-07 19:33:46 +00:00
for (int i = all.size() - 1; i >= 0; i--)
if (all.get(i) instanceof TaskImpl)
2018-04-06 20:36:30 +00:00
return all.get(i);
2022-03-07 19:33:46 +00:00
2018-04-06 20:36:30 +00:00
return null;
}
public void addSeparator(String comment) {
2021-04-07 18:02:23 +00:00
TaskSeparator separator = new TaskSeparator(getSkinParam().getCurrentStyleBuilder(), comment, tasks.size());
2018-04-06 20:36:30 +00:00
tasks.put(separator.getCode(), separator);
}
2022-08-18 16:55:09 +00:00
private TaskGroup currentGroup = null;
2022-08-19 16:34:21 +00:00
public CommandExecutionResult addGroup(String name) {
TaskGroup group = new TaskGroup(this.currentGroup, getSkinParam().getCurrentStyleBuilder(), name);
if (this.currentGroup != null)
this.currentGroup.addTask(group);
this.currentGroup = group;
2022-08-18 16:55:09 +00:00
tasks.put(group.getCode(), group);
2022-08-19 16:34:21 +00:00
return CommandExecutionResult.ok();
}
public CommandExecutionResult endGroup() {
if (this.currentGroup == null)
return CommandExecutionResult.error("No group to be closed");
this.currentGroup = this.currentGroup.getParent();
return CommandExecutionResult.ok();
2022-08-18 16:55:09 +00:00
}
2017-02-01 18:55:51 +00:00
public void addContraint(GanttConstraint constraint) {
constraints.add(constraint);
}
2020-03-18 10:50:02 +00:00
public HColorSet getIHtmlColorSet() {
2017-02-01 18:55:51 +00:00
return colorSet;
}
2020-03-18 10:50:02 +00:00
public void setStartingDate(Day start) {
2022-08-17 17:34:24 +00:00
openClose.setStartingDay(start);
2020-09-30 20:57:58 +00:00
this.min = start;
2017-02-26 16:26:11 +00:00
}
2020-03-18 10:50:02 +00:00
public Day getStartingDate() {
2022-08-17 17:34:24 +00:00
if (openClose.getStartingDay() == null)
return min;
2022-03-07 19:33:46 +00:00
2022-08-17 17:34:24 +00:00
return openClose.getStartingDay();
2019-02-09 21:56:24 +00:00
}
2018-11-26 18:46:22 +00:00
public int daysInWeek() {
2020-11-21 17:33:24 +00:00
return openClose.daysInWeek();
2018-11-26 18:46:22 +00:00
}
2020-03-18 10:50:02 +00:00
public boolean isOpen(Day day) {
2020-11-21 17:33:24 +00:00
return openClose.getLoadAt(day) > 0;
2018-06-25 19:05:58 +00:00
}
2021-01-10 20:52:19 +00:00
public boolean affectResource(Task result, String description) {
2019-02-09 21:56:24 +00:00
final Pattern p = Pattern.compile("([^:]+)(:(\\d+))?");
final Matcher m = p.matcher(description);
2022-03-07 19:33:46 +00:00
if (m.find() == false)
2019-02-09 21:56:24 +00:00
throw new IllegalArgumentException();
2022-03-07 19:33:46 +00:00
2019-02-09 21:56:24 +00:00
final Resource resource = getResource(m.group(1));
int percentage = 100;
2022-03-07 19:33:46 +00:00
if (m.group(3) != null)
2019-02-09 21:56:24 +00:00
percentage = Integer.parseInt(m.group(3));
2022-03-07 19:33:46 +00:00
if (percentage == 0)
2021-01-10 20:52:19 +00:00
return false;
2022-03-07 19:33:46 +00:00
2019-02-09 21:56:24 +00:00
result.addResource(resource, percentage);
2021-01-10 20:52:19 +00:00
return true;
2018-04-06 20:36:30 +00:00
}
public Resource getResource(String resourceName) {
Resource resource = resources.get(resourceName);
2022-03-07 19:33:46 +00:00
if (resource == null)
2020-11-21 17:33:24 +00:00
resource = new Resource(resourceName);
2022-03-07 19:33:46 +00:00
2018-04-06 20:36:30 +00:00
resources.put(resourceName, resource);
return resource;
}
2020-09-30 20:57:58 +00:00
public int getLoadForResource(Resource res, Day i) {
2018-04-06 20:36:30 +00:00
int result = 0;
for (Task task : tasks.values()) {
2022-03-07 19:33:46 +00:00
if (task instanceof TaskSeparator)
2018-04-06 20:36:30 +00:00
continue;
2022-03-07 19:33:46 +00:00
2018-04-06 20:36:30 +00:00
final TaskImpl task2 = (TaskImpl) task;
result += task2.loadForResource(res, i);
}
return result;
}
2018-11-26 18:46:22 +00:00
public Moment getExistingMoment(String id) {
Moment result = getExistingTask(id);
if (result == null) {
2020-03-18 10:50:02 +00:00
Day start = null;
Day end = null;
for (Map.Entry<Day, String> ent : nameDays.entrySet()) {
2022-03-07 19:33:46 +00:00
if (ent.getValue().equalsIgnoreCase(id) == false)
2018-11-26 18:46:22 +00:00
continue;
2022-03-07 19:33:46 +00:00
2018-11-26 18:46:22 +00:00
start = min(start, ent.getKey());
end = max(end, ent.getKey());
}
2022-03-07 19:33:46 +00:00
if (start != null)
2020-09-30 20:57:58 +00:00
result = new MomentImpl(start, end);
2022-03-07 19:33:46 +00:00
2018-11-26 18:46:22 +00:00
}
return result;
}
2020-03-18 10:50:02 +00:00
private Day min(Day d1, Day d2) {
2022-03-07 19:33:46 +00:00
if (d1 == null)
2018-11-26 18:46:22 +00:00
return d2;
2022-03-07 19:33:46 +00:00
if (d1.compareTo(d2) > 0)
2018-11-26 18:46:22 +00:00
return d2;
2022-03-07 19:33:46 +00:00
2018-11-26 18:46:22 +00:00
return d1;
}
2020-03-18 10:50:02 +00:00
private Day max(Day d1, Day d2) {
2022-03-07 19:33:46 +00:00
if (d1 == null)
2018-11-26 18:46:22 +00:00
return d2;
2022-03-07 19:33:46 +00:00
if (d1.compareTo(d2) < 0)
2018-11-26 18:46:22 +00:00
return d2;
2022-03-07 19:33:46 +00:00
2018-11-26 18:46:22 +00:00
return d1;
}
2020-03-18 10:50:02 +00:00
public void colorDay(Day day, HColor color) {
2021-05-06 21:23:05 +00:00
colorDaysInternal.put(day, color);
2018-06-25 19:05:58 +00:00
}
public void colorDay(DayOfWeek day, HColor color) {
colorDaysOfWeek.put(day, color);
}
2021-01-10 20:52:19 +00:00
2020-03-18 10:50:02 +00:00
public void nameDay(Day day, String name) {
2018-06-25 19:05:58 +00:00
nameDays.put(day, name);
}
2020-08-25 17:24:17 +00:00
public void setTodayColors(CenterBorderColor colors) {
2022-03-07 19:33:46 +00:00
if (today == null)
2020-03-18 10:50:02 +00:00
this.today = Day.today();
2022-03-07 19:33:46 +00:00
2021-05-06 21:23:05 +00:00
colorDaysToday.put(today, colors.getCenter());
2019-02-09 21:56:24 +00:00
}
2020-03-18 10:50:02 +00:00
public CommandExecutionResult setToday(Day date) {
2019-02-09 21:56:24 +00:00
this.today = date;
return CommandExecutionResult.ok();
}
2019-03-29 22:14:07 +00:00
public CommandExecutionResult deleteTask(Task task) {
2022-08-19 16:34:21 +00:00
task.setColors(new CenterBorderColor(HColors.WHITE, HColors.BLACK));
2019-03-29 22:14:07 +00:00
return CommandExecutionResult.ok();
}
2020-03-18 10:50:02 +00:00
public void setPrintInterval(Day start, Day end) {
2020-02-18 21:24:31 +00:00
this.printStart = start;
this.printEnd = end;
}
2020-08-25 17:24:17 +00:00
public TaskDraw getTaskDraw(Task task) {
return draws.get(task);
}
public CommandExecutionResult addNote(Display note) {
Task last = null;
for (Task current : tasks.values())
last = current;
2022-03-07 19:33:46 +00:00
if (last == null)
2020-08-25 17:24:17 +00:00
return CommandExecutionResult.error("No task defined");
2022-03-07 19:33:46 +00:00
2020-08-25 17:24:17 +00:00
last.setNote(note);
return CommandExecutionResult.ok();
}
2020-11-21 17:33:24 +00:00
public LoadPlanable getDefaultPlan() {
return openClose;
}
private boolean showFootbox = true;
public void setShowFootbox(boolean footbox) {
this.showFootbox = footbox;
}
@Override
public ClockwiseTopRightBottomLeft getDefaultMargins() {
return ClockwiseTopRightBottomLeft.none();
}
2021-04-07 18:02:23 +00:00
2021-05-06 21:23:05 +00:00
public void setLabelStrategy(LabelStrategy strategy) {
this.labelStrategy = strategy;
}
private boolean withCalendarDate;
public void setWithCalendarDate(boolean withCalendarDate) {
this.withCalendarDate = withCalendarDate;
}
2022-10-05 20:32:57 +00:00
private boolean hideResourceName;
private boolean hideResourceFoobox;
2022-01-03 13:38:18 +00:00
2022-10-05 20:32:57 +00:00
public CommandExecutionResult hideResourceName() {
this.hideResourceName = true;
2022-01-03 13:38:18 +00:00
return CommandExecutionResult.ok();
}
2022-10-05 20:32:57 +00:00
public CommandExecutionResult hideResourceFootbox() {
this.hideResourceFoobox = true;
2022-01-03 17:35:17 +00:00
return CommandExecutionResult.ok();
}
2022-08-18 16:55:09 +00:00
private final Set<Day> verticalSeparatorBefore = new HashSet<>();
public void addVerticalSeparatorBefore(Day day) {
verticalSeparatorBefore.add(day);
}
}