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