1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.document.ui;
14 import java.util.function.Supplier;
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;
39 * @author Antti Villberg;
41 public class CSSEditor extends TextEditor {
43 public static final String EDITOR_ID = "org.simantics.document.ui.csseditor";
45 private boolean disposed = false;
47 private ParametrizedRead<IResourceEditorInput, Boolean> INPUT_VALIDATOR =
49 InputValidationCombinators.hasURI(),
50 InputValidationCombinators.extractInputResource()
53 private ResourceEditorSupport support;
61 public boolean isTabsToSpacesConversionEnabled() {
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);
73 protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
74 return INPUT_VALIDATOR;
78 public void init(IEditorSite site, IEditorInput input)
79 throws PartInitException {
80 super.init(site, input);
82 support = new ResourceEditorSupport(this, getInputValidator());
83 support.activateValidation();
85 getResourceInput().init(null);
86 } catch (DatabaseException e) {
87 throw new PartInitException("Failed to initialize " + input, e);
91 protected IResourceEditorInput getResourceInput() {
92 return (IResourceEditorInput) getEditorInput();
96 public void createPartControl(Composite parent) {
97 super.createPartControl(parent);
98 StatusLineContributionItem statusLineContribution = new StatusLineContributionItem(
99 ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION,
101 setStatusField(statusLineContribution,
102 ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION);
103 getEditorSite().getActionBars().getStatusLineManager().add(statusLineContribution);
104 getEditorSite().getActionBars().updateActionBars();
108 protected void updatePartName() {
109 setPartName(getEditorInput().getName());
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));
124 public void dispose() {
126 if (support != null) {
133 public boolean isDisposed() {