+/*******************************************************************************\r
+ * Copyright (c) 2010, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.model.labeldecorators;\r
+\r
+import org.eclipse.jface.resource.ColorDescriptor;\r
+import org.eclipse.jface.resource.FontDescriptor;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.graphics.RGB;\r
+import org.simantics.browsing.ui.content.LabelDecorator;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.viewpoint.ontology.ViewpointResource;\r
+\r
+/**\r
+ * Constant label decoration rule decorates the label in a fixed way.\r
+ * @author Hannu Niemistö\r
+ */\r
+public class ConstantLabelDecorationRule extends AbstractLabelDecorator implements LabelDecorationRule {\r
+\r
+ String prefix;\r
+ String postfix;\r
+ ColorDescriptor foregroundColor;\r
+ ColorDescriptor backgroundColor;\r
+ int style;\r
+ \r
+ public ConstantLabelDecorationRule(String format,\r
+ ColorDescriptor foregroundColor, ColorDescriptor backgroundColor,\r
+ String style) {\r
+ if(format != null) {\r
+ String[] split = format.split("%s", 2);\r
+ if(split.length == 1) {\r
+ prefix = "";\r
+ postfix = split[0];\r
+ }\r
+ else {\r
+ prefix = split[0];\r
+ postfix = split[1];\r
+ }\r
+ }\r
+ this.foregroundColor = foregroundColor;\r
+ this.backgroundColor = backgroundColor;\r
+ this.style = SWT.NORMAL;\r
+ if(style != null)\r
+ for(char c : style.toCharArray())\r
+ switch(c) {\r
+ case 'B':\r
+ case 'b':\r
+ this.style |= SWT.BOLD;\r
+ break;\r
+ case 'I':\r
+ case 'i':\r
+ this.style |= SWT.ITALIC;\r
+ break;\r
+ default:\r
+ System.err.println("Invalid character '" + c + "' in style string. Only B and I recognized.");\r
+ }\r
+ }\r
+\r
+ private static ColorDescriptor getPossibleRelatedColor(ReadGraph g, Resource subject, Resource predicate) throws DatabaseException {\r
+ Resource value = g.getPossibleObject(subject, predicate);\r
+ if(value == null)\r
+ return null;\r
+ RGB rgb = g.adapt(value, RGB.class);\r
+ return ColorDescriptor.createFrom(rgb);\r
+ }\r
+ \r
+ public static ConstantLabelDecorationRule create(ReadGraph g, Resource r) throws DatabaseException {\r
+ ViewpointResource vr = ViewpointResource.getInstance(g); \r
+ String format = g.getPossibleRelatedValue(r, vr.ConstantLabelDecorationRule_HasFormat, Bindings.STRING);\r
+\r
+ ColorDescriptor foregroundColor = getPossibleRelatedColor(g, r, vr.ConstantLabelDecorationRule_HasForegroundColor);\r
+ ColorDescriptor backgroundColor = getPossibleRelatedColor(g, r, vr.ConstantLabelDecorationRule_HasBackgroundColor);\r
+ \r
+ String style = g.getPossibleRelatedValue(r, vr.ConstantLabelDecorationRule_HasStyle, Bindings.STRING);\r
+ \r
+ return new ConstantLabelDecorationRule(\r
+ format, \r
+ foregroundColor, \r
+ backgroundColor,\r
+ style\r
+ );\r
+ }\r
+ \r
+ @Override\r
+ public boolean isCompatible(Class<?> contentType) {\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public LabelDecorator getLabelDecorator(ReadGraph graph, Object content)\r
+ throws DatabaseException {\r
+ return this;\r
+ }\r
+ \r
+ @Override\r
+ public String decorateLabel(String label, String column, int itemIndex) {\r
+ if(postfix == null)\r
+ return label;\r
+ else\r
+ return prefix + label + postfix;\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ public <Color> Color decorateForeground(Color color, String column, int itemIndex) {\r
+ if(foregroundColor == null)\r
+ return color;\r
+ else\r
+ return (Color)foregroundColor;\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ public <Color> Color decorateBackground(Color color, String column, int itemIndex) {\r
+ if(backgroundColor == null)\r
+ return color;\r
+ else\r
+ return (Color)backgroundColor;\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ public <Font> Font decorateFont(Font font, String column, int itemIndex) {\r
+ if(style == 0)\r
+ return font;\r
+ else {\r
+ FontDescriptor desc = (FontDescriptor)font;\r
+ return (Font)desc.withStyle(style);\r
+ }\r
+ }\r
+}\r