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.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;
26 * Constant label decoration rule decorates the label in a fixed way.
27 * @author Hannu Niemistö
29 public class ConstantLabelDecorationRule extends AbstractLabelDecorator implements LabelDecorationRule {
33 ColorDescriptor foregroundColor;
34 ColorDescriptor backgroundColor;
37 public ConstantLabelDecorationRule(String format,
38 ColorDescriptor foregroundColor, ColorDescriptor backgroundColor,
41 String[] split = format.split("%s", 2);
42 if(split.length == 1) {
51 this.foregroundColor = foregroundColor;
52 this.backgroundColor = backgroundColor;
53 this.style = SWT.NORMAL;
55 for(char c : style.toCharArray())
59 this.style |= SWT.BOLD;
63 this.style |= SWT.ITALIC;
66 System.err.println("Invalid character '" + c + "' in style string. Only B and I recognized.");
70 private static ColorDescriptor getPossibleRelatedColor(ReadGraph g, Resource subject, Resource predicate) throws DatabaseException {
71 Resource value = g.getPossibleObject(subject, predicate);
74 RGB rgb = g.adapt(value, RGB.class);
75 return ColorDescriptor.createFrom(rgb);
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);
82 ColorDescriptor foregroundColor = getPossibleRelatedColor(g, r, vr.ConstantLabelDecorationRule_HasForegroundColor);
83 ColorDescriptor backgroundColor = getPossibleRelatedColor(g, r, vr.ConstantLabelDecorationRule_HasBackgroundColor);
85 String style = g.getPossibleRelatedValue(r, vr.ConstantLabelDecorationRule_HasStyle, Bindings.STRING);
87 return new ConstantLabelDecorationRule(
96 public boolean isCompatible(Class<?> contentType) {
101 public LabelDecorator getLabelDecorator(ReadGraph graph, Object content)
102 throws DatabaseException {
107 public String decorateLabel(String label, String column, int itemIndex) {
111 return prefix + label + postfix;
114 @SuppressWarnings("unchecked")
115 public <Color> Color decorateForeground(Color color, String column, int itemIndex) {
116 if(foregroundColor == null)
119 return (Color)foregroundColor;
122 @SuppressWarnings("unchecked")
123 public <Color> Color decorateBackground(Color color, String column, int itemIndex) {
124 if(backgroundColor == null)
127 return (Color)backgroundColor;
130 @SuppressWarnings("unchecked")
131 public <Font> Font decorateFont(Font font, String column, int itemIndex) {
135 FontDescriptor desc = (FontDescriptor)font;
136 return (Font)desc.withStyle(style);