]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.event/src/org/simantics/event/view/contribution/EventLabelRule.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / view / contribution / EventLabelRule.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.event.view.contribution;
13
14 import java.math.RoundingMode;
15 import java.text.NumberFormat;
16 import java.util.Locale;
17 import java.util.Map;
18
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;
32
33 /**
34  * @author Tuukka Lehtonen
35  */
36 public enum EventLabelRule implements LabelRule {
37
38     INSTANCE;
39
40     public static EventLabelRule get() {
41         return INSTANCE;
42     }
43
44     private static final NumberFormat secondFormatter;
45     private static final TimeFormat timeFormat = new TimeFormat(Double.MAX_VALUE, 3);
46
47     static {
48         secondFormatter = NumberFormat.getNumberInstance(Locale.US);
49         secondFormatter.setMinimumFractionDigits(2);
50         secondFormatter.setMaximumFractionDigits(3);
51         secondFormatter.setRoundingMode(RoundingMode.HALF_EVEN);
52     }
53
54     public static String formatSeconds(double time) {
55         return secondFormatter.format(time);
56     }
57
58     /**
59      * Formats a time specified as a double value in seconds in format
60      * <code>hh:mm:ss.SSS</code>
61      * 
62      * @param time
63      * @return
64      */
65     public static String formatHMSS(double time) {
66         return timeFormat.format(time);
67         }
68
69     @Override
70     public boolean isCompatible(Class<?> contentType) {
71         return contentType.equals(Resource.class);
72     }
73
74     public Map<String, String> getLabel(ReadGraph graph, Object content, boolean allInformation) throws DatabaseException {
75
76         IEclipsePreferences chartPreferenceNode = InstanceScope.INSTANCE.getNode( "org.simantics.charts" );
77         boolean timeFormat = chartPreferenceNode.get("chart.timeformat", "Time").equals("Time");  // Time/Decimal
78
79         Resource event = (Resource) content;
80
81         Layer0 L0 = Layer0.getInstance(graph);
82         EventResource EVENT = EventResource.getInstance(graph);
83
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);
88             if (message == null)
89                 message = graph.getPossibleRelatedValue(event, L0.HasName, Bindings.STRING);
90             if (message == null)
91                 message = NameUtils.getSafeLabel(graph, event);
92         }
93
94         StringBuilder eventType = new StringBuilder();
95         Integer eventTypeNumber = graph.getPossibleRelatedValue(event, EVENT.Event_typeNumber);
96         Resource et = graph.getPossibleObject(event, EVENT.Event_type);
97         if (et != null) {
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);
103         }
104
105         double timeNumeric = getTimestamp(graph, event, 0.0);
106         String time = getTimestampString(graph, timeNumeric, timeFormat);
107
108         Integer index = graph.getPossibleRelatedValue(event, EVENT.Event_index, Bindings.INTEGER);
109         String indexS = "";
110         if (index != null)
111             indexS = String.valueOf(index);
112
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);
119         }
120
121         String sourceName = graph.getPossibleRelatedValue(event, EVENT.Event_sourceName);
122         String milestone = null;
123         String returned = null;
124
125         if (allInformation) {
126             returned = returnedBy != null ? "Yes" : "";
127             if (returnTime == null)
128                 returnTime = "";
129
130             milestone = "";
131             if (graph.hasStatement(event, EVENT.Milestone)) {
132                 milestone = graph.getPossibleRelatedValue(event, EVENT.Event_milestoneLabel);
133                 if (milestone == null) milestone = "?";
134             }
135         }
136
137         return ArrayMap.make(Constants.COLUMN_KEYS, new String[] {
138                 indexS,
139                 time,
140                 String.valueOf(timeNumeric),
141                 milestone,
142                 eventType.toString(),
143                 returned,
144                 tag,
145                 message,
146                 returnTime,
147                 String.valueOf(returnTimeNumeric),
148                 sourceName
149         });
150     }
151
152     @Override
153     public Map<String, String> getLabel(ReadGraph graph, Object content) throws DatabaseException {
154         return getLabel(graph, content, false);
155     }
156
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;
161     }
162
163     public static String getTimestampString(ReadGraph graph, double t, boolean timeFormat) {
164         return timeFormat ? formatHMSS(t) : formatSeconds(t);
165     }
166
167 }