X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fscl%2FscriptEditor%2FSCLScriptEditorInput.java;fp=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fscl%2FscriptEditor%2FSCLScriptEditorInput.java;h=90a87cba9267b4f356f3b19ec7d9f6c59254fcfb;hb=e36d0cb0313ea18c76479e8dbfaf8499fa52db98;hp=0000000000000000000000000000000000000000;hpb=0cf894be3bd64a393d8883b7500822275c2a15fe;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/scriptEditor/SCLScriptEditorInput.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/scriptEditor/SCLScriptEditorInput.java new file mode 100644 index 000000000..90a87cba9 --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/scriptEditor/SCLScriptEditorInput.java @@ -0,0 +1,114 @@ +/******************************************************************************* + * Copyright (c) 2017 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: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.ui.scl.scriptEditor; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IMemento; +import org.eclipse.ui.IPersistableElement; +import org.simantics.Simantics; +import org.simantics.db.exception.DatabaseException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Tuukka Lehtonen + * @since 1.31.0 + */ +public class SCLScriptEditorInput implements IEditorInput, IPersistableElement { + + private static final Logger LOGGER = LoggerFactory.getLogger(SCLScriptEditorInput.class); + + private final String scriptURI; + + public SCLScriptEditorInput(String scriptURI) { + this.scriptURI = scriptURI; + } + + public String getScriptURI() { + return scriptURI; + } + + @Override + public boolean exists() { + // Not worth it testing this from the database. + return true; + } + + @SuppressWarnings("unchecked") + @Override + public T getAdapter(Class adapter) { + if (adapter.equals(SCLScriptSource.class)) { + try { + return (T) Simantics.getSession().syncRequest(new ReadSCLScriptSource(scriptURI)); + } catch (DatabaseException e) { + LOGGER.error("Failed to read SCL script source", e); + } + } + if (adapter.equals(IPersistableElement.class)) + return (T) this; + return null; + } + + @Override + public String getFactoryId() { + return "org.simantics.modeling.ui.scl.scriptEditor.inputFactory"; + } + + @Override + public String getName() { + int p = scriptURI.lastIndexOf('/'); + if(p >= 0) + return scriptURI.substring(p+1); + else + return scriptURI; + } + + @Override + public String getToolTipText() { + return scriptURI; + } + + @Override + public ImageDescriptor getImageDescriptor() { + return null; + } + + @Override + public IPersistableElement getPersistable() { + return this; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((scriptURI == null) ? 0 : scriptURI.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + SCLScriptEditorInput other = (SCLScriptEditorInput) obj; + return scriptURI.equals(other.scriptURI); + } + + @Override + public void saveState(IMemento memento) { + memento.putTextData(scriptURI); + } + +}