]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/SymbolUtil.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/SymbolUtil.java
new file mode 100644 (file)
index 0000000..a12c700
--- /dev/null
@@ -0,0 +1,85 @@
+/*******************************************************************************\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.g2d.participant;\r
+\r
+import java.awt.GraphicsConfiguration;\r
+\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
+import org.simantics.g2d.image.Image;\r
+import org.simantics.utils.datastructures.cache.IProvider;\r
+import org.simantics.utils.datastructures.hints.HintContext;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+\r
+/**\r
+ * Utility participant used for acquiring and storing symbols.\r
+ * \r
+ * //@deprecated\r
+ * //TODO: Toni, explain why this is deprecated ??\r
+ * @author Toni Kalajainen\r
+ */\r
+public class SymbolUtil extends AbstractCanvasParticipant {\r
+\r
+    static class SymbolKey extends Key {\r
+        public final IProvider<Image> provider;\r
+        public final GraphicsConfiguration gc;\r
+        private final int hash;\r
+        public SymbolKey(GraphicsConfiguration gc,\r
+                IProvider<Image> provider) {\r
+            assert(gc!=null && provider!=null);\r
+            this.gc = gc;\r
+            this.provider = provider;\r
+            this.hash = gc.hashCode() ^provider.hashCode();\r
+        }\r
+        @Override\r
+        public boolean equals(Object obj) {\r
+            if (!(obj instanceof SymbolKey)) return false;\r
+            SymbolKey other = (SymbolKey) obj;\r
+            if (!other.provider.equals(provider)) return false;\r
+            if (!other.gc.equals(gc)) return false;\r
+            return true;\r
+        }\r
+        @Override\r
+        public int hashCode() {\r
+            return hash;\r
+        }\r
+        @Override\r
+        public boolean isValueAccepted(Object value) {\r
+            return value instanceof Image;\r
+        }\r
+    }\r
+\r
+    HintContext hintCtx = new HintContext();\r
+\r
+    @Override\r
+    public void removedFromContext(ICanvasContext ctx) {\r
+        hintCtx = null;\r
+        super.removedFromContext(ctx);\r
+    }\r
+\r
+    /**\r
+     * Acquire and store symbol.\r
+     * \r
+     * @param provider\r
+     * @param gc\r
+     * @return\r
+     */\r
+    public Image get(IProvider<Image> provider, GraphicsConfiguration gc) {\r
+        SymbolKey key = new SymbolKey(gc, provider);\r
+        Image ps = hintCtx.getHint(key);\r
+        if (ps!=null) return ps;\r
+        ps = provider.get();\r
+        hintCtx.setHint(key, ps);\r
+        return ps;\r
+    }\r
+\r
+}\r