]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/contribution/IssueImageDecorationRule.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / contribution / IssueImageDecorationRule.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.issues.ui.contribution;
13
14 import java.util.HashSet;
15 import java.util.Set;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.viewers.IDecoration;
19 import org.simantics.browsing.ui.content.ImageDecorator;
20 import org.simantics.browsing.ui.model.imagedecorators.ImageDecorationRule;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.issues.Severity;
25 import org.simantics.issues.common.MaxIssueSeverityRecursive;
26 import org.simantics.issues.ontology.IssueResource;
27 import org.simantics.issues.ui.internal.Activator;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.utils.ui.gfx.DecorationOverlayIcon;
30
31 /**
32  * @author Tuukka Lehtonen
33  */
34 public class IssueImageDecorationRule implements ImageDecorationRule {
35
36     Set<Resource> typesToSearch;
37
38     /**
39      * @param graph
40      * @param typesToSearch URIs of types to recurse into while searching for
41      *        issues
42      * @throws DatabaseException 
43      */
44     public IssueImageDecorationRule(ReadGraph graph, String typeToSearch) throws DatabaseException {
45         this(graph, new String[] { typeToSearch });
46     }
47
48     /**
49      * @param graph
50      * @param typesToSearch URIs of types to recurse into while searching for
51      *        issues
52      * @throws DatabaseException 
53      */
54     public IssueImageDecorationRule(ReadGraph graph, String type1, String type2) throws DatabaseException {
55         this(graph, new String[] { type1, type2});
56     }
57
58     /**
59      * @param graph
60      * @param typesToSearch URIs of types to recurse into while searching for
61      *        issues
62      * @throws DatabaseException 
63      */
64     public IssueImageDecorationRule(ReadGraph graph, String... typesToSearch) throws DatabaseException {
65         this.typesToSearch = new HashSet<Resource>(typesToSearch.length);
66         for (String uri : typesToSearch)
67             this.typesToSearch.add(graph.getResource(uri));
68     }
69
70     @Override
71     public boolean isCompatible(Class<?> contentType) {
72         return contentType.equals(Resource.class);
73     }
74
75     @Override
76     public ImageDecorator getImageDecorator(ReadGraph graph, Object content) throws DatabaseException {
77         Resource r = (Resource) content;
78
79         // Make sure IssueResource service is initialized.
80         Layer0 L0 = Layer0.getInstance(graph);
81         IssueResource.getInstance(graph);
82
83         //Severity maxSeverity = graph.syncRequest(new MaxIssueSeveritySingle(r));
84         //System.out.println("calculate maxSeverity(" + NameUtils.getSafeName(graph, r, true) + ")");
85         Severity maxSeverity = graph.syncRequest(new MaxIssueSeverityRecursive(r, L0.ConsistsOf, typesToSearch));
86         //System.out.println("maxSeverity(" + NameUtils.getSafeName(graph, r, true) + "): " + maxSeverity);
87         if (maxSeverity == null)
88             return null;
89
90         final Severity severity = maxSeverity;
91         return new ImageDecorator() {
92             @SuppressWarnings("unchecked")
93             @Override
94             public <Image> Image decorateImage(Image image, String column, int itemIndex) {
95                 return (Image) getDecoration((ImageDescriptor) image, severity);
96             }
97         };
98     }
99
100     private ImageDescriptor getDecoration(ImageDescriptor original, Severity severity) {
101         ImageDescriptor img = Activator.getDefault().getImageRegistry().getDescriptor(severity.toString());
102         return (original == null || original.getImageData() == null) ? img
103                 : new DecorationOverlayIcon(original, img, IDecoration.BOTTOM_LEFT);
104     }
105
106 }