--- /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.issues.ui.contribution;\r
+\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.eclipse.jface.viewers.IDecoration;\r
+import org.simantics.browsing.ui.content.ImageDecorator;\r
+import org.simantics.browsing.ui.model.imagedecorators.ImageDecorationRule;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.issues.Severity;\r
+import org.simantics.issues.common.MaxIssueSeverityRecursive;\r
+import org.simantics.issues.ontology.IssueResource;\r
+import org.simantics.issues.ui.internal.Activator;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.ui.gfx.DecorationOverlayIcon;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class IssueImageDecorationRule implements ImageDecorationRule {\r
+\r
+ Set<Resource> typesToSearch;\r
+\r
+ /**\r
+ * @param graph\r
+ * @param typesToSearch URIs of types to recurse into while searching for\r
+ * issues\r
+ * @throws DatabaseException \r
+ */\r
+ public IssueImageDecorationRule(ReadGraph graph, String typeToSearch) throws DatabaseException {\r
+ this(graph, new String[] { typeToSearch });\r
+ }\r
+\r
+ /**\r
+ * @param graph\r
+ * @param typesToSearch URIs of types to recurse into while searching for\r
+ * issues\r
+ * @throws DatabaseException \r
+ */\r
+ public IssueImageDecorationRule(ReadGraph graph, String type1, String type2) throws DatabaseException {\r
+ this(graph, new String[] { type1, type2});\r
+ }\r
+\r
+ /**\r
+ * @param graph\r
+ * @param typesToSearch URIs of types to recurse into while searching for\r
+ * issues\r
+ * @throws DatabaseException \r
+ */\r
+ public IssueImageDecorationRule(ReadGraph graph, String... typesToSearch) throws DatabaseException {\r
+ this.typesToSearch = new HashSet<Resource>(typesToSearch.length);\r
+ for (String uri : typesToSearch)\r
+ this.typesToSearch.add(graph.getResource(uri));\r
+ }\r
+\r
+ @Override\r
+ public boolean isCompatible(Class<?> contentType) {\r
+ return contentType.equals(Resource.class);\r
+ }\r
+\r
+ @Override\r
+ public ImageDecorator getImageDecorator(ReadGraph graph, Object content) throws DatabaseException {\r
+ Resource r = (Resource) content;\r
+\r
+ // Make sure IssueResource service is initialized.\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ IssueResource.getInstance(graph);\r
+\r
+ //Severity maxSeverity = graph.syncRequest(new MaxIssueSeveritySingle(r));\r
+ //System.out.println("calculate maxSeverity(" + NameUtils.getSafeName(graph, r, true) + ")");\r
+ Severity maxSeverity = graph.syncRequest(new MaxIssueSeverityRecursive(r, L0.ConsistsOf, typesToSearch));\r
+ //System.out.println("maxSeverity(" + NameUtils.getSafeName(graph, r, true) + "): " + maxSeverity);\r
+ if (maxSeverity == null)\r
+ return null;\r
+\r
+ final Severity severity = maxSeverity;\r
+ return new ImageDecorator() {\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <Image> Image decorateImage(Image image, String column, int itemIndex) {\r
+ return (Image) getDecoration((ImageDescriptor) image, severity);\r
+ }\r
+ };\r
+ }\r
+\r
+ private ImageDescriptor getDecoration(ImageDescriptor original, Severity severity) {\r
+ ImageDescriptor img = Activator.getDefault().getImageRegistry().getDescriptor(severity.toString());\r
+ return (original == null || original.getImageData() == null) ? img\r
+ : new DecorationOverlayIcon(original, img, IDecoration.BOTTOM_LEFT);\r
+ }\r
+\r
+}\r