blob: b67cd5a9da754859cd527843e9df85d24a9f5d91 [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.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.IDocumentModelProvider;
import org.eclipse.statet.ltk.model.core.ISourceUnitManager;
import org.eclipse.statet.ltk.model.core.elements.ISourceUnit;
import org.eclipse.statet.ltk.model.core.elements.IWorkspaceSourceUnit;
public class SourceDocumentProvider<T extends ISourceUnit> extends TextFileDocumentProvider
implements IDocumentModelProvider {
public static class SourceFileInfo extends FileInfo {
private ISourceUnit 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 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 ISourceUnitManager suManager= LTK.getSourceUnitManager();
if (ifile != null) {
m.setWorkRemaining(2);
final ISourceUnit 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 T getWorkingCopy(final Object element) {
final FileInfo fileInfo= getFileInfo(element);
if (fileInfo instanceof SourceFileInfo) {
return (T) ((SourceFileInfo) fileInfo).workingCopy;
}
return null;
}
@Override
public IAnnotationModel getAnnotationModel(Object element) {
if (element instanceof IWorkspaceSourceUnit) {
element= new FileEditorInput((IFile) ((IWorkspaceSourceUnit) element).getResource());
}
return super.getAnnotationModel(element);
}
public ProblemRequestor createProblemRequestor(final ISourceUnit element) {
final IAnnotationModel annotationModel= getAnnotationModel(element);
if (annotationModel instanceof SourceAnnotationModel) {
return ((SourceAnnotationModel) annotationModel).createProblemRequestor();
}
return null;
}
}