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