-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.ui.workbench;\r
-\r
-import java.lang.ref.Reference;\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.eclipse.core.runtime.IAdaptable;\r
-import org.eclipse.core.runtime.PlatformObject;\r
-import org.eclipse.jface.resource.ImageDescriptor;\r
-import org.eclipse.ui.IMemento;\r
-import org.eclipse.ui.IPersistableElement;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.common.ResourceArray;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.exception.AdaptionException;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.service.LifecycleSupport;\r
-import org.simantics.ui.icons.ImageDescriptorProvider;\r
-import org.simantics.ui.workbench.editor.input.ResourceEditorInputMatchingStrategy;\r
-import org.simantics.utils.ObjectUtils;\r
-import org.simantics.utils.datastructures.cache.ProvisionException;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-import org.simantics.utils.ui.workbench.StringMemento;\r
-\r
-/**\r
- * A basic editor input for Simantics database {@link Resource} instances.\r
- * \r
- * Editor extensions requiring these as input should always use\r
- * {@link ResourceEditorInputMatchingStrategy} as their matchingStrategy.\r
- * \r
- * @author Tuukka Lehtonen\r
- * \r
- * @see ResourceEditorInput2\r
- * @see ResourceEditorInputMatchingStrategy\r
- */\r
-public class ResourceEditorInput extends PlatformObject implements IResourceEditorInput, IPersistableElement {\r
-\r
- static final String NO_NAME = "(no name)";\r
-\r
- private final String editorID;\r
-\r
- private List<String> resourceIds;\r
-\r
- private transient Reference<ResourceArray> resources;\r
-\r
- private transient boolean exists;\r
-\r
- private transient String name;\r
-\r
- private transient String tooltip;\r
-\r
- private transient ImageDescriptor imageDesc;\r
-\r
- /** Persistent memento for external data */\r
- private final StringMemento persistentStore = new StringMemento();\r
-\r
- /**\r
- * @param editorID\r
- * @param r\r
- */\r
- public ResourceEditorInput(String editorID, Resource r) {\r
- this(editorID, new ResourceArray(r));\r
- }\r
-\r
- /**\r
- * @param editorID\r
- * @param ra\r
- */\r
- public ResourceEditorInput(String editorID, ResourceArray ra) {\r
- if (editorID == null)\r
- throw new IllegalArgumentException("null editor id");\r
- if (ra == null)\r
- throw new IllegalArgumentException("null resource array");\r
- if (ra.isEmpty())\r
- throw new IllegalArgumentException("input resource array is empty, expected non-empty list");\r
- for (Resource r : ra.resources)\r
- if (r == null)\r
- throw new IllegalArgumentException("input resource array contains null resources: " + ra);\r
-\r
- this.editorID = editorID;\r
- this.resources = ResourceInputs.makeReference(ra);\r
- this.resourceIds = ResourceInputs.getRandomAccessIds(ra);\r
-\r
- setNonExistant();\r
- }\r
-\r
- /**\r
- * @param editorID\r
- * @param randomAccessResourceId\r
- */\r
- public ResourceEditorInput(String editorID, String randomAccessResourceId) {\r
- this(editorID, Collections.singletonList(randomAccessResourceId));\r
- }\r
-\r
- /**\r
- * @param editorID\r
- * @param randomAccessResourceId a non-empty list of random access resource\r
- * ids\r
- * @throws IllegalArgumentException if the specified random access id list\r
- * is <code>null</code> or empty\r
- */\r
- public ResourceEditorInput(String editorID, List<String> randomAccessResourceId) {\r
- if (randomAccessResourceId == null)\r
- throw new IllegalArgumentException("null resource id list");\r
- if (randomAccessResourceId.isEmpty())\r
- throw new IllegalArgumentException("input resource id list is empty, expected non-empty list");\r
- for (String id : randomAccessResourceId)\r
- if (id == null)\r
- throw new IllegalArgumentException("input resource id list contains null IDs: " + randomAccessResourceId);\r
-\r
- this.editorID = editorID;\r
- if (editorID == null)\r
- editorID = "";\r
- this.resourceIds = Collections.unmodifiableList(new ArrayList<String>(randomAccessResourceId));\r
- this.resources = ResourceInputs.makeReference(ResourceArray.EMPTY);\r
-\r
- setNonExistant();\r
- }\r
-\r
- @Override\r
- public void init(IAdaptable adapter) throws DatabaseException {\r
- // Initialize resource array if at all possible\r
- ResourceArray ra = getResourceArray();\r
- if (!ra.isEmpty())\r
- updateCaches(true);\r
- }\r
-\r
- @Override\r
- public void dispose() {\r
- //System.out.println("dispose resource editor input: " + name);\r
- // NOTE: this has to be done since Eclipse will cache these IEditorInput\r
- // instances within EditorHistoryItem's that are stored in an EditorHistory\r
- // instance. They are held by strong reference which means that the session\r
- // cannot be collected if it is not nulled here.\r
- resources = null;\r
- }\r
-\r
- @Override\r
- public boolean exists() {\r
- return exists;\r
- }\r
-\r
- @Override\r
- public boolean exists(ReadGraph graph) throws DatabaseException {\r
- for (Resource r : getResourceArray().resources)\r
- if (!graph.hasStatement(r))\r
- return false;\r
- return true;\r
- }\r
-\r
- @Override\r
- public Resource getResource() {\r
- ResourceArray ra = getResourceArray();\r
- return ra.isEmpty() ? null : ra.resources[0];\r
- }\r
-\r
- public ResourceArray getResourceArray0() throws DatabaseException {\r
- ResourceArray ra = tryGetResourceArray();\r
- if (!ra.isEmpty())\r
- return ra;\r
-\r
- Session s = ResourceInputs.peekSession();\r
- if (s == null)\r
- return ResourceArray.EMPTY;\r
-\r
- ra = ResourceInputs.makeResourceArray( s, resourceIds );\r
- this.resources = ResourceInputs.makeReference( ra );\r
- return ra;\r
- }\r
-\r
- @Override\r
- public ResourceArray getResourceArray() {\r
- try {\r
- return getResourceArray0();\r
- } catch (DatabaseException e) {\r
- ErrorLogger.defaultLogError(e);\r
- return ResourceArray.EMPTY;\r
- }\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see org.eclipse.ui.IEditorInput#getImageDescriptor()\r
- */\r
- @Override\r
- public ImageDescriptor getImageDescriptor() {\r
- return imageDesc;\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see org.eclipse.ui.IEditorInput#getName()\r
- */\r
- @Override\r
- public String getName() {\r
- return name;\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see org.eclipse.ui.IEditorInput#getToolTipText()\r
- */\r
- @Override\r
- public String getToolTipText() {\r
- return tooltip;\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see org.eclipse.ui.IEditorInput#getPersistable()\r
- */\r
- @Override\r
- public IPersistableElement getPersistable() {\r
- // Don't allow persistability when it's not possible.\r
- if (!isPersistable())\r
- return null;\r
- return this;\r
- }\r
-\r
- protected boolean isPersistable() {\r
- Session session = Simantics.peekSession();\r
- if (session == null)\r
- return false;\r
- LifecycleSupport lc = session.peekService(LifecycleSupport.class);\r
- if (lc == null)\r
- return false;\r
- if (lc.isClosed())\r
- return false;\r
- return true;\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see org.eclipse.ui.IPersistableElement#getFactoryId()\r
- */\r
- @Override\r
- public String getFactoryId() {\r
- return ResourceEditorInputFactory.getFactoryId();\r
- }\r
-\r
- /**\r
- * Saves the state of the given resource editor input into the given memento.\r
- *\r
- * @param memento the storage area for element state\r
- * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)\r
- */\r
- @Override\r
- public void saveState(IMemento memento) {\r
- for (String id : resourceIds) {\r
- IMemento child = memento.createChild(ResourceEditorInputFactory.TAG_RESOURCE_ID);\r
- child.putTextData(id);\r
- }\r
- memento.putString(ResourceEditorInputFactory.TAG_EDITOR_ID, editorID);\r
- memento.putString(ResourceEditorInputFactory.TAG_EXTERNAL_MEMENTO_ID, persistentStore.toString());\r
- }\r
-\r
- @Override\r
- public int hashCode() {\r
- final int prime = 31;\r
- int result = 1;\r
- result = prime * result + editorID.hashCode();\r
- result = prime * result + ObjectUtils.hashCode(resourceIds);\r
- return result;\r
- }\r
-\r
- @Override\r
- public boolean equals(Object obj) {\r
- if (this == obj)\r
- return true;\r
- if (obj == null)\r
- return false;\r
- if (getClass() != obj.getClass())\r
- return false;\r
- final ResourceEditorInput other = (ResourceEditorInput) obj;\r
- if (!editorID.equals(other.editorID))\r
- return false;\r
- if (!ObjectUtils.objectEquals(resourceIds, other.resourceIds))\r
- return false;\r
- return true;\r
- }\r
-\r
- private void updateCaches(boolean sync) throws DatabaseException {\r
- ReadRequest req = new ReadRequest() {\r
- @Override\r
- public void run(ReadGraph g) throws DatabaseException {\r
- update(g);\r
- }\r
- };\r
- Session s = ResourceInputs.getSession();\r
- if (sync) {\r
- s.syncRequest(req);\r
- } else {\r
- s.asyncRequest(req);\r
- }\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see org.simantics.ui.workbench.IResourceEditorInput#update(org.simantics.db.Graph)\r
- */\r
- @Override\r
- public void update(ReadGraph g) throws DatabaseException {\r
- Resource r = getResource();\r
- if (r == null)\r
- return;\r
-\r
- exists = g.hasStatement(r);\r
- if (exists) {\r
- name = g.syncRequest(new TitleRequest(editorID, this));\r
- if (name == null)\r
- name = NO_NAME;\r
-\r
- tooltip = g.syncRequest(new ToolTipRequest(editorID, this));\r
- if (tooltip == null)\r
- tooltip = NO_NAME;\r
-\r
- try {\r
- ImageDescriptorProvider idp = g.adapt(r, ImageDescriptorProvider.class);\r
- imageDesc = idp.get();\r
- } catch (AdaptionException e) {\r
- imageDesc = ImageDescriptor.getMissingImageDescriptor();\r
- } catch (ProvisionException e) {\r
- imageDesc = ImageDescriptor.getMissingImageDescriptor();\r
- ErrorLogger.defaultLogError(e);\r
- }\r
- } else {\r
- setNonExistant();\r
- }\r
- }\r
-\r
- private void setNonExistant() {\r
- exists = false;\r
- tooltip = name = NO_NAME;\r
- imageDesc = ImageDescriptor.getMissingImageDescriptor();\r
- }\r
-\r
- public IMemento getPersistentStore() {\r
- return persistentStore;\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- return getClass().getSimpleName() + " [name=" + getName() + ", resourceIds=" + resourceIds + ", resources=" + resources + "]";\r
- }\r
-\r
- private ResourceArray tryGetResourceArray() {\r
- Reference<ResourceArray> ref = resources;\r
- if (ref == null)\r
- return ResourceArray.EMPTY;\r
- ResourceArray ra = ref.get();\r
- return ra == null ? ResourceArray.EMPTY : ra;\r
- }\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.ui.workbench;
+
+import java.lang.ref.Reference;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.PlatformObject;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.ui.IMemento;
+import org.eclipse.ui.IPersistableElement;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.common.ResourceArray;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.exception.AdaptionException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.LifecycleSupport;
+import org.simantics.ui.icons.ImageDescriptorProvider;
+import org.simantics.ui.workbench.editor.input.ResourceEditorInputMatchingStrategy;
+import org.simantics.utils.ObjectUtils;
+import org.simantics.utils.datastructures.cache.ProvisionException;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.workbench.StringMemento;
+
+/**
+ * A basic editor input for Simantics database {@link Resource} instances.
+ *
+ * Editor extensions requiring these as input should always use
+ * {@link ResourceEditorInputMatchingStrategy} as their matchingStrategy.
+ *
+ * @author Tuukka Lehtonen
+ *
+ * @see ResourceEditorInput2
+ * @see ResourceEditorInputMatchingStrategy
+ */
+public class ResourceEditorInput extends PlatformObject implements IResourceEditorInput, IPersistableElement {
+
+ static final String NO_NAME = "(no name)";
+
+ private final String editorID;
+
+ private List<String> resourceIds;
+
+ private transient Reference<ResourceArray> resources;
+
+ private transient boolean exists;
+
+ private transient String name;
+
+ private transient String tooltip;
+
+ private transient ImageDescriptor imageDesc;
+
+ /** Persistent memento for external data */
+ private final StringMemento persistentStore = new StringMemento();
+
+ /**
+ * @param editorID
+ * @param r
+ */
+ public ResourceEditorInput(String editorID, Resource r) {
+ this(editorID, new ResourceArray(r));
+ }
+
+ /**
+ * @param editorID
+ * @param ra
+ */
+ public ResourceEditorInput(String editorID, ResourceArray ra) {
+ if (editorID == null)
+ throw new IllegalArgumentException("null editor id");
+ if (ra == null)
+ throw new IllegalArgumentException("null resource array");
+ if (ra.isEmpty())
+ throw new IllegalArgumentException("input resource array is empty, expected non-empty list");
+ for (Resource r : ra.resources)
+ if (r == null)
+ throw new IllegalArgumentException("input resource array contains null resources: " + ra);
+
+ this.editorID = editorID;
+ this.resources = ResourceInputs.makeReference(ra);
+ this.resourceIds = ResourceInputs.getRandomAccessIds(ra);
+
+ setNonExistant();
+ }
+
+ /**
+ * @param editorID
+ * @param randomAccessResourceId
+ */
+ public ResourceEditorInput(String editorID, String randomAccessResourceId) {
+ this(editorID, Collections.singletonList(randomAccessResourceId));
+ }
+
+ /**
+ * @param editorID
+ * @param randomAccessResourceId a non-empty list of random access resource
+ * ids
+ * @throws IllegalArgumentException if the specified random access id list
+ * is <code>null</code> or empty
+ */
+ public ResourceEditorInput(String editorID, List<String> randomAccessResourceId) {
+ if (randomAccessResourceId == null)
+ throw new IllegalArgumentException("null resource id list");
+ if (randomAccessResourceId.isEmpty())
+ throw new IllegalArgumentException("input resource id list is empty, expected non-empty list");
+ for (String id : randomAccessResourceId)
+ if (id == null)
+ throw new IllegalArgumentException("input resource id list contains null IDs: " + randomAccessResourceId);
+
+ this.editorID = editorID;
+ if (editorID == null)
+ editorID = "";
+ this.resourceIds = Collections.unmodifiableList(new ArrayList<String>(randomAccessResourceId));
+ this.resources = ResourceInputs.makeReference(ResourceArray.EMPTY);
+
+ setNonExistant();
+ }
+
+ @Override
+ public void init(IAdaptable adapter) throws DatabaseException {
+ // Initialize resource array if at all possible
+ ResourceArray ra = getResourceArray();
+ if (!ra.isEmpty())
+ updateCaches(true);
+ }
+
+ @Override
+ public void dispose() {
+ //System.out.println("dispose resource editor input: " + name);
+ // NOTE: this has to be done since Eclipse will cache these IEditorInput
+ // instances within EditorHistoryItem's that are stored in an EditorHistory
+ // instance. They are held by strong reference which means that the session
+ // cannot be collected if it is not nulled here.
+ resources = null;
+ }
+
+ @Override
+ public boolean exists() {
+ return exists;
+ }
+
+ @Override
+ public boolean exists(ReadGraph graph) throws DatabaseException {
+ for (Resource r : getResourceArray().resources)
+ if (!graph.hasStatement(r))
+ return false;
+ return true;
+ }
+
+ @Override
+ public Resource getResource() {
+ ResourceArray ra = getResourceArray();
+ return ra.isEmpty() ? null : ra.resources[0];
+ }
+
+ public ResourceArray getResourceArray0() throws DatabaseException {
+ ResourceArray ra = tryGetResourceArray();
+ if (!ra.isEmpty())
+ return ra;
+
+ Session s = ResourceInputs.peekSession();
+ if (s == null)
+ return ResourceArray.EMPTY;
+
+ ra = ResourceInputs.makeResourceArray( s, resourceIds );
+ this.resources = ResourceInputs.makeReference( ra );
+ return ra;
+ }
+
+ @Override
+ public ResourceArray getResourceArray() {
+ try {
+ return getResourceArray0();
+ } catch (DatabaseException e) {
+ ErrorLogger.defaultLogError(e);
+ return ResourceArray.EMPTY;
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
+ */
+ @Override
+ public ImageDescriptor getImageDescriptor() {
+ return imageDesc;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IEditorInput#getName()
+ */
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IEditorInput#getToolTipText()
+ */
+ @Override
+ public String getToolTipText() {
+ return tooltip;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IEditorInput#getPersistable()
+ */
+ @Override
+ public IPersistableElement getPersistable() {
+ // Don't allow persistability when it's not possible.
+ if (!isPersistable())
+ return null;
+ return this;
+ }
+
+ protected boolean isPersistable() {
+ Session session = Simantics.peekSession();
+ if (session == null)
+ return false;
+ LifecycleSupport lc = session.peekService(LifecycleSupport.class);
+ if (lc == null)
+ return false;
+ if (lc.isClosed())
+ return false;
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IPersistableElement#getFactoryId()
+ */
+ @Override
+ public String getFactoryId() {
+ return ResourceEditorInputFactory.getFactoryId();
+ }
+
+ /**
+ * Saves the state of the given resource editor input into the given memento.
+ *
+ * @param memento the storage area for element state
+ * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
+ */
+ @Override
+ public void saveState(IMemento memento) {
+ for (String id : resourceIds) {
+ IMemento child = memento.createChild(ResourceEditorInputFactory.TAG_RESOURCE_ID);
+ child.putTextData(id);
+ }
+ memento.putString(ResourceEditorInputFactory.TAG_EDITOR_ID, editorID);
+ memento.putString(ResourceEditorInputFactory.TAG_EXTERNAL_MEMENTO_ID, persistentStore.toString());
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + editorID.hashCode();
+ result = prime * result + ObjectUtils.hashCode(resourceIds);
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final ResourceEditorInput other = (ResourceEditorInput) obj;
+ if (!editorID.equals(other.editorID))
+ return false;
+ if (!ObjectUtils.objectEquals(resourceIds, other.resourceIds))
+ return false;
+ return true;
+ }
+
+ private void updateCaches(boolean sync) throws DatabaseException {
+ ReadRequest req = new ReadRequest() {
+ @Override
+ public void run(ReadGraph g) throws DatabaseException {
+ update(g);
+ }
+ };
+ Session s = ResourceInputs.getSession();
+ if (sync) {
+ s.syncRequest(req);
+ } else {
+ s.asyncRequest(req);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.simantics.ui.workbench.IResourceEditorInput#update(org.simantics.db.Graph)
+ */
+ @Override
+ public void update(ReadGraph g) throws DatabaseException {
+ Resource r = getResource();
+ if (r == null)
+ return;
+
+ exists = g.hasStatement(r);
+ if (exists) {
+ name = g.syncRequest(new TitleRequest(editorID, this));
+ if (name == null)
+ name = NO_NAME;
+
+ tooltip = g.syncRequest(new ToolTipRequest(editorID, this));
+ if (tooltip == null)
+ tooltip = NO_NAME;
+
+ try {
+ ImageDescriptorProvider idp = g.adapt(r, ImageDescriptorProvider.class);
+ imageDesc = idp.get();
+ } catch (AdaptionException e) {
+ imageDesc = ImageDescriptor.getMissingImageDescriptor();
+ } catch (ProvisionException e) {
+ imageDesc = ImageDescriptor.getMissingImageDescriptor();
+ ErrorLogger.defaultLogError(e);
+ }
+ } else {
+ setNonExistant();
+ }
+ }
+
+ private void setNonExistant() {
+ exists = false;
+ tooltip = name = NO_NAME;
+ imageDesc = ImageDescriptor.getMissingImageDescriptor();
+ }
+
+ public IMemento getPersistentStore() {
+ return persistentStore;
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + " [name=" + getName() + ", resourceIds=" + resourceIds + ", resources=" + resources + "]";
+ }
+
+ private ResourceArray tryGetResourceArray() {
+ Reference<ResourceArray> ref = resources;
+ if (ref == null)
+ return ResourceArray.EMPTY;
+ ResourceArray ra = ref.get();
+ return ra == null ? ResourceArray.EMPTY : ra;
+ }
+
}
\ No newline at end of file