summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/gui/lifecycle/instance/ActivityRenderer.java
blob: 76d027ac53263b5293259aec4dfa3f7df6d05046 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.c2kernel.gui.lifecycle.instance;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Polygon;

import com.c2kernel.common.GTimeStamp;
import com.c2kernel.common.InvalidData;
import com.c2kernel.graph.model.GraphPoint;
import com.c2kernel.graph.model.Vertex;
import com.c2kernel.gui.graph.view.VertexRenderer;
import com.c2kernel.lifecycle.instance.Activity;
import com.c2kernel.utils.DateUtility;
import com.c2kernel.utils.Language;
public class ActivityRenderer implements VertexRenderer
{
	private Paint mActivePaint = new Color(100, 255, 100);
	private Paint mActiveCompPaint = new Color(100, 255, 255);
	private Paint mInactivePaint = new Color(255, 255, 255);
	private Paint mInactiveCompPaint = new Color(200, 200, 255);
	private Paint mErrorPaint = new Color(255, 50, 0);
	private Paint mTextPaint = Color.black;
	@Override
	public void draw(Graphics2D g2d, Vertex vertex)
	{
		Activity activity = (Activity) vertex;
		boolean active = activity.getActive();
		boolean hasError = !activity.verify();
		boolean isComposite = activity.getIsComposite();
		GraphPoint centrePoint = activity.getCentrePoint();
		//String description = activity.getDescription();
		String[] linesOfText = new String[3];
		linesOfText[0] = "(" + activity.getType() + ")";
		linesOfText[1] = activity.getName();
		if (hasError)
			linesOfText[2] = Language.translate(activity.getErrors());
		else
		{
			boolean showTime = activity.getActive() && ((Boolean) activity.getProperties().get("Show time")).booleanValue();
			String stateName = "Invalid State"; 
			try {
				stateName = activity.getStateName();
			} catch (InvalidData ex) { }
			
			linesOfText[2] = 
					Language.translate(stateName) + (showTime ? " " + getWaitTime(activity.getStateDate()) : "");
		}

		FontMetrics metrics = g2d.getFontMetrics();
		int lineWidth = 0;
		int lineHeight = metrics.getHeight();
		int linesHeight = lineHeight * linesOfText.length;
		int linesStartY = centrePoint.y - linesHeight / 2 + lineHeight * 2 / 3;
		int x = 0;
		int y = 0;
		int i = 0;
		GraphPoint[] outline = vertex.getOutlinePoints();
		Paint actColour;
		if (hasError)
			actColour = mErrorPaint;
		else if (active)
			if (isComposite)
				actColour = mActiveCompPaint;
			else
				actColour = mActivePaint;
		else if (isComposite)
			actColour = mInactiveCompPaint;
		else
			actColour = mInactivePaint;
		g2d.setPaint(actColour);
		//g2d.fill3DRect( centrePoint.x - mSize.width / 2, centrePoint.y - mSize.height / 2, mSize.width, mSize.height, true );
		g2d.fill(graphPointsToPolygon(outline));
		g2d.setPaint(mTextPaint);
		for (i = 0; i < linesOfText.length; i++)
		{
			lineWidth = metrics.stringWidth(linesOfText[i]);
			x = centrePoint.x - lineWidth / 2;
			y = linesStartY + i * lineHeight;
			g2d.drawString(linesOfText[i], x, y);
		}
	}
	private static Polygon graphPointsToPolygon(GraphPoint[] points)
	{
		Polygon polygon = new Polygon();
		int i = 0;
		for (i = 0; i < points.length; i++)
		{
			polygon.addPoint(points[i].x, points[i].y);
		}
		return polygon;
	}
	private static String getWaitTime(GTimeStamp date)
	{
		GTimeStamp now = new GTimeStamp();
		DateUtility.setToNow(now);
		long diff = DateUtility.diff(now, date);
		long secondes = diff % 60;
		long minutes = (diff / 60) % 60;
		long hours = (diff / 3600) % 24;
		long days = (diff / 3600 / 24);
		if (days > 0)
			return days + " " + Language.translate("d") + " " + hours + " " + Language.translate("h");
		if (hours > 0)
			return hours + " " + Language.translate("h") + " " + minutes + " " + Language.translate("min");
		if (minutes > 0)
			return minutes + " " + Language.translate("min");
		return secondes + " " + Language.translate("sec");
	}
}