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