]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/IssueImageRule.java
10de114c780b55a6b2cb1162940fc57d6ecc7053
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / IssueImageRule.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2017 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  *     Semantum Oy - #6948
12  *******************************************************************************/
13 package org.simantics.issues.ui;
14
15 import java.util.Collections;
16 import java.util.Map;
17
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.simantics.browsing.ui.common.ColumnKeys;
20 import org.simantics.browsing.ui.model.images.ImageRule;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.variable.Variable;
25 import org.simantics.issues.ontology.IssueResource;
26 import org.simantics.silk.SilkResources;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class IssueImageRule implements ImageRule {
32
33     private final String DESCRIPTION = ColumnKeys.SINGLE;
34
35     private final ImageDescriptor tick;
36     private final ImageDescriptor fatal;
37     private final ImageDescriptor error;
38     private final ImageDescriptor warning;
39     private final ImageDescriptor info;
40     private final ImageDescriptor note;
41     private final ImageDescriptor help;
42
43     public IssueImageRule(ReadGraph graph) throws DatabaseException {
44         SilkResources SILK = SilkResources.getInstance(graph);
45         tick = graph.adapt(SILK.tick, ImageDescriptor.class);
46         fatal = graph.adapt(SILK.bomb, ImageDescriptor.class);
47         error = graph.adapt(SILK.exclamation, ImageDescriptor.class);
48         warning = graph.adapt(SILK.error, ImageDescriptor.class);
49         info = graph.adapt(SILK.information, ImageDescriptor.class);
50         note = graph.adapt(SILK.note, ImageDescriptor.class);
51         help = graph.adapt(SILK.help, ImageDescriptor.class);
52     }
53
54     @Override
55     public boolean isCompatible(Class<?> contentType) {
56         return contentType.equals(Variable.class);
57     }
58
59     @Override
60     public Map<String, ImageDescriptor> getImage(ReadGraph graph, Object content) throws DatabaseException {
61         Variable issue = (Variable) content;
62         String severity = issue.getPossiblePropertyValue(graph, "severity");
63         if (severity == null)
64             return Collections.emptyMap();
65         boolean resolved = isResolved(graph, issue);
66         return Collections.singletonMap(DESCRIPTION, resolved ? tick : toImageDescriptor(severity));
67     }
68
69     public boolean isResolved(ReadGraph graph, Variable issue) throws DatabaseException {
70         Resource issueResource = issue.getPossibleRepresents(graph);
71         if (issueResource != null)
72             return graph.hasStatement(issueResource, IssueResource.getInstance(graph).Resolved);
73
74         Boolean resolved = issue.getPossiblePropertyValue(graph, "resolved");
75         return Boolean.TRUE.equals(resolved);
76     }
77
78     private ImageDescriptor toImageDescriptor(String severity) {
79         switch (severity) {
80         case "Fatal":   return fatal;
81         case "Error":   return error;
82         case "Warning": return warning;
83         case "Info":    return info;
84         case "Note":    return note;
85         default:        return help;
86         }
87     }
88
89 }