]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/internal/Activator.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / internal / Activator.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.internal;\r
13 \r
14 import java.util.HashMap;\r
15 import java.util.Map;\r
16 \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
26 \r
27 /**\r
28  * @author Tuukka Lehtonen\r
29  */\r
30 public class Activator extends AbstractUIPlugin {\r
31 \r
32     // The plug-in ID\r
33     public static final String PLUGIN_ID = "org.simantics.diagram";\r
34 \r
35     // The shared instance\r
36     private static Activator       plugin;\r
37 \r
38     public static ImageDescriptor LINK_ICON;\r
39     public static ImageDescriptor LINK_BREAK_ICON;\r
40     \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
43 \r
44         public static Function3<String, RGB.Integer, Vec2d, String> ICON_PROVIDER = new FunctionImpl3<String, RGB.Integer, Vec2d, String>() {\r
45                 \r
46                 private String hex2(int value) {\r
47                         String result = Integer.toHexString(value);\r
48                         if(result.length() == 1) result = "0" + result;\r
49                         return result;\r
50                 }\r
51 \r
52                 private int saturate(int in, double factor) {\r
53                         return (int)((double)in + factor*(double)(255-in));\r
54                 }\r
55 \r
56                 private int darken(int in, double factor) {\r
57                         return (int)((double)in*factor);\r
58                 }\r
59                 \r
60                 @Override\r
61                 public String apply(String iconName, RGB.Integer rgb, Vec2d scale) {\r
62                         \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
66                 \r
67                 int c4 = (r << 16) + (g << 8) + b;\r
68                 String key = iconName+c4+"#" + scale.x + "#" + scale.y;\r
69                 \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
84                 }\r
85                 return cached;\r
86                         \r
87                 }\r
88                 \r
89         };\r
90     \r
91     /*\r
92      * (non-Javadoc)\r
93      * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)\r
94      */\r
95     @Override\r
96     public void start(BundleContext context) throws Exception {\r
97         super.start(context);\r
98         plugin = this;\r
99 \r
100         Bundle bundle = context.getBundle();\r
101 \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
105         \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
108         \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
111         \r
112     }\r
113 \r
114     /*\r
115      * (non-Javadoc)\r
116      * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)\r
117      */\r
118     @Override\r
119     public void stop(BundleContext context) throws Exception {\r
120         plugin = null;\r
121         super.stop(context);\r
122     }\r
123 \r
124     /**\r
125      * Returns the shared instance\r
126      *\r
127      * @return the shared instance\r
128      */\r
129     public static Activator getDefault() {\r
130         return plugin;\r
131     }\r
132 \r
133 }\r