GanttTaskTable.java

/* ========================================================================
 * PlantUML : a free UML diagram generator
 * ========================================================================
 *
 * (C) Copyright 2009-2024, Arnaud Roques
 *
 * Project Info:  https://plantuml.com
 *
 * If you like this project or if you find it useful, you can support us at:
 *
 * https://plantuml.com/patreon (only 1$ per month!)
 * https://plantuml.com/paypal
 *
 * 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.gantt;

import java.util.List;
import java.util.Locale;

import net.sourceforge.plantuml.gantt.GanttTaskTableColumn.Context;
import net.sourceforge.plantuml.gantt.core.Task;
import net.sourceforge.plantuml.gantt.core.TaskImpl;
import net.sourceforge.plantuml.gantt.data.GanttModelData;
import net.sourceforge.plantuml.gantt.data.TaskDrawRegistryData;
import net.sourceforge.plantuml.gantt.data.TimeBoundsData;
import net.sourceforge.plantuml.gantt.data.TimelineStyleData;
import net.sourceforge.plantuml.gantt.draw.TaskDraw;
import net.sourceforge.plantuml.gantt.time.TimePoint;
import net.sourceforge.plantuml.klimt.UTranslate;
import net.sourceforge.plantuml.klimt.color.HColor;
import net.sourceforge.plantuml.klimt.creole.Display;
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
import net.sourceforge.plantuml.klimt.font.FontConfiguration;
import net.sourceforge.plantuml.klimt.font.StringBounder;
import net.sourceforge.plantuml.klimt.geom.HorizontalAlignment;
import net.sourceforge.plantuml.klimt.geom.XDimension2D;
import net.sourceforge.plantuml.klimt.shape.TextBlock;
import net.sourceforge.plantuml.klimt.shape.ULine;
import net.sourceforge.plantuml.klimt.sprite.SpriteContainerEmpty;
import net.sourceforge.plantuml.style.SName;
import net.sourceforge.plantuml.style.Style;
import net.sourceforge.plantuml.style.StyleSignatureBasic;

/**
 * Draws, on the left side of a Gantt diagram, a textual table giving for each
 * task its code, its start date, its end date and its duration (in days). Each
 * row is vertically aligned with the corresponding task bar.
 */
public final class GanttTaskTable {

	private static final double CELL_PADDING = 5;

	private final GanttModelData modelData;
	private final TimeBoundsData timeBounds;
	private final TaskDrawRegistryData drawRegistry;
	private final TimelineStyleData timelineStyle;
	private final Locale locale;
	private final double headerHeight;

	private final Context context;
	private final List<GanttTaskTableColumn> displayedColumn;

	// One boundary per column edge: columnEdges[0] is the left border, the last
	// entry is the right border of the last column. With N displayed columns there
	// are N+1 edges.
	private final double[] columnEdges;

	public GanttTaskTable(GanttModelData modelData, TimeBoundsData timeBounds, TaskDrawRegistryData drawRegistry,
			TimelineStyleData timelineStyle, double headerHeight, Locale locale, StringBounder stringBounder,
			List<GanttTaskTableColumn> displayedColumn) {
		this.displayedColumn = displayedColumn;
		this.modelData = modelData;
		this.timeBounds = timeBounds;
		this.drawRegistry = drawRegistry;
		this.timelineStyle = timelineStyle;
		this.headerHeight = headerHeight;
		this.locale = locale;
		this.context = new Context(locale, isRelativeMode(), timeBounds.getMinDay());

		this.columnEdges = new double[displayedColumn.size() + 1];
		this.columnEdges[0] = 0;
		for (int i = 0; i < displayedColumn.size(); i++) {
			final double colWidth = columnWidth(stringBounder, displayedColumn.get(i));
			this.columnEdges[i + 1] = this.columnEdges[i] + colWidth;
		}
	}

	private double columnWidth(StringBounder stringBounder, GanttTaskTableColumn column) {
		double width = widthOf(stringBounder, column.header(locale));
		for (Task task : modelData.getTasks()) {
			if (isDrawable(task) == false)
				continue;

			width = Math.max(width, widthOf(stringBounder, column.valueOf(task, context)));
		}
		return width + 2 * CELL_PADDING;
	}

	public double getWidth() {
		return columnEdges[columnEdges.length - 1];
	}

	public void drawU(UGraphic ug, double totalHeightWithoutFooter) {
		final StringBounder stringBounder = ug.getStringBounder();
		final HColor line = timelineStyle.getLineColor();

		drawGrid(ug.apply(line), totalHeightWithoutFooter);

		drawHeaderRow(ug, headerHeight / 2);

		for (Task task : modelData.getTasks()) {
			if (isDrawable(task) == false)
				continue;

			final TaskDraw draw = drawRegistry.getTaskDraw(task);
			final double yCenter = draw.getY(stringBounder).getCurrentValue()
					+ draw.getFullHeightTask(stringBounder) / 2;
			drawTaskRow(ug, yCenter, task);
		}
	}

	private void drawGrid(UGraphic ug, double totalHeightWithoutFooter) {
		final double width = getWidth();

		ug.draw(ULine.hline(width));
		ug.apply(UTranslate.dy(headerHeight)).draw(ULine.hline(width));
		ug.apply(UTranslate.dy(totalHeightWithoutFooter)).draw(ULine.hline(width));

		for (double x : columnEdges)
			ug.apply(UTranslate.dx(x)).draw(ULine.vline(totalHeightWithoutFooter));
	}

	private void drawHeaderRow(UGraphic ug, double yCenter) {
		for (int i = 0; i < displayedColumn.size(); i++)
			drawCell(ug, columnEdges[i], yCenter, displayedColumn.get(i).header(locale));
	}

	private void drawTaskRow(UGraphic ug, double yCenter, Task task) {
		for (int i = 0; i < displayedColumn.size(); i++)
			drawCell(ug, columnEdges[i], yCenter, displayedColumn.get(i).valueOf(task, context));
	}

	private void drawCell(UGraphic ug, double xLeft, double yCenter, String text) {
		final StringBounder stringBounder = ug.getStringBounder();
		final TextBlock block = createTextBlock(text);
		final XDimension2D dim = block.calculateDimension(stringBounder);
		block.drawU(ug.apply(new UTranslate(xLeft + CELL_PADDING, yCenter - dim.getHeight() / 2)));
	}

	private boolean isDrawable(Task task) {
		if (task instanceof TaskImpl == false)
			return false;

		if (timeBounds.isHidden(task))
			return false;

		if (drawRegistry.getTaskDraw(task) == null)
			return false;

		return true;
	}

	private boolean isRelativeMode() {
		return timeBounds.getMinDay().equals(TimePoint.epoch());
	}

	private double widthOf(StringBounder stringBounder, String text) {
		return createTextBlock(text).calculateDimension(stringBounder).getWidth();
	}

	private TextBlock createTextBlock(String text) {
		return Display.getWithNewlines(timelineStyle.getPragma(), text).create(getFontConfiguration(),
				HorizontalAlignment.LEFT, new SpriteContainerEmpty());
	}

	private Style getStyle() {
		return StyleSignatureBasic.of(SName.root, SName.element, SName.ganttDiagram, SName.timeline)
				.getMergedStyle(timelineStyle.getSkinParam().getCurrentStyleBuilder());
	}

	private FontConfiguration getFontConfiguration() {
		return getStyle().getFontConfiguration(timelineStyle.getColorSet());
	}

}