]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphfile/src/org/simantics/graphfile/hack/GraphFileEditorInputFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graphfile / src / org / simantics / graphfile / hack / GraphFileEditorInputFactory.java
1 /*******************************************************************************\r
2  * Copyright (c) 2013 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.graphfile.hack;\r
13 \r
14 import org.eclipse.core.resources.IWorkspace;\r
15 import org.eclipse.core.resources.ResourcesPlugin;\r
16 import org.eclipse.core.runtime.IAdaptable;\r
17 import org.eclipse.ui.IElementFactory;\r
18 import org.eclipse.ui.IMemento;\r
19 import org.simantics.Simantics;\r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.db.request.Read;\r
24 import org.simantics.db.service.SerialisationSupport;\r
25 \r
26 /**\r
27  * FileEditorInputFactory for GraphFiles. Handles loading and saving GraphFile references.\r
28  * \r
29  * TODO : uses resource id to reference resources. That is unreliable.\r
30  * \r
31  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
32  *\r
33  */\r
34 public class GraphFileEditorInputFactory 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.graphfile.hack.GraphFileEditorInputFactory"; //$NON-NLS-1$\r
40 \r
41     /**\r
42      * Tag for the IFile.fullPath of the file resource.\r
43      */\r
44     private static final String TAG_RESOURCE_ID = "id"; //$NON-NLS-1$\r
45 \r
46     /**\r
47      * Creates a new factory.\r
48      */\r
49     public GraphFileEditorInputFactory() {\r
50     }\r
51 \r
52     /* (non-Javadoc)\r
53      * Method declared on IElementFactory.\r
54      */\r
55     public IAdaptable createElement(IMemento memento) {\r
56         // Get the file name.\r
57         String sid = memento.getString(TAG_RESOURCE_ID);\r
58         if (sid == null) {\r
59                         return null;\r
60                 }\r
61         final long id = Long.parseLong(sid);\r
62         \r
63         Resource fileResource = null;\r
64         try {\r
65                 fileResource = Simantics.getSession().syncRequest(new Read<Resource>() {\r
66                         @Override\r
67                         public Resource perform(ReadGraph graph) throws DatabaseException {\r
68                                   SerialisationSupport support = graph.getService(SerialisationSupport.class);\r
69                               try {\r
70                                   Resource r = support.getResource(id);\r
71                                   return r;\r
72                               } catch (Exception e) {\r
73                                   return null;\r
74                               }\r
75                                   \r
76                         }\r
77                 });\r
78         } catch (DatabaseException e) {\r
79                 return null;\r
80         }\r
81         \r
82         if (fileResource == null)\r
83                 return null;\r
84 \r
85         // Get a handle to the IFile...which can be a handle\r
86         // to a resource that does not exist in workspace\r
87         IWorkspace ws = (IWorkspace) ResourcesPlugin.getWorkspace();\r
88         GraphFile file = new GraphFile(fileResource, ws);\r
89         return new GraphFileEditorInput(file);\r
90     }\r
91 \r
92     /**\r
93      * Returns the element factory id for this class.\r
94      * \r
95      * @return the element factory id\r
96      */\r
97     public static String getFactoryId() {\r
98         return ID_FACTORY;\r
99     }\r
100 \r
101     /**\r
102      * Saves the state of the given file editor input into the given memento.\r
103      *\r
104      * @param memento the storage area for element state\r
105      * @param input the file editor input\r
106      */\r
107     public static void saveState(IMemento memento, GraphFileEditorInput input) {\r
108         GraphFile file = (GraphFile)input.getFile();\r
109         memento.putString(TAG_RESOURCE_ID, Long.toString(file.getFileResource().getResourceId()));\r
110     }\r
111 }\r