+/*******************************************************************************\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
+/*\r
+ *\r
+ * @author Toni Kalajainen\r
+ */\r
+package org.simantics.utils.datastructures.hints;\r
+\r
+import java.util.Map;\r
+\r
+/**\r
+ * Hint context interface\r
+ *\r
+ * @see Key\r
+ * @see HintContext\r
+ * @see HintStack\r
+ * @see IHintObservable\r
+ */\r
+public interface IHintContext extends IHintObservable {\r
+\r
+ /**\r
+ * Set a value for a hint. \r
+ * @param key key\r
+ * @param value the value, <code>null</code> is not accepted\r
+ */\r
+ void setHint(Key key, Object value);\r
+ \r
+ /**\r
+ * Set a set of hints\r
+ * @param hints\r
+ */\r
+ void setHints(Map<Key, Object> hints);\r
+ \r
+ /**\r
+ * Remove a hint. In stack hint implementation lower priority hint will\r
+ * become effective. \r
+\r
+ * @param <E> type of the object bound to the specified key\r
+ * @param key key to use for the hint\r
+ * @return the typed object currently bound to the specified key\r
+ */\r
+ <E> E removeHint(Key key);\r
+\r
+ /**\r
+ * Clears the context of all current hints without notifying any listeners.\r
+ */\r
+ void clearWithoutNotification();\r
+\r
+ /**\r
+ * Hint key\r
+ */\r
+ public static abstract class Key {\r
+ /**\r
+ * Test if value is accepted\r
+ * @param value the value\r
+ * @return true if value is accepted, false if not\r
+ */\r
+ public abstract boolean isValueAccepted(Object value); \r
+ }\r
+ \r
+ /**\r
+ * Mouse id specific key. With this key there is unique value for each mouse id.\r
+ */\r
+ public static abstract class MouseSpecificKey extends Key {\r
+ private final int mouseId;\r
+ public MouseSpecificKey(int mouseId)\r
+ {\r
+ this.mouseId = mouseId;\r
+ }\r
+ public int getMouseId()\r
+ {\r
+ return mouseId;\r
+ }\r
+ @Override\r
+ public int hashCode() {\r
+ return super.hashCode() ^ mouseId;\r
+ } \r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (!(obj instanceof MouseSpecificKey)) return false;\r
+ if (((MouseSpecificKey)obj).mouseId != mouseId) return false;\r
+ if (!obj.getClass().equals(getClass())) return false;\r
+ return true;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Key whose value is of a specific given class. \r
+ */\r
+ public static class KeyOf extends Key {\r
+ final Class<?> clazz;\r
+ final String keyName;\r
+ public KeyOf(Class<?> clazz)\r
+ {\r
+ this.clazz = clazz;\r
+ this.keyName = null;\r
+ }\r
+ public KeyOf(Class<?> clazz, String keyName)\r
+ {\r
+ this.clazz = clazz;\r
+ this.keyName = keyName;\r
+ }\r
+ @Override\r
+ public boolean isValueAccepted(Object value) {\r
+ return clazz.isInstance(value);\r
+ }\r
+ @Override\r
+ public String toString() {\r
+ if (keyName == null)\r
+ return "Key Of "+clazz.getName();\r
+ return keyName + "(" + clazz.getSimpleName() + ")";\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * String based key\r
+ */\r
+ public static abstract class StringKey extends Key {\r
+ protected final String str; \r
+ protected int hash;\r
+ public StringKey(String str)\r
+ {\r
+ this.str = str;\r
+ hash = getClass().hashCode() ^str.hashCode(); \r
+ }\r
+ public String getString()\r
+ {\r
+ return str;\r
+ }\r
+ @Override\r
+ public int hashCode() {\r
+ return hash;\r
+ } \r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (!(obj instanceof StringKey)) return false;\r
+ if (!((StringKey)obj).str.equals(str)) return false;\r
+ if (obj.getClass()!=getClass()) return false;\r
+ return true;\r
+ }\r
+ } \r
+ \r
+ /**\r
+ * String based key whose value is of a specific class.\r
+ */\r
+ public static class StringKeyOf extends StringKey {\r
+ final Class<?> clazz; \r
+ public StringKeyOf(String str, Class<?> clazz)\r
+ {\r
+ super(str);\r
+ this.clazz = clazz;\r
+ }\r
+ @Override\r
+ public boolean isValueAccepted(Object value) {\r
+ return clazz.isInstance(value);\r
+ }\r
+ @Override\r
+ public String toString() {\r
+ return "Key Of ("+clazz.getName()+", "+str+")";\r
+ } \r
+ }\r
+ \r
+\r
+ public static class MouseSpecificKeyOf extends MouseSpecificKey {\r
+ public final Class<?> clazz;\r
+ public final int mouseId;\r
+ final int hash;\r
+ public MouseSpecificKeyOf(int mouseId, Class<?> clazz)\r
+ {\r
+ super(mouseId);\r
+ this.clazz = clazz;\r
+ this.mouseId = mouseId;\r
+ hash = getClass().hashCode() ^ mouseId ^ 24392439;\r
+ }\r
+ @Override\r
+ public boolean isValueAccepted(Object value) {\r
+ return clazz.isInstance(value);\r
+ }\r
+ @Override\r
+ public int hashCode() {\r
+ return hash;\r
+ }\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (obj.getClass()!=this.getClass()) return false;\r
+ MouseSpecificKeyOf other = (MouseSpecificKeyOf) obj;\r
+ return other.mouseId == mouseId;\r
+ }\r
+ }\r
+ \r
+ \r
+}\r