blob: add79152bb7b3819b428b151872e2ef6884f44dd [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2005, 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 org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.text.AbstractDocument;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
import org.eclipse.statet.jcommons.lang.Nullable;
import org.eclipse.statet.ecommons.text.PartitionerDocumentSetupParticipant;
import org.eclipse.statet.ltk.core.Ltk;
import org.eclipse.statet.ltk.issues.core.ProblemRequestor;
import org.eclipse.statet.ltk.model.core.DocumentModelProvider;
import org.eclipse.statet.ltk.model.core.LtkModels;
import org.eclipse.statet.ltk.model.core.SourceUnitManager;
import org.eclipse.statet.ltk.model.core.element.SourceUnit;
import org.eclipse.statet.ltk.model.core.element.WorkspaceSourceUnit;
@NonNullByDefault
public class SourceDocumentProvider<T extends SourceUnit> extends TextFileDocumentProvider
implements DocumentModelProvider {
public static class SourceFileInfo extends FileInfo {
private @Nullable SourceUnit workingCopy;
}
private final String modelTypeId;
private final PartitionerDocumentSetupParticipant documentSetupParticipant;
public SourceDocumentProvider(final String modelTypeId, final PartitionerDocumentSetupParticipant documentSetupParticipant) {
if (modelTypeId == null) {
throw new NullPointerException("modelTypeId"); //$NON-NLS-1$
}
this.modelTypeId= modelTypeId;
this.documentSetupParticipant= documentSetupParticipant;
// final IDocumentProvider provider= new ForwardingDocumentProvider(documentSetupParticipant.getPartitioningId(),
// this.documentSetupParticipant, new TextFileDocumentProvider());
// setParentDocumentProvider(provider);
}
@Override
protected FileInfo createEmptyFileInfo() {
return new SourceFileInfo();
}
@Override
public void disconnect(final Object element) {
final FileInfo info= getFileInfo(element);
if (info instanceof SourceFileInfo) {
final SourceFileInfo rinfo= (SourceFileInfo) info;
if (rinfo.fCount == 1 && rinfo.workingCopy != null) {
final SubMonitor m= SubMonitor.convert(getProgressMonitor(), 1);
try {
rinfo.workingCopy.disconnect(m.newChild(1));
}
finally {
rinfo.workingCopy= null;
m.done();
}
}
}
super.disconnect(element);
}
@Override
protected @Nullable FileInfo createFileInfo(final Object element) throws CoreException {
final FileInfo info= super.createFileInfo(element);
if (!(info instanceof SourceFileInfo)) {
return null;
}
{ final IDocument document= getDocument(element);
if (document instanceof AbstractDocument) {
setupDocument((AbstractDocument) document);
}
}
final IAdaptable adaptable= (IAdaptable) element;
final SourceFileInfo sourceInfo= (SourceFileInfo) info;
final SubMonitor m= SubMonitor.convert(getProgressMonitor());
try {
final Object ifile= adaptable.getAdapter(IFile.class);
final SourceUnitManager suManager= LtkModels.getSourceUnitManager();
if (ifile != null) {
m.setWorkRemaining(2);
final SourceUnit pUnit= suManager.getSourceUnit(this.modelTypeId, Ltk.PERSISTENCE_CONTEXT,
ifile, true, m.newChild(1) );
sourceInfo.workingCopy= suManager.getSourceUnit(this.modelTypeId, Ltk.EDITOR_CONTEXT,
pUnit, true, m.newChild(1) );
}
else if (element instanceof IURIEditorInput) {
m.setWorkRemaining(1);
final IFileStore store;
try {
store= EFS.getStore(((IURIEditorInput) element).getURI());
}
catch (final CoreException e) {
return sourceInfo;
}
sourceInfo.workingCopy= suManager.getSourceUnit(this.modelTypeId, Ltk.EDITOR_CONTEXT,
store, true, m.newChild(1) );
}
}
finally {
m.done();
}
return sourceInfo;
}
protected void setupDocument(final AbstractDocument document) {
if (this.documentSetupParticipant != null) {
this.documentSetupParticipant.setup(document);
}
}
@Override
public @Nullable T getWorkingCopy(final Object element) {
final FileInfo fileInfo= getFileInfo(element);
if (fileInfo instanceof SourceFileInfo) {
return (T)((SourceFileInfo)fileInfo).workingCopy;
}
return null;
}
@Override
public @Nullable IAnnotationModel getAnnotationModel(@Nullable Object element) {
if (element instanceof WorkspaceSourceUnit) {
element= new FileEditorInput((IFile)((WorkspaceSourceUnit)element).getResource());
}
return super.getAnnotationModel(element);
}
public @Nullable ProblemRequestor createProblemRequestor(final SourceUnit element) {
final IAnnotationModel annotationModel= getAnnotationModel(element);
if (annotationModel instanceof SourceAnnotationModel) {
return ((SourceAnnotationModel) annotationModel).createProblemRequestor();
}
return null;
}
}