]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/contribution/IssueImageDecorationRule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / contribution / IssueImageDecorationRule.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.issues.ui.contribution;\r
13 \r
14 import java.util.HashSet;\r
15 import java.util.Set;\r
16 \r
17 import org.eclipse.jface.resource.ImageDescriptor;\r
18 import org.eclipse.jface.viewers.IDecoration;\r
19 import org.simantics.browsing.ui.content.ImageDecorator;\r
20 import org.simantics.browsing.ui.model.imagedecorators.ImageDecorationRule;\r
21 import org.simantics.db.ReadGraph;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.issues.Severity;\r
25 import org.simantics.issues.common.MaxIssueSeverityRecursive;\r
26 import org.simantics.issues.ontology.IssueResource;\r
27 import org.simantics.issues.ui.internal.Activator;\r
28 import org.simantics.layer0.Layer0;\r
29 import org.simantics.utils.ui.gfx.DecorationOverlayIcon;\r
30 \r
31 /**\r
32  * @author Tuukka Lehtonen\r
33  */\r
34 public class IssueImageDecorationRule implements ImageDecorationRule {\r
35 \r
36     Set<Resource> typesToSearch;\r
37 \r
38     /**\r
39      * @param graph\r
40      * @param typesToSearch URIs of types to recurse into while searching for\r
41      *        issues\r
42      * @throws DatabaseException \r
43      */\r
44     public IssueImageDecorationRule(ReadGraph graph, String typeToSearch) throws DatabaseException {\r
45         this(graph, new String[] { typeToSearch });\r
46     }\r
47 \r
48     /**\r
49      * @param graph\r
50      * @param typesToSearch URIs of types to recurse into while searching for\r
51      *        issues\r
52      * @throws DatabaseException \r
53      */\r
54     public IssueImageDecorationRule(ReadGraph graph, String type1, String type2) throws DatabaseException {\r
55         this(graph, new String[] { type1, type2});\r
56     }\r
57 \r
58     /**\r
59      * @param graph\r
60      * @param typesToSearch URIs of types to recurse into while searching for\r
61      *        issues\r
62      * @throws DatabaseException \r
63      */\r
64     public IssueImageDecorationRule(ReadGraph graph, String... typesToSearch) throws DatabaseException {\r
65         this.typesToSearch = new HashSet<Resource>(typesToSearch.length);\r
66         for (String uri : typesToSearch)\r
67             this.typesToSearch.add(graph.getResource(uri));\r
68     }\r
69 \r
70     @Override\r
71     public boolean isCompatible(Class<?> contentType) {\r
72         return contentType.equals(Resource.class);\r
73     }\r
74 \r
75     @Override\r
76     public ImageDecorator getImageDecorator(ReadGraph graph, Object content) throws DatabaseException {\r
77         Resource r = (Resource) content;\r
78 \r
79         // Make sure IssueResource service is initialized.\r
80         Layer0 L0 = Layer0.getInstance(graph);\r
81         IssueResource.getInstance(graph);\r
82 \r
83         //Severity maxSeverity = graph.syncRequest(new MaxIssueSeveritySingle(r));\r
84         //System.out.println("calculate maxSeverity(" + NameUtils.getSafeName(graph, r, true) + ")");\r
85         Severity maxSeverity = graph.syncRequest(new MaxIssueSeverityRecursive(r, L0.ConsistsOf, typesToSearch));\r
86         //System.out.println("maxSeverity(" + NameUtils.getSafeName(graph, r, true) + "): " + maxSeverity);\r
87         if (maxSeverity == null)\r
88             return null;\r
89 \r
90         final Severity severity = maxSeverity;\r
91         return new ImageDecorator() {\r
92             @SuppressWarnings("unchecked")\r
93             @Override\r
94             public <Image> Image decorateImage(Image image, String column, int itemIndex) {\r
95                 return (Image) getDecoration((ImageDescriptor) image, severity);\r
96             }\r
97         };\r
98     }\r
99 \r
100     private ImageDescriptor getDecoration(ImageDescriptor original, Severity severity) {\r
101         ImageDescriptor img = Activator.getDefault().getImageRegistry().getDescriptor(severity.toString());\r
102         return (original == null || original.getImageData() == null) ? img\r
103                 : new DecorationOverlayIcon(original, img, IDecoration.BOTTOM_LEFT);\r
104     }\r
105 \r
106 }\r