-/*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.event.view.contribution;\r
-\r
-import java.math.RoundingMode;\r
-import java.text.NumberFormat;\r
-import java.util.Locale;\r
-import java.util.Map;\r
-\r
-import org.eclipse.core.runtime.preferences.IEclipsePreferences;\r
-import org.eclipse.core.runtime.preferences.InstanceScope;\r
-import org.simantics.browsing.ui.model.labels.LabelRule;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.event.ontology.EventResource;\r
-import org.simantics.event.view.Constants;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.utils.datastructures.ArrayMap;\r
-import org.simantics.utils.format.TimeFormat;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public enum EventLabelRule implements LabelRule {\r
-\r
- INSTANCE;\r
-\r
- public static EventLabelRule get() {\r
- return INSTANCE;\r
- }\r
-\r
- private static final NumberFormat secondFormatter;\r
- private static final TimeFormat timeFormat = new TimeFormat(Double.MAX_VALUE, 3);\r
-\r
- static {\r
- secondFormatter = NumberFormat.getNumberInstance(Locale.US);\r
- secondFormatter.setMinimumFractionDigits(2);\r
- secondFormatter.setMaximumFractionDigits(3);\r
- secondFormatter.setRoundingMode(RoundingMode.HALF_EVEN);\r
- }\r
-\r
- public static String formatSeconds(double time) {\r
- return secondFormatter.format(time);\r
- }\r
-\r
- /**\r
- * Formats a time specified as a double value in seconds in format\r
- * <code>hh:mm:ss.SSS</code>\r
- * \r
- * @param time\r
- * @return\r
- */\r
- public static String formatHMSS(double time) {\r
- return timeFormat.format(time);\r
- }\r
-\r
- @Override\r
- public boolean isCompatible(Class<?> contentType) {\r
- return contentType.equals(Resource.class);\r
- }\r
-\r
- public Map<String, String> getLabel(ReadGraph graph, Object content, boolean allInformation) throws DatabaseException {\r
-\r
- IEclipsePreferences chartPreferenceNode = InstanceScope.INSTANCE.getNode( "org.simantics.charts" );\r
- boolean timeFormat = chartPreferenceNode.get("chart.timeformat", "Time").equals("Time"); // Time/Decimal\r
-\r
- Resource event = (Resource) content;\r
-\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- EventResource EVENT = EventResource.getInstance(graph);\r
-\r
- String tag = graph.getPossibleRelatedValue(event, EVENT.Event_tag, Bindings.STRING);\r
- String message = graph.getPossibleRelatedValue(event, EVENT.Event_message, Bindings.STRING);\r
- if (message == null) {\r
- message = graph.getPossibleRelatedValue(event, L0.HasLabel, Bindings.STRING);\r
- if (message == null)\r
- message = graph.getPossibleRelatedValue(event, L0.HasName, Bindings.STRING);\r
- if (message == null)\r
- message = NameUtils.getSafeLabel(graph, event);\r
- }\r
-\r
- StringBuilder eventType = new StringBuilder();\r
- Integer eventTypeNumber = graph.getPossibleRelatedValue(event, EVENT.Event_typeNumber);\r
- Resource et = graph.getPossibleObject(event, EVENT.Event_type);\r
- if (et != null) {\r
- if (eventTypeNumber != null)\r
- eventType.append(eventTypeNumber).append(": ");\r
- eventType.append(NameUtils.getSafeLabel(graph, et));\r
- } else if (eventTypeNumber != null) {\r
- eventType.append(eventTypeNumber);\r
- }\r
-\r
- double timeNumeric = getTimestamp(graph, event, 0.0);\r
- String time = getTimestampString(graph, timeNumeric, timeFormat);\r
-\r
- Integer index = graph.getPossibleRelatedValue(event, EVENT.Event_index, Bindings.INTEGER);\r
- String indexS = "";\r
- if (index != null)\r
- indexS = String.valueOf(index);\r
-\r
- double returnTimeNumeric = 0;\r
- String returnTime = null;\r
- Resource returnedBy = graph.getPossibleObject(event, EVENT.ReturnedBy);\r
- if (returnedBy != null) {\r
- returnTimeNumeric = getTimestamp(graph, returnedBy, 0.0);\r
- returnTime = getTimestampString(graph, returnTimeNumeric, timeFormat);\r
- }\r
-\r
- String sourceName = graph.getPossibleRelatedValue(event, EVENT.Event_sourceName);\r
- String milestone = null;\r
- String returned = null;\r
-\r
- if (allInformation) {\r
- returned = returnedBy != null ? "Yes" : "";\r
- if (returnTime == null)\r
- returnTime = "";\r
-\r
- milestone = "";\r
- if (graph.hasStatement(event, EVENT.Milestone)) {\r
- milestone = graph.getPossibleRelatedValue(event, EVENT.Event_milestoneLabel);\r
- if (milestone == null) milestone = "?";\r
- }\r
- }\r
-\r
- return ArrayMap.make(Constants.COLUMN_KEYS, new String[] {\r
- indexS,\r
- time,\r
- String.valueOf(timeNumeric),\r
- milestone,\r
- eventType.toString(),\r
- returned,\r
- tag,\r
- message,\r
- returnTime,\r
- String.valueOf(returnTimeNumeric),\r
- sourceName\r
- });\r
- }\r
-\r
- @Override\r
- public Map<String, String> getLabel(ReadGraph graph, Object content) throws DatabaseException {\r
- return getLabel(graph, content, false);\r
- }\r
-\r
- public static double getTimestamp(ReadGraph graph, Resource event, double defaultValue) throws DatabaseException {\r
- EventResource EVENT = EventResource.getInstance(graph);\r
- Double d = graph.getPossibleRelatedValue(event, EVENT.HasTimestamp, Bindings.DOUBLE);\r
- return d != null ? d : defaultValue;\r
- }\r
-\r
- public static String getTimestampString(ReadGraph graph, double t, boolean timeFormat) {\r
- return timeFormat ? formatHMSS(t) : formatSeconds(t);\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.event.view.contribution;
+
+import java.math.RoundingMode;
+import java.text.NumberFormat;
+import java.util.Locale;
+import java.util.Map;
+
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.simantics.browsing.ui.model.labels.LabelRule;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.event.ontology.EventResource;
+import org.simantics.event.view.Constants;
+import org.simantics.layer0.Layer0;
+import org.simantics.utils.datastructures.ArrayMap;
+import org.simantics.utils.format.TimeFormat;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public enum EventLabelRule implements LabelRule {
+
+ INSTANCE;
+
+ public static EventLabelRule get() {
+ return INSTANCE;
+ }
+
+ private static final NumberFormat secondFormatter;
+ private static final TimeFormat timeFormat = new TimeFormat(Double.MAX_VALUE, 3);
+
+ static {
+ secondFormatter = NumberFormat.getNumberInstance(Locale.US);
+ secondFormatter.setMinimumFractionDigits(2);
+ secondFormatter.setMaximumFractionDigits(3);
+ secondFormatter.setRoundingMode(RoundingMode.HALF_EVEN);
+ }
+
+ public static String formatSeconds(double time) {
+ return secondFormatter.format(time);
+ }
+
+ /**
+ * Formats a time specified as a double value in seconds in format
+ * <code>hh:mm:ss.SSS</code>
+ *
+ * @param time
+ * @return
+ */
+ public static String formatHMSS(double time) {
+ return timeFormat.format(time);
+ }
+
+ @Override
+ public boolean isCompatible(Class<?> contentType) {
+ return contentType.equals(Resource.class);
+ }
+
+ public Map<String, String> getLabel(ReadGraph graph, Object content, boolean allInformation) throws DatabaseException {
+
+ IEclipsePreferences chartPreferenceNode = InstanceScope.INSTANCE.getNode( "org.simantics.charts" );
+ boolean timeFormat = chartPreferenceNode.get("chart.timeformat", "Time").equals("Time"); // Time/Decimal
+
+ Resource event = (Resource) content;
+
+ Layer0 L0 = Layer0.getInstance(graph);
+ EventResource EVENT = EventResource.getInstance(graph);
+
+ String tag = graph.getPossibleRelatedValue(event, EVENT.Event_tag, Bindings.STRING);
+ String message = graph.getPossibleRelatedValue(event, EVENT.Event_message, Bindings.STRING);
+ if (message == null) {
+ message = graph.getPossibleRelatedValue(event, L0.HasLabel, Bindings.STRING);
+ if (message == null)
+ message = graph.getPossibleRelatedValue(event, L0.HasName, Bindings.STRING);
+ if (message == null)
+ message = NameUtils.getSafeLabel(graph, event);
+ }
+
+ StringBuilder eventType = new StringBuilder();
+ Integer eventTypeNumber = graph.getPossibleRelatedValue(event, EVENT.Event_typeNumber);
+ Resource et = graph.getPossibleObject(event, EVENT.Event_type);
+ if (et != null) {
+ if (eventTypeNumber != null)
+ eventType.append(eventTypeNumber).append(": ");
+ eventType.append(NameUtils.getSafeLabel(graph, et));
+ } else if (eventTypeNumber != null) {
+ eventType.append(eventTypeNumber);
+ }
+
+ double timeNumeric = getTimestamp(graph, event, 0.0);
+ String time = getTimestampString(graph, timeNumeric, timeFormat);
+
+ Integer index = graph.getPossibleRelatedValue(event, EVENT.Event_index, Bindings.INTEGER);
+ String indexS = "";
+ if (index != null)
+ indexS = String.valueOf(index);
+
+ double returnTimeNumeric = 0;
+ String returnTime = null;
+ Resource returnedBy = graph.getPossibleObject(event, EVENT.ReturnedBy);
+ if (returnedBy != null) {
+ returnTimeNumeric = getTimestamp(graph, returnedBy, 0.0);
+ returnTime = getTimestampString(graph, returnTimeNumeric, timeFormat);
+ }
+
+ String sourceName = graph.getPossibleRelatedValue(event, EVENT.Event_sourceName);
+ String milestone = null;
+ String returned = null;
+
+ if (allInformation) {
+ returned = returnedBy != null ? "Yes" : "";
+ if (returnTime == null)
+ returnTime = "";
+
+ milestone = "";
+ if (graph.hasStatement(event, EVENT.Milestone)) {
+ milestone = graph.getPossibleRelatedValue(event, EVENT.Event_milestoneLabel);
+ if (milestone == null) milestone = "?";
+ }
+ }
+
+ return ArrayMap.make(Constants.COLUMN_KEYS, new String[] {
+ indexS,
+ time,
+ String.valueOf(timeNumeric),
+ milestone,
+ eventType.toString(),
+ returned,
+ tag,
+ message,
+ returnTime,
+ String.valueOf(returnTimeNumeric),
+ sourceName
+ });
+ }
+
+ @Override
+ public Map<String, String> getLabel(ReadGraph graph, Object content) throws DatabaseException {
+ return getLabel(graph, content, false);
+ }
+
+ public static double getTimestamp(ReadGraph graph, Resource event, double defaultValue) throws DatabaseException {
+ EventResource EVENT = EventResource.getInstance(graph);
+ Double d = graph.getPossibleRelatedValue(event, EVENT.HasTimestamp, Bindings.DOUBLE);
+ return d != null ? d : defaultValue;
+ }
+
+ public static String getTimestampString(ReadGraph graph, double t, boolean timeFormat) {
+ return timeFormat ? formatHMSS(t) : formatSeconds(t);
+ }
+
+}