| /*=============================================================================# |
| # Copyright (c) 2007, 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.model.core.impl; |
| |
| import java.util.Collections; |
| import java.util.List; |
| |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.SubMonitor; |
| import org.eclipse.jface.text.AbstractDocument; |
| |
| import org.eclipse.statet.ecommons.text.core.sections.IDocContentSections; |
| |
| import org.eclipse.statet.ltk.ast.core.AstInfo; |
| import org.eclipse.statet.ltk.core.ElementName; |
| import org.eclipse.statet.ltk.core.SourceContent; |
| import org.eclipse.statet.ltk.model.core.elements.IModelElement; |
| import org.eclipse.statet.ltk.model.core.elements.ISourceUnit; |
| import org.eclipse.statet.ltk.model.core.elements.ISourceUnitModelInfo; |
| |
| |
| /** |
| * Generic source unit for working copies based on the same unit in the underlying context |
| */ |
| public abstract class GenericSourceUnitWorkingCopy implements ISourceUnit { |
| |
| |
| private final ISourceUnit from; |
| private IWorkingBuffer buffer; |
| |
| private int counter= 0; |
| |
| |
| /** |
| * Creates new working copy of the source unit |
| * |
| * @param from the underlying unit to create a working copy from |
| */ |
| public GenericSourceUnitWorkingCopy(final ISourceUnit from) { |
| this.from= from; |
| } |
| |
| |
| @Override |
| public final ISourceUnit getUnderlyingUnit() { |
| return this.from; |
| } |
| |
| protected final IWorkingBuffer getWorkingBuffer() { |
| return this.buffer; |
| } |
| |
| @Override |
| public boolean isSynchronized() { |
| return this.buffer.isSynchronized(); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public String getModelTypeId() { |
| return this.from.getModelTypeId(); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public int getElementType() { |
| return this.from.getElementType(); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public ElementName getElementName() { |
| return this.from.getElementName(); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public String getId() { |
| return this.from.getId(); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public boolean exists() { |
| return this.counter > 0; |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public boolean isReadOnly() { |
| return false; |
| } |
| |
| @Override |
| public boolean checkState(final boolean validate, final IProgressMonitor monitor) { |
| return this.buffer.checkState(validate, monitor); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public Object getResource() { |
| return this.from.getResource(); |
| } |
| |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public AbstractDocument getDocument(final IProgressMonitor monitor) { |
| return this.buffer.getDocument(monitor); |
| } |
| |
| @Override |
| public IDocContentSections getDocumentContentInfo() { |
| return this.from.getDocumentContentInfo(); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public long getContentStamp(final IProgressMonitor monitor) { |
| return this.buffer.getContentStamp(monitor); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public SourceContent getContent(final IProgressMonitor monitor) { |
| return this.buffer.getContent(monitor); |
| } |
| |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public AstInfo getAstInfo(final String type, final boolean ensureSync, final IProgressMonitor monitor) { |
| return null; |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public ISourceUnitModelInfo getModelInfo(final String type, final int syncLevel, final IProgressMonitor monitor) { |
| return null; |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public IModelElement getModelParent() { |
| return null; // directory |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public boolean hasModelChildren(final Filter filter) { |
| return false; |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public List<? extends IModelElement> getModelChildren(final Filter filter) { |
| return Collections.EMPTY_LIST; |
| } |
| |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public synchronized final void connect(final IProgressMonitor monitor) { |
| this.counter++; |
| if (this.counter == 1) { |
| final SubMonitor m= SubMonitor.convert(monitor, 1); |
| if (this.buffer == null) { |
| m.setWorkRemaining(2); |
| this.buffer= createWorkingBuffer(m.newChild(1)); |
| } |
| register(); |
| this.from.connect(m.newChild(1)); |
| } |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @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(); |
| this.from.disconnect(m.newChild(1)); |
| } |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public synchronized boolean isConnected() { |
| return (this.counter > 0); |
| } |
| |
| protected abstract IWorkingBuffer createWorkingBuffer(SubMonitor m); |
| |
| protected void register() { |
| } |
| |
| protected void unregister() { |
| } |
| |
| @Override |
| public <T> T getAdapter(final Class<T> adapterType) { |
| return this.from.getAdapter(adapterType); |
| } |
| |
| |
| @Override |
| public String toString() { |
| return getModelTypeId() + '/' + getWorkingContext() + ": " + getId(); //$NON-NLS-1$ |
| } |
| |
| } |