]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/participant/SymbolUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / participant / SymbolUtil.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.g2d.participant;\r
13 \r
14 import java.awt.GraphicsConfiguration;\r
15 \r
16 import org.simantics.g2d.canvas.ICanvasContext;\r
17 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
18 import org.simantics.g2d.image.Image;\r
19 import org.simantics.utils.datastructures.cache.IProvider;\r
20 import org.simantics.utils.datastructures.hints.HintContext;\r
21 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
22 \r
23 /**\r
24  * Utility participant used for acquiring and storing symbols.\r
25  * \r
26  * //@deprecated\r
27  * //TODO: Toni, explain why this is deprecated ??\r
28  * @author Toni Kalajainen\r
29  */\r
30 public class SymbolUtil extends AbstractCanvasParticipant {\r
31 \r
32     static class SymbolKey extends Key {\r
33         public final IProvider<Image> provider;\r
34         public final GraphicsConfiguration gc;\r
35         private final int hash;\r
36         public SymbolKey(GraphicsConfiguration gc,\r
37                 IProvider<Image> provider) {\r
38             assert(gc!=null && provider!=null);\r
39             this.gc = gc;\r
40             this.provider = provider;\r
41             this.hash = gc.hashCode() ^provider.hashCode();\r
42         }\r
43         @Override\r
44         public boolean equals(Object obj) {\r
45             if (!(obj instanceof SymbolKey)) return false;\r
46             SymbolKey other = (SymbolKey) obj;\r
47             if (!other.provider.equals(provider)) return false;\r
48             if (!other.gc.equals(gc)) return false;\r
49             return true;\r
50         }\r
51         @Override\r
52         public int hashCode() {\r
53             return hash;\r
54         }\r
55         @Override\r
56         public boolean isValueAccepted(Object value) {\r
57             return value instanceof Image;\r
58         }\r
59     }\r
60 \r
61     HintContext hintCtx = new HintContext();\r
62 \r
63     @Override\r
64     public void removedFromContext(ICanvasContext ctx) {\r
65         hintCtx = null;\r
66         super.removedFromContext(ctx);\r
67     }\r
68 \r
69     /**\r
70      * Acquire and store symbol.\r
71      * \r
72      * @param provider\r
73      * @param gc\r
74      * @return\r
75      */\r
76     public Image get(IProvider<Image> provider, GraphicsConfiguration gc) {\r
77         SymbolKey key = new SymbolKey(gc, provider);\r
78         Image ps = hintCtx.getHint(key);\r
79         if (ps!=null) return ps;\r
80         ps = provider.get();\r
81         hintCtx.setHint(key, ps);\r
82         return ps;\r
83     }\r
84 \r
85 }\r