]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/hints/IHintContext.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / hints / IHintContext.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 /*
13  *
14  * @author Toni Kalajainen
15  */
16 package org.simantics.utils.datastructures.hints;
17
18 import java.util.Map;
19
20 /**
21  * Hint context interface
22  *
23  * @see Key
24  * @see HintContext
25  * @see HintStack
26  * @see IHintObservable
27  */
28 public interface IHintContext extends IHintObservable {
29
30         /**
31          * Set a value for a hint. 
32          * @param key key
33          * @param value the value, <code>null</code> is not accepted
34          */
35         void setHint(Key key, Object value);
36         
37         /**
38          * Set a set of hints
39          * @param hints
40          */
41         void setHints(Map<Key, Object> hints);
42         
43         /**
44          * Remove a hint. In stack hint implementation lower priority hint will
45          * become effective. 
46
47          * @param <E> type of the object bound to the specified key
48          * @param key key to use for the hint
49          * @return the typed object currently bound to the specified key
50          */
51         <E> E removeHint(Key key);
52
53         /**
54          * Clears the context of all current hints without notifying any listeners.
55          */
56         void clearWithoutNotification();
57
58         /**
59          * Hint key
60          */
61         public static abstract class Key {
62                 /**
63                  * Test if value is accepted
64                  * @param value the value
65                  * @return true if value is accepted, false if not
66                  */
67                 public abstract boolean isValueAccepted(Object value);          
68         }
69         
70         /**
71          * Mouse id specific key. With this key there is unique value for each mouse id.
72          */
73         public static abstract class MouseSpecificKey extends Key {
74                 private final int mouseId;
75                 public MouseSpecificKey(int mouseId)
76                 {
77                         this.mouseId = mouseId;
78                 }
79                 public int getMouseId()
80                 {
81                         return mouseId;
82                 }
83                 @Override
84                 public int hashCode() {
85                         return super.hashCode() ^ mouseId;
86                 }               
87                 @Override
88                 public boolean equals(Object obj) {
89                         if (!(obj instanceof MouseSpecificKey)) return false;
90                         if (((MouseSpecificKey)obj).mouseId != mouseId) return false;
91                         if (!obj.getClass().equals(getClass())) return false;
92                         return true;
93                 }
94         }
95
96         /**
97          * Key whose value is of a specific given class. 
98          */
99         public static class KeyOf extends Key {
100                 final Class<?> clazz;
101                 final String keyName;
102         public KeyOf(Class<?> clazz)
103         {
104             this.clazz = clazz;
105             this.keyName = null;
106         }
107         public KeyOf(Class<?> clazz, String keyName)
108         {
109             this.clazz = clazz;
110             this.keyName = keyName;
111         }
112                 @Override
113                 public boolean isValueAccepted(Object value) {
114                         return clazz.isInstance(value);
115                 }
116                 @Override
117                 public String toString() {
118                     if (keyName == null)
119                         return "Key Of "+clazz.getName();
120                     return keyName + "(" + clazz.getSimpleName() + ")";
121                 }
122         }
123         
124         /**
125          * String based key
126          */
127         public static abstract class StringKey extends Key {
128                 protected final String str;             
129                 protected int hash;
130                 public StringKey(String str)
131                 {
132                         this.str = str;
133                         hash = getClass().hashCode() ^str.hashCode();                   
134                 }
135                 public String getString()
136                 {
137                         return str;
138                 }
139                 @Override
140                 public int hashCode() {
141                         return hash;
142                 }               
143                 @Override
144                 public boolean equals(Object obj) {
145                         if (!(obj instanceof StringKey)) return false;
146                         if (!((StringKey)obj).str.equals(str)) return false;
147                         if (obj.getClass()!=getClass()) return false;
148                         return true;
149                 }
150         }       
151         
152         /**
153          * String based key whose value is of a specific class.
154          */
155         public static class StringKeyOf extends StringKey {
156                 final Class<?> clazz;           
157                 public StringKeyOf(String str, Class<?> clazz)
158                 {
159                         super(str);
160                         this.clazz = clazz;
161                 }
162                 @Override
163                 public boolean isValueAccepted(Object value) {
164                         return clazz.isInstance(value);
165                 }
166                 @Override
167                 public String toString() {
168                         return "Key Of ("+clazz.getName()+", "+str+")";
169                 }               
170         }
171         
172
173         public static class MouseSpecificKeyOf extends MouseSpecificKey {
174                 public final Class<?> clazz;
175                 public final int mouseId;
176                 final int hash;
177                 public MouseSpecificKeyOf(int mouseId, Class<?> clazz)
178                 {
179                         super(mouseId);
180                         this.clazz = clazz;
181                         this.mouseId = mouseId;
182                         hash = getClass().hashCode() ^ mouseId ^ 24392439;
183                 }
184                 @Override
185                 public boolean isValueAccepted(Object value) {
186                         return clazz.isInstance(value);
187                 }
188                 @Override
189                 public int hashCode() {
190                         return hash;
191                 }
192                 @Override
193                 public boolean equals(Object obj) {
194                         if (obj.getClass()!=this.getClass()) return false;
195                         MouseSpecificKeyOf other = (MouseSpecificKeyOf) obj;
196                         return other.mouseId == mouseId;
197                 }
198         }
199         
200         
201 }