]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/TerminalDataImpl.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / TerminalDataImpl.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.element.handler.impl;
13
14 import org.simantics.g2d.diagram.handler.Topology.Terminal;
15 import org.simantics.g2d.element.IElement;
16 import org.simantics.g2d.element.handler.TerminalData;
17 import org.simantics.utils.datastructures.hints.HintContext;
18 import org.simantics.utils.datastructures.hints.IHintContext;
19 import org.simantics.utils.datastructures.hints.IHintContext.Key;
20 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
21
22 /**
23  * A straight-forward implementation of the {@link TerminalData} element handler
24  * interface. It uses an element's hint context to store a separate HintContext
25  * for terminals.
26  * 
27  * @author Tuukka Lehtonen
28  */
29 public class TerminalDataImpl implements TerminalData {
30
31     private static final long serialVersionUID = -2131030904280472077L;
32
33     public static final TerminalDataImpl INSTANCE = new TerminalDataImpl();
34
35     static class TerminalKey extends KeyOf {
36         private final Terminal terminal;
37         public TerminalKey(Terminal terminal) {
38             super(IHintContext.class);
39             if (terminal == null)
40                 throw new NullPointerException("null terminal");
41             this.terminal = terminal;
42         }
43         @Override
44         public int hashCode() {
45             return terminal.hashCode();
46         }
47         @Override
48         public boolean equals(Object obj) {
49             if (this == obj)
50                 return true;
51             if (!(obj instanceof TerminalKey))
52                 return false;
53             TerminalKey other = (TerminalKey) obj;
54             return terminal.equals(other.terminal);
55         }
56         @Override
57         public String toString() {
58             return "TERMINAL_DATA(" + terminal + ")";
59         }
60     }
61
62     @Override
63     public <T> T getHint(IElement e, Terminal t, Key key) {
64         IHintContext context = e.getHint(new TerminalKey(t));
65         if (context == null)
66             return null;
67         return context.getHint(key);
68     }
69
70     @Override
71     public void setHint(IElement e, Terminal t, Key key, Object value) {
72         Key terminalKey = new TerminalKey(t);
73         IHintContext context = e.getHint(terminalKey);
74         if (context == null) {
75             context = new HintContext();
76             e.setHint(terminalKey, context);
77         }
78         context.setHint(key, value);
79     }
80
81 }