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