]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceEditorInputFactory2.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceEditorInputFactory2.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.ui.workbench;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.ui.IElementFactory;
19 import org.eclipse.ui.IMemento;
20 import org.simantics.utils.ui.ErrorLogger;
21 import org.simantics.utils.ui.workbench.StringMemento;
22
23 /**
24  * Factory for restoring a <code>ResourceEditorInput2</code>. The stored
25  * representation of a <code>ResourceEditorInput2</code> remembers a reference
26  * to the input resource, which is a string ID, a model URI and an <a
27  * href="https://www.simantics.org/wiki/index.php/Terminology#R">RVI</a>.
28  * 
29  * <p>
30  * The workbench will automatically create instances of this class as required.
31  * It is not intended to be instantiated or subclassed by the client.
32  * </p>
33  */
34 public class ResourceEditorInputFactory2 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.ui.workbench.resourceEditorInputFactory2"; //$NON-NLS-1$
40
41     /**
42      */
43     public static final String TAG_RESOURCE_ID = ResourceEditorInputFactory.TAG_RESOURCE_ID; //$NON-NLS-1$
44
45     /**
46      */
47     public static final String TAG_EDITOR_ID = ResourceEditorInputFactory.TAG_EDITOR_ID; //$NON-NLS-1$
48
49     /**
50      * 
51      */
52     public static final String TAG_MODEL_ID = "modelId";
53
54     /**
55      * 
56      */
57     public static final String TAG_RVI = "rvi";
58
59     /** Tag ID for node that contains external data */
60     public static final String TAG_EXTERNAL_MEMENTO_ID = "external"; //$NON-NLS-1$
61
62     /**
63      * Creates a new factory.
64      */
65     public ResourceEditorInputFactory2() {
66     }
67
68     /* (non-Javadoc)
69      * Method declared on IElementFactory.
70      */
71     @Override
72     public IAdaptable createElement(IMemento memento) {
73         // Get the resource id array.
74         IMemento[] children = memento.getChildren(TAG_RESOURCE_ID);
75         if (children.length != 1)
76             return null;
77
78         List<String> ids = new ArrayList<String>();
79         for (IMemento child : children)
80             ids.add(child.getTextData());
81
82         String editorId = memento.getString(TAG_EDITOR_ID);
83         String modelId = memento.getString(TAG_MODEL_ID);
84         String rvi = memento.getString(TAG_RVI);
85
86         try {
87             ResourceEditorInput2 result = new ResourceEditorInput2(editorId, ids.iterator().next(), modelId, rvi);
88
89             // Read external persistent memento
90             String externalMementoStr = memento.getString(TAG_EXTERNAL_MEMENTO_ID);
91             if (externalMementoStr != null) {
92                 StringMemento sm = new StringMemento(externalMementoStr);
93                 sm.writeToMemento(result.getPersistentStore());
94             }
95             return result;
96         } catch (NumberFormatException e) {
97             return null;
98         } catch (IllegalArgumentException e) {
99             ErrorLogger.defaultLogError(e);
100             return null;
101         }
102     }
103
104     /**
105      * Returns the element factory id for this class.
106      * 
107      * @return the element factory id
108      */
109     public static String getFactoryId() {
110         return ID_FACTORY;
111     }
112
113 }