1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.common;
14 import java.util.Arrays;
15 import java.util.HashMap;
17 import java.util.Map.Entry;
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;
29 * A custom internal hint context implementation that does not support
30 * notifications to achieve a more space-optimized implementation.
32 * @author Tuukka Lehtonen
34 public class AdaptableHintContext implements IHintContext, IAdaptable, WorkbenchSelectionElement {
36 private final Key[] keysToTry;
37 protected final Map<Key, Object> hints;
39 public AdaptableHintContext() {
40 this(SelectionHints.STD_KEYS);
43 public AdaptableHintContext(Key... keys) {
44 this(new HashMap<Key, Object>(4), keys);
47 public AdaptableHintContext(Map<Key, Object> hints, Key... keys) {
49 throw new NullPointerException("null hints");
50 this.keysToTry = keys;
54 @SuppressWarnings({ "unchecked", "rawtypes" })
56 public Object getAdapter(Class adapter) {
57 for (Key key : keysToTry) {
58 Object o = getHint(key);
59 if (adapter.isAssignableFrom(o.getClass()))
61 if (o instanceof IAdaptable) {
62 Object adapted = ((IAdaptable) o).getAdapter(adapter);
63 if (adapted != null) {
72 public String toString() {
73 return super.toString() + getHints();
77 public void clearWithoutNotification() {
82 public boolean containsHint(Key key) {
83 return hints.get(key) != null;
86 @SuppressWarnings("unchecked")
88 public <E> E getHint(Key key) {
90 throw new IllegalArgumentException("key is null");
92 return (E) hints.get(key);
97 public synchronized Map<Key, Object> getHints() {
98 return new HashMap<Key, Object>(hints);
102 public Map<Key, Object> getHintsUnsafe() {
106 @SuppressWarnings("unchecked")
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())) {
114 result = new HashMap<E, Object>(4);
115 result.put((E) key, e.getValue());
122 public void setHint(Key key, Object value) {
124 throw new IllegalArgumentException("key is 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());
130 synchronized (this) {
131 hints.put(key, value);
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();
142 throw new IllegalArgumentException("a value is null for key " + e.getKey());
143 this.hints.put(key, value);
148 @SuppressWarnings("unchecked")
150 public <E> E removeHint(Key key) {
152 throw new IllegalArgumentException("key is null");
154 Object oldValue = null;
155 synchronized (this) {
156 oldValue = hints.remove(key);
158 if (oldValue == null)
164 public void addHintListener(IHintListener listener) {
165 throw new UnsupportedOperationException();
169 public void removeHintListener(IHintListener listener) {
170 throw new UnsupportedOperationException();
174 public void addKeyHintListener(Key key, IHintListener listener) {
175 throw new UnsupportedOperationException();
179 public void removeKeyHintListener(Key key, IHintListener listener) {
180 throw new UnsupportedOperationException();
184 public void addHintListener(IThreadWorkQueue threadAccess, IHintListener listener) {
185 throw new UnsupportedOperationException();
189 public void removeHintListener(IThreadWorkQueue threadAccess, IHintListener listener) {
190 throw new UnsupportedOperationException();
194 public void addKeyHintListener(IThreadWorkQueue threadAccess, Key key, IHintListener listener) {
195 throw new UnsupportedOperationException();
199 public void removeKeyHintListener(IThreadWorkQueue threadAccess, Key key, IHintListener listener) {
200 throw new UnsupportedOperationException();
204 public int hashCode() {
205 return ((hints.hashCode() * 31) + Arrays.hashCode(keysToTry)) * 31;
209 public boolean equals(Object obj) {
214 if (getClass() != obj.getClass())
216 AdaptableHintContext other = (AdaptableHintContext) obj;
217 return Arrays.equals(keysToTry, other.keysToTry) && ObjectUtils.objectEquals(hints, other.hints);
221 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {