| /** |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| * |
| * This copyright notice shows up in the generated Java code |
| */ |
| package org.eclipse.osbp.xtext.menu.validation; |
| |
| import java.util.Collection; |
| import java.util.HashSet; |
| import java.util.LinkedHashSet; |
| import java.util.Set; |
| import org.eclipse.emf.common.util.EList; |
| import org.eclipse.osbp.xtext.menu.MenuCategory; |
| import org.eclipse.osbp.xtext.menu.MenuDslPackage; |
| import org.eclipse.osbp.xtext.menu.MenuTree; |
| import org.eclipse.osbp.xtext.menu.validation.AbstractMenuDslValidator; |
| import org.eclipse.xtext.validation.Check; |
| |
| /** |
| * Custom validation rules. |
| * |
| * see http://www.eclipse.org/Xtext/documentation.html#validation |
| */ |
| @SuppressWarnings("all") |
| public class MenuDslValidator extends AbstractMenuDslValidator { |
| private Set<MenuTree> findDuplicates(final Collection<MenuTree> list) { |
| LinkedHashSet<MenuTree> duplicates = new LinkedHashSet<MenuTree>(); |
| HashSet<String> uniques = new HashSet<String>(); |
| for (final MenuTree tree : list) { |
| boolean _add = uniques.add(tree.getName()); |
| boolean _not = (!_add); |
| if (_not) { |
| duplicates.add(tree); |
| } |
| } |
| return duplicates; |
| } |
| |
| @Check |
| public void checkMenuTreeDuplicates(final MenuCategory menu) { |
| Set<MenuTree> duplicates = this.findDuplicates(menu.getTrees()); |
| int idx = 0; |
| EList<MenuTree> _trees = menu.getTrees(); |
| for (final MenuTree menuTree : _trees) { |
| { |
| boolean _contains = duplicates.contains(menuTree); |
| if (_contains) { |
| this.error("Duplicated menu tree names are not allowed in a menu category definition", MenuDslPackage.Literals.MENU_CATEGORY__TREES, idx); |
| } |
| idx++; |
| } |
| } |
| } |
| } |