]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSEditor.java
ListIndexRoots request
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / CSSEditor.java
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.document.ui;
13
14 import java.util.function.Supplier;
15
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.ui.IEditorInput;
19 import org.eclipse.ui.IEditorSite;
20 import org.eclipse.ui.PartInitException;
21 import org.eclipse.ui.PlatformUI;
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.ui.workbench.IResourceEditorInput;
31 import org.simantics.ui.workbench.ResourceEditorSupport;
32 import org.simantics.ui.workbench.TitleUpdater;
33 import org.simantics.ui.workbench.TitleWithParentNameRequest;
34 import org.simantics.ui.workbench.ToolTipRequest;
35 import org.simantics.ui.workbench.editor.input.InputValidationCombinators;
36
37
38 /**
39  * @author Antti Villberg;
40  */
41 public class CSSEditor extends TextEditor {
42
43         public static final String EDITOR_ID = "org.simantics.document.ui.csseditor";
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
55     public CSSEditor() {
56         super();
57         preInitialize();
58     }
59     
60     @Override
61     public boolean isTabsToSpacesConversionEnabled() {
62         return true;
63     }
64     
65     protected void preInitialize() {
66         //showOverviewRuler();
67         Display display = PlatformUI.getWorkbench().getDisplay();
68         CSSSourceViewerConfiguration sourceViewer = new CSSSourceViewerConfiguration(display, new SharedTextColors(display));
69         setDocumentProvider(new CSSModuleEditorDocumentProvider(sourceViewer));
70         setSourceViewerConfiguration(sourceViewer);
71     }
72
73     protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
74         return INPUT_VALIDATOR;
75     }
76
77     @Override
78     public void init(IEditorSite site, IEditorInput input)
79             throws PartInitException {
80         super.init(site, input);
81
82         support = new ResourceEditorSupport(this, getInputValidator());
83         support.activateValidation();
84         try {
85             getResourceInput().init(null);
86         } catch (DatabaseException e) {
87             throw new PartInitException("Failed to initialize " + input, e);
88         }
89     }
90
91     protected IResourceEditorInput getResourceInput() {
92         return (IResourceEditorInput) getEditorInput();
93     }
94
95     @Override
96     public void createPartControl(Composite parent) {
97         super.createPartControl(parent);
98         StatusLineContributionItem statusLineContribution = new StatusLineContributionItem(
99                 ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION,
100                 true, 14);
101         setStatusField(statusLineContribution,
102                 ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION);
103         getEditorSite().getActionBars().getStatusLineManager().add(statusLineContribution);
104         getEditorSite().getActionBars().updateActionBars();
105         updatePartName();
106     }
107
108     protected void updatePartName() {
109         setPartName(getEditorInput().getName());
110
111         Session session = Simantics.peekSession();
112         if (session != null) {
113             Supplier<Boolean> disposedCallback = this::isDisposed;
114             session.asyncRequest(
115                     new TitleWithParentNameRequest(getResourceInput()),
116                     new TitleUpdater(getSite().getShell().getDisplay(), this::setPartName, disposedCallback));
117             session.asyncRequest(
118                     new ToolTipRequest(getSite().getId(), getResourceInput()),
119                     new TitleUpdater(getSite().getShell().getDisplay(), this::setTitleToolTip, disposedCallback));
120         }
121     }
122
123     @Override
124     public void dispose() {
125         disposed = true;
126         if (support != null) {
127             support.dispose();
128             support = null;
129         }
130         super.dispose();
131     }
132
133     public boolean isDisposed() {
134         return disposed;
135     }
136
137 }