1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-02 00:20:49 +00:00
plantuml/src/net/sourceforge/plantuml/timingdiagram/TimingRuler.java

201 lines
5.7 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.timingdiagram;
import java.awt.geom.Dimension2D;
2017-04-19 18:30:16 +00:00
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Set;
2017-04-05 17:37:42 +00:00
import java.util.SortedSet;
import java.util.TreeSet;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.FontParam;
import net.sourceforge.plantuml.ISkinParam;
import net.sourceforge.plantuml.cucadiagram.Display;
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
2017-04-19 18:30:16 +00:00
import net.sourceforge.plantuml.graphic.HtmlColorUtils;
2017-10-07 09:46:53 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.graphic.TextBlock;
2017-04-19 18:30:16 +00:00
import net.sourceforge.plantuml.ugraphic.UChangeColor;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.ULine;
import net.sourceforge.plantuml.ugraphic.UStroke;
import net.sourceforge.plantuml.ugraphic.UTranslate;
public class TimingRuler {
2017-04-05 17:37:42 +00:00
private final SortedSet<TimeTick> times = new TreeSet<TimeTick>();
2017-04-19 18:30:16 +00:00
private long highestCommonFactor = -1;
2017-02-01 18:55:51 +00:00
private final ISkinParam skinParam;
2017-04-19 18:30:16 +00:00
private long tickIntervalInPixels = 50;
private long tickUnitary;
2018-06-12 20:50:45 +00:00
public TimingRuler(ISkinParam skinParam) {
this.skinParam = skinParam;
this.times.add(new TimeTick(BigDecimal.ZERO));
}
2017-04-19 18:30:16 +00:00
public void scaleInPixels(long tick, long pixel) {
this.tickIntervalInPixels = pixel;
this.tickUnitary = tick;
}
private long tickUnitary() {
if (tickUnitary == 0) {
return highestCommonFactor;
}
return tickUnitary;
}
private int getNbTick() {
2018-03-09 21:37:34 +00:00
if (times.size() == 0) {
return 1;
}
2018-06-12 20:50:45 +00:00
final long delta = getMax().getTime().longValue() - getMin().getTime().longValue();
return (int) (1 + delta / tickUnitary());
2017-04-19 18:30:16 +00:00
}
public double getWidth() {
return getPosInPixel(new BigDecimal((getNbTick()) * tickUnitary()));
}
private double getPosInPixel(double time) {
2018-06-12 20:50:45 +00:00
time -= getMin().getTime().doubleValue();
2017-04-19 18:30:16 +00:00
return time / tickUnitary() * tickIntervalInPixels;
}
2017-02-01 18:55:51 +00:00
public void addTime(TimeTick time) {
2017-04-05 17:37:42 +00:00
final boolean added = times.add(time);
if (added) {
2017-04-19 18:30:16 +00:00
long tick = time.getTime().longValue();
2017-04-05 17:37:42 +00:00
if (tick > 0) {
if (highestCommonFactor == -1) {
2017-04-19 18:30:16 +00:00
highestCommonFactor = time.getTime().longValue();
2017-04-05 17:37:42 +00:00
} else {
2018-06-12 20:50:45 +00:00
highestCommonFactor = computeHighestCommonFactor(highestCommonFactor,
Math.abs(time.getTime().longValue()));
2017-04-05 17:37:42 +00:00
}
2017-02-01 18:55:51 +00:00
}
}
}
private FontConfiguration getFontConfiguration() {
return new FontConfiguration(skinParam, FontParam.ACTIVITY, null);
}
2017-04-19 18:30:16 +00:00
private TextBlock getTimeTextBlock(long time) {
final Display display = Display.getWithNewlines("" + time);
2017-02-01 18:55:51 +00:00
return display.create(getFontConfiguration(), HorizontalAlignment.LEFT, skinParam);
}
public void draw(UGraphic ug) {
2017-04-19 18:30:16 +00:00
ug = ug.apply(new UStroke(2.0)).apply(new UChangeColor(HtmlColorUtils.BLACK));
2017-02-01 18:55:51 +00:00
final double tickHeight = 5;
final ULine line = new ULine(0, tickHeight);
2017-04-19 18:30:16 +00:00
final int nb = getNbTick();
2017-02-01 18:55:51 +00:00
for (int i = 0; i <= nb; i++) {
ug.apply(new UTranslate(tickIntervalInPixels * i, 0)).draw(line);
}
ug.draw(new ULine(nb * tickIntervalInPixels, 0));
2017-04-19 18:30:16 +00:00
for (long round : roundValues()) {
final TextBlock text = getTimeTextBlock(round);
2017-02-01 18:55:51 +00:00
final Dimension2D dim = text.calculateDimension(ug.getStringBounder());
2017-04-19 18:30:16 +00:00
text.drawU(ug.apply(new UTranslate(getPosInPixel(round) - dim.getWidth() / 2, tickHeight + 1)));
2017-02-01 18:55:51 +00:00
}
}
2017-10-07 09:46:53 +00:00
public double getHeight(StringBounder stringBounder) {
return getTimeTextBlock(0).calculateDimension(stringBounder).getHeight();
}
2017-04-19 18:30:16 +00:00
private Collection<Long> roundValues() {
final Set<Long> result = new TreeSet<Long>();
if (tickUnitary == 0) {
for (TimeTick tick : times) {
final long round = tick.getTime().longValue();
result.add(round);
}
} else {
final int nb = getNbTick();
for (int i = 0; i <= nb; i++) {
final long round = tickUnitary * i;
result.add(round);
}
}
return result;
2017-02-01 18:55:51 +00:00
}
private TimeTick getMax() {
2018-06-12 20:50:45 +00:00
// if (times.size() == 0) {
// throw new IllegalStateException("Empty list!");
// }
2017-04-05 17:37:42 +00:00
return times.last();
2017-02-01 18:55:51 +00:00
}
2018-06-12 20:50:45 +00:00
private TimeTick getMin() {
// if (times.size() == 0) {
// throw new IllegalStateException("Empty list!");
// }
return times.first();
}
2017-04-19 18:30:16 +00:00
private static long computeHighestCommonFactor(long a, long b) {
long r = a;
2017-02-01 18:55:51 +00:00
while (r != 0) {
r = a % b;
a = b;
b = r;
}
return (Math.abs(a));
}
2017-04-19 18:30:16 +00:00
public final double getPosInPixel(BigDecimal time) {
return getPosInPixel(time.doubleValue());
2017-02-01 18:55:51 +00:00
}
2017-04-19 18:30:16 +00:00
public final double getPosInPixel(TimeTick when) {
2017-02-01 18:55:51 +00:00
return getPosInPixel(when.getTime());
}
2017-04-19 18:30:16 +00:00
public final double getMaxPosInPixel() {
2017-03-12 17:22:02 +00:00
return getPosInPixel(getMax());
}
2017-02-01 18:55:51 +00:00
}