]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceEditorPart.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceEditorPart.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2013 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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *     Semantum Oy - issue #4384\r
12  *******************************************************************************/\r
13 package org.simantics.ui.workbench;\r
14 \r
15 import java.util.function.Supplier;\r
16 \r
17 import org.eclipse.core.runtime.IProgressMonitor;\r
18 import org.eclipse.jface.action.IStatusLineManager;\r
19 import org.eclipse.ui.IActionBars;\r
20 import org.eclipse.ui.IEditorInput;\r
21 import org.eclipse.ui.IEditorSite;\r
22 import org.eclipse.ui.PartInitException;\r
23 import org.eclipse.ui.part.EditorPart;\r
24 import org.simantics.db.Resource;\r
25 import org.simantics.db.Session;\r
26 import org.simantics.db.common.request.ParametrizedRead;\r
27 import org.simantics.db.event.ChangeListener;\r
28 import org.simantics.db.management.ISessionContext;\r
29 import org.simantics.ui.SimanticsUI;\r
30 \r
31 /**\r
32  * ResourceEditorPart is a base implementation for editors that support\r
33  * {@link ResourceEditorInput} style inputs for working on top of the Simantics\r
34  * graph database.\r
35  * \r
36  * <p>\r
37  * If you want your ResourceEditorPart implementation to receive notifications\r
38  * for all graph change events through the {@link ChangeListener} interface,\r
39  * just implement it and it will be automatically invoked by this base\r
40  * implementation.\r
41  * </p>\r
42  * \r
43  * @author Tuukka Lehtonen\r
44  */\r
45 public abstract class ResourceEditorPart extends EditorPart implements IResourceEditorPart {\r
46 \r
47     protected boolean               disposed = false;\r
48     protected ResourceEditorSupport support;\r
49 \r
50     /**\r
51      * Override to define your own input resource editor input validator that\r
52      * the view uses by default in {@link #init(IEditorSite, IEditorInput)}.\r
53      * \r
54      * @return\r
55      */\r
56     protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {\r
57         return null;\r
58     }\r
59 \r
60     @Override\r
61     public void init(IEditorSite site, IEditorInput input) throws PartInitException {\r
62         init(site, input, getInputValidator());\r
63     }\r
64     \r
65     protected void createSupport(ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) throws PartInitException {\r
66         support = new ResourceEditorSupport(this, inputValidator);\r
67     }\r
68 \r
69     protected void init(IEditorSite site, IEditorInput input,\r
70             ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) throws PartInitException {\r
71         if (!(input instanceof IResourceEditorInput))\r
72             throw new PartInitException("Invalid input: must be IResourceEditorInput");\r
73 \r
74         setSite(site);\r
75         setInput(input);\r
76         createSupport(inputValidator);\r
77 \r
78         // Set initial part name according to the name given by IEditorInput\r
79         setPartName(getEditorInput().getName());\r
80 \r
81         Session session = SimanticsUI.peekSession();\r
82         if (session != null) {\r
83             Supplier<Boolean> disposedCallback = () -> disposed;\r
84             session.asyncRequest(\r
85                     new TitleRequest(site.getId(), getResourceInput()),\r
86                     new TitleUpdater(site.getShell().getDisplay(), this::setPartName, disposedCallback));\r
87             session.asyncRequest(\r
88                     new ToolTipRequest(site.getId(), getResourceInput()),\r
89                     new TitleUpdater(site.getShell().getDisplay(), this::setTitleToolTip, disposedCallback));\r
90         }\r
91     }\r
92 \r
93     @Override\r
94     public void dispose() {\r
95         disposed = true;\r
96         support.dispose();\r
97         super.dispose();\r
98     }\r
99 \r
100     protected void activateValidation() {\r
101         support.activateValidation();\r
102     }\r
103 \r
104     public ISessionContext getSessionContext() {\r
105         return support.getSessionContext();\r
106     }\r
107 \r
108     public Session getSession() {\r
109         return support.getSession();\r
110     }\r
111 \r
112     /**\r
113      * A resource editor does not need to perform any save operations since the\r
114      * graph model is global and different parts of it need not be saved\r
115      * separately.\r
116      * \r
117      * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)\r
118      */\r
119     @Override\r
120     public void doSave(IProgressMonitor monitor) {\r
121         //System.out.println("[ResourceEditorPart] doSave: " + getPartName());\r
122     }\r
123 \r
124     @Override\r
125     public void doSaveAs() {\r
126         //System.out.println("[ResourceEditorPart] doSaveAs: " + getPartName());\r
127     }\r
128 \r
129     /**\r
130      * A resource editor should never be dirty since its purpose is to reflect\r
131      * the current state of the graph model.\r
132      * \r
133      * @see org.eclipse.ui.part.EditorPart#isDirty()\r
134      */\r
135     @Override\r
136     public boolean isDirty() {\r
137         //System.out.println("[ResourceEditorPart] isDirty: " + getPartName());\r
138         return false;\r
139     }\r
140 \r
141     @Override\r
142     public boolean isSaveAsAllowed() {\r
143         // Graph edits are always immediately sent to "UndoCore" which means\r
144         // that resource graph editors do not support save-features as such.\r
145         return false;\r
146     }\r
147 \r
148     @Override\r
149     public IResourceEditorInput getResourceInput() {\r
150         return (IResourceEditorInput) getEditorInput();\r
151     }\r
152 \r
153     //-------\r
154     // UTILS\r
155     //-------\r
156 \r
157     public IStatusLineManager getStatusLineManager() {\r
158         IActionBars bars = getEditorSite().getActionBars();\r
159         IStatusLineManager mgr = bars.getStatusLineManager();\r
160         return mgr;\r
161     }\r
162 \r
163     /**\r
164      * @param message <code>null</code> to remove message\r
165      */\r
166     public void setStatusMessage(String message) {\r
167         getStatusLineManager().setMessage(message);\r
168     }\r
169 \r
170     /**\r
171      * @param message <code>null</code> to remove message\r
172      */\r
173     public void setStatusErrorMessage(String message) {\r
174         getStatusLineManager().setErrorMessage(message);\r
175     }\r
176 \r
177     protected Resource getInputResource() {\r
178         return getResourceInput().getResource();\r
179     }\r
180 \r
181     protected String getInputName() {\r
182         return getEditorInput().getName();\r
183     }\r
184 \r
185     protected String getTitleText() {\r
186         return getInputName();\r
187     }\r
188 \r
189     protected String getTitleTooltip() {\r
190         return getInputName();\r
191     }\r
192 \r
193     protected void updateTitle() {\r
194         setPartName(getTitleText());\r
195         setTitleToolTip(getTitleTooltip());\r
196     }\r
197 \r
198     /**\r
199      * A utility method for easier invocation of Runnables asynchronously in the\r
200      * SWT UI thread.\r
201      * \r
202      * @param run\r
203      */\r
204     protected void asyncExec(Runnable run) {\r
205         getSite().getShell().getDisplay().asyncExec(run);\r
206     }\r
207 \r
208     @SuppressWarnings("unchecked")\r
209     @Override\r
210     public <T> T getAdapter(Class<T> adapter) {\r
211         if (adapter == Session.class)\r
212             return (T) getSession();\r
213         return super.getAdapter(adapter);\r
214     }\r
215 \r
216 }\r