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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.graphfile.hack;
\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
27 * FileEditorInputFactory for GraphFiles. Handles loading and saving GraphFile references.
\r
29 * TODO : uses resource id to reference resources. That is unreliable.
\r
31 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
34 public class GraphFileEditorInputFactory implements IElementFactory{
\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
39 private static final String ID_FACTORY = "org.simantics.graphfile.hack.GraphFileEditorInputFactory"; //$NON-NLS-1$
\r
42 * Tag for the IFile.fullPath of the file resource.
\r
44 private static final String TAG_RESOURCE_ID = "id"; //$NON-NLS-1$
\r
47 * Creates a new factory.
\r
49 public GraphFileEditorInputFactory() {
\r
53 * Method declared on IElementFactory.
\r
55 public IAdaptable createElement(IMemento memento) {
\r
56 // Get the file name.
\r
57 String sid = memento.getString(TAG_RESOURCE_ID);
\r
61 final long id = Long.parseLong(sid);
\r
63 Resource fileResource = null;
\r
65 fileResource = Simantics.getSession().syncRequest(new Read<Resource>() {
\r
67 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
68 SerialisationSupport support = graph.getService(SerialisationSupport.class);
\r
70 Resource r = support.getResource(id);
\r
72 } catch (Exception e) {
\r
78 } catch (DatabaseException e) {
\r
82 if (fileResource == null)
\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
93 * Returns the element factory id for this class.
\r
95 * @return the element factory id
\r
97 public static String getFactoryId() {
\r
102 * Saves the state of the given file editor input into the given memento.
\r
104 * @param memento the storage area for element state
\r
105 * @param input the file editor input
\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