]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/scriptEditor/SCLScriptEditorInput.java
Support for SCL script database storage, editing and execution
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / scl / scriptEditor / SCLScriptEditorInput.java
1 /*******************************************************************************
2  * Copyright (c) 2017 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.scl.scriptEditor;
13
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.ui.IEditorInput;
16 import org.eclipse.ui.IMemento;
17 import org.eclipse.ui.IPersistableElement;
18 import org.simantics.Simantics;
19 import org.simantics.db.exception.DatabaseException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * @author Tuukka Lehtonen
25  * @since 1.31.0
26  */
27 public class SCLScriptEditorInput implements IEditorInput, IPersistableElement {
28
29     private static final Logger LOGGER = LoggerFactory.getLogger(SCLScriptEditorInput.class);
30
31     private final String scriptURI;
32
33     public SCLScriptEditorInput(String scriptURI) {
34         this.scriptURI = scriptURI;
35     }
36
37     public String getScriptURI() {
38         return scriptURI;
39     }
40
41     @Override
42     public boolean exists() {
43         // Not worth it testing this from the database.
44         return true;
45     }
46
47     @SuppressWarnings("unchecked")
48     @Override
49     public <T> T getAdapter(Class<T> adapter) {
50         if (adapter.equals(SCLScriptSource.class)) {
51             try {
52                 return (T) Simantics.getSession().syncRequest(new ReadSCLScriptSource(scriptURI));
53             } catch (DatabaseException e) {
54                 LOGGER.error("Failed to read SCL script source", e);
55             }
56         }
57         if (adapter.equals(IPersistableElement.class))
58             return (T) this;
59         return null;
60     }
61
62     @Override
63     public String getFactoryId() {
64         return "org.simantics.modeling.ui.scl.scriptEditor.inputFactory";
65     }
66
67     @Override
68     public String getName() {
69         int p = scriptURI.lastIndexOf('/');
70         if(p >= 0)
71             return scriptURI.substring(p+1);
72         else
73             return scriptURI;
74     }
75
76     @Override
77     public String getToolTipText() {
78         return scriptURI;
79     }
80
81     @Override
82     public ImageDescriptor getImageDescriptor() {
83         return null;
84     }
85
86     @Override
87     public IPersistableElement getPersistable() {
88         return this;
89     }
90
91     @Override
92     public int hashCode() {
93         final int prime = 31;
94         int result = 1;
95         result = prime * result + ((scriptURI == null) ? 0 : scriptURI.hashCode());
96         return result;
97     }
98
99     @Override
100     public boolean equals(Object obj) {
101         if (this == obj)
102             return true;
103         if (obj == null || getClass() != obj.getClass())
104             return false;
105         SCLScriptEditorInput other = (SCLScriptEditorInput) obj;
106         return scriptURI.equals(other.scriptURI);
107     }
108
109     @Override
110     public void saveState(IMemento memento) {
111         memento.putTextData(scriptURI);
112     }
113
114 }