]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceEditorInputFactory2.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceEditorInputFactory2.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;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.List;\r
16 \r
17 import org.eclipse.core.runtime.IAdaptable;\r
18 import org.eclipse.ui.IElementFactory;\r
19 import org.eclipse.ui.IMemento;\r
20 import org.simantics.utils.ui.ErrorLogger;\r
21 import org.simantics.utils.ui.workbench.StringMemento;\r
22 \r
23 /**\r
24  * Factory for restoring a <code>ResourceEditorInput2</code>. The stored\r
25  * representation of a <code>ResourceEditorInput2</code> remembers a reference\r
26  * to the input resource, which is a string ID, a model URI and an <a\r
27  * href="https://www.simantics.org/wiki/index.php/Terminology#R">RVI</a>.\r
28  * \r
29  * <p>\r
30  * The workbench will automatically create instances of this class as required.\r
31  * It is not intended to be instantiated or subclassed by the client.\r
32  * </p>\r
33  */\r
34 public class ResourceEditorInputFactory2 implements IElementFactory {\r
35     /**\r
36      * Factory id. The workbench plug-in registers a factory by this name\r
37      * with the "org.eclipse.ui.elementFactories" extension point.\r
38      */\r
39     private static final String ID_FACTORY = "org.simantics.ui.workbench.resourceEditorInputFactory2"; //$NON-NLS-1$\r
40 \r
41     /**\r
42      */\r
43     public static final String TAG_RESOURCE_ID = ResourceEditorInputFactory.TAG_RESOURCE_ID; //$NON-NLS-1$\r
44 \r
45     /**\r
46      */\r
47     public static final String TAG_EDITOR_ID = ResourceEditorInputFactory.TAG_EDITOR_ID; //$NON-NLS-1$\r
48 \r
49     /**\r
50      * \r
51      */\r
52     public static final String TAG_MODEL_ID = "modelId";\r
53 \r
54     /**\r
55      * \r
56      */\r
57     public static final String TAG_RVI = "rvi";\r
58 \r
59     /** Tag ID for node that contains external data */\r
60     public static final String TAG_EXTERNAL_MEMENTO_ID = "external"; //$NON-NLS-1$\r
61 \r
62     /**\r
63      * Creates a new factory.\r
64      */\r
65     public ResourceEditorInputFactory2() {\r
66     }\r
67 \r
68     /* (non-Javadoc)\r
69      * Method declared on IElementFactory.\r
70      */\r
71     @Override\r
72     public IAdaptable createElement(IMemento memento) {\r
73         // Get the resource id array.\r
74         IMemento[] children = memento.getChildren(TAG_RESOURCE_ID);\r
75         if (children.length != 1)\r
76             return null;\r
77 \r
78         List<String> ids = new ArrayList<String>();\r
79         for (IMemento child : children)\r
80             ids.add(child.getTextData());\r
81 \r
82         String editorId = memento.getString(TAG_EDITOR_ID);\r
83         String modelId = memento.getString(TAG_MODEL_ID);\r
84         String rvi = memento.getString(TAG_RVI);\r
85 \r
86         try {\r
87             ResourceEditorInput2 result = new ResourceEditorInput2(editorId, ids.iterator().next(), modelId, rvi);\r
88 \r
89             // Read external persistent memento\r
90             String externalMementoStr = memento.getString(TAG_EXTERNAL_MEMENTO_ID);\r
91             if (externalMementoStr != null) {\r
92                 StringMemento sm = new StringMemento(externalMementoStr);\r
93                 sm.writeToMemento(result.getPersistentStore());\r
94             }\r
95             return result;\r
96         } catch (NumberFormatException e) {\r
97             return null;\r
98         } catch (IllegalArgumentException e) {\r
99             ErrorLogger.defaultLogError(e);\r
100             return null;\r
101         }\r
102     }\r
103 \r
104     /**\r
105      * Returns the element factory id for this class.\r
106      * \r
107      * @return the element factory id\r
108      */\r
109     public static String getFactoryId() {\r
110         return ID_FACTORY;\r
111     }\r
112 \r
113 }\r