]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.workbench.editor;\r
13 \r
14 import java.lang.ref.WeakReference;\r
15 import java.util.WeakHashMap;\r
16 \r
17 /**\r
18  * A class for holding a set of (Object, ResourceEditorAdapter) bindings. These\r
19  * bindings are used to store the latest editor opened for input objects. This\r
20  * is used by the default UI double click actions.\r
21  * \r
22  * @author Tuukka Lehtonen\r
23  */\r
24 public class EditorMappingImpl implements EditorMapping {\r
25 \r
26     /**\r
27      * Prevent the map from holding on to resource editor adapters.\r
28      */\r
29     WeakHashMap<Object, WeakReference<EditorAdapter>> resourceToAdapter = new WeakHashMap<Object, WeakReference<EditorAdapter>>();\r
30 \r
31     @Override\r
32     public synchronized void put(Object o, EditorAdapter adapter) {\r
33         assert o != null;\r
34         if (adapter != null) {\r
35             resourceToAdapter.put(o, new WeakReference<EditorAdapter>(adapter));\r
36         } else {\r
37             resourceToAdapter.remove(o);\r
38         }\r
39     }\r
40 \r
41     @Override\r
42     public synchronized EditorAdapter get(Object o) {\r
43         //System.out.println("EditorMapping.get(" + o + "): key set=" + resourceToAdapter.keySet());\r
44         WeakReference<EditorAdapter> ref = resourceToAdapter.get(o);\r
45         if (ref == null)\r
46             return null;\r
47         EditorAdapter adapter = ref.get();\r
48         if (adapter == null) {\r
49             resourceToAdapter.remove(o);\r
50             return null;\r
51         }\r
52         return adapter;\r
53     }\r
54 \r
55     @Override\r
56     public synchronized void clear() {\r
57         resourceToAdapter.clear();\r
58     }\r
59 \r
60 }\r