| /*=============================================================================# |
| # Copyright (c) 2008, 2019 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.ltk.ui.templates.config; |
| |
| import java.util.Map; |
| import java.util.Set; |
| |
| import org.eclipse.jface.resource.JFaceResources; |
| import org.eclipse.jface.text.AbstractDocument; |
| import org.eclipse.jface.text.Document; |
| import org.eclipse.jface.text.IDocument; |
| import org.eclipse.jface.text.source.ISourceViewer; |
| import org.eclipse.jface.text.source.SourceViewer; |
| import org.eclipse.jface.text.templates.ContextTypeRegistry; |
| import org.eclipse.jface.text.templates.DocumentTemplateContext; |
| import org.eclipse.jface.text.templates.Template; |
| import org.eclipse.jface.text.templates.TemplateContextType; |
| import org.eclipse.jface.text.templates.persistence.TemplateStore; |
| import org.eclipse.jface.window.Window; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.graphics.Point; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.ui.editors.text.EditorsUI; |
| import org.eclipse.ui.texteditor.templates.AbstractTemplatesPage; |
| import org.eclipse.ui.texteditor.templates.ITemplatesPage; |
| |
| import org.eclipse.statet.jcommons.text.core.TextRegion; |
| |
| import org.eclipse.statet.ecommons.preferences.ui.SettingsUpdater; |
| import org.eclipse.statet.ecommons.templates.TemplateVariableProcessor; |
| import org.eclipse.statet.ecommons.text.ui.TextViewerEditorColorUpdater; |
| import org.eclipse.statet.ecommons.text.ui.TextViewerJFaceUpdater; |
| import org.eclipse.statet.ecommons.ui.ISettingsChangedHandler; |
| import org.eclipse.statet.ecommons.ui.util.UIAccess; |
| |
| import org.eclipse.statet.ltk.ui.sourceediting.ISourceEditor; |
| import org.eclipse.statet.ltk.ui.sourceediting.SourceEditor1; |
| import org.eclipse.statet.ltk.ui.sourceediting.SourceEditorViewerConfigurator; |
| import org.eclipse.statet.ltk.ui.sourceediting.ViewerSourceEditorAdapter; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.TemplateProposal; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.TemplateProposal.TemplateProposalParameters; |
| import org.eclipse.statet.ltk.ui.templates.EnhTemplateStore; |
| import org.eclipse.statet.ltk.ui.templates.EnhTemplateStore.WorkingCopy; |
| import org.eclipse.statet.ltk.ui.util.LTKSelectionUtils; |
| |
| |
| /** |
| * Abstract {@link ITemplatesPage} for SourceEditor1/SourceViewerConfigurator |
| */ |
| public abstract class AbstractEditorTemplatesPage extends AbstractTemplatesPage { |
| |
| |
| private final EnhTemplateStore templateStore; |
| private final EnhTemplateStore.WorkingCopy templateStoreWorkingCopy; |
| private Runnable templateStoreListener; |
| |
| private final SourceEditor1 editor; |
| |
| private ISourceEditor previewEditor; |
| private final TemplateVariableProcessor previewTemplateProcessor; |
| private final TemplateVariableProcessor editTemplateProcessor; |
| |
| private SourceEditorViewerConfigurator currentPreviewConfigurator; |
| private TextViewerJFaceUpdater currentPreviewUpdater; |
| |
| |
| protected AbstractEditorTemplatesPage(final EnhTemplateStore templateStore, |
| final SourceEditor1 editor, final ISourceViewer viewer) { |
| super(editor, viewer); |
| |
| this.templateStore= templateStore; |
| this.templateStoreWorkingCopy= this.templateStore.getWorkingCopy(); |
| |
| this.editor= editor; |
| |
| this.previewTemplateProcessor= new TemplateVariableProcessor(); |
| this.editTemplateProcessor= new TemplateVariableProcessor(); |
| } |
| |
| |
| protected SourceEditor1 getEditor() { |
| return this.editor; |
| } |
| |
| @Override |
| public TemplateStore getTemplateStore() { |
| return this.templateStoreWorkingCopy; |
| } |
| |
| @Override |
| protected ContextTypeRegistry getContextTypeRegistry() { |
| return this.templateStore.getContextTypeRegistry(); |
| } |
| |
| |
| @Override |
| protected boolean isValidTemplate(final IDocument document, final Template template, |
| final int offset, final int length) { |
| final String[] contextIds= getContextTypeIds(document, offset); |
| for (int i= 0; i < contextIds.length; i++) { |
| if (contextIds[i].equals(template.getContextTypeId())) { |
| final DocumentTemplateContext context= createContext(document, template, offset, length); |
| return context.canEvaluate(template); |
| } |
| } |
| return false; |
| } |
| |
| @Override |
| protected void insertTemplate(final Template template, final IDocument document) { |
| final ISourceEditor sourceEditor= this.editor.getAdapter(ISourceEditor.class); |
| if (!sourceEditor.isEditable(true)) { |
| return; |
| } |
| final SourceViewer sourceViewer= sourceEditor.getViewer(); |
| final Point selectedRange= sourceViewer.getSelectedRange(); |
| final DocumentTemplateContext context= createContext(document, template, selectedRange.x, selectedRange.y); |
| if (context == null) { |
| return; |
| } |
| final TextRegion region= LTKSelectionUtils.toTextRegion(selectedRange); |
| final TemplateProposal proposal= new TemplateProposal( |
| new TemplateProposalParameters<>(template, context, region) ); |
| this.editor.getSite().getPage().activate(this.editor); |
| proposal.apply(sourceViewer, (char) 0, 0, region.getStartOffset()); |
| } |
| |
| |
| @Override |
| public void createControl(final Composite ancestor) { |
| if (this.templateStoreListener == null) { |
| this.templateStoreListener= new Runnable() { |
| @Override |
| public void run() { |
| final WorkingCopy templateStore= AbstractEditorTemplatesPage.this.templateStoreWorkingCopy; |
| if (templateStore != null) { |
| templateStore.load(); |
| } |
| } |
| }; |
| this.templateStore.addListener(this.templateStoreListener); |
| } |
| this.templateStoreListener.run(); |
| |
| super.createControl(ancestor); |
| } |
| |
| @Override |
| public void dispose() { |
| if (this.templateStoreListener != null) { |
| this.templateStore.removeListener(this.templateStoreListener); |
| this.templateStoreListener= null; |
| } |
| |
| super.dispose(); |
| } |
| |
| @Override |
| protected SourceViewer createPatternViewer(final Composite parent) { |
| final SourceViewer viewer= new SourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); |
| viewer.setEditable(false); |
| viewer.getTextWidget().setFont(JFaceResources.getFont(JFaceResources.TEXT_FONT)); |
| new TextViewerEditorColorUpdater(viewer, EditorsUI.getPreferenceStore()); |
| |
| final IDocument document= new Document(); |
| viewer.setDocument(document); |
| |
| this.previewEditor= new ViewerSourceEditorAdapter(viewer, null); |
| new SettingsUpdater(new ISettingsChangedHandler() { |
| @Override |
| public void handleSettingsChanged(final Set<String> groupIds, final Map<String, Object> options) { |
| if (AbstractEditorTemplatesPage.this.currentPreviewConfigurator != null) { |
| AbstractEditorTemplatesPage.this.currentPreviewConfigurator.handleSettingsChanged(groupIds, options); |
| } |
| } |
| }, viewer.getControl()); |
| |
| return viewer; |
| } |
| |
| @Override |
| protected void updatePatternViewer(final Template template) { |
| final SourceViewer patternViewer= getPatternViewer(); |
| if (patternViewer == null || !(UIAccess.isOkToUse(patternViewer.getControl())) ) { |
| return; |
| } |
| |
| if (template != null) { |
| final SourceEditorViewerConfigurator configurator= getTemplatePreviewConfig(template, this.previewTemplateProcessor); |
| final TemplateContextType type= getContextTypeRegistry().getContextType(template.getContextTypeId()); |
| this.previewTemplateProcessor.setContextType(type); |
| |
| if (configurator != this.currentPreviewConfigurator) { |
| if (this.currentPreviewUpdater != null) { |
| this.currentPreviewUpdater.dispose(); |
| this.currentPreviewUpdater= null; |
| } |
| if (this.currentPreviewConfigurator != null) { |
| this.currentPreviewConfigurator.unconfigureTarget(); |
| } |
| |
| this.currentPreviewConfigurator= configurator; |
| this.currentPreviewConfigurator.setTarget(this.previewEditor); |
| this.currentPreviewUpdater= new TextViewerJFaceUpdater(patternViewer, |
| this.currentPreviewConfigurator.getSourceViewerConfiguration().getPreferences() ); |
| |
| final AbstractDocument document= new Document(); |
| this.currentPreviewConfigurator.getDocumentSetupParticipant().setup(document); |
| configureDocument(document, type, configurator); |
| document.set(template.getPattern()); |
| patternViewer.setDocument(document); |
| } |
| else { |
| final AbstractDocument document= (AbstractDocument) patternViewer.getDocument(); |
| document.set(""); //$NON-NLS-1$ |
| configureDocument(document, type, configurator); |
| document.set(template.getPattern()); |
| } |
| |
| } |
| else { |
| patternViewer.getDocument().set(""); //$NON-NLS-1$ |
| } |
| patternViewer.setSelectedRange(0, 0); |
| } |
| |
| @Override |
| protected Template editTemplate(final Template template, final boolean edit, final boolean isNameModifiable) { |
| final SourceEditorViewerConfigurator configurator= getTemplateEditConfig(template, this.editTemplateProcessor); |
| final org.eclipse.statet.ltk.ui.templates.config.EditTemplateDialog dialog= new org.eclipse.statet.ltk.ui.templates.config.EditTemplateDialog( |
| getSite().getShell(), template, edit, |
| org.eclipse.statet.ltk.ui.templates.config.EditTemplateDialog.EDITOR_TEMPLATE, |
| configurator, this.editTemplateProcessor, getContextTypeRegistry(), |
| TemplateConfigUI.PREF_QUALIFIER ) { |
| |
| @Override |
| protected void configureForContext(final TemplateContextType contextType) { |
| super.configureForContext(contextType); |
| final SourceViewer sourceViewer= getSourceViewer(); |
| final AbstractDocument document= (AbstractDocument) sourceViewer.getDocument(); |
| AbstractEditorTemplatesPage.this.configureDocument(document, contextType, getSourceViewerConfigurator()); |
| } |
| }; |
| if (dialog.open() == Window.OK) { |
| return dialog.getTemplate(); |
| } |
| return null; |
| } |
| |
| |
| protected abstract DocumentTemplateContext createContext(IDocument document, |
| Template template, int offset, int length); |
| |
| protected abstract SourceEditorViewerConfigurator getTemplatePreviewConfig( |
| Template template, TemplateVariableProcessor templateProcessor); |
| |
| protected abstract SourceEditorViewerConfigurator getTemplateEditConfig( |
| Template template, TemplateVariableProcessor templateProcessor); |
| |
| /** |
| * Can be implemented to configure the document when the context is changed |
| * |
| * @param document the document to adapt |
| * @param contextType the new context |
| * @param configurator the configurator of the viewer/document (preview or edit) |
| */ |
| protected void configureDocument(final AbstractDocument document, |
| final TemplateContextType contextType, final SourceEditorViewerConfigurator configurator) { |
| } |
| |
| } |