| /******************************************************************************* |
| * Copyright (c) 2011, Istvan Rath, Zoltan Ujhelyi, Daniel Varro |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.visualisation.common.filters; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import org.eclipse.jface.viewers.Viewer; |
| import org.eclipse.jface.viewers.ViewerFilter; |
| import org.eclipse.viatra2.core.IModelElement; |
| import org.eclipse.viatra2.core.IModelManager; |
| import org.eclipse.viatra2.core.IModelSpace; |
| |
| /** |
| * Type-based abstract viewer filter, provides facilities |
| * to filter the model space view according to a given set of |
| * - allowed |
| * - disallowed |
| * VPM types. |
| * |
| * @author Istvan Rath |
| * |
| */ |
| public abstract class TypeBasedFilter extends ViewerFilter { |
| |
| protected final static List<String> allowedTypeIDs = new ArrayList<String>(); |
| |
| protected final static List<String> disallowedTypeIDs = new ArrayList<String>(); |
| |
| /** |
| * hash used to detect when the filter is being used for a different modelspace than previously. |
| */ |
| int modelSpaceHash; |
| |
| @Override |
| public final boolean select(Viewer viewer, Object parentElement, Object element) { |
| if (element instanceof IModelElement) { |
| IModelElement modelElement = (IModelElement) element; |
| IModelSpace ms = modelElement.getModelSpace(); |
| IModelManager mm = ms.getModelManager(); |
| |
| if(!allowedTypeIDs.isEmpty() && ms.hashCode() != modelSpaceHash){ |
| modelSpaceHash = ms.hashCode(); |
| allowedTypeIDs.clear(); |
| } |
| if(allowedTypeIDs.isEmpty()){ |
| fillAllowedTypeIDs(mm); |
| } |
| if(!disallowedTypeIDs.isEmpty() && ms.hashCode() != modelSpaceHash){ |
| modelSpaceHash = ms.hashCode(); |
| allowedTypeIDs.clear(); |
| } |
| if(disallowedTypeIDs.isEmpty()){ |
| fillDisallowedTypeIDs(mm); |
| } |
| if (!allowedTypeIDs.isEmpty()) { |
| for (String typeId : allowedTypeIDs) { |
| IModelElement type = mm.getElementByID(typeId); |
| if (modelElement.isInstanceOf(type)) { |
| return true; |
| } |
| } |
| return false; |
| } |
| else if (!disallowedTypeIDs.isEmpty()) { |
| for (String typeId : disallowedTypeIDs) { |
| IModelElement type = mm.getElementByID(typeId); |
| if (modelElement.isInstanceOf(type)) { |
| return false; |
| } |
| } |
| return true; |
| } |
| } |
| return true; |
| } |
| |
| protected abstract void fillAllowedTypeIDs(IModelManager manager); |
| |
| protected abstract void fillDisallowedTypeIDs(IModelManager manager); |
| |
| } |