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