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