1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-07 10:50:53 +00:00
plantuml/src/net/sourceforge/plantuml/project/GanttDiagram.java

570 lines
16 KiB
Java
Raw Normal View History

2017-02-01 18:55:51 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, Arnaud Roques
2017-02-01 18:55:51 +00:00
*
* Project Info: http://plantuml.com
*
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
*
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
*
*
*/
2020-02-18 21:24:31 +00:00
package net.sourceforge.plantuml.project;
2017-02-01 18:55:51 +00:00
2019-04-21 20:40:01 +00:00
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
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;
import java.util.EnumSet;
2017-02-26 16:26:11 +00:00
import java.util.HashMap;
2018-04-06 20:36:30 +00:00
import java.util.HashSet;
2017-02-01 18:55:51 +00:00
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
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
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.AnnotatedWorker;
import net.sourceforge.plantuml.Dimension2DDouble;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.FileFormatOption;
2018-01-28 22:08:15 +00:00
import net.sourceforge.plantuml.Scale;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.SkinParam;
import net.sourceforge.plantuml.TitledDiagram;
import net.sourceforge.plantuml.UmlDiagramType;
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;
2018-04-06 20:36:30 +00:00
import net.sourceforge.plantuml.graphic.HtmlColor;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.graphic.HtmlColorSetSimple;
import net.sourceforge.plantuml.graphic.HtmlColorUtils;
import net.sourceforge.plantuml.graphic.IHtmlColorSet;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.graphic.InnerStrategy;
import net.sourceforge.plantuml.graphic.StringBounder;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.graphic.TextBlock;
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;
import net.sourceforge.plantuml.project.core.TaskImpl;
import net.sourceforge.plantuml.project.core.TaskInstant;
import net.sourceforge.plantuml.project.core.TaskSeparator;
import net.sourceforge.plantuml.project.core.Wink;
import net.sourceforge.plantuml.project.draw.ResourceDraw;
import net.sourceforge.plantuml.project.draw.TaskDraw;
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;
import net.sourceforge.plantuml.project.draw.TimeHeaderSimple;
import net.sourceforge.plantuml.project.draw.TimeHeaderWeekly;
import net.sourceforge.plantuml.project.lang.ComplementColors;
import net.sourceforge.plantuml.project.lang.Subject;
import net.sourceforge.plantuml.project.timescale.TimeScale;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.svek.TextBlockBackcolored;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.ugraphic.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.ImageBuilder;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.ugraphic.MinMax;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UTranslate;
2019-04-21 20:40:01 +00:00
public class GanttDiagram extends TitledDiagram implements Subject {
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>();
2017-02-01 18:55:51 +00:00
private final List<GanttConstraint> constraints = new ArrayList<GanttConstraint>();
2017-02-26 16:26:11 +00:00
private final IHtmlColorSet colorSet = new HtmlColorSetSimple();
2020-02-18 21:24:31 +00:00
2018-04-06 20:36:30 +00:00
private final Collection<DayOfWeek> closedDayOfWeek = EnumSet.noneOf(DayOfWeek.class);
private final Collection<DayAsDate> closedDayAsDate = new HashSet<DayAsDate>();
2018-12-22 11:11:40 +00:00
private final Collection<DayAsDate> openedDayAsDate = new HashSet<DayAsDate>();
2020-02-18 21:24:31 +00:00
private final Map<String, Resource> resources = new LinkedHashMap<String, Resource>();
private final Map<DayAsDate, HtmlColor> colorDays = new HashMap<DayAsDate, HtmlColor>();
private final Map<DayAsDate, String> nameDays = new HashMap<DayAsDate, String>();
private PrintScale printScale = PrintScale.DAILY;
private DayAsDate today;
2017-02-26 16:26:11 +00:00
private GCalendar calendar;
2020-02-18 21:24:31 +00:00
private double totalHeight;
private Wink min = new Wink(0);
private Wink max;
2017-02-26 16:26:11 +00:00
2020-02-18 21:24:31 +00:00
private DayAsDate printStart;
private DayAsDate printEnd;
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
}
2018-04-06 20:36:30 +00:00
private int horizontalPages = 1;
private int verticalPages = 1;
final public int getHorizontalPages() {
return horizontalPages;
}
final public void setHorizontalPages(int horizontalPages) {
this.horizontalPages = horizontalPages;
}
final public int getVerticalPages() {
return verticalPages;
}
final public void setVerticalPages(int verticalPages) {
this.verticalPages = verticalPages;
}
@Override
public int getNbImages() {
return this.horizontalPages * this.verticalPages;
}
public final int getDpi(FileFormatOption fileFormatOption) {
return 96;
}
2017-02-01 18:55:51 +00:00
@Override
2017-04-19 18:30:16 +00:00
protected ImageData exportDiagramNow(OutputStream os, int index, FileFormatOption fileFormatOption, long seed)
2017-02-01 18:55:51 +00:00
throws IOException {
final double margin = 10;
2018-01-28 22:08:15 +00:00
final Scale scale = getScale();
2017-02-01 18:55:51 +00:00
2018-01-28 22:08:15 +00:00
final double dpiFactor = scale == null ? 1 : scale.getScale(100, 100);
final ImageBuilder imageBuilder = new ImageBuilder(new ColorMapperIdentity(), dpiFactor, null, "", "", 0, 0,
null, false);
2019-04-21 20:40:01 +00:00
final SkinParam skinParam = SkinParam.create(UmlDiagramType.TIMING);
TextBlock result = getTextBlock();
result = new AnnotatedWorker(this, skinParam, fileFormatOption.getDefaultStringBounder()).addAdd(result);
2017-11-20 16:10:36 +00:00
imageBuilder.setUDrawable(result);
2017-02-01 18:55:51 +00:00
2017-04-19 18:30:16 +00:00
return imageBuilder.writeImageTOBEMOVED(fileFormatOption, seed, 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;
}
2019-04-21 20:40:01 +00:00
private TextBlockBackcolored getTextBlock() {
2020-02-18 21:24:31 +00:00
if (printStart == null) {
initMinMax();
} else {
this.min = calendar.fromDayAsDate(printStart);
this.max = calendar.fromDayAsDate(printEnd);
}
final TimeHeader timeHeader;
if (calendar == null) {
timeHeader = new TimeHeaderSimple(min, max);
} else if (printScale == PrintScale.WEEKLY) {
timeHeader = new TimeHeaderWeekly(calendar, min, max, getDefaultPlan(), colorDays, nameDays);
} else {
timeHeader = new TimeHeaderDaily(calendar, min, max, getDefaultPlan(), colorDays, nameDays, printStart,
printEnd);
}
initTaskAndResourceDraws(timeHeader.getTimeScale(), timeHeader.getFullHeaderHeight());
2019-04-21 20:40:01 +00:00
return new TextBlockBackcolored() {
2017-02-01 18:55:51 +00:00
public void drawU(UGraphic ug) {
2020-02-18 21:24:31 +00:00
timeHeader.drawTimeHeader(ug, totalHeight);
drawConstraints(ug, timeHeader.getTimeScale());
drawTasksRect(ug);
drawTasksTitle(ug);
drawResources(ug);
2017-02-01 18:55:51 +00:00
}
2019-04-21 20:40:01 +00:00
public Rectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
return null;
}
public Dimension2D calculateDimension(StringBounder stringBounder) {
2020-02-18 21:24:31 +00:00
final double xmin = timeHeader.getTimeScale().getStartingPosition(min);
final double xmax = timeHeader.getTimeScale().getEndingPosition(max);
2019-04-21 20:40:01 +00:00
return new Dimension2DDouble(xmax - xmin, totalHeight);
}
public MinMax getMinMax(StringBounder stringBounder) {
throw new UnsupportedOperationException();
}
public HtmlColor getBackcolor() {
return null;
}
2017-02-01 18:55:51 +00:00
};
}
2020-02-18 21:24:31 +00:00
private void drawTasksRect(UGraphic ug) {
for (Task task : tasks.values()) {
final TaskDraw draw = task.getTaskDraw();
final UTranslate move = new UTranslate(0, draw.getY());
draw.drawU(ug.apply(move));
}
}
private void drawConstraints(final UGraphic ug, TimeScale timeScale) {
for (GanttConstraint constraint : constraints) {
constraint.getUDrawable(timeScale).drawU(ug);
2018-04-06 20:36:30 +00:00
}
2017-03-12 17:22:02 +00:00
}
2020-02-18 21:24:31 +00:00
private void drawTasksTitle(final UGraphic ug1) {
for (Task task : tasks.values()) {
final TaskDraw draw = task.getTaskDraw();
final UTranslate move = new UTranslate(0, draw.getY());
draw.drawTitle(ug1.apply(move));
}
}
private void drawResources(UGraphic ug) {
for (Resource res : resources.values()) {
final ResourceDraw draw = res.getResourceDraw();
final UTranslate move = new UTranslate(0, draw.getY());
draw.drawU(ug.apply(move));
}
2018-04-06 20:36:30 +00:00
}
2018-06-25 19:05:58 +00:00
public final LoadPlanable getDefaultPlan() {
2018-04-06 20:36:30 +00:00
return new LoadPlanable() {
2020-02-18 21:24:31 +00:00
public int getLoadAt(Wink instant) {
2018-04-06 20:36:30 +00:00
if (calendar == null) {
return 100;
}
2020-02-18 21:24:31 +00:00
final DayAsDate day = calendar.toDayAsDate((Wink) instant);
2018-12-22 11:11:40 +00:00
if (isClosed(day)) {
2018-04-06 20:36:30 +00:00
return 0;
}
return 100;
}
};
}
2018-12-22 11:11:40 +00:00
private boolean isClosed(final DayAsDate day) {
if (openedDayAsDate.contains(day)) {
return false;
}
final DayOfWeek dayOfWeek = day.getDayOfWeek();
return closedDayOfWeek.contains(dayOfWeek) || closedDayAsDate.contains(day);
}
public void closeDayOfWeek(DayOfWeek day) {
closedDayOfWeek.add(day);
}
public void closeDayAsDate(DayAsDate day) {
closedDayAsDate.add(day);
}
public void openDayAsDate(DayAsDate day) {
openedDayAsDate.add(day);
}
2020-02-18 21:24:31 +00:00
private void initTaskAndResourceDraws(TimeScale timeScale, double headerHeight) {
double y = 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) {
draw = new TaskDrawSeparator((TaskSeparator) task, timeScale, y, min, max);
} else {
draw = new TaskDrawRegular((TaskImpl) task, timeScale, y);
}
2017-02-26 16:26:11 +00:00
task.setTaskDraw(draw);
y += draw.getHeight();
2018-04-06 20:36:30 +00:00
}
for (Resource res : resources.values()) {
final ResourceDraw draw = new ResourceDraw(this, res, timeScale, y, min, max);
res.setTaskDraw(draw);
y += draw.getHeight();
2017-02-26 16:26:11 +00:00
}
2019-04-21 20:40:01 +00:00
this.totalHeight = y;
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()) {
if (task instanceof TaskSeparator) {
continue;
}
2020-02-18 21:24:31 +00:00
final Wink start = task.getStart();
final Wink end = task.getEnd();
2018-11-26 18:46:22 +00:00
// if (min.compareTo(start) > 0) {
// min = start;
// }
2019-03-29 22:14:07 +00:00
if (max == null || max.compareTo(end) < 0) {
2018-11-26 18:46:22 +00:00
max = end;
}
}
}
if (calendar != null) {
for (DayAsDate d : colorDays.keySet()) {
2020-02-18 21:24:31 +00:00
final Wink instant = calendar.fromDayAsDate(d);
2018-11-26 18:46:22 +00:00
if (instant.compareTo(max) > 0) {
max = instant;
}
2018-04-06 20:36:30 +00:00
}
2018-11-26 18:46:22 +00:00
for (DayAsDate d : nameDays.keySet()) {
2020-02-18 21:24:31 +00:00
final Wink instant = calendar.fromDayAsDate(d);
2018-11-26 18:46:22 +00:00
if (instant.compareTo(max) > 0) {
max = instant;
}
2017-02-26 16:26:11 +00:00
}
}
}
2018-11-26 18:46:22 +00:00
public DayAsDate getThenDate() {
DayAsDate result = getStartingDate();
for (DayAsDate d : colorDays.keySet()) {
if (d.compareTo(result) > 0) {
result = d;
}
}
for (DayAsDate d : nameDays.keySet()) {
if (d.compareTo(result) > 0) {
result = d;
}
}
return result;
}
2017-02-26 16:26:11 +00:00
public Task getExistingTask(String id) {
if (id == null) {
throw new IllegalArgumentException();
}
Task result = byShortName.get(id);
if (result != null) {
return result;
}
final TaskCode code = new TaskCode(id);
return tasks.get(code);
}
2018-04-06 20:36:30 +00:00
public void setTaskOrder(final Task task1, final Task task2) {
final TaskInstant end1 = new TaskInstant(task1, TaskAttribute.END);
task2.setStart(end1.getInstantPrecise());
addContraint(new GanttConstraint(end1, new TaskInstant(task2, TaskAttribute.START)));
}
public Task getOrCreateTask(String codeOrShortName, String shortName, boolean linkedToPrevious) {
2017-02-26 16:26:11 +00:00
if (codeOrShortName == null) {
throw new IllegalArgumentException();
}
Task result = shortName == null ? null : byShortName.get(shortName);
if (result != null) {
return result;
}
result = byShortName.get(codeOrShortName);
if (result != null) {
return result;
}
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;
if (linkedToPrevious) {
previous = getLastCreatedTask();
}
result = new TaskImpl(code, getDefaultPlan());
2017-02-01 18:55:51 +00:00
tasks.put(code, result);
2017-02-26 16:26:11 +00:00
if (byShortName != null) {
byShortName.put(shortName, result);
}
2018-04-06 20:36:30 +00:00
if (previous != null) {
setTaskOrder(previous, result);
}
2017-02-01 18:55:51 +00:00
}
return result;
}
2018-04-06 20:36:30 +00:00
private Task getLastCreatedTask() {
final List<Task> all = new ArrayList<Task>(tasks.values());
for (int i = all.size() - 1; i >= 0; i--) {
if (all.get(i) instanceof TaskImpl) {
return all.get(i);
}
}
return null;
}
public void addSeparator(String comment) {
TaskSeparator separator = new TaskSeparator(comment, tasks.size());
tasks.put(separator.getCode(), separator);
}
2017-02-01 18:55:51 +00:00
public void addContraint(GanttConstraint constraint) {
constraints.add(constraint);
}
public IHtmlColorSet getIHtmlColorSet() {
return colorSet;
}
2017-02-26 16:26:11 +00:00
public void setStartingDate(DayAsDate start) {
2020-02-18 21:24:31 +00:00
this.calendar = new GCalendar(start);
2017-02-26 16:26:11 +00:00
}
2017-11-20 16:10:36 +00:00
public DayAsDate getStartingDate() {
2018-06-25 19:05:58 +00:00
if (this.calendar == null) {
return null;
}
2017-11-20 16:10:36 +00:00
return this.calendar.getStartingDate();
}
2019-02-09 21:56:24 +00:00
public DayAsDate getStartingDate(int nday) {
if (this.calendar == null) {
return null;
}
2020-02-18 21:24:31 +00:00
return this.calendar.toDayAsDate(new Wink(nday));
2019-02-09 21:56:24 +00:00
}
2018-11-26 18:46:22 +00:00
public int daysInWeek() {
return 7 - closedDayOfWeek.size();
}
2020-02-18 21:24:31 +00:00
public Wink convert(DayAsDate day) {
2018-04-06 20:36:30 +00:00
return calendar.fromDayAsDate(day);
}
2018-06-25 19:05:58 +00:00
public boolean isOpen(DayAsDate day) {
return getDefaultPlan().getLoadAt(convert(day)) > 0;
}
2019-02-09 21:56:24 +00:00
public void affectResource(Task result, String description) {
final Pattern p = Pattern.compile("([^:]+)(:(\\d+))?");
final Matcher m = p.matcher(description);
if (m.find() == false) {
throw new IllegalArgumentException();
}
final Resource resource = getResource(m.group(1));
int percentage = 100;
if (m.group(3) != null) {
percentage = Integer.parseInt(m.group(3));
}
result.addResource(resource, percentage);
2018-04-06 20:36:30 +00:00
}
public Resource getResource(String resourceName) {
Resource resource = resources.get(resourceName);
if (resource == null) {
2020-02-18 21:24:31 +00:00
resource = new Resource(resourceName, getDefaultPlan(), calendar);
2018-04-06 20:36:30 +00:00
}
resources.put(resourceName, resource);
return resource;
}
2020-02-18 21:24:31 +00:00
public int getLoadForResource(Resource res, Wink i) {
2018-04-06 20:36:30 +00:00
int result = 0;
for (Task task : tasks.values()) {
if (task instanceof TaskSeparator) {
continue;
}
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) {
DayAsDate start = null;
DayAsDate end = null;
for (Map.Entry<DayAsDate, String> ent : nameDays.entrySet()) {
if (ent.getValue().equalsIgnoreCase(id) == false) {
continue;
}
start = min(start, ent.getKey());
end = max(end, ent.getKey());
}
if (start != null) {
result = new MomentImpl(convert(start), convert(end));
}
}
return result;
}
private DayAsDate min(DayAsDate d1, DayAsDate d2) {
if (d1 == null) {
return d2;
}
if (d1.compareTo(d2) > 0) {
return d2;
}
return d1;
}
private DayAsDate max(DayAsDate d1, DayAsDate d2) {
if (d1 == null) {
return d2;
}
if (d1.compareTo(d2) < 0) {
return d2;
}
return d1;
}
2018-06-25 19:05:58 +00:00
public void colorDay(DayAsDate day, HtmlColor color) {
colorDays.put(day, color);
}
public void nameDay(DayAsDate day, String name) {
nameDays.put(day, name);
}
2019-02-09 21:56:24 +00:00
public void setTodayColors(ComplementColors colors) {
if (today == null) {
this.today = DayAsDate.today();
}
colorDay(today, colors.getCenter());
}
public CommandExecutionResult setToday(DayAsDate date) {
this.today = date;
return CommandExecutionResult.ok();
}
2019-03-29 22:14:07 +00:00
public CommandExecutionResult deleteTask(Task task) {
task.setColors(new ComplementColors(HtmlColorUtils.WHITE, HtmlColorUtils.BLACK));
return CommandExecutionResult.ok();
}
2020-02-18 21:24:31 +00:00
public void setPrintInterval(DayAsDate start, DayAsDate end) {
this.printStart = start;
this.printEnd = end;
}
2017-02-01 18:55:51 +00:00
}