]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceEditorInputFactory.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceEditorInputFactory.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 /**
25  * Factory for restoring a <code>ResourceEditorInput</code>.
26  * The stored representation of a <code>ResourceEditorInput</code> remembers
27  * a reference to the input resource, which is a string ID.
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 ResourceEditorInputFactory 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.resourceEditorInputFactory"; //$NON-NLS-1$
40
41     /**
42      */
43     public static final String TAG_RESOURCE_ID = "resourceId"; //$NON-NLS-1$
44
45     /**
46      */
47     public static final String TAG_EDITOR_ID = "editorId"; //$NON-NLS-1$
48
49     /** Tag ID for node that contains external data */
50     public static final String TAG_EXTERNAL_MEMENTO_ID = "external"; //$NON-NLS-1$
51
52     /**
53      * Creates a new factory.
54      */
55     public ResourceEditorInputFactory() {
56     }
57
58     /* (non-Javadoc)
59      * Method declared on IElementFactory.
60      */
61     public IAdaptable createElement(IMemento memento) {
62         // Get the resource id array.
63         IMemento[] children = memento.getChildren(TAG_RESOURCE_ID);
64         if (children.length == 0)
65             return null;
66
67         List<String> ids = new ArrayList<String>();
68         for (IMemento child : children)
69             ids.add(child.getTextData());
70
71         String editorId = memento.getString(TAG_EDITOR_ID);
72
73         try {
74             ResourceEditorInput result = new ResourceEditorInput(editorId, ids);
75
76             // Read external persistent memento
77             String externalMementoStr = memento.getString(TAG_EXTERNAL_MEMENTO_ID);
78             if (externalMementoStr != null) {
79                 StringMemento sm = new StringMemento(externalMementoStr);
80                 sm.writeToMemento(result.getPersistentStore());
81             }
82             return result;
83         } catch (NumberFormatException e) {
84             return null;
85         } catch (IllegalArgumentException e) {
86             ErrorLogger.defaultLogError(e);
87             return null;
88         }
89     }
90
91     /**
92      * Returns the element factory id for this class.
93      * 
94      * @return the element factory id
95      */
96     public static String getFactoryId() {
97         return ID_FACTORY;
98     }
99
100 }