1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 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.ui.workbench;
\r
14 import java.util.ArrayList;
\r
15 import java.util.List;
\r
17 import org.eclipse.core.runtime.IAdaptable;
\r
18 import org.eclipse.ui.IElementFactory;
\r
19 import org.eclipse.ui.IMemento;
\r
20 import org.simantics.utils.ui.ErrorLogger;
\r
21 import org.simantics.utils.ui.workbench.StringMemento;
\r
25 * Factory for restoring a <code>ResourceEditorInput</code>.
\r
26 * The stored representation of a <code>ResourceEditorInput</code> remembers
\r
27 * a reference to the input resource, which is a string ID.
\r
30 * The workbench will automatically create instances of this class as required.
\r
31 * It is not intended to be instantiated or subclassed by the client.
\r
34 public class ResourceEditorInputFactory 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.ui.workbench.resourceEditorInputFactory"; //$NON-NLS-1$
\r
43 public static final String TAG_RESOURCE_ID = "resourceId"; //$NON-NLS-1$
\r
47 public static final String TAG_EDITOR_ID = "editorId"; //$NON-NLS-1$
\r
49 /** Tag ID for node that contains external data */
\r
50 public static final String TAG_EXTERNAL_MEMENTO_ID = "external"; //$NON-NLS-1$
\r
53 * Creates a new factory.
\r
55 public ResourceEditorInputFactory() {
\r
59 * Method declared on IElementFactory.
\r
61 public IAdaptable createElement(IMemento memento) {
\r
62 // Get the resource id array.
\r
63 IMemento[] children = memento.getChildren(TAG_RESOURCE_ID);
\r
64 if (children.length == 0)
\r
67 List<String> ids = new ArrayList<String>();
\r
68 for (IMemento child : children)
\r
69 ids.add(child.getTextData());
\r
71 String editorId = memento.getString(TAG_EDITOR_ID);
\r
74 ResourceEditorInput result = new ResourceEditorInput(editorId, ids);
\r
76 // Read external persistent memento
\r
77 String externalMementoStr = memento.getString(TAG_EXTERNAL_MEMENTO_ID);
\r
78 if (externalMementoStr != null) {
\r
79 StringMemento sm = new StringMemento(externalMementoStr);
\r
80 sm.writeToMemento(result.getPersistentStore());
\r
83 } catch (NumberFormatException e) {
\r
85 } catch (IllegalArgumentException e) {
\r
86 ErrorLogger.defaultLogError(e);
\r
92 * Returns the element factory id for this class.
\r
94 * @return the element factory id
\r
96 public static String getFactoryId() {
\r