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

503 lines
16 KiB
Java
Raw Normal View History

2017-02-01 18:55:51 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2017, Arnaud Roques
*
* 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
*
*
*/
package net.sourceforge.plantuml.project3;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
2018-04-06 20:36:30 +00:00
import java.util.Collection;
2017-02-01 18:55:51 +00:00
import java.util.Collections;
import java.util.Comparator;
2018-04-06 20:36:30 +00:00
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;
import net.sourceforge.plantuml.AbstractPSystem;
import net.sourceforge.plantuml.FileFormatOption;
2018-01-28 22:08:15 +00:00
import net.sourceforge.plantuml.Scale;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.SpriteContainerEmpty;
import net.sourceforge.plantuml.core.DiagramDescription;
import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.cucadiagram.Display;
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
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;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.graphic.UDrawable;
import net.sourceforge.plantuml.ugraphic.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.ImageBuilder;
2018-04-06 20:36:30 +00:00
import net.sourceforge.plantuml.ugraphic.UChangeBackColor;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.ugraphic.UChangeColor;
import net.sourceforge.plantuml.ugraphic.UFont;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.ULine;
2018-04-06 20:36:30 +00:00
import net.sourceforge.plantuml.ugraphic.URectangle;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.ugraphic.UTranslate;
2017-02-26 16:26:11 +00:00
public class GanttDiagram extends AbstractPSystem 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();
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>();
2017-02-26 16:26:11 +00:00
private GCalendar calendar;
2017-11-20 16:10:36 +00:00
private final Instant min = new InstantDay(0);
2017-02-26 16:26:11 +00:00
private Instant max;
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;
// public ImageBuilder(ColorMapper colorMapper, double dpiFactor, HtmlColor mybackcolor, String metadata,
// String warningOrError, double margin1, double margin2, Animation animation, boolean useHandwritten) {
sortTasks();
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);
2017-11-20 16:10:36 +00:00
final UDrawable result = getUDrawable();
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
}
private void sortTasks() {
final TaskCodeSimpleOrder order = getCanonicalOrder(1);
final List<Task> list = new ArrayList<Task>(tasks.values());
Collections.sort(list, new Comparator<Task>() {
public int compare(Task task1, Task task2) {
return order.compare(task1.getCode(), task2.getCode());
}
});
tasks.clear();
for (Task task : list) {
tasks.put(task.getCode(), task);
}
}
private UDrawable getUDrawable() {
return new UDrawable() {
public void drawU(UGraphic ug) {
2017-02-26 16:26:11 +00:00
initMinMax();
2017-03-12 17:22:02 +00:00
final TimeScale timeScale = getTimeScale();
2017-02-26 16:26:11 +00:00
drawTimeHeader(ug, timeScale);
drawTasks(ug, timeScale);
2017-02-01 18:55:51 +00:00
drawConstraints(ug, timeScale);
}
};
}
2017-03-12 17:22:02 +00:00
private TimeScale getTimeScale() {
2018-04-06 20:36:30 +00:00
if (calendar == null) {
return new TimeScaleBasic();
}
return new TimeScaleBasic2(getCalendarSimple());
2017-03-12 17:22:02 +00:00
// return new TimeScaleWithoutWeekEnd(calendar);
}
2018-04-06 20:36:30 +00:00
private GCalendarSimple getCalendarSimple() {
return (GCalendarSimple) calendar;
}
public LoadPlanable getDefaultPlan() {
return new LoadPlanable() {
public int getLoadAt(Instant instant) {
if (calendar == null) {
return 100;
}
final DayAsDate day = getCalendarSimple().toDayAsDate((InstantDay) instant);
final DayOfWeek dayOfWeek = day.getDayOfWeek();
if (closedDayOfWeek.contains(dayOfWeek) || closedDayAsDate.contains(day)) {
return 0;
}
return 100;
}
};
}
2017-02-01 18:55:51 +00:00
private void drawConstraints(final UGraphic ug, TimeScale timeScale) {
for (GanttConstraint constraint : constraints) {
constraint.getUDrawable(timeScale).drawU(ug);
}
}
2017-02-26 16:26:11 +00:00
private void drawTimeHeader(final UGraphic ug, TimeScale timeScale) {
2017-02-01 18:55:51 +00:00
2018-04-06 20:36:30 +00:00
final double yTotal = initTaskAndResourceDraws(timeScale);
2017-02-01 18:55:51 +00:00
2017-03-12 17:22:02 +00:00
final double xmin = timeScale.getStartingPosition(min);
2018-04-06 20:36:30 +00:00
final double xmax = timeScale.getEndingPosition(max);
2017-02-26 16:26:11 +00:00
ug.apply(new UChangeColor(HtmlColorUtils.LIGHT_GRAY)).draw(new ULine(xmax - xmin, 0));
ug.apply(new UChangeColor(HtmlColorUtils.LIGHT_GRAY)).apply(new UTranslate(0, getHeaderHeight() - 3))
.draw(new ULine(xmax - xmin, 0));
if (calendar == null) {
drawSimpleDayCounter(ug, timeScale, yTotal);
} else {
drawCalendar(ug, timeScale, yTotal);
}
}
2018-04-06 20:36:30 +00:00
private final HtmlColor veryLightGray = new HtmlColorSetSimple().getColorIfValid("#E0E8E8");
private double getHeaderHeight() {
if (calendar != null) {
return Y_WEEKDAY + Y_NUMDAY;
}
return 16;
}
private static final int Y_WEEKDAY = 16;
private static final int Y_NUMDAY = 28;
2017-02-26 16:26:11 +00:00
private void drawCalendar(final UGraphic ug, TimeScale timeScale, final double yTotal) {
2018-04-06 20:36:30 +00:00
timeScale = new TimeScaleBasic();
final ULine vbar = new ULine(0, yTotal - Y_WEEKDAY);
2017-02-26 16:26:11 +00:00
Month lastMonth = null;
2018-04-06 20:36:30 +00:00
final GCalendarSimple calendarAll = getCalendarSimple();
final Instant max2 = calendarAll.fromDayAsDate(calendar.toDayAsDate((InstantDay) max));
for (Instant i = min; i.compareTo(max2.increment()) <= 0; i = i.increment()) {
final DayAsDate day = calendarAll.toDayAsDate((InstantDay) i);
final DayOfWeek dayOfWeek = day.getDayOfWeek();
final boolean isWorkingDay = getDefaultPlan().getLoadAt(i) > 0;
2017-02-26 16:26:11 +00:00
final String d1 = "" + day.getDayOfMonth();
2018-04-06 20:36:30 +00:00
final TextBlock num = getTextBlock(d1, 10);
2017-03-12 17:22:02 +00:00
final double x1 = timeScale.getStartingPosition(i);
2018-04-06 20:36:30 +00:00
final double x2 = timeScale.getEndingPosition(i);
if (i.compareTo(max2.increment()) < 0) {
final TextBlock weekDay = getTextBlock(dayOfWeek.shortName(), 10);
if (isWorkingDay) {
drawCenter(ug.apply(new UTranslate(0, Y_NUMDAY)), num, x1, x2);
drawCenter(ug.apply(new UTranslate(0, Y_WEEKDAY)), weekDay, x1, x2);
} else {
final URectangle rect = new URectangle(x2 - x1 - 1, yTotal - Y_WEEKDAY);
ug.apply(new UChangeColor(null)).apply(new UChangeBackColor(veryLightGray))
.apply(new UTranslate(x1 + 1, Y_WEEKDAY)).draw(rect);
}
2017-02-26 16:26:11 +00:00
if (lastMonth != day.getMonth()) {
2018-04-06 20:36:30 +00:00
final int delta = 5;
if (lastMonth != null) {
final TextBlock lastMonthBlock = getTextBlock(lastMonth.name(), 12);
lastMonthBlock.drawU(ug.apply(new UTranslate(x1
- lastMonthBlock.calculateDimension(ug.getStringBounder()).getWidth() - delta, 0)));
}
final TextBlock month = getTextBlock(day.getMonth().name(), 12);
month.drawU(ug.apply(new UTranslate(x1 + delta, 0)));
ug.apply(new UChangeColor(HtmlColorUtils.LIGHT_GRAY)).apply(new UTranslate(x1, 0))
.draw(new ULine(0, Y_WEEKDAY));
2017-02-26 16:26:11 +00:00
}
lastMonth = day.getMonth();
2017-02-01 18:55:51 +00:00
}
2018-04-06 20:36:30 +00:00
ug.apply(new UChangeColor(HtmlColorUtils.LIGHT_GRAY)).apply(new UTranslate(x1, Y_WEEKDAY)).draw(vbar);
2017-02-01 18:55:51 +00:00
}
2017-02-26 16:26:11 +00:00
}
2017-02-01 18:55:51 +00:00
2018-04-06 20:36:30 +00:00
private TextBlock getTextBlock(final String text, int size) {
return Display.getWithNewlines(text).create(getFontConfiguration(size), HorizontalAlignment.LEFT,
new SpriteContainerEmpty());
2017-02-26 16:26:11 +00:00
}
2017-02-01 18:55:51 +00:00
2017-02-26 16:26:11 +00:00
private void drawCenter(final UGraphic ug, final TextBlock text, final double x1, final double x2) {
final double width = text.calculateDimension(ug.getStringBounder()).getWidth();
final double delta = (x2 - x1) - width;
2017-03-12 17:22:02 +00:00
if (delta < 0) {
return;
}
2017-02-26 16:26:11 +00:00
text.drawU(ug.apply(new UTranslate(x1 + delta / 2, 0)));
}
2017-02-01 18:55:51 +00:00
2017-02-26 16:26:11 +00:00
private void drawSimpleDayCounter(final UGraphic ug, TimeScale timeScale, final double yTotal) {
final ULine vbar = new ULine(0, yTotal);
2017-02-01 18:55:51 +00:00
for (Instant i = min; i.compareTo(max.increment()) <= 0; i = i.increment()) {
2018-04-06 20:36:30 +00:00
final TextBlock num = Display.getWithNewlines(i.toShortString()).create(getFontConfiguration(10),
2017-02-01 18:55:51 +00:00
HorizontalAlignment.LEFT, new SpriteContainerEmpty());
2017-03-12 17:22:02 +00:00
final double x1 = timeScale.getStartingPosition(i);
2018-04-06 20:36:30 +00:00
final double x2 = timeScale.getEndingPosition(i);
2017-02-01 18:55:51 +00:00
final double width = num.calculateDimension(ug.getStringBounder()).getWidth();
final double delta = (x2 - x1) - width;
if (i.compareTo(max.increment()) < 0) {
num.drawU(ug.apply(new UTranslate(x1 + delta / 2, 0)));
}
ug.apply(new UChangeColor(HtmlColorUtils.LIGHT_GRAY)).apply(new UTranslate(x1, 0)).draw(vbar);
}
2017-02-26 16:26:11 +00:00
}
2017-02-01 18:55:51 +00:00
2018-04-06 20:36:30 +00:00
private double initTaskAndResourceDraws(TimeScale timeScale) {
2017-02-26 16:26:11 +00:00
double y = getHeaderHeight();
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
}
return y;
}
private void initMinMax() {
2017-11-20 16:10:36 +00:00
// min = tasks.values().iterator().next().getStart();
2017-02-26 16:26:11 +00:00
max = tasks.values().iterator().next().getEnd();
for (Task task : tasks.values()) {
2018-04-06 20:36:30 +00:00
if (task instanceof TaskSeparator) {
continue;
}
2017-02-26 16:26:11 +00:00
final Instant start = task.getStart();
final Instant end = task.getEnd();
2017-11-20 16:10:36 +00:00
// if (min.compareTo(start) > 0) {
// min = start;
// }
2017-02-26 16:26:11 +00:00
if (max.compareTo(end) < 0) {
max = end;
}
}
}
private void drawTasks(final UGraphic ug, TimeScale timeScale) {
2017-02-01 18:55:51 +00:00
for (Task task : tasks.values()) {
final TaskDraw draw = task.getTaskDraw();
draw.drawU(ug.apply(new UTranslate(0, draw.getY())));
2018-04-06 20:36:30 +00:00
draw.drawTitle(ug.apply(new UTranslate(0, draw.getY())));
}
for (Resource res : resources.values()) {
final ResourceDraw draw = res.getResourceDraw();
draw.drawU(ug.apply(new UTranslate(0, draw.getY())));
2017-02-01 18:55:51 +00:00
}
}
2018-04-06 20:36:30 +00:00
private FontConfiguration getFontConfiguration(int size) {
UFont font = UFont.serif(size);
if (size > 10) {
font = font.bold();
}
return new FontConfiguration(font, HtmlColorUtils.BLACK, HtmlColorUtils.BLACK, false);
2017-02-01 18:55:51 +00:00
}
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
private TaskCodeSimpleOrder getCanonicalOrder(int hierarchyHeader) {
final List<TaskCode> codes = new ArrayList<TaskCode>();
for (TaskCode code : tasks.keySet()) {
if (code.getHierarchySize() >= hierarchyHeader) {
codes.add(code.truncateHierarchy(hierarchyHeader));
}
}
return new TaskCodeSimpleOrder(codes, hierarchyHeader);
}
private int getMaxHierarchySize() {
int max = Integer.MIN_VALUE;
for (TaskCode code : tasks.keySet()) {
max = Math.max(max, code.getHierarchySize());
}
return max;
}
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) {
this.calendar = new GCalendarSimple(start);
}
2017-11-20 16:10:36 +00:00
public DayAsDate getStartingDate() {
return this.calendar.getStartingDate();
}
2018-04-06 20:36:30 +00:00
public void closeDayOfWeek(DayOfWeek day) {
closedDayOfWeek.add(day);
}
public void closeDayAsDate(DayAsDate day) {
closedDayAsDate.add(day);
}
public Instant convert(DayAsDate day) {
return calendar.fromDayAsDate(day);
}
private final Map<String, Resource> resources = new LinkedHashMap<String, Resource>();
public void affectResource(Task result, String resourceName) {
Resource resource = getResource(resourceName);
result.addResource(resource);
}
public Resource getResource(String resourceName) {
Resource resource = resources.get(resourceName);
if (resource == null) {
resource = new Resource(resourceName, getDefaultPlan());
}
resources.put(resourceName, resource);
return resource;
}
public int getLoadForResource(Resource res, Instant i) {
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;
}
2017-02-01 18:55:51 +00:00
}