]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/labeldecorators/ConstantLabelDecorationRule.java
Replace System.err and System.out with SLF4J Logging
[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     ColorDescriptor foregroundColor;
43     ColorDescriptor backgroundColor;
44     int style;
45     
46     public ConstantLabelDecorationRule(String format,
47             ColorDescriptor foregroundColor, ColorDescriptor backgroundColor,
48             String style) {
49         if(format != null) {
50             String[] split = format.split("%s", 2);
51             if(split.length == 1) {
52                 prefix = "";
53                 postfix = split[0];
54             }
55             else {
56                 prefix = split[0];
57                 postfix = split[1];
58             }
59         }
60         this.foregroundColor = foregroundColor;
61         this.backgroundColor = backgroundColor;
62         this.style = SWT.NORMAL;
63         if(style != null)
64             for(char c : style.toCharArray())
65                 switch(c) {
66                 case 'B':
67                 case 'b':
68                     this.style |= SWT.BOLD;
69                     break;
70                 case 'I':
71                 case 'i':
72                     this.style |= SWT.ITALIC;
73                     break;
74                 default:
75                    LOGGER.info("Invalid character '" + c + "' in style string. Only B and I recognized.");
76                 }
77     }
78
79     private static ColorDescriptor getPossibleRelatedColor(ReadGraph g, Resource subject, Resource predicate) throws DatabaseException {
80         Resource value = g.getPossibleObject(subject, predicate);
81         if(value == null)
82             return null;
83         RGB rgb = g.adapt(value, RGB.class);
84         return ColorDescriptor.createFrom(rgb);
85     }
86     
87     public static ConstantLabelDecorationRule create(ReadGraph g, Resource r) throws DatabaseException {
88         ViewpointResource vr = ViewpointResource.getInstance(g);        
89         String format = g.getPossibleRelatedValue(r, vr.ConstantLabelDecorationRule_HasFormat, Bindings.STRING);
90
91         ColorDescriptor foregroundColor = getPossibleRelatedColor(g, r, vr.ConstantLabelDecorationRule_HasForegroundColor);
92         ColorDescriptor backgroundColor = getPossibleRelatedColor(g, r, vr.ConstantLabelDecorationRule_HasBackgroundColor);
93         
94         String style = g.getPossibleRelatedValue(r, vr.ConstantLabelDecorationRule_HasStyle, Bindings.STRING);
95         
96         return new ConstantLabelDecorationRule(
97                 format, 
98                 foregroundColor, 
99                 backgroundColor,
100                 style
101         );
102     }
103     
104     @Override
105     public boolean isCompatible(Class<?> contentType) {
106         return true;
107     }
108
109     @Override
110     public LabelDecorator getLabelDecorator(ReadGraph graph, Object content)
111             throws DatabaseException {
112         return this;
113     }
114     
115     @Override
116     public String decorateLabel(String label, String column, int itemIndex) {
117         if(postfix == null)
118             return label;
119         else
120             return prefix + label + postfix;
121     }
122     
123     @SuppressWarnings("unchecked")
124     public <Color> Color decorateForeground(Color color, String column, int itemIndex) {
125         if(foregroundColor == null)
126             return color;
127         else
128             return (Color)foregroundColor;
129     }
130
131     @SuppressWarnings("unchecked")
132     public <Color> Color decorateBackground(Color color, String column, int itemIndex) {
133         if(backgroundColor == null)
134             return color;
135         else
136             return (Color)backgroundColor;
137     }
138     
139     @SuppressWarnings("unchecked")
140     public <Font> Font decorateFont(Font font, String column, int itemIndex) {
141         if(style == 0)
142             return font;
143         else {
144             FontDescriptor desc = (FontDescriptor)font;
145             if(desc == null)
146                 desc = DEFAULT_FONT_DESCRIPTOR;
147             return (Font)desc.withStyle(style);
148         }
149     }
150 }