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