]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/contribution/IssueLabelDecorationRule.java
Allow overriding issue hidden-ness/hiding logic in inheriting ontologies
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / contribution / IssueLabelDecorationRule.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 org.eclipse.jface.resource.FontDescriptor;
15 import org.eclipse.swt.SWT;
16 import org.simantics.browsing.ui.content.LabelDecorator;
17 import org.simantics.browsing.ui.model.labeldecorators.LabelDecorationRule;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.variable.Variable;
23 import org.simantics.issues.ontology.IssueResource;
24
25 /**
26  * @author Tuukka Lehtonen
27  */
28 public enum IssueLabelDecorationRule implements LabelDecorationRule {
29
30     INSTANCE;
31
32     public static IssueLabelDecorationRule get() {
33         return INSTANCE;
34     }
35
36     @Override
37     public boolean isCompatible(Class<?> contentType) {
38         return contentType.equals(Variable.class);
39     }
40
41     @Override
42     public LabelDecorator getLabelDecorator(ReadGraph graph, Object content) throws DatabaseException {
43         Variable issue = (Variable) content;
44
45         boolean hidden = false;
46         boolean user = false;
47         boolean resolved = false;
48
49         Resource issueR = issue.getPossibleRepresents(graph);
50         if (issueR != null) {
51             IssueResource ISSUE = IssueResource.getInstance(graph);
52             hidden = graph.hasStatement(issueR, ISSUE.Hidden);
53             user = graph.hasStatement(issueR, ISSUE.UserIssue);
54             resolved = graph.hasStatement(issueR, ISSUE.Resolved);
55         } else {
56             hidden = Boolean.TRUE.equals(issue.getPossiblePropertyValue(graph, "hidden", Bindings.BOOLEAN));
57         }
58
59         int index = (hidden ? 1 : 0) + (user ? 2 : 0) + (resolved ? 4 : 0);
60         return DECORATORS[index];
61     }
62
63     private static final Decorator[] DECORATORS = {
64             null,
65             new Decorator(true, false, false),
66             new Decorator(false, true, false),
67             new Decorator(true, true, false),
68             new Decorator(false, false, true),
69             new Decorator(true, false, true),
70             new Decorator(false, true, true),
71             new Decorator(true, true, true),
72     };
73
74     private static class Decorator extends LabelDecorator.Stub {
75         private boolean hidden;
76         private boolean user;
77         private boolean resolved;
78
79         public Decorator(boolean hidden, boolean user, boolean resolved) {
80             this.hidden = hidden;
81             this.user = user;
82             this.resolved = resolved;
83         }
84
85         @SuppressWarnings("unchecked")
86         @Override
87         public <F> F decorateFont(F font, String column, int itemIndex) {
88             int style = 0;
89             style |= resolved ? SWT.ITALIC : 0;
90             //style |= hidden ? SWT.BOLD : 0;
91             return style != 0 ? (F) ((FontDescriptor) font).setStyle(style) : font;
92         }
93         @SuppressWarnings("unchecked")
94         @Override
95         public <C> C decorateForeground(C color, String column, int itemIndex) {
96             if (hidden)
97                 return (C) Constants.HIDDEN_FG;
98             return color;
99         }
100         @SuppressWarnings("unchecked")
101         @Override
102         public <C> C decorateBackground(C color, String column, int itemIndex) {
103             if (user)
104                 return (C) Constants.USER_BG;
105             return color;
106         }
107     };
108
109 }