]> gerrit.simantics Code Review - simantics/platform.git/blob
7e6426f076b5e11ec95cf2544db47c76583d65b2
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * 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  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.componentTypeEditor;
13
14 import java.util.function.Supplier;
15
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.jface.resource.LocalResourceManager;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IEditorSite;
21 import org.eclipse.ui.PartInitException;
22 import org.eclipse.ui.editors.text.TextEditor;
23 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
24 import org.eclipse.ui.texteditor.StatusLineContributionItem;
25 import org.simantics.Simantics;
26 import org.simantics.db.Session;
27 import org.simantics.db.common.request.ParametrizedRead;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.layer0.request.combinations.Combinators;
30 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
31 import org.simantics.ui.workbench.IResourceEditorInput;
32 import org.simantics.ui.workbench.ResourceEditorSupport;
33 import org.simantics.ui.workbench.TitleUpdater;
34 import org.simantics.ui.workbench.TitleWithParentNameRequest;
35 import org.simantics.ui.workbench.ToolTipRequest;
36 import org.simantics.ui.workbench.editor.input.InputValidationCombinators;
37
38
39 /**
40  * @author Antti Villberg;
41  * @author Hannu Niemistö
42  */
43 public class SCLModuleEditor extends TextEditor {
44
45     private boolean disposed = false;
46
47     private ParametrizedRead<IResourceEditorInput, Boolean> INPUT_VALIDATOR =
48             Combinators.compose(
49                     InputValidationCombinators.hasURI(),
50                     InputValidationCombinators.extractInputResource()
51             );
52
53     private ResourceEditorSupport support;
54     protected LocalResourceManager resourceManager;
55
56     public SCLModuleEditor() {
57         super();
58         resourceManager = new LocalResourceManager(JFaceResources.getResources());
59         preInitialize();
60     }
61     
62     @Override
63     public boolean isTabsToSpacesConversionEnabled() {
64         return true;
65     }
66     
67     protected void preInitialize() {
68         //showOverviewRuler();
69         SCLSourceViewerConfigurationNew sourceViewerConfiguration = new SCLSourceViewerConfigurationNew(resourceManager);
70         setDocumentProvider(new SCLModuleEditorDocumentProvider(sourceViewerConfiguration));
71         setSourceViewerConfiguration(sourceViewerConfiguration);
72     }
73
74     protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
75         return INPUT_VALIDATOR;
76     }
77
78     @Override
79     public void init(IEditorSite site, IEditorInput input)
80             throws PartInitException {
81         super.init(site, input);
82
83         support = new ResourceEditorSupport(this, getInputValidator());
84         support.activateValidation();
85         try {
86             getResourceInput().init(null);
87         } catch (DatabaseException e) {
88             throw new PartInitException("Failed to initialize " + input, e);
89         }
90     }
91
92     protected IResourceEditorInput getResourceInput() {
93         return (IResourceEditorInput) getEditorInput();
94     }
95
96     @Override
97     public void createPartControl(Composite parent) {
98         super.createPartControl(parent);
99         StatusLineContributionItem statusLineContribution = new StatusLineContributionItem(
100                 ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION,
101                 true, 14);
102         setStatusField(statusLineContribution,
103                 ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION);
104         getEditorSite().getActionBars().getStatusLineManager().add(statusLineContribution);
105         getEditorSite().getActionBars().updateActionBars();
106         updatePartName();
107     }
108
109     protected void updatePartName() {
110         setPartName(getEditorInput().getName());
111
112         Session session = Simantics.peekSession();
113         if (session != null) {
114             Supplier<Boolean> disposedCallback = this::isDisposed;
115             session.asyncRequest(
116                     new TitleWithParentNameRequest(getResourceInput()),
117                     new TitleUpdater(getSite().getShell().getDisplay(), this::setPartName, disposedCallback));
118             session.asyncRequest(
119                     new ToolTipRequest(getSite().getId(), getResourceInput()),
120                     new TitleUpdater(getSite().getShell().getDisplay(), this::setTitleToolTip, disposedCallback));
121         }
122     }
123
124     @Override
125     public void dispose() {
126         disposed = true;
127         if (support != null) {
128             support.dispose();
129             support = null;
130         }
131         super.dispose();
132         resourceManager.dispose();
133     }
134
135     public boolean isDisposed() {
136         return disposed;
137     }
138
139 }