]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/internal/Activator.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / internal / Activator.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.internal;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.ui.plugin.AbstractUIPlugin;
19 import org.osgi.framework.Bundle;
20 import org.osgi.framework.BundleContext;
21 import org.simantics.datatypes.literal.RGB;
22 import org.simantics.datatypes.literal.Vec2d;
23 import org.simantics.scl.runtime.function.Function3;
24 import org.simantics.scl.runtime.function.FunctionImpl3;
25 import org.simantics.utils.FileUtils;
26
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class Activator extends AbstractUIPlugin {
31
32     // The plug-in ID
33     public static final String PLUGIN_ID = "org.simantics.diagram";
34
35     // The shared instance
36     private static Activator       plugin;
37
38     public static ImageDescriptor LINK_ICON;
39     public static ImageDescriptor LINK_BREAK_ICON;
40     
41     private static Map<String, String> iconTexts = new HashMap<String, String>();
42     private static Map<String, String> iconDocumentCache = new HashMap<String, String>(); 
43
44         public static Function3<String, RGB.Integer, Vec2d, String> ICON_PROVIDER = new FunctionImpl3<String, RGB.Integer, Vec2d, String>() {
45                 
46                 private String hex2(int value) {
47                         String result = Integer.toHexString(value);
48                         if(result.length() == 1) result = "0" + result;
49                         return result;
50                 }
51
52                 private int saturate(int in, double factor) {
53                         return (int)((double)in + factor*(double)(255-in));
54                 }
55
56                 private int darken(int in, double factor) {
57                         return (int)((double)in*factor);
58                 }
59                 
60                 @Override
61                 public String apply(String iconName, RGB.Integer rgb, Vec2d scale) {
62                         
63                 int r = rgb.red, g = rgb.green, b = rgb.blue; 
64                 String base = iconTexts.get(iconName);
65                 if(base == null) return null;
66                 
67                 int c4 = (r << 16) + (g << 8) + b;
68                 String key = iconName+c4+"#" + scale.x + "#" + scale.y;
69                 
70                 String cached = iconDocumentCache.get(key);
71                 if(cached == null) {
72                         int c1 = (saturate(r,0.83) << 16) + (saturate(g, 0.83) << 8) + saturate(b, 0.83);
73                         int c2 = (saturate(r,0.5) << 16) + (saturate(g, 0.5) << 8) + saturate(b, 0.5);
74                         int c3 = (saturate(r,0.16) << 16) + (saturate(g, 0.16) << 8) + saturate(b, 0.16);
75                         int c5 = (darken(r,0.83) << 16) + (darken(g, 0.83) << 8) + darken(b, 0.83);
76                         base = base.replace("!!c1!!", hex2(c1));
77                         base = base.replace("!!c2!!", hex2(c2));
78                         base = base.replace("!!c3!!", hex2(c3));
79                         base = base.replace("!!c4!!", hex2(c4));
80                         base = base.replace("!!scalex!!", "" + scale.x);
81                         base = base.replace("!!scaley!!", "" + scale.y);
82                         cached = base.replace("!!c5!!", hex2(c5));
83                         iconDocumentCache.put(key, cached);
84                 }
85                 return cached;
86                         
87                 }
88                 
89         };
90     
91     /*
92      * (non-Javadoc)
93      * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
94      */
95     @Override
96     public void start(BundleContext context) throws Exception {
97         super.start(context);
98         plugin = this;
99
100         Bundle bundle = context.getBundle();
101
102         iconTexts.put("LED", FileUtils.getContents(bundle.getResource("icons/IconLed.svg")));
103         iconTexts.put("TERMINAL", FileUtils.getContents(bundle.getResource("icons/IconTerminal.svg")));
104         iconTexts.put("SQUARE", FileUtils.getContents(bundle.getResource("icons/IconSquare.svg")));
105         
106         iconTexts.put("BUTTON_ON", FileUtils.getContents(bundle.getResource("icons/ButtonOn.svg")));
107         iconTexts.put("BUTTON_OFF", FileUtils.getContents(bundle.getResource("icons/ButtonOff.svg")));
108         
109         LINK_ICON = ImageDescriptor.createFromURL(bundle.getResource("icons/link.png"));
110         LINK_BREAK_ICON = ImageDescriptor.createFromURL(bundle.getResource("icons/link_break.png"));
111         
112     }
113
114     /*
115      * (non-Javadoc)
116      * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
117      */
118     @Override
119     public void stop(BundleContext context) throws Exception {
120         plugin = null;
121         super.stop(context);
122     }
123
124     /**
125      * Returns the shared instance
126      *
127      * @return the shared instance
128      */
129     public static Activator getDefault() {
130         return plugin;
131     }
132
133 }