| /*=============================================================================# |
| # Copyright (c) 2009, 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.model.core.impl; |
| |
| import java.lang.reflect.InvocationTargetException; |
| |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IResource; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.SubMonitor; |
| import org.eclipse.jface.text.AbstractDocument; |
| |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| import org.eclipse.statet.jcommons.lang.Nullable; |
| import org.eclipse.statet.jcommons.lang.ObjectUtils; |
| |
| import org.eclipse.statet.ecommons.text.IMarkerPositionResolver; |
| |
| import org.eclipse.statet.ltk.ast.core.AstInfo; |
| import org.eclipse.statet.ltk.core.ElementName; |
| import org.eclipse.statet.ltk.core.Ltk; |
| import org.eclipse.statet.ltk.core.SourceContent; |
| import org.eclipse.statet.ltk.core.WorkingContext; |
| import org.eclipse.statet.ltk.model.core.element.LtkModelElement; |
| import org.eclipse.statet.ltk.model.core.element.SourceDocumentRunnable; |
| import org.eclipse.statet.ltk.model.core.element.SourceUnit; |
| import org.eclipse.statet.ltk.model.core.element.SourceUnitModelInfo; |
| import org.eclipse.statet.ltk.model.core.element.WorkspaceSourceUnit; |
| |
| |
| /** |
| * Generic source unit for files in the Eclipse workspace (IFile). |
| */ |
| @NonNullByDefault |
| public abstract class GenericResourceSourceUnit implements WorkspaceSourceUnit { |
| |
| |
| public static String createResourceId(final IResource file) { |
| return AbstractFilePersistenceSourceUnitFactory.createResourceId(file); |
| } |
| |
| protected static ElementName createResourceName(final IFile file) { |
| return new ElementName() { |
| @Override |
| public int getType() { |
| return 0x011; // see RElementName |
| } |
| @Override |
| public String getDisplayName() { |
| return file.getName(); |
| } |
| @Override |
| public String getSegmentName() { |
| return file.getName(); |
| } |
| @Override |
| public @Nullable ElementName getNextSegment() { |
| return null; |
| } |
| }; |
| } |
| |
| |
| private final String id; |
| private final ElementName name; |
| |
| private final IFile file; |
| private WorkingBuffer buffer; |
| |
| private int counter= 0; |
| |
| |
| @SuppressWarnings("null") |
| public GenericResourceSourceUnit(final String id, final ElementName name, final IFile file) { |
| this.id= id; |
| this.name= name; |
| this.file= ObjectUtils.nonNullAssert(file); |
| } |
| |
| public GenericResourceSourceUnit(final String id, final IFile file) { |
| this(id, createResourceName(file), file); |
| } |
| |
| |
| /** |
| * {@inheritDoc} |
| * |
| * A source unit of this type usually doesn't have a underlying unit. |
| */ |
| @Override |
| public @Nullable SourceUnit getUnderlyingUnit() { |
| return null; |
| } |
| |
| @Override |
| public boolean isSynchronized() { |
| return true; |
| } |
| |
| @Override |
| public final String getId() { |
| return this.id; |
| } |
| |
| /** |
| * {@inheritDoc} |
| * |
| * A source unit of this type usually belongs to the |
| * {@link Ltk#PERSISTENCE_CONTEXT}. |
| */ |
| @Override |
| public WorkingContext getWorkingContext() { |
| return Ltk.PERSISTENCE_CONTEXT; |
| } |
| |
| /** |
| * {@inheritDoc} |
| * |
| * A source unit of this type is usually of the type |
| * {@link LtkModelElement#C2_SOURCE_FILE C2_SOURCE_FILE}. |
| */ |
| @Override |
| public int getElementType() { |
| return C2_SOURCE_FILE; |
| } |
| |
| @Override |
| public final ElementName getElementName() { |
| return this.name; |
| } |
| |
| @Override |
| public boolean exists() { |
| return true; |
| } |
| |
| /** |
| * {@inheritDoc} |
| * |
| * A source unit of this type is usually not read only. |
| */ |
| @Override |
| public boolean isReadOnly() { |
| // true only for e.g. libraries, not because of read only flag |
| return false; |
| } |
| |
| |
| @Override |
| public final IResource getResource() { |
| return this.file; |
| } |
| |
| @Override |
| public @Nullable IMarkerPositionResolver getMarkerPositionResolver() { |
| return null; |
| } |
| |
| |
| @Override |
| public boolean checkState(final boolean validate, final IProgressMonitor monitor) { |
| return this.buffer.checkState(validate, monitor); |
| } |
| |
| @Override |
| public AbstractDocument getDocument(final @Nullable IProgressMonitor monitor) { |
| return this.buffer.getDocument(monitor); |
| } |
| |
| @Override |
| public long getContentStamp(final IProgressMonitor monitor) { |
| return this.buffer.getContentStamp(monitor); |
| } |
| |
| @Override |
| public SourceContent getContent(final IProgressMonitor monitor) { |
| return this.buffer.getContent(monitor); |
| } |
| |
| @Override |
| public void syncExec(final SourceDocumentRunnable runnable) throws InvocationTargetException { |
| throw new UnsupportedOperationException(); |
| } |
| |
| @Override |
| @SuppressWarnings("unchecked") |
| public <T> @Nullable T getAdapter(final Class<T> adapterType) { |
| if (adapterType.equals(IResource.class)) { |
| return (T)getResource(); |
| } |
| return null; |
| } |
| |
| |
| @Override |
| public @Nullable AstInfo getAstInfo(final @Nullable String type, final boolean ensureSync, |
| final IProgressMonitor monitor) { |
| return null; |
| } |
| |
| @Override |
| public @Nullable SourceUnitModelInfo getModelInfo(final @Nullable String type, final int syncLevel, |
| final IProgressMonitor monitor) { |
| return null; |
| } |
| |
| |
| @Override |
| @SuppressWarnings("unused") |
| public synchronized final void connect(final IProgressMonitor monitor) { |
| this.counter++; |
| if (this.counter == 1) { |
| if (this.buffer == null) { |
| this.buffer= new BasicWorkingBuffer(this); |
| } |
| register(); |
| } |
| } |
| |
| @Override |
| public synchronized final void disconnect(final IProgressMonitor monitor) { |
| this.counter--; |
| if (this.counter == 0) { |
| final SubMonitor m= SubMonitor.convert(monitor, 2); |
| this.buffer.releaseDocument(m.newChild(1)); |
| unregister(); |
| } |
| } |
| |
| @Override |
| public synchronized boolean isConnected() { |
| return (this.counter > 0); |
| } |
| |
| protected void register() { |
| } |
| |
| protected void unregister() { |
| } |
| |
| |
| @Override |
| public int hashCode() { |
| return this.id.hashCode(); |
| } |
| |
| @Override |
| public boolean equals(final @Nullable Object obj) { |
| if (this == obj) { |
| return true; |
| } |
| if (obj instanceof SourceUnit) { |
| final SourceUnit other= (SourceUnit)obj; |
| return (getElementType() == other.getElementType() |
| && getWorkingContext() == other.getWorkingContext() |
| && getId().equals(other.getId()) |
| && getModelTypeId().equals(other.getModelTypeId()) ); |
| } |
| return false; |
| } |
| |
| @Override |
| public String toString() { |
| return getModelTypeId() + '/' + getWorkingContext() + ": " + getId(); //$NON-NLS-1$ |
| } |
| |
| } |