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