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