1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\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.diagram.internal;
\r
14 import java.util.HashMap;
\r
15 import java.util.Map;
\r
17 import org.eclipse.jface.resource.ImageDescriptor;
\r
18 import org.eclipse.ui.plugin.AbstractUIPlugin;
\r
19 import org.osgi.framework.Bundle;
\r
20 import org.osgi.framework.BundleContext;
\r
21 import org.simantics.datatypes.literal.RGB;
\r
22 import org.simantics.datatypes.literal.Vec2d;
\r
23 import org.simantics.scl.runtime.function.Function3;
\r
24 import org.simantics.scl.runtime.function.FunctionImpl3;
\r
25 import org.simantics.utils.FileUtils;
\r
28 * @author Tuukka Lehtonen
\r
30 public class Activator extends AbstractUIPlugin {
\r
33 public static final String PLUGIN_ID = "org.simantics.diagram";
\r
35 // The shared instance
\r
36 private static Activator plugin;
\r
38 public static ImageDescriptor LINK_ICON;
\r
39 public static ImageDescriptor LINK_BREAK_ICON;
\r
41 private static Map<String, String> iconTexts = new HashMap<String, String>();
\r
42 private static Map<String, String> iconDocumentCache = new HashMap<String, String>();
\r
44 public static Function3<String, RGB.Integer, Vec2d, String> ICON_PROVIDER = new FunctionImpl3<String, RGB.Integer, Vec2d, String>() {
\r
46 private String hex2(int value) {
\r
47 String result = Integer.toHexString(value);
\r
48 if(result.length() == 1) result = "0" + result;
\r
52 private int saturate(int in, double factor) {
\r
53 return (int)((double)in + factor*(double)(255-in));
\r
56 private int darken(int in, double factor) {
\r
57 return (int)((double)in*factor);
\r
61 public String apply(String iconName, RGB.Integer rgb, Vec2d scale) {
\r
63 int r = rgb.red, g = rgb.green, b = rgb.blue;
\r
64 String base = iconTexts.get(iconName);
\r
65 if(base == null) return null;
\r
67 int c4 = (r << 16) + (g << 8) + b;
\r
68 String key = iconName+c4+"#" + scale.x + "#" + scale.y;
\r
70 String cached = iconDocumentCache.get(key);
\r
71 if(cached == null) {
\r
72 int c1 = (saturate(r,0.83) << 16) + (saturate(g, 0.83) << 8) + saturate(b, 0.83);
\r
73 int c2 = (saturate(r,0.5) << 16) + (saturate(g, 0.5) << 8) + saturate(b, 0.5);
\r
74 int c3 = (saturate(r,0.16) << 16) + (saturate(g, 0.16) << 8) + saturate(b, 0.16);
\r
75 int c5 = (darken(r,0.83) << 16) + (darken(g, 0.83) << 8) + darken(b, 0.83);
\r
76 base = base.replace("!!c1!!", hex2(c1));
\r
77 base = base.replace("!!c2!!", hex2(c2));
\r
78 base = base.replace("!!c3!!", hex2(c3));
\r
79 base = base.replace("!!c4!!", hex2(c4));
\r
80 base = base.replace("!!scalex!!", "" + scale.x);
\r
81 base = base.replace("!!scaley!!", "" + scale.y);
\r
82 cached = base.replace("!!c5!!", hex2(c5));
\r
83 iconDocumentCache.put(key, cached);
\r
93 * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
\r
96 public void start(BundleContext context) throws Exception {
\r
97 super.start(context);
\r
100 Bundle bundle = context.getBundle();
\r
102 iconTexts.put("LED", FileUtils.getContents(bundle.getResource("icons/IconLed.svg")));
\r
103 iconTexts.put("TERMINAL", FileUtils.getContents(bundle.getResource("icons/IconTerminal.svg")));
\r
104 iconTexts.put("SQUARE", FileUtils.getContents(bundle.getResource("icons/IconSquare.svg")));
\r
106 iconTexts.put("BUTTON_ON", FileUtils.getContents(bundle.getResource("icons/ButtonOn.svg")));
\r
107 iconTexts.put("BUTTON_OFF", FileUtils.getContents(bundle.getResource("icons/ButtonOff.svg")));
\r
109 LINK_ICON = ImageDescriptor.createFromURL(bundle.getResource("icons/link.png"));
\r
110 LINK_BREAK_ICON = ImageDescriptor.createFromURL(bundle.getResource("icons/link_break.png"));
\r
116 * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
\r
119 public void stop(BundleContext context) throws Exception {
\r
121 super.stop(context);
\r
125 * Returns the shared instance
\r
127 * @return the shared instance
\r
129 public static Activator getDefault() {
\r