]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceEditorInputFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceEditorInputFactory.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 /**\r
25  * Factory for restoring a <code>ResourceEditorInput</code>.\r
26  * The stored representation of a <code>ResourceEditorInput</code> remembers\r
27  * a reference to the input resource, which is a string ID.\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 ResourceEditorInputFactory 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.resourceEditorInputFactory"; //$NON-NLS-1$\r
40 \r
41     /**\r
42      */\r
43     public static final String TAG_RESOURCE_ID = "resourceId"; //$NON-NLS-1$\r
44 \r
45     /**\r
46      */\r
47     public static final String TAG_EDITOR_ID = "editorId"; //$NON-NLS-1$\r
48 \r
49     /** Tag ID for node that contains external data */\r
50     public static final String TAG_EXTERNAL_MEMENTO_ID = "external"; //$NON-NLS-1$\r
51 \r
52     /**\r
53      * Creates a new factory.\r
54      */\r
55     public ResourceEditorInputFactory() {\r
56     }\r
57 \r
58     /* (non-Javadoc)\r
59      * Method declared on IElementFactory.\r
60      */\r
61     public IAdaptable createElement(IMemento memento) {\r
62         // Get the resource id array.\r
63         IMemento[] children = memento.getChildren(TAG_RESOURCE_ID);\r
64         if (children.length == 0)\r
65             return null;\r
66 \r
67         List<String> ids = new ArrayList<String>();\r
68         for (IMemento child : children)\r
69             ids.add(child.getTextData());\r
70 \r
71         String editorId = memento.getString(TAG_EDITOR_ID);\r
72 \r
73         try {\r
74             ResourceEditorInput result = new ResourceEditorInput(editorId, ids);\r
75 \r
76             // Read external persistent memento\r
77             String externalMementoStr = memento.getString(TAG_EXTERNAL_MEMENTO_ID);\r
78             if (externalMementoStr != null) {\r
79                 StringMemento sm = new StringMemento(externalMementoStr);\r
80                 sm.writeToMemento(result.getPersistentStore());\r
81             }\r
82             return result;\r
83         } catch (NumberFormatException e) {\r
84             return null;\r
85         } catch (IllegalArgumentException e) {\r
86             ErrorLogger.defaultLogError(e);\r
87             return null;\r
88         }\r
89     }\r
90 \r
91     /**\r
92      * Returns the element factory id for this class.\r
93      * \r
94      * @return the element factory id\r
95      */\r
96     public static String getFactoryId() {\r
97         return ID_FACTORY;\r
98     }\r
99 \r
100 }\r