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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.issues.ui.contribution;
\r
14 import java.util.HashSet;
\r
15 import java.util.Set;
\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
32 * @author Tuukka Lehtonen
\r
34 public class IssueImageDecorationRule implements ImageDecorationRule {
\r
36 Set<Resource> typesToSearch;
\r
40 * @param typesToSearch URIs of types to recurse into while searching for
\r
42 * @throws DatabaseException
\r
44 public IssueImageDecorationRule(ReadGraph graph, String typeToSearch) throws DatabaseException {
\r
45 this(graph, new String[] { typeToSearch });
\r
50 * @param typesToSearch URIs of types to recurse into while searching for
\r
52 * @throws DatabaseException
\r
54 public IssueImageDecorationRule(ReadGraph graph, String type1, String type2) throws DatabaseException {
\r
55 this(graph, new String[] { type1, type2});
\r
60 * @param typesToSearch URIs of types to recurse into while searching for
\r
62 * @throws DatabaseException
\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
71 public boolean isCompatible(Class<?> contentType) {
\r
72 return contentType.equals(Resource.class);
\r
76 public ImageDecorator getImageDecorator(ReadGraph graph, Object content) throws DatabaseException {
\r
77 Resource r = (Resource) content;
\r
79 // Make sure IssueResource service is initialized.
\r
80 Layer0 L0 = Layer0.getInstance(graph);
\r
81 IssueResource.getInstance(graph);
\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
90 final Severity severity = maxSeverity;
\r
91 return new ImageDecorator() {
\r
92 @SuppressWarnings("unchecked")
\r
94 public <Image> Image decorateImage(Image image, String column, int itemIndex) {
\r
95 return (Image) getDecoration((ImageDescriptor) image, severity);
\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