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.g2d.participant;
\r
14 import java.awt.GraphicsConfiguration;
\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
24 * Utility participant used for acquiring and storing symbols.
\r
27 * //TODO: Toni, explain why this is deprecated ??
\r
28 * @author Toni Kalajainen
\r
30 public class SymbolUtil extends AbstractCanvasParticipant {
\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
40 this.provider = provider;
\r
41 this.hash = gc.hashCode() ^provider.hashCode();
\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
52 public int hashCode() {
\r
56 public boolean isValueAccepted(Object value) {
\r
57 return value instanceof Image;
\r
61 HintContext hintCtx = new HintContext();
\r
64 public void removedFromContext(ICanvasContext ctx) {
\r
66 super.removedFromContext(ctx);
\r
70 * Acquire and store symbol.
\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