]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/EditorMappingImpl.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / editor / EditorMappingImpl.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/EditorMappingImpl.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/EditorMappingImpl.java
new file mode 100644 (file)
index 0000000..a3355be
--- /dev/null
@@ -0,0 +1,60 @@
+/*******************************************************************************\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.ui.workbench.editor;\r
+\r
+import java.lang.ref.WeakReference;\r
+import java.util.WeakHashMap;\r
+\r
+/**\r
+ * A class for holding a set of (Object, ResourceEditorAdapter) bindings. These\r
+ * bindings are used to store the latest editor opened for input objects. This\r
+ * is used by the default UI double click actions.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class EditorMappingImpl implements EditorMapping {\r
+\r
+    /**\r
+     * Prevent the map from holding on to resource editor adapters.\r
+     */\r
+    WeakHashMap<Object, WeakReference<EditorAdapter>> resourceToAdapter = new WeakHashMap<Object, WeakReference<EditorAdapter>>();\r
+\r
+    @Override\r
+    public synchronized void put(Object o, EditorAdapter adapter) {\r
+        assert o != null;\r
+        if (adapter != null) {\r
+            resourceToAdapter.put(o, new WeakReference<EditorAdapter>(adapter));\r
+        } else {\r
+            resourceToAdapter.remove(o);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public synchronized EditorAdapter get(Object o) {\r
+        //System.out.println("EditorMapping.get(" + o + "): key set=" + resourceToAdapter.keySet());\r
+        WeakReference<EditorAdapter> ref = resourceToAdapter.get(o);\r
+        if (ref == null)\r
+            return null;\r
+        EditorAdapter adapter = ref.get();\r
+        if (adapter == null) {\r
+            resourceToAdapter.remove(o);\r
+            return null;\r
+        }\r
+        return adapter;\r
+    }\r
+\r
+    @Override\r
+    public synchronized void clear() {\r
+        resourceToAdapter.clear();\r
+    }\r
+\r
+}\r