]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/AdaptableHintContext.java
Add workbenchselection json fetcher to SCL interface
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / AdaptableHintContext.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.browsing.ui.common;
13
14 import java.util.Arrays;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Map.Entry;
18
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.simantics.db.layer0.SelectionHints;
21 import org.simantics.ui.selection.WorkbenchSelectionContentType;
22 import org.simantics.ui.selection.WorkbenchSelectionElement;
23 import org.simantics.utils.ObjectUtils;
24 import org.simantics.utils.datastructures.hints.IHintContext;
25 import org.simantics.utils.datastructures.hints.IHintListener;
26 import org.simantics.utils.threads.IThreadWorkQueue;
27
28 /**
29  * A custom internal hint context implementation that does not support
30  * notifications to achieve a more space-optimized implementation.
31  * 
32  * @author Tuukka Lehtonen
33  */
34 public class AdaptableHintContext implements IHintContext, IAdaptable, WorkbenchSelectionElement {
35
36     private final Key[]              keysToTry;
37     protected final Map<Key, Object> hints;
38
39     public AdaptableHintContext() {
40         this(SelectionHints.STD_KEYS);
41     }
42
43     public AdaptableHintContext(Key... keys) {
44         this(new HashMap<Key, Object>(4), keys);
45     }
46
47     public AdaptableHintContext(Map<Key, Object> hints, Key... keys) {
48         if (hints == null)
49             throw new NullPointerException("null hints");
50         this.keysToTry = keys;
51         this.hints = hints;
52     }
53
54     @SuppressWarnings({ "unchecked", "rawtypes" })
55     @Override
56     public Object getAdapter(Class adapter) {
57         for (Key key : keysToTry) {
58             Object o = getHint(key);
59             if (adapter.isAssignableFrom(o.getClass()))
60                 return o;
61             if (o instanceof IAdaptable) {
62                 Object adapted = ((IAdaptable) o).getAdapter(adapter);
63                 if (adapted != null) {
64                     return adapted;
65                 }
66             }
67         }
68         return null;
69     }
70
71     @Override
72     public String toString() {
73         return super.toString() + getHints();
74     }
75
76     @Override
77     public void clearWithoutNotification() {
78         hints.clear();
79     }
80
81     @Override
82     public boolean containsHint(Key key) {
83         return hints.get(key) != null;
84     }
85
86     @SuppressWarnings("unchecked")
87     @Override
88     public <E> E getHint(Key key) {
89         if (key == null)
90             throw new IllegalArgumentException("key is null");
91         synchronized (this) {
92             return (E) hints.get(key);
93         }
94     }
95
96     @Override
97     public synchronized Map<Key, Object> getHints() {
98         return new HashMap<Key, Object>(hints);
99     }
100
101     @Override
102     public Map<Key, Object> getHintsUnsafe() {
103         return hints;
104     }
105
106     @SuppressWarnings("unchecked")
107     @Override
108     public <E extends Key> Map<E, Object> getHintsOfClass(Class<E> clazz) {
109         Map<E, Object> result = null;
110         for (Entry<Key, Object> e : hints.entrySet()) {
111             Key key = e.getKey();
112             if (clazz.isAssignableFrom(key.getClass())) {
113                 if (result == null)
114                     result = new HashMap<E, Object>(4);
115                 result.put((E) key, e.getValue());
116             }
117         }
118         return result;
119     }
120
121     @Override
122     public void setHint(Key key, Object value) {
123         if (key == null)
124             throw new IllegalArgumentException("key is null");
125         if (value == null)
126             throw new IllegalArgumentException("value is null");
127         if (!key.isValueAccepted(value))
128             throw new RuntimeException("Value \"" + value + "\" is not accepted with key " + key.getClass().getName());
129
130         synchronized (this) {
131             hints.put(key, value);
132         }
133     }
134
135     @Override
136     public void setHints(Map<Key, Object> hints) {
137         synchronized (this) {
138             for (Entry<Key, Object> e : hints.entrySet()) {
139                 Key key = e.getKey();
140                 Object value = e.getValue();
141                 if (value == null)
142                     throw new IllegalArgumentException("a value is null for key " + e.getKey());
143                 this.hints.put(key, value);
144             }
145         }
146     }
147
148     @SuppressWarnings("unchecked")
149     @Override
150     public <E> E removeHint(Key key) {
151         if (key == null)
152             throw new IllegalArgumentException("key is null");
153
154         Object oldValue = null;
155         synchronized (this) {
156             oldValue = hints.remove(key);
157         }
158         if (oldValue == null)
159             return null;
160         return (E) oldValue;
161     }
162
163     @Override
164     public void addHintListener(IHintListener listener) {
165         throw new UnsupportedOperationException();
166     }
167
168     @Override
169     public void removeHintListener(IHintListener listener) {
170         throw new UnsupportedOperationException();
171     }
172
173     @Override
174     public void addKeyHintListener(Key key, IHintListener listener) {
175         throw new UnsupportedOperationException();
176     }
177
178     @Override
179     public void removeKeyHintListener(Key key, IHintListener listener) {
180         throw new UnsupportedOperationException();
181     }
182
183     @Override
184     public void addHintListener(IThreadWorkQueue threadAccess, IHintListener listener) {
185         throw new UnsupportedOperationException();
186     }
187
188     @Override
189     public void removeHintListener(IThreadWorkQueue threadAccess, IHintListener listener) {
190         throw new UnsupportedOperationException();
191     }
192
193     @Override
194     public void addKeyHintListener(IThreadWorkQueue threadAccess, Key key, IHintListener listener) {
195         throw new UnsupportedOperationException();
196     }
197
198     @Override
199     public void removeKeyHintListener(IThreadWorkQueue threadAccess, Key key, IHintListener listener) {
200         throw new UnsupportedOperationException();
201     }
202
203     @Override
204     public int hashCode() {
205         return ((hints.hashCode() * 31) + Arrays.hashCode(keysToTry)) * 31;
206     }
207
208     @Override
209     public boolean equals(Object obj) {
210         if (this == obj)
211             return true;
212         if (obj == null)
213             return false;
214         if (getClass() != obj.getClass())
215             return false;
216         AdaptableHintContext other = (AdaptableHintContext) obj;
217         return Arrays.equals(keysToTry, other.keysToTry) && ObjectUtils.objectEquals(hints, other.hints);
218     }
219
220         @Override
221         public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
222                 return null;
223         }
224
225 }