blob: 65e9609e4f9e0b7b80ed8e5f46e2e40184e8eedc [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2009, 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.nico.ui.console;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.AbstractDocument;
import org.eclipse.statet.ecommons.text.core.util.AbstractFragmentDocument;
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.elements.IModelElement;
import org.eclipse.statet.ltk.model.core.elements.ISourceUnit;
import org.eclipse.statet.ltk.model.core.impl.WorkingBuffer;
/**
* Generic source unit for console
*/
public abstract class GenericConsoleSourceUnit implements ISourceUnit {
private final String id;
private final ElementName name;
private final AbstractFragmentDocument document;
private int counter= 0;
public GenericConsoleSourceUnit(final String id, final AbstractFragmentDocument document) {
this.id= id;
this.name= new ElementName() {
@Override
public int getType() {
return 0x011; // see RElementName
}
@Override
public String getDisplayName() {
return GenericConsoleSourceUnit.this.id;
}
@Override
public String getSegmentName() {
return GenericConsoleSourceUnit.this.id;
}
@Override
public ElementName getNextSegment() {
return null;
}
};
this.document= document;
}
/**
* {@inheritDoc}
*/
@Override
public WorkingContext getWorkingContext() {
return LTK.EDITOR_CONTEXT;
}
/**
* {@inheritDoc}
*/
@Override
public ISourceUnit getUnderlyingUnit() {
return null;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSynchronized() {
return true;
}
/**
* {@inheritDoc}
*/
@Override
public int getElementType() {
return IModelElement.C2_SOURCE_CHUNK;
}
/**
* {@inheritDoc}
*/
@Override
public ElementName getElementName() {
return this.name;
}
/**
* {@inheritDoc}
*/
@Override
public String getId() {
return this.id;
}
/**
* {@inheritDoc}
*/
@Override
public boolean exists() {
return this.counter > 0;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isReadOnly() {
return false;
}
/**
* {@inheritDoc}
*
* A console is always modifiable.
*/
@Override
public boolean checkState(final boolean validate, final IProgressMonitor monitor) {
return true;
}
/**
* {@inheritDoc}
*
* A console has no resource.
*/
@Override
public Object getResource() {
return null;
}
protected final AbstractFragmentDocument getDocument() {
return this.document;
}
@Override
public AbstractDocument getDocument(final IProgressMonitor monitor) {
return this.document;
}
@Override
public long getContentStamp(final IProgressMonitor monitor) {
return this.document.getModificationStamp();
}
@Override
public SourceContent getContent(final IProgressMonitor monitor) {
return WorkingBuffer.createContentFromDocument(this.document);
}
@Override
public <T> T getAdapter(final Class<T> adapterType) {
return null;
}
@Override
public IModelElement getModelParent() {
return null;
}
@Override
public boolean hasModelChildren(final Filter filter) {
return false;
}
@Override
public List<? extends IModelElement> getModelChildren(final Filter filter) {
return Collections.EMPTY_LIST;
}
@Override
public synchronized final void connect(final IProgressMonitor monitor) {
this.counter++;
}
@Override
public synchronized final void disconnect(final IProgressMonitor monitor) {
this.counter--;
}
@Override
public synchronized boolean isConnected() {
return (this.counter > 0);
}
@Override
public String toString() {
return getModelTypeId() + '/' + getWorkingContext() + ": " + getId(); //$NON-NLS-1$
}
}