-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.browsing.ui.swt;\r
-\r
-import java.util.Arrays;\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-import java.util.Map.Entry;\r
-\r
-import org.eclipse.core.runtime.IAdaptable;\r
-import org.simantics.db.layer0.SelectionHints;\r
-import org.simantics.ui.selection.WorkbenchSelectionContentType;\r
-import org.simantics.ui.selection.WorkbenchSelectionElement;\r
-import org.simantics.utils.ObjectUtils;\r
-import org.simantics.utils.datastructures.hints.IHintContext;\r
-import org.simantics.utils.datastructures.hints.IHintListener;\r
-import org.simantics.utils.threads.IThreadWorkQueue;\r
-\r
-/**\r
- * A custom internal hint context implementation that does not support\r
- * notifications to achieve a more space-optimized implementation.\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class AdaptableHintContext implements IHintContext, IAdaptable, WorkbenchSelectionElement {\r
-\r
- private final Key[] keysToTry;\r
- protected final Map<Key, Object> hints;\r
-\r
- public AdaptableHintContext() {\r
- this(SelectionHints.STD_KEYS);\r
- }\r
-\r
- public AdaptableHintContext(Key... keys) {\r
- this(new HashMap<Key, Object>(4), keys);\r
- }\r
-\r
- public AdaptableHintContext(Map<Key, Object> hints, Key... keys) {\r
- if (hints == null)\r
- throw new NullPointerException("null hints");\r
- this.keysToTry = keys;\r
- this.hints = hints;\r
- }\r
-\r
- @SuppressWarnings({ "unchecked", "rawtypes" })\r
- @Override\r
- public Object getAdapter(Class adapter) {\r
- for (Key key : keysToTry) {\r
- Object o = getHint(key);\r
- if (adapter.isAssignableFrom(o.getClass()))\r
- return o;\r
- if (o instanceof IAdaptable) {\r
- Object adapted = ((IAdaptable) o).getAdapter(adapter);\r
- if (adapted != null) {\r
- return adapted;\r
- }\r
- }\r
- }\r
- return null;\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- return super.toString() + getHints();\r
- }\r
-\r
- @Override\r
- public void clearWithoutNotification() {\r
- hints.clear();\r
- }\r
-\r
- @Override\r
- public boolean containsHint(Key key) {\r
- return hints.get(key) != null;\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- @Override\r
- public <E> E getHint(Key key) {\r
- if (key == null)\r
- throw new IllegalArgumentException("key is null");\r
- synchronized (this) {\r
- return (E) hints.get(key);\r
- }\r
- }\r
-\r
- @Override\r
- public synchronized Map<Key, Object> getHints() {\r
- return new HashMap<Key, Object>(hints);\r
- }\r
-\r
- @Override\r
- public Map<Key, Object> getHintsUnsafe() {\r
- return hints;\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- @Override\r
- public <E extends Key> Map<E, Object> getHintsOfClass(Class<E> clazz) {\r
- Map<E, Object> result = null;\r
- for (Entry<Key, Object> e : hints.entrySet()) {\r
- Key key = e.getKey();\r
- if (clazz.isAssignableFrom(key.getClass())) {\r
- if (result == null)\r
- result = new HashMap<E, Object>(4);\r
- result.put((E) key, e.getValue());\r
- }\r
- }\r
- return result;\r
- }\r
-\r
- @Override\r
- public void setHint(Key key, Object value) {\r
- if (key == null)\r
- throw new IllegalArgumentException("key is null");\r
- if (value == null)\r
- throw new IllegalArgumentException("value is null");\r
- if (!key.isValueAccepted(value))\r
- throw new RuntimeException("Value \"" + value + "\" is not accepted with key " + key.getClass().getName());\r
-\r
- synchronized (this) {\r
- hints.put(key, value);\r
- }\r
- }\r
-\r
- @Override\r
- public void setHints(Map<Key, Object> hints) {\r
- synchronized (this) {\r
- for (Entry<Key, Object> e : hints.entrySet()) {\r
- Key key = e.getKey();\r
- Object value = e.getValue();\r
- if (value == null)\r
- throw new IllegalArgumentException("a value is null for key " + e.getKey());\r
- this.hints.put(key, value);\r
- }\r
- }\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- @Override\r
- public <E> E removeHint(Key key) {\r
- if (key == null)\r
- throw new IllegalArgumentException("key is null");\r
-\r
- Object oldValue = null;\r
- synchronized (this) {\r
- oldValue = hints.remove(key);\r
- }\r
- if (oldValue == null)\r
- return null;\r
- return (E) oldValue;\r
- }\r
-\r
- @Override\r
- public void addHintListener(IHintListener listener) {\r
- throw new UnsupportedOperationException();\r
- }\r
-\r
- @Override\r
- public void removeHintListener(IHintListener listener) {\r
- throw new UnsupportedOperationException();\r
- }\r
-\r
- @Override\r
- public void addKeyHintListener(Key key, IHintListener listener) {\r
- throw new UnsupportedOperationException();\r
- }\r
-\r
- @Override\r
- public void removeKeyHintListener(Key key, IHintListener listener) {\r
- throw new UnsupportedOperationException();\r
- }\r
-\r
- @Override\r
- public void addHintListener(IThreadWorkQueue threadAccess, IHintListener listener) {\r
- throw new UnsupportedOperationException();\r
- }\r
-\r
- @Override\r
- public void removeHintListener(IThreadWorkQueue threadAccess, IHintListener listener) {\r
- throw new UnsupportedOperationException();\r
- }\r
-\r
- @Override\r
- public void addKeyHintListener(IThreadWorkQueue threadAccess, Key key, IHintListener listener) {\r
- throw new UnsupportedOperationException();\r
- }\r
-\r
- @Override\r
- public void removeKeyHintListener(IThreadWorkQueue threadAccess, Key key, IHintListener listener) {\r
- throw new UnsupportedOperationException();\r
- }\r
-\r
- @Override\r
- public int hashCode() {\r
- return ((hints.hashCode() * 31) + Arrays.hashCode(keysToTry)) * 31;\r
- }\r
-\r
- @Override\r
- public boolean equals(Object obj) {\r
- if (this == obj)\r
- return true;\r
- if (obj == null)\r
- return false;\r
- if (getClass() != obj.getClass())\r
- return false;\r
- AdaptableHintContext other = (AdaptableHintContext) obj;\r
- return Arrays.equals(keysToTry, other.keysToTry) && ObjectUtils.objectEquals(hints, other.hints);\r
- }\r
-\r
- @Override\r
- public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {\r
- return null;\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.browsing.ui.swt;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.eclipse.core.runtime.IAdaptable;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.ui.selection.WorkbenchSelectionContentType;
+import org.simantics.ui.selection.WorkbenchSelectionElement;
+import org.simantics.utils.ObjectUtils;
+import org.simantics.utils.datastructures.hints.IHintContext;
+import org.simantics.utils.datastructures.hints.IHintListener;
+import org.simantics.utils.threads.IThreadWorkQueue;
+
+/**
+ * A custom internal hint context implementation that does not support
+ * notifications to achieve a more space-optimized implementation.
+ *
+ * @author Tuukka Lehtonen
+ */
+public class AdaptableHintContext implements IHintContext, IAdaptable, WorkbenchSelectionElement {
+
+ private final Key[] keysToTry;
+ protected final Map<Key, Object> hints;
+
+ public AdaptableHintContext() {
+ this(SelectionHints.STD_KEYS);
+ }
+
+ public AdaptableHintContext(Key... keys) {
+ this(new HashMap<Key, Object>(4), keys);
+ }
+
+ public AdaptableHintContext(Map<Key, Object> hints, Key... keys) {
+ if (hints == null)
+ throw new NullPointerException("null hints");
+ this.keysToTry = keys;
+ this.hints = hints;
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ @Override
+ public Object getAdapter(Class adapter) {
+ for (Key key : keysToTry) {
+ Object o = getHint(key);
+ if (adapter.isAssignableFrom(o.getClass()))
+ return o;
+ if (o instanceof IAdaptable) {
+ Object adapted = ((IAdaptable) o).getAdapter(adapter);
+ if (adapted != null) {
+ return adapted;
+ }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ return super.toString() + getHints();
+ }
+
+ @Override
+ public void clearWithoutNotification() {
+ hints.clear();
+ }
+
+ @Override
+ public boolean containsHint(Key key) {
+ return hints.get(key) != null;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <E> E getHint(Key key) {
+ if (key == null)
+ throw new IllegalArgumentException("key is null");
+ synchronized (this) {
+ return (E) hints.get(key);
+ }
+ }
+
+ @Override
+ public synchronized Map<Key, Object> getHints() {
+ return new HashMap<Key, Object>(hints);
+ }
+
+ @Override
+ public Map<Key, Object> getHintsUnsafe() {
+ return hints;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <E extends Key> Map<E, Object> getHintsOfClass(Class<E> clazz) {
+ Map<E, Object> result = null;
+ for (Entry<Key, Object> e : hints.entrySet()) {
+ Key key = e.getKey();
+ if (clazz.isAssignableFrom(key.getClass())) {
+ if (result == null)
+ result = new HashMap<E, Object>(4);
+ result.put((E) key, e.getValue());
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public void setHint(Key key, Object value) {
+ if (key == null)
+ throw new IllegalArgumentException("key is null");
+ if (value == null)
+ throw new IllegalArgumentException("value is null");
+ if (!key.isValueAccepted(value))
+ throw new RuntimeException("Value \"" + value + "\" is not accepted with key " + key.getClass().getName());
+
+ synchronized (this) {
+ hints.put(key, value);
+ }
+ }
+
+ @Override
+ public void setHints(Map<Key, Object> hints) {
+ synchronized (this) {
+ for (Entry<Key, Object> e : hints.entrySet()) {
+ Key key = e.getKey();
+ Object value = e.getValue();
+ if (value == null)
+ throw new IllegalArgumentException("a value is null for key " + e.getKey());
+ this.hints.put(key, value);
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <E> E removeHint(Key key) {
+ if (key == null)
+ throw new IllegalArgumentException("key is null");
+
+ Object oldValue = null;
+ synchronized (this) {
+ oldValue = hints.remove(key);
+ }
+ if (oldValue == null)
+ return null;
+ return (E) oldValue;
+ }
+
+ @Override
+ public void addHintListener(IHintListener listener) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void removeHintListener(IHintListener listener) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void addKeyHintListener(Key key, IHintListener listener) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void removeKeyHintListener(Key key, IHintListener listener) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void addHintListener(IThreadWorkQueue threadAccess, IHintListener listener) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void removeHintListener(IThreadWorkQueue threadAccess, IHintListener listener) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void addKeyHintListener(IThreadWorkQueue threadAccess, Key key, IHintListener listener) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void removeKeyHintListener(IThreadWorkQueue threadAccess, Key key, IHintListener listener) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int hashCode() {
+ return ((hints.hashCode() * 31) + Arrays.hashCode(keysToTry)) * 31;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ AdaptableHintContext other = (AdaptableHintContext) obj;
+ return Arrays.equals(keysToTry, other.keysToTry) && ObjectUtils.objectEquals(hints, other.hints);
+ }
+
+ @Override
+ public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
+ return null;
+ }
+
+}