blob: dafc3e06dc55dd74ba7e0bb6ac4de375b56071fe [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2011, 2020 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.sourceediting;
import java.util.IdentityHashMap;
import java.util.Map;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.information.IInformationProvider;
import org.eclipse.jface.text.information.IInformationProviderExtension;
import org.eclipse.jface.text.information.IInformationProviderExtension2;
import org.eclipse.statet.ecommons.text.core.sections.DocContentSections;
/**
* Source viewer information provider supporting separate implementations for each document content
* section type.
*
* Implement {@link #createHandler(String)} for lazy initialization of the information provider
* of the section types.
*
* @see DocContentSections
*/
public class MultiContentSectionInformationProvider implements
IInformationProvider, IInformationProviderExtension, IInformationProviderExtension2 {
private static final Object NULL= new Object();
private final DocContentSections sections;
private final Map<String, Object> handlers= new IdentityHashMap<>(8);
private EditorInformationProvider activeProvider;
public MultiContentSectionInformationProvider(final DocContentSections sections) {
if (sections == null) {
throw new NullPointerException("sections"); //$NON-NLS-1$
}
this.sections= sections;
}
public MultiContentSectionInformationProvider(final DocContentSections sections,
final String key1, final EditorInformationProvider provider1) {
this(sections, key1, provider1, null, null);
}
public MultiContentSectionInformationProvider(final DocContentSections sections,
final String sectionType1, final EditorInformationProvider provider1,
final String sectionType2, final EditorInformationProvider provider2) {
this(sections);
if (sectionType1 != null) {
registerProvider(sectionType1, provider1);
}
if (sectionType2 != null) {
registerProvider(sectionType2, provider2);
}
}
protected final DocContentSections getSections() {
return this.sections;
}
public void registerProvider(final String sectionType, final EditorInformationProvider provider) {
if (sectionType == null) {
throw new NullPointerException("sectionType"); //$NON-NLS-1$
}
this.handlers.put(sectionType, (provider != null) ? provider : NULL);
}
protected final EditorInformationProvider getProvider(final String sectionType) {
if (sectionType == DocContentSections.ERROR) {
return null;
}
Object handler= this.handlers.get(sectionType);
if (handler == null) {
handler= NULL;
try {
final EditorInformationProvider newHandler= createHandler(sectionType);
// newProvider.init
if (newHandler != null) {
handler= newHandler;
}
}
finally {
this.handlers.put(sectionType, handler);
}
}
return (handler != NULL) ? (EditorInformationProvider) handler : null;
}
protected EditorInformationProvider createHandler(final String sectionType) {
return null;
}
@Override
public IRegion getSubject(final ITextViewer textViewer, final int offset) {
this.activeProvider= null;
final EditorInformationProvider provider= getProvider(
this.sections.getType(textViewer.getDocument(), offset) );
if (provider != null) {
this.activeProvider= provider;
return this.activeProvider.getSubject(textViewer, offset);
}
return null;
}
@Override
public String getInformation(final ITextViewer textViewer, final IRegion subject) {
return null;
}
@Override
public Object getInformation2(final ITextViewer textViewer, final IRegion subject) {
if (this.activeProvider == null) {
return null;
}
return this.activeProvider.getInformation2(textViewer, subject);
}
@Override
public IInformationControlCreator getInformationPresenterControlCreator() {
if (this.activeProvider == null) {
return null;
}
return this.activeProvider.getInformationPresenterControlCreator();
}
}