]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/CanvasAdapterFactory.java
Combination of Simantics-platform related changes and fixes for district
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / CanvasAdapterFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2011 Association for Decentralized Information Management in
3  * 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.diagram.adapter;
13
14 import java.util.concurrent.atomic.AtomicBoolean;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.core.runtime.IAdapterFactory;
18 import org.simantics.g2d.canvas.ICanvasContext;
19
20 /**
21  * @author Tuukka Lehtonen
22  */
23 public class CanvasAdapterFactory implements IAdapterFactory {
24
25     /**
26      * Hack for preventing eternal {@link #getAdapter(Object, Class)} recursion
27      */
28     AtomicBoolean adapting = new AtomicBoolean(false);
29
30     @SuppressWarnings("rawtypes")
31     @Override
32     public Object getAdapter(Object adaptableObject, Class adapterType) {
33         if (!adapting.compareAndSet(false, true)) {
34             return null;
35         }
36         try {
37             if (ICanvasContext.class.equals(adapterType)) {
38                 if (adaptableObject instanceof IAdaptable) {
39                     return ((IAdaptable) adaptableObject).getAdapter(adapterType);
40                 }
41             }
42             return null;
43         } finally {
44             adapting.set(false);
45         }
46     }
47
48     @SuppressWarnings("rawtypes")
49     @Override
50     public Class[] getAdapterList() {
51         return new Class[] { ICanvasContext.class };
52     }
53
54 }