]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui.workbench/src/org/simantics/utils/ui/workbench/IAdaptableUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui.workbench / src / org / simantics / utils / ui / workbench / IAdaptableUtils.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.utils.ui.workbench;
13
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.ui.IElementFactory;
16 import org.eclipse.ui.IMemento;
17 import org.eclipse.ui.PlatformUI;
18
19 /**
20  * @author Tuukka Lehtonen
21  */
22 public final class IAdaptableUtils {
23
24     public static IAdaptable getAdaptable(String factoryId, IMemento memento) {
25         IElementFactory factory = PlatformUI.getWorkbench().getElementFactory(factoryId);
26         if (factory == null)
27             return null;
28         return factory.createElement(memento);
29     }
30
31     @SuppressWarnings("unchecked")
32     public static <T> T adapt(String factoryId, IMemento memento, Class<T> clazz) {
33         IAdaptable adaptable = getAdaptable(factoryId, memento);
34         if (adaptable == null)
35             return null;
36         return (T) adaptable.getAdapter(clazz);
37     }
38
39 }