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