Java tutorial
/******************************************************************************* * MontiCore Language Workbench, www.monticore.de * Copyright (c) 2017, MontiCore, All rights reserved. * * This project is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this project. If not, see <http://www.gnu.org/licenses/>. *******************************************************************************/ package de.monticore.umlcd4a.texteditor; import static de.se_rwth.langeditor.util.Misc.loadImage; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Optional; import java.util.function.Supplier; import org.eclipse.core.resources.IProject; import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.templates.TemplateProposal; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import de.monticore.ast.ASTNode; import de.monticore.symboltable.ArtifactScope; import de.monticore.symboltable.SymbolKind; import de.monticore.umlcd4a.CD4ACoCos; import de.monticore.umlcd4a.cd4analysis._ast.ASTCDAssociation; import de.monticore.umlcd4a.cd4analysis._ast.ASTCDClass; import de.monticore.umlcd4a.cd4analysis._ast.ASTCDCompilationUnit; import de.monticore.umlcd4a.cd4analysis._ast.ASTCDDefinition; import de.monticore.umlcd4a.cd4analysis._ast.ASTCDEnum; import de.monticore.umlcd4a.cd4analysis._ast.ASTCDInterface; import de.monticore.umlcd4a.cd4analysis._ast.ASTCDMethod; import de.monticore.umlcd4a.cd4analysis._cocos.CD4AnalysisCoCoChecker; import de.monticore.umlcd4a.cd4analysis._parser.CD4AnalysisAntlrLexer; import de.monticore.umlcd4a.cd4analysis._parser.CD4AnalysisAntlrParser; import de.monticore.umlcd4a.symboltable.CDTypeSymbol; import de.monticore.umlcd4a.texteditor.templates.AssociationTemplate; import de.monticore.umlcd4a.texteditor.templates.ClassInterfaceTemplate; import de.monticore.umlcd4a.texteditor.templates.EnumTemplate; import de.se_rwth.commons.logging.Log; import de.se_rwth.langeditor.language.Language; import de.se_rwth.langeditor.language.OutlineElementSet; import de.se_rwth.langeditor.language.OutlineElementSet.Builder; import de.se_rwth.langeditor.language.ParserConfig; import de.se_rwth.langeditor.modelstates.ModelState; public class CDLanguage implements Language { private final Resolving resolving = new Resolving(); private final ImmutableList<String> keywords = ImmutableList.of("return", "package", "import", "boolean", "char", "byte", "short", "int", "long", "float", "double", "public", "private", "protected", "default", "abstract", "static", "void", "extends", "implements", "classdiagram", "class", "interface", "enum", "association"); private final Collection<? extends SymbolKind> completionKinds = Sets.newHashSet(CDTypeSymbol.KIND); @Override public String getExtension() { return "cd"; } @Override public ParserConfig<?> getParserConfig() { return new ParserConfig<>(CD4AnalysisAntlrLexer::new, CD4AnalysisAntlrParser::new, CD4AnalysisAntlrParser::cDCompilationUnit); } @Override public void buildProject(IProject project, ImmutableSet<ModelState> modelStates, ImmutableList<Path> modelPath) { Log.getFindings().clear(); resolving.buildProject(project, modelStates, modelPath); modelStates.forEach(state -> { Log.getFindings().clear(); checkContextConditions(state); }); } @Override public void buildModel(ModelState modelState) { Log.getFindings().clear(); resolving.buildModel(modelState); checkContextConditions(modelState); } @Override public ImmutableList<String> getKeywords() { return keywords; } @Override public Collection<? extends SymbolKind> getCompletionKinds() { return completionKinds; } @Override public OutlineElementSet getOutlineElementSet() { Builder builder = OutlineElementSet.builder(); builder.add(ASTCDDefinition.class, cdDefinition -> cdDefinition.getName(), loadImage("icons/search_decl_obj.gif")); builder.add(ASTCDClass.class, cdClass -> cdClass.getName(), loadImage("icons/class_obj.gif")); builder.add(ASTCDInterface.class, cdInterface -> cdInterface.getName(), loadImage("icons/int_obj.gif")); builder.add(ASTCDEnum.class, cdEnum -> cdEnum.getName(), loadImage("icons/enum_obj.gif")); builder.add(ASTCDAssociation.class, cdAssociation -> cdAssociation.getName() .orElse(cdAssociation.getLeftReferenceName() + "-" + cdAssociation.getRightReferenceName()), loadImage("icons/link_obj.gif")); builder.add(ASTCDMethod.class, cdMethod -> cdMethod.getName(), Optional.empty()); return builder.build(); } @Override public Optional<Supplier<Optional<ASTNode>>> createResolver(ASTNode astNode) { return resolving.createResolver(astNode); } private void checkContextConditions(ModelState modelState) { if (modelState.getRootNode() instanceof ASTCDCompilationUnit) { CD4AnalysisCoCoChecker cocoChecker = new CD4ACoCos().getCheckerForAllCoCos(); try { cocoChecker.handle((ASTCDCompilationUnit) modelState.getRootNode()); } catch (Exception e) { // Do nothing } Log.getFindings().stream().forEach(finding -> { modelState.addAdditionalError(finding); }); } } /** * @see de.se_rwth.langeditor.language.Language#getScope(de.monticore.ast.ASTNode) */ @Override public Optional<ArtifactScope> getScope(ASTNode node) { if (node instanceof ASTCDCompilationUnit) { return Language.super.getScope(((ASTCDCompilationUnit) node).getCDDefinition()); } return Language.super.getScope(node); } /** * @see de.se_rwth.langeditor.language.Language#getTemplateProposals() */ @Override public List<TemplateProposal> getTemplateProposals(ITextViewer viewer, int offset, String prefix) { List<TemplateProposal> templates = new ArrayList<>(); templates.addAll( (new AssociationTemplate()).getTemplateProposals(viewer, offset, CDContext.CLASSDIAGRAM, prefix)); templates.addAll((new ClassInterfaceTemplate()).getTemplateProposals(viewer, offset, CDContext.CLASSDIAGRAM, prefix)); templates.addAll((new EnumTemplate()).getTemplateProposals(viewer, offset, CDContext.CLASSDIAGRAM, prefix)); return templates; } }