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