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