| /*=============================================================================# |
| # 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 org.eclipse.core.runtime.IProgressMonitor; |
| |
| import org.eclipse.statet.ltk.ast.core.AstInfo; |
| 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.issues.core.ProblemRequestor; |
| import org.eclipse.statet.ltk.model.core.IModelManager; |
| import org.eclipse.statet.ltk.model.core.elements.ISourceUnit; |
| import org.eclipse.statet.ltk.model.core.elements.ISourceUnitModelInfo; |
| import org.eclipse.statet.ltk.model.core.elements.IWorkspaceSourceUnit; |
| |
| |
| public abstract class SourceUnitModelContainer<U extends ISourceUnit, M extends ISourceUnitModelInfo> { |
| |
| |
| private final WorkingContext mode; |
| |
| private final U unit; |
| |
| private AstInfo astInfo; |
| |
| private M modelInfo; |
| |
| |
| public SourceUnitModelContainer(final U unit) { |
| this.unit= unit; |
| this.mode= getMode(unit); |
| } |
| |
| |
| public abstract boolean isContainerFor(String modelTypeId); |
| |
| public abstract Class<?> getAdapterClass(); |
| |
| |
| protected WorkingContext getMode(final U su) { |
| if (su instanceof IWorkspaceSourceUnit) { |
| return su.getWorkingContext(); |
| } |
| return null; |
| } |
| |
| protected final WorkingContext getMode() { |
| return this.mode; |
| } |
| |
| |
| public U getSourceUnit() { |
| return this.unit; |
| } |
| |
| public SourceContent getParseContent(final IProgressMonitor monitor) { |
| return this.unit.getContent(monitor); |
| } |
| |
| public AstInfo getAstInfo(final boolean ensureSync, final IProgressMonitor monitor) { |
| if (ensureSync) { |
| getModelManager().reconcile(this, IModelManager.AST, monitor); |
| } |
| return this.astInfo; |
| } |
| |
| public M getModelInfo(final int syncLevel, final IProgressMonitor monitor) { |
| if ((syncLevel & IModelManager.REFRESH) != 0) { |
| clear(); |
| } |
| if ((syncLevel & 0xf) >= IModelManager.MODEL_FILE) { |
| final M currentModel= this.modelInfo; |
| if ((syncLevel & IModelManager.RECONCILE) != 0 |
| || currentModel == null |
| || currentModel.getStamp().getSourceStamp() == 0 |
| || currentModel.getStamp().getSourceStamp() != this.unit.getContentStamp(monitor) ) { |
| getModelManager().reconcile(this, syncLevel, monitor); |
| } |
| } |
| return this.modelInfo; |
| } |
| |
| protected abstract IModelManager getModelManager(); |
| |
| |
| public void clear() { |
| this.astInfo= null; |
| this.modelInfo= null; |
| } |
| |
| public AstInfo getCurrentAst() { |
| if (this.mode == LTK.PERSISTENCE_CONTEXT) { |
| final M model= getCurrentModel(); |
| if (model != null) { |
| return model.getAst(); |
| } |
| return null; |
| } |
| return this.astInfo; |
| } |
| |
| public void setAst(final AstInfo ast) { |
| if (this.mode == LTK.PERSISTENCE_CONTEXT) { |
| return; |
| } |
| this.astInfo= ast; |
| } |
| |
| public M getCurrentModel() { |
| return this.modelInfo; |
| } |
| |
| public void setModel(final M modelInfo) { |
| if (modelInfo != null |
| && (this.astInfo == null || this.astInfo.getStamp().equals(modelInfo.getAst().getStamp())) ) { |
| // otherwise, the ast is probably newer |
| setAst(modelInfo.getAst()); |
| } |
| this.modelInfo= modelInfo; |
| } |
| |
| |
| public ProblemRequestor createProblemRequestor() { |
| return null; |
| } |
| |
| } |