1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.event.view.contribution;
\r
14 import java.math.RoundingMode;
\r
15 import java.text.NumberFormat;
\r
16 import java.util.Locale;
\r
17 import java.util.Map;
\r
19 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
\r
20 import org.eclipse.core.runtime.preferences.InstanceScope;
\r
21 import org.simantics.browsing.ui.model.labels.LabelRule;
\r
22 import org.simantics.databoard.Bindings;
\r
23 import org.simantics.db.ReadGraph;
\r
24 import org.simantics.db.Resource;
\r
25 import org.simantics.db.common.utils.NameUtils;
\r
26 import org.simantics.db.exception.DatabaseException;
\r
27 import org.simantics.event.ontology.EventResource;
\r
28 import org.simantics.event.view.Constants;
\r
29 import org.simantics.layer0.Layer0;
\r
30 import org.simantics.utils.datastructures.ArrayMap;
\r
31 import org.simantics.utils.format.TimeFormat;
\r
34 * @author Tuukka Lehtonen
\r
36 public enum EventLabelRule implements LabelRule {
\r
40 public static EventLabelRule get() {
\r
44 private static final NumberFormat secondFormatter;
\r
45 private static final TimeFormat timeFormat = new TimeFormat(Double.MAX_VALUE, 3);
\r
48 secondFormatter = NumberFormat.getNumberInstance(Locale.US);
\r
49 secondFormatter.setMinimumFractionDigits(2);
\r
50 secondFormatter.setMaximumFractionDigits(3);
\r
51 secondFormatter.setRoundingMode(RoundingMode.HALF_EVEN);
\r
54 public static String formatSeconds(double time) {
\r
55 return secondFormatter.format(time);
\r
59 * Formats a time specified as a double value in seconds in format
\r
60 * <code>hh:mm:ss.SSS</code>
\r
65 public static String formatHMSS(double time) {
\r
66 return timeFormat.format(time);
\r
70 public boolean isCompatible(Class<?> contentType) {
\r
71 return contentType.equals(Resource.class);
\r
74 public Map<String, String> getLabel(ReadGraph graph, Object content, boolean allInformation) throws DatabaseException {
\r
76 IEclipsePreferences chartPreferenceNode = InstanceScope.INSTANCE.getNode( "org.simantics.charts" );
\r
77 boolean timeFormat = chartPreferenceNode.get("chart.timeformat", "Time").equals("Time"); // Time/Decimal
\r
79 Resource event = (Resource) content;
\r
81 Layer0 L0 = Layer0.getInstance(graph);
\r
82 EventResource EVENT = EventResource.getInstance(graph);
\r
84 String tag = graph.getPossibleRelatedValue(event, EVENT.Event_tag, Bindings.STRING);
\r
85 String message = graph.getPossibleRelatedValue(event, EVENT.Event_message, Bindings.STRING);
\r
86 if (message == null) {
\r
87 message = graph.getPossibleRelatedValue(event, L0.HasLabel, Bindings.STRING);
\r
88 if (message == null)
\r
89 message = graph.getPossibleRelatedValue(event, L0.HasName, Bindings.STRING);
\r
90 if (message == null)
\r
91 message = NameUtils.getSafeLabel(graph, event);
\r
94 StringBuilder eventType = new StringBuilder();
\r
95 Integer eventTypeNumber = graph.getPossibleRelatedValue(event, EVENT.Event_typeNumber);
\r
96 Resource et = graph.getPossibleObject(event, EVENT.Event_type);
\r
98 if (eventTypeNumber != null)
\r
99 eventType.append(eventTypeNumber).append(": ");
\r
100 eventType.append(NameUtils.getSafeLabel(graph, et));
\r
101 } else if (eventTypeNumber != null) {
\r
102 eventType.append(eventTypeNumber);
\r
105 double timeNumeric = getTimestamp(graph, event, 0.0);
\r
106 String time = getTimestampString(graph, timeNumeric, timeFormat);
\r
108 Integer index = graph.getPossibleRelatedValue(event, EVENT.Event_index, Bindings.INTEGER);
\r
109 String indexS = "";
\r
111 indexS = String.valueOf(index);
\r
113 double returnTimeNumeric = 0;
\r
114 String returnTime = null;
\r
115 Resource returnedBy = graph.getPossibleObject(event, EVENT.ReturnedBy);
\r
116 if (returnedBy != null) {
\r
117 returnTimeNumeric = getTimestamp(graph, returnedBy, 0.0);
\r
118 returnTime = getTimestampString(graph, returnTimeNumeric, timeFormat);
\r
121 String sourceName = graph.getPossibleRelatedValue(event, EVENT.Event_sourceName);
\r
122 String milestone = null;
\r
123 String returned = null;
\r
125 if (allInformation) {
\r
126 returned = returnedBy != null ? "Yes" : "";
\r
127 if (returnTime == null)
\r
131 if (graph.hasStatement(event, EVENT.Milestone)) {
\r
132 milestone = graph.getPossibleRelatedValue(event, EVENT.Event_milestoneLabel);
\r
133 if (milestone == null) milestone = "?";
\r
137 return ArrayMap.make(Constants.COLUMN_KEYS, new String[] {
\r
140 String.valueOf(timeNumeric),
\r
142 eventType.toString(),
\r
147 String.valueOf(returnTimeNumeric),
\r
153 public Map<String, String> getLabel(ReadGraph graph, Object content) throws DatabaseException {
\r
154 return getLabel(graph, content, false);
\r
157 public static double getTimestamp(ReadGraph graph, Resource event, double defaultValue) throws DatabaseException {
\r
158 EventResource EVENT = EventResource.getInstance(graph);
\r
159 Double d = graph.getPossibleRelatedValue(event, EVENT.HasTimestamp, Bindings.DOUBLE);
\r
160 return d != null ? d : defaultValue;
\r
163 public static String getTimestampString(ReadGraph graph, double t, boolean timeFormat) {
\r
164 return timeFormat ? formatHMSS(t) : formatSeconds(t);
\r