/*
* Copyright (c) 2012-2016 Marsha Chechik, Alessio Di Sandro, Michalis Famelis,
* Rick Salay.
* 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:
* Alessio Di Sandro - Implementation.
*/
package edu.toronto.cs.se.modelepedia.istar.diagram.providers;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.ui.services.modelingassistant.ModelingAssistantProvider;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import edu.toronto.cs.se.modelepedia.istar.diagram.edit.parts.ActorActorIntentionsCompartmentEditPart;
import edu.toronto.cs.se.modelepedia.istar.diagram.edit.parts.ActorEditPart;
import edu.toronto.cs.se.modelepedia.istar.diagram.edit.parts.Goal2EditPart;
import edu.toronto.cs.se.modelepedia.istar.diagram.edit.parts.GoalEditPart;
import edu.toronto.cs.se.modelepedia.istar.diagram.edit.parts.IStarEditPart;
import edu.toronto.cs.se.modelepedia.istar.diagram.edit.parts.Resource2EditPart;
import edu.toronto.cs.se.modelepedia.istar.diagram.edit.parts.ResourceEditPart;
import edu.toronto.cs.se.modelepedia.istar.diagram.edit.parts.SoftGoal2EditPart;
import edu.toronto.cs.se.modelepedia.istar.diagram.edit.parts.SoftGoalEditPart;
import edu.toronto.cs.se.modelepedia.istar.diagram.edit.parts.Task2EditPart;
import edu.toronto.cs.se.modelepedia.istar.diagram.edit.parts.TaskEditPart;
import edu.toronto.cs.se.modelepedia.istar.diagram.part.IStarDiagramEditorPlugin;
import edu.toronto.cs.se.modelepedia.istar.diagram.part.Messages;
/**
* @generated
*/
public class IStarModelingAssistantProvider extends ModelingAssistantProvider {
/**
* @generated
*/
public List getTypesForPopupBar(IAdaptable host) {
IGraphicalEditPart editPart = (IGraphicalEditPart) host
.getAdapter(IGraphicalEditPart.class);
if (editPart instanceof IStarEditPart) {
ArrayList<IElementType> types = new ArrayList<IElementType>(5);
types.add(IStarElementTypes.SoftGoal_2001);
types.add(IStarElementTypes.Task_2002);
types.add(IStarElementTypes.Resource_2003);
types.add(IStarElementTypes.Goal_2004);
types.add(IStarElementTypes.Actor_2005);
return types;
}
if (editPart instanceof ActorActorIntentionsCompartmentEditPart) {
ArrayList<IElementType> types = new ArrayList<IElementType>(4);
types.add(IStarElementTypes.SoftGoal_3001);
types.add(IStarElementTypes.Task_3002);
types.add(IStarElementTypes.Resource_3003);
types.add(IStarElementTypes.Goal_3004);
return types;
}
return Collections.EMPTY_LIST;
}
/**
* @generated
*/
public List getRelTypesOnSource(IAdaptable source) {
IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source
.getAdapter(IGraphicalEditPart.class);
if (sourceEditPart instanceof SoftGoalEditPart) {
return ((SoftGoalEditPart) sourceEditPart).getMARelTypesOnSource();
}
if (sourceEditPart instanceof TaskEditPart) {
return ((TaskEditPart) sourceEditPart).getMARelTypesOnSource();
}
if (sourceEditPart instanceof ResourceEditPart) {
return ((ResourceEditPart) sourceEditPart).getMARelTypesOnSource();
}
if (sourceEditPart instanceof GoalEditPart) {
return ((GoalEditPart) sourceEditPart).getMARelTypesOnSource();
}
if (sourceEditPart instanceof ActorEditPart) {
return ((ActorEditPart) sourceEditPart).getMARelTypesOnSource();
}
if (sourceEditPart instanceof SoftGoal2EditPart) {
return ((SoftGoal2EditPart) sourceEditPart).getMARelTypesOnSource();
}
if (sourceEditPart instanceof Task2EditPart) {
return ((Task2EditPart) sourceEditPart).getMARelTypesOnSource();
}
if (sourceEditPart instanceof Resource2EditPart) {
return ((Resource2EditPart) sourceEditPart).getMARelTypesOnSource();
}
if (sourceEditPart instanceof Goal2EditPart) {
return ((Goal2EditPart) sourceEditPart).getMARelTypesOnSource();
}
return Collections.EMPTY_LIST;
}
/**
* @generated
*/
public List getRelTypesOnTarget(IAdaptable target) {
IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target
.getAdapter(IGraphicalEditPart.class);
if (targetEditPart instanceof SoftGoalEditPart) {
return ((SoftGoalEditPart) targetEditPart).getMARelTypesOnTarget();
}
if (targetEditPart instanceof TaskEditPart) {
return ((TaskEditPart) targetEditPart).getMARelTypesOnTarget();
}
if (targetEditPart instanceof ResourceEditPart) {
return ((ResourceEditPart) targetEditPart).getMARelTypesOnTarget();
}
if (targetEditPart instanceof GoalEditPart) {
return ((GoalEditPart) targetEditPart).getMARelTypesOnTarget();
}
if (targetEditPart instanceof ActorEditPart) {
return ((ActorEditPart) targetEditPart).getMARelTypesOnTarget();
}
if (targetEditPart instanceof SoftGoal2EditPart) {
return ((SoftGoal2EditPart) targetEditPart).getMARelTypesOnTarget();
}
if (targetEditPart instanceof Task2EditPart) {
return ((Task2EditPart) targetEditPart).getMARelTypesOnTarget();
}
if (targetEditPart instanceof Resource2EditPart) {
return ((Resource2EditPart) targetEditPart).getMARelTypesOnTarget();
}
if (targetEditPart instanceof Goal2EditPart) {
return ((Goal2EditPart) targetEditPart).getMARelTypesOnTarget();
}
return Collections.EMPTY_LIST;
}
/**
* @generated
*/
public List getRelTypesOnSourceAndTarget(IAdaptable source,
IAdaptable target) {
IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source
.getAdapter(IGraphicalEditPart.class);
IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target
.getAdapter(IGraphicalEditPart.class);
if (sourceEditPart instanceof SoftGoalEditPart) {
return ((SoftGoalEditPart) sourceEditPart)
.getMARelTypesOnSourceAndTarget(targetEditPart);
}
if (sourceEditPart instanceof TaskEditPart) {
return ((TaskEditPart) sourceEditPart)
.getMARelTypesOnSourceAndTarget(targetEditPart);
}
if (sourceEditPart instanceof ResourceEditPart) {
return ((ResourceEditPart) sourceEditPart)
.getMARelTypesOnSourceAndTarget(targetEditPart);
}
if (sourceEditPart instanceof GoalEditPart) {
return ((GoalEditPart) sourceEditPart)
.getMARelTypesOnSourceAndTarget(targetEditPart);
}
if (sourceEditPart instanceof ActorEditPart) {
return ((ActorEditPart) sourceEditPart)
.getMARelTypesOnSourceAndTarget(targetEditPart);
}
if (sourceEditPart instanceof SoftGoal2EditPart) {
return ((SoftGoal2EditPart) sourceEditPart)
.getMARelTypesOnSourceAndTarget(targetEditPart);
}
if (sourceEditPart instanceof Task2EditPart) {
return ((Task2EditPart) sourceEditPart)
.getMARelTypesOnSourceAndTarget(targetEditPart);
}
if (sourceEditPart instanceof Resource2EditPart) {
return ((Resource2EditPart) sourceEditPart)
.getMARelTypesOnSourceAndTarget(targetEditPart);
}
if (sourceEditPart instanceof Goal2EditPart) {
return ((Goal2EditPart) sourceEditPart)
.getMARelTypesOnSourceAndTarget(targetEditPart);
}
return Collections.EMPTY_LIST;
}
/**
* @generated
*/
public List getTypesForSource(IAdaptable target,
IElementType relationshipType) {
IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target
.getAdapter(IGraphicalEditPart.class);
if (targetEditPart instanceof SoftGoalEditPart) {
return ((SoftGoalEditPart) targetEditPart)
.getMATypesForSource(relationshipType);
}
if (targetEditPart instanceof TaskEditPart) {
return ((TaskEditPart) targetEditPart)
.getMATypesForSource(relationshipType);
}
if (targetEditPart instanceof ResourceEditPart) {
return ((ResourceEditPart) targetEditPart)
.getMATypesForSource(relationshipType);
}
if (targetEditPart instanceof GoalEditPart) {
return ((GoalEditPart) targetEditPart)
.getMATypesForSource(relationshipType);
}
if (targetEditPart instanceof ActorEditPart) {
return ((ActorEditPart) targetEditPart)
.getMATypesForSource(relationshipType);
}
if (targetEditPart instanceof SoftGoal2EditPart) {
return ((SoftGoal2EditPart) targetEditPart)
.getMATypesForSource(relationshipType);
}
if (targetEditPart instanceof Task2EditPart) {
return ((Task2EditPart) targetEditPart)
.getMATypesForSource(relationshipType);
}
if (targetEditPart instanceof Resource2EditPart) {
return ((Resource2EditPart) targetEditPart)
.getMATypesForSource(relationshipType);
}
if (targetEditPart instanceof Goal2EditPart) {
return ((Goal2EditPart) targetEditPart)
.getMATypesForSource(relationshipType);
}
return Collections.EMPTY_LIST;
}
/**
* @generated
*/
public List getTypesForTarget(IAdaptable source,
IElementType relationshipType) {
IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source
.getAdapter(IGraphicalEditPart.class);
if (sourceEditPart instanceof SoftGoalEditPart) {
return ((SoftGoalEditPart) sourceEditPart)
.getMATypesForTarget(relationshipType);
}
if (sourceEditPart instanceof TaskEditPart) {
return ((TaskEditPart) sourceEditPart)
.getMATypesForTarget(relationshipType);
}
if (sourceEditPart instanceof ResourceEditPart) {
return ((ResourceEditPart) sourceEditPart)
.getMATypesForTarget(relationshipType);
}
if (sourceEditPart instanceof GoalEditPart) {
return ((GoalEditPart) sourceEditPart)
.getMATypesForTarget(relationshipType);
}
if (sourceEditPart instanceof ActorEditPart) {
return ((ActorEditPart) sourceEditPart)
.getMATypesForTarget(relationshipType);
}
if (sourceEditPart instanceof SoftGoal2EditPart) {
return ((SoftGoal2EditPart) sourceEditPart)
.getMATypesForTarget(relationshipType);
}
if (sourceEditPart instanceof Task2EditPart) {
return ((Task2EditPart) sourceEditPart)
.getMATypesForTarget(relationshipType);
}
if (sourceEditPart instanceof Resource2EditPart) {
return ((Resource2EditPart) sourceEditPart)
.getMATypesForTarget(relationshipType);
}
if (sourceEditPart instanceof Goal2EditPart) {
return ((Goal2EditPart) sourceEditPart)
.getMATypesForTarget(relationshipType);
}
return Collections.EMPTY_LIST;
}
/**
* @generated
*/
public EObject selectExistingElementForSource(IAdaptable target,
IElementType relationshipType) {
return selectExistingElement(target,
getTypesForSource(target, relationshipType));
}
/**
* @generated
*/
public EObject selectExistingElementForTarget(IAdaptable source,
IElementType relationshipType) {
return selectExistingElement(source,
getTypesForTarget(source, relationshipType));
}
/**
* @generated
*/
protected EObject selectExistingElement(IAdaptable host, Collection types) {
if (types.isEmpty()) {
return null;
}
IGraphicalEditPart editPart = (IGraphicalEditPart) host
.getAdapter(IGraphicalEditPart.class);
if (editPart == null) {
return null;
}
Diagram diagram = (Diagram) editPart.getRoot().getContents().getModel();
HashSet<EObject> elements = new HashSet<EObject>();
for (Iterator<EObject> it = diagram.getElement().eAllContents(); it
.hasNext();) {
EObject element = it.next();
if (isApplicableElement(element, types)) {
elements.add(element);
}
}
if (elements.isEmpty()) {
return null;
}
return selectElement((EObject[]) elements.toArray(new EObject[elements
.size()]));
}
/**
* @generated
*/
protected boolean isApplicableElement(EObject element, Collection types) {
IElementType type = ElementTypeRegistry.getInstance().getElementType(
element);
return types.contains(type);
}
/**
* @generated
*/
protected EObject selectElement(EObject[] elements) {
Shell shell = Display.getCurrent().getActiveShell();
ILabelProvider labelProvider = new AdapterFactoryLabelProvider(
IStarDiagramEditorPlugin.getInstance()
.getItemProvidersAdapterFactory());
ElementListSelectionDialog dialog = new ElementListSelectionDialog(
shell, labelProvider);
dialog.setMessage(Messages.IStarModelingAssistantProviderMessage);
dialog.setTitle(Messages.IStarModelingAssistantProviderTitle);
dialog.setMultipleSelection(false);
dialog.setElements(elements);
EObject selected = null;
if (dialog.open() == Window.OK) {
selected = (EObject) dialog.getFirstResult();
}
return selected;
}
}