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