--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in 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.diagram.internal;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.eclipse.ui.plugin.AbstractUIPlugin;\r
+import org.osgi.framework.Bundle;\r
+import org.osgi.framework.BundleContext;\r
+import org.simantics.datatypes.literal.RGB;\r
+import org.simantics.datatypes.literal.Vec2d;\r
+import org.simantics.scl.runtime.function.Function3;\r
+import org.simantics.scl.runtime.function.FunctionImpl3;\r
+import org.simantics.utils.FileUtils;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class Activator extends AbstractUIPlugin {\r
+\r
+ // The plug-in ID\r
+ public static final String PLUGIN_ID = "org.simantics.diagram";\r
+\r
+ // The shared instance\r
+ private static Activator plugin;\r
+\r
+ public static ImageDescriptor LINK_ICON;\r
+ public static ImageDescriptor LINK_BREAK_ICON;\r
+ \r
+ private static Map<String, String> iconTexts = new HashMap<String, String>();\r
+ private static Map<String, String> iconDocumentCache = new HashMap<String, String>(); \r
+\r
+ public static Function3<String, RGB.Integer, Vec2d, String> ICON_PROVIDER = new FunctionImpl3<String, RGB.Integer, Vec2d, String>() {\r
+ \r
+ private String hex2(int value) {\r
+ String result = Integer.toHexString(value);\r
+ if(result.length() == 1) result = "0" + result;\r
+ return result;\r
+ }\r
+\r
+ private int saturate(int in, double factor) {\r
+ return (int)((double)in + factor*(double)(255-in));\r
+ }\r
+\r
+ private int darken(int in, double factor) {\r
+ return (int)((double)in*factor);\r
+ }\r
+ \r
+ @Override\r
+ public String apply(String iconName, RGB.Integer rgb, Vec2d scale) {\r
+ \r
+ int r = rgb.red, g = rgb.green, b = rgb.blue; \r
+ String base = iconTexts.get(iconName);\r
+ if(base == null) return null;\r
+ \r
+ int c4 = (r << 16) + (g << 8) + b;\r
+ String key = iconName+c4+"#" + scale.x + "#" + scale.y;\r
+ \r
+ String cached = iconDocumentCache.get(key);\r
+ if(cached == null) {\r
+ int c1 = (saturate(r,0.83) << 16) + (saturate(g, 0.83) << 8) + saturate(b, 0.83);\r
+ int c2 = (saturate(r,0.5) << 16) + (saturate(g, 0.5) << 8) + saturate(b, 0.5);\r
+ int c3 = (saturate(r,0.16) << 16) + (saturate(g, 0.16) << 8) + saturate(b, 0.16);\r
+ int c5 = (darken(r,0.83) << 16) + (darken(g, 0.83) << 8) + darken(b, 0.83);\r
+ base = base.replace("!!c1!!", hex2(c1));\r
+ base = base.replace("!!c2!!", hex2(c2));\r
+ base = base.replace("!!c3!!", hex2(c3));\r
+ base = base.replace("!!c4!!", hex2(c4));\r
+ base = base.replace("!!scalex!!", "" + scale.x);\r
+ base = base.replace("!!scaley!!", "" + scale.y);\r
+ cached = base.replace("!!c5!!", hex2(c5));\r
+ iconDocumentCache.put(key, cached);\r
+ }\r
+ return cached;\r
+ \r
+ }\r
+ \r
+ };\r
+ \r
+ /*\r
+ * (non-Javadoc)\r
+ * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)\r
+ */\r
+ @Override\r
+ public void start(BundleContext context) throws Exception {\r
+ super.start(context);\r
+ plugin = this;\r
+\r
+ Bundle bundle = context.getBundle();\r
+\r
+ iconTexts.put("LED", FileUtils.getContents(bundle.getResource("icons/IconLed.svg")));\r
+ iconTexts.put("TERMINAL", FileUtils.getContents(bundle.getResource("icons/IconTerminal.svg")));\r
+ iconTexts.put("SQUARE", FileUtils.getContents(bundle.getResource("icons/IconSquare.svg")));\r
+ \r
+ iconTexts.put("BUTTON_ON", FileUtils.getContents(bundle.getResource("icons/ButtonOn.svg")));\r
+ iconTexts.put("BUTTON_OFF", FileUtils.getContents(bundle.getResource("icons/ButtonOff.svg")));\r
+ \r
+ LINK_ICON = ImageDescriptor.createFromURL(bundle.getResource("icons/link.png"));\r
+ LINK_BREAK_ICON = ImageDescriptor.createFromURL(bundle.getResource("icons/link_break.png"));\r
+ \r
+ }\r
+\r
+ /*\r
+ * (non-Javadoc)\r
+ * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)\r
+ */\r
+ @Override\r
+ public void stop(BundleContext context) throws Exception {\r
+ plugin = null;\r
+ super.stop(context);\r
+ }\r
+\r
+ /**\r
+ * Returns the shared instance\r
+ *\r
+ * @return the shared instance\r
+ */\r
+ public static Activator getDefault() {\r
+ return plugin;\r
+ }\r
+\r
+}\r