1 /*******************************************************************************
2 * Copyright (c) 2010, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.model.labeldecorators;
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;
28 * Constant label decoration rule decorates the label in a fixed way.
29 * @author Hannu Niemistö
31 public class ConstantLabelDecorationRule extends AbstractLabelDecorator implements LabelDecorationRule {
35 ColorDescriptor foregroundColor;
36 ColorDescriptor backgroundColor;
39 public ConstantLabelDecorationRule(String format,
40 ColorDescriptor foregroundColor, ColorDescriptor backgroundColor,
43 String[] split = format.split("%s", 2);
44 if(split.length == 1) {
53 this.foregroundColor = foregroundColor;
54 this.backgroundColor = backgroundColor;
55 this.style = SWT.NORMAL;
57 for(char c : style.toCharArray())
61 this.style |= SWT.BOLD;
65 this.style |= SWT.ITALIC;
68 System.err.println("Invalid character '" + c + "' in style string. Only B and I recognized.");
72 private static ColorDescriptor getPossibleRelatedColor(ReadGraph g, Resource subject, Resource predicate) throws DatabaseException {
73 Resource value = g.getPossibleObject(subject, predicate);
76 RGB rgb = g.adapt(value, RGB.class);
77 return ColorDescriptor.createFrom(rgb);
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);
84 ColorDescriptor foregroundColor = getPossibleRelatedColor(g, r, vr.ConstantLabelDecorationRule_HasForegroundColor);
85 ColorDescriptor backgroundColor = getPossibleRelatedColor(g, r, vr.ConstantLabelDecorationRule_HasBackgroundColor);
87 String style = g.getPossibleRelatedValue(r, vr.ConstantLabelDecorationRule_HasStyle, Bindings.STRING);
89 return new ConstantLabelDecorationRule(
98 public boolean isCompatible(Class<?> contentType) {
103 public LabelDecorator getLabelDecorator(ReadGraph graph, Object content)
104 throws DatabaseException {
109 public String decorateLabel(String label, String column, int itemIndex) {
113 return prefix + label + postfix;
116 @SuppressWarnings("unchecked")
117 public <Color> Color decorateForeground(Color color, String column, int itemIndex) {
118 if(foregroundColor == null)
121 return (Color)foregroundColor;
124 @SuppressWarnings("unchecked")
125 public <Color> Color decorateBackground(Color color, String column, int itemIndex) {
126 if(backgroundColor == null)
129 return (Color)backgroundColor;
132 @SuppressWarnings("unchecked")
133 public <Font> Font decorateFont(Font font, String column, int itemIndex) {
137 FontDescriptor desc = (FontDescriptor)font;
138 if(desc == null) desc = FontDescriptor.createFrom(JFaceResources.getDialogFont().getFontData());
139 return (Font)desc.withStyle(style);