--- /dev/null
+/*******************************************************************************\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 gnu.trove.map.hash.TObjectDoubleHashMap;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.List;\r
+\r
+import org.simantics.browsing.ui.model.children.ChildRule;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.service.QueryControl;\r
+import org.simantics.event.ontology.EventResource;\r
+import org.simantics.event.view.Constants;\r
+import org.simantics.event.view.preference.EventPrefs;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+\r
+/**\r
+ * A child rule that looks for all event logs within the active models of a\r
+ * project. The rule takes into account the preference setting for showing hidden\r
+ * event logs.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public enum ProjectEventsRule implements ChildRule {\r
+\r
+ INSTANCE;\r
+\r
+ public static ProjectEventsRule get() {\r
+ return INSTANCE;\r
+ }\r
+\r
+ private static class Prefs {\r
+ public final boolean hideInfoEvents;\r
+ public final boolean hideWarningEvents;\r
+ public final boolean hideReturnEvents;\r
+ public final boolean showHiddenEvents;\r
+ public final boolean showOnlyMilestones;\r
+ public final boolean showOnlyActiveEvents;\r
+ public Prefs(\r
+ boolean hideInfoEvents,\r
+ boolean hideWarningEvents,\r
+ boolean hideReturnEvents,\r
+ boolean showHiddenEvents,\r
+ boolean showOnlyMilestones,\r
+ boolean showOnlyActiveEvents) {\r
+ this.hideInfoEvents = hideInfoEvents;\r
+ this.hideWarningEvents = hideWarningEvents;\r
+ this.hideReturnEvents = hideReturnEvents;\r
+ this.showHiddenEvents = showHiddenEvents;\r
+ this.showOnlyMilestones = showOnlyMilestones;\r
+ this.showOnlyActiveEvents = showOnlyActiveEvents;\r
+ }\r
+\r
+ public static Prefs read(ReadGraph graph, Resource project) throws DatabaseException {\r
+ return new Prefs(\r
+ EventPrefs.hideInfoEvents(graph, project),\r
+ EventPrefs.hideWarningEvents(graph, project),\r
+ EventPrefs.hideReturnEvents(graph, project),\r
+ EventPrefs.showHiddenEvents(graph, project),\r
+ EventPrefs.showOnlyMilestones(graph, project),\r
+ EventPrefs.showOnlyActiveEvents(graph, project));\r
+ }\r
+ }\r
+\r
+ public Collection<?> getChildrenImpl(ReadGraph graph, Resource project, Prefs prefs, List<Resource> eventLogs) throws DatabaseException {\r
+\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+\r
+ EventResource EVENT = EventResource.getInstance(graph);\r
+\r
+ final boolean hideInfoEvents = prefs.hideInfoEvents;\r
+ final boolean hideWarningEvents = prefs.hideWarningEvents;\r
+ final boolean hideReturnEvents = prefs.hideReturnEvents;\r
+ final boolean showHiddenEvents = prefs.showHiddenEvents;\r
+ final boolean showOnlyMilestones = prefs.showOnlyMilestones;\r
+ final boolean showOnlyActiveEvents= prefs.showOnlyActiveEvents;\r
+\r
+ ArrayList<Resource> allEvents = new ArrayList<Resource>();\r
+ for (Resource log : eventLogs) {\r
+ for (Resource slice : graph.getObjects(log, L0.ConsistsOf)) {\r
+ Collection<Resource> events = graph.getObjects(slice, L0.ConsistsOf);\r
+ allEvents.addAll(events);\r
+ }\r
+ }\r
+\r
+ List<Object> result = new ArrayList<Object>();\r
+ final TObjectDoubleHashMap<Object> times = new TObjectDoubleHashMap<Object>();\r
+ final Double defaultTime = 0.0;\r
+\r
+ for (Resource event : allEvents) {\r
+ if (showOnlyMilestones) {\r
+ boolean isMilestone = graph.hasStatement(event, EVENT.Milestone);\r
+ if (!isMilestone)\r
+ continue;\r
+ }\r
+\r
+ if (!showHiddenEvents && graph.hasStatement(event, EVENT.Hidden))\r
+ continue;\r
+\r
+ if (showOnlyActiveEvents\r
+ && (graph.hasStatement(event, EVENT.Returns)\r
+ || graph.hasStatement(event, EVENT.ReturnedBy)\r
+ || graph.hasStatement(event, EVENT.NoReturn)))\r
+ {\r
+ continue;\r
+ }\r
+\r
+ // Skip return events if thus preferred.\r
+ if (hideReturnEvents && graph.hasStatement(event, EVENT.ReturnEvent)) {\r
+ if (graph.getPossibleObject(event, EVENT.Returns) != null)\r
+ continue;\r
+ }\r
+\r
+ // Filter by event type severity\r
+ Resource eventType = graph.getPossibleObject(event, EVENT.Event_type);\r
+ if (eventType != null) {\r
+ Integer severity = graph.getPossibleRelatedValue(eventType, EVENT.EventType_severity);\r
+ if (severity != null) {\r
+ if (hideInfoEvents && severity >= Constants.EVENT_SEVERITY_INFO\r
+ && severity < (Constants.EVENT_SEVERITY_INFO + Constants.EVENT_SEVERITY_STEP))\r
+ continue;\r
+ if (hideWarningEvents && severity >= Constants.EVENT_SEVERITY_WARNING\r
+ && severity < (Constants.EVENT_SEVERITY_WARNING + Constants.EVENT_SEVERITY_STEP))\r
+ continue;\r
+ }\r
+ }\r
+\r
+ double timeNumeric = EventLabelRule.getTimestamp(graph, event, defaultTime);\r
+ times.put(event, timeNumeric);\r
+ result.add(event);\r
+ }\r
+\r
+ Collections.sort(result, new Comparator<Object>() {\r
+ @Override\r
+ public int compare(Object o1, Object o2) {\r
+ double t1 = times.get(o1);\r
+ double t2 = times.get(o2);\r
+ return Double.compare(t1, t2);\r
+ }\r
+ });\r
+\r
+ return result;\r
+\r
+ }\r
+ \r
+ @Override\r
+ public Collection<?> getChildren(ReadGraph graph, Object parent) throws DatabaseException {\r
+\r
+ Layer0X L0X = Layer0X.getInstance(graph);\r
+ SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+ EventResource EVENT = EventResource.getInstance(graph);\r
+\r
+ Resource project = (Resource) parent;\r
+\r
+ List<Resource> eventLogs = new ArrayList<Resource>();\r
+ for (Resource activeModel : graph.syncRequest(new ObjectsWithType(project, L0X.Activates, SIMU.Model))) {\r
+ for (Resource eventLog : graph.syncRequest(new ObjectsWithType(activeModel, EVENT.HasEventLog, EVENT.EventLog))) {\r
+ if (!graph.hasStatement(eventLog, EVENT.Hidden)) {\r
+ eventLogs.add(eventLog);\r
+ graph.getRelatedValue(eventLog, EVENT.HasModificationCounter, Bindings.INTEGER);\r
+ }\r
+ }\r
+ }\r
+\r
+ QueryControl qc = graph.getService(QueryControl.class);\r
+\r
+ return getChildrenImpl(qc.getIndependentGraph(graph), project, Prefs.read(graph, project), eventLogs);\r
+\r
+ }\r
+\r
+ @Override\r
+ public Collection<?> getParents(ReadGraph graph, Object child) throws DatabaseException {\r
+ if (!(child instanceof Resource))\r
+ return Collections.emptyList();\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ return graph.getObjects((Resource) child, L0.PartOf);\r
+ }\r
+\r
+ @Override\r
+ public boolean isCompatible(Class<?> contentType) {\r
+ return contentType.equals(Resource.class);\r
+ }\r
+\r
+}\r