]> gerrit.simantics Code Review - simantics/platform.git/blob
44ceec1a65ff8ab9ec44da426c92fa82e945409a
[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 // FIXME: SCLQueryEditor should extend SCLModuleEditor
44 public class SCLQueryEditor extends TextEditor {
45
46     private boolean disposed = false;
47
48     private ParametrizedRead<IResourceEditorInput, Boolean> INPUT_VALIDATOR =
49             Combinators.compose(
50                     InputValidationCombinators.hasURI(),
51                     InputValidationCombinators.extractInputResource()
52             );
53
54     private ResourceEditorSupport support;
55     protected LocalResourceManager resourceManager;
56     
57     public SCLQueryEditor() {
58         super();
59         //showOverviewRuler();
60         setDocumentProvider(new SCLQueryEditorDocumentProvider(this));
61         resourceManager = new LocalResourceManager(JFaceResources.getResources());
62         SCLSourceViewerConfigurationNew sourceViewerConfiguration = new SCLSourceViewerConfigurationNew(resourceManager);
63         setSourceViewerConfiguration(sourceViewerConfiguration);
64     }
65
66     @Override
67     public boolean isTabsToSpacesConversionEnabled() {
68         return true;
69     }    
70     
71     @Override
72     public void init(IEditorSite site, IEditorInput input)
73             throws PartInitException {
74         super.init(site, input);
75
76         support = new ResourceEditorSupport(this, INPUT_VALIDATOR);
77         support.activateValidation();
78         try {
79             getResourceInput().init(null);
80         } catch (DatabaseException e) {
81             throw new PartInitException("Failed to initialize " + input, e);
82         }
83     }
84
85     protected IResourceEditorInput getResourceInput() {
86         return (IResourceEditorInput) getEditorInput();
87     }
88
89     @Override
90     public void createPartControl(Composite parent) {
91         super.createPartControl(parent);
92         StatusLineContributionItem statusLineContribution = new StatusLineContributionItem(
93                 ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION,
94                 true, 14);
95         setStatusField(statusLineContribution,
96                 ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION);
97         getEditorSite().getActionBars().getStatusLineManager().add(statusLineContribution);
98         getEditorSite().getActionBars().updateActionBars();
99         updatePartName();
100     }
101
102     private void updatePartName() {
103         setPartName(getEditorInput().getName());
104
105         Session session = Simantics.peekSession();
106         if (session != null) {
107             Supplier<Boolean> disposedCallback = () -> disposed;
108             session.asyncRequest(
109                     new TitleWithParentNameRequest(getResourceInput()),
110                     new TitleUpdater(getSite().getShell().getDisplay(), this::setPartName, disposedCallback));
111             session.asyncRequest(
112                     new ToolTipRequest(getSite().getId(), getResourceInput()),
113                     new TitleUpdater(getSite().getShell().getDisplay(), this::setTitleToolTip, disposedCallback));
114         }
115     }
116
117     @Override
118     public void dispose() {
119         disposed = true;
120         if (support != null) {
121             support.dispose();
122             support = null;
123         }
124         super.dispose();
125         resourceManager.dispose();
126     }
127
128 }