]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/labeldecorators/ConstantLabelDecorationRule.java
2df8e52ee93ca8e98b1b85e9ba5655e0e6c57197
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / labeldecorators / ConstantLabelDecorationRule.java
1 /*******************************************************************************
2  * Copyright (c) 2010, 2011 Association for Decentralized Information Management in
3  * 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.browsing.ui.model.labeldecorators;
13
14 import org.eclipse.jface.resource.ColorDescriptor;
15 import org.eclipse.jface.resource.FontDescriptor;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.FontData;
18 import org.eclipse.swt.graphics.RGB;
19 import org.simantics.browsing.ui.content.LabelDecorator;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.viewpoint.ontology.ViewpointResource;
25
26 /**
27  * Constant label decoration rule decorates the label in a fixed way.
28  * @author Hannu Niemistö
29  */
30 public class ConstantLabelDecorationRule extends AbstractLabelDecorator implements LabelDecorationRule {
31
32     /**
33      * For headless instances where no Display is available
34      */
35     private static final FontDescriptor DEFAULT_FONT_DESCRIPTOR = FontDescriptor.createFrom(new FontData("Arial", 11, 0));
36
37     String prefix;
38     String postfix;
39     ColorDescriptor foregroundColor;
40     ColorDescriptor backgroundColor;
41     int style;
42     
43     public ConstantLabelDecorationRule(String format,
44             ColorDescriptor foregroundColor, ColorDescriptor backgroundColor,
45             String style) {
46         if(format != null) {
47             String[] split = format.split("%s", 2);
48             if(split.length == 1) {
49                 prefix = "";
50                 postfix = split[0];
51             }
52             else {
53                 prefix = split[0];
54                 postfix = split[1];
55             }
56         }
57         this.foregroundColor = foregroundColor;
58         this.backgroundColor = backgroundColor;
59         this.style = SWT.NORMAL;
60         if(style != null)
61             for(char c : style.toCharArray())
62                 switch(c) {
63                 case 'B':
64                 case 'b':
65                     this.style |= SWT.BOLD;
66                     break;
67                 case 'I':
68                 case 'i':
69                     this.style |= SWT.ITALIC;
70                     break;
71                 default:
72                     System.err.println("Invalid character '" + c + "' in style string. Only B and I recognized.");
73                 }
74     }
75
76     private static ColorDescriptor getPossibleRelatedColor(ReadGraph g, Resource subject, Resource predicate) throws DatabaseException {
77         Resource value = g.getPossibleObject(subject, predicate);
78         if(value == null)
79             return null;
80         RGB rgb = g.adapt(value, RGB.class);
81         return ColorDescriptor.createFrom(rgb);
82     }
83     
84     public static ConstantLabelDecorationRule create(ReadGraph g, Resource r) throws DatabaseException {
85         ViewpointResource vr = ViewpointResource.getInstance(g);        
86         String format = g.getPossibleRelatedValue(r, vr.ConstantLabelDecorationRule_HasFormat, Bindings.STRING);
87
88         ColorDescriptor foregroundColor = getPossibleRelatedColor(g, r, vr.ConstantLabelDecorationRule_HasForegroundColor);
89         ColorDescriptor backgroundColor = getPossibleRelatedColor(g, r, vr.ConstantLabelDecorationRule_HasBackgroundColor);
90         
91         String style = g.getPossibleRelatedValue(r, vr.ConstantLabelDecorationRule_HasStyle, Bindings.STRING);
92         
93         return new ConstantLabelDecorationRule(
94                 format, 
95                 foregroundColor, 
96                 backgroundColor,
97                 style
98         );
99     }
100     
101     @Override
102     public boolean isCompatible(Class<?> contentType) {
103         return true;
104     }
105
106     @Override
107     public LabelDecorator getLabelDecorator(ReadGraph graph, Object content)
108             throws DatabaseException {
109         return this;
110     }
111     
112     @Override
113     public String decorateLabel(String label, String column, int itemIndex) {
114         if(postfix == null)
115             return label;
116         else
117             return prefix + label + postfix;
118     }
119     
120     @SuppressWarnings("unchecked")
121     public <Color> Color decorateForeground(Color color, String column, int itemIndex) {
122         if(foregroundColor == null)
123             return color;
124         else
125             return (Color)foregroundColor;
126     }
127
128     @SuppressWarnings("unchecked")
129     public <Color> Color decorateBackground(Color color, String column, int itemIndex) {
130         if(backgroundColor == null)
131             return color;
132         else
133             return (Color)backgroundColor;
134     }
135     
136     @SuppressWarnings("unchecked")
137     public <Font> Font decorateFont(Font font, String column, int itemIndex) {
138         if(style == 0)
139             return font;
140         else {
141             FontDescriptor desc = (FontDescriptor)font;
142             if(desc == null)
143                 desc = DEFAULT_FONT_DESCRIPTOR;
144             return (Font)desc.withStyle(style);
145         }
146     }
147 }