/*******************************************************************************
* Copyright (c) 2006-2012
* Software Technology Group, Dresden University of Technology
* DevBoost GmbH, Berlin, Amtsgericht Charlottenburg, HRB 140026
*
* 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:
* Software Technology Group - TU Dresden, Germany;
* DevBoost GmbH - Berlin, Germany
* - initial API and implementation
******************************************************************************/
/*
* Copyright (c) 2006, 2007 Borland Software Corporation
*
* 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:
* Dmitry Stadnik (Borland) - initial API and implementation
*/
package org.reuseware.application.taipan.gmf.editor.edit.policies;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.commands.DeferredLayoutCommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CanonicalConnectionEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateConnectionViewRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.View;
import org.reuseware.application.taipan.TaiPanPackage;
import org.reuseware.application.taipan.gmf.editor.edit.parts.AquatoryEditPart;
import org.reuseware.application.taipan.gmf.editor.edit.parts.BesiegePortOrderEditPart;
import org.reuseware.application.taipan.gmf.editor.edit.parts.EmptyBoxEditPart;
import org.reuseware.application.taipan.gmf.editor.edit.parts.EscortShipsOrderEditPart;
import org.reuseware.application.taipan.gmf.editor.edit.parts.ItemHookEditPart;
import org.reuseware.application.taipan.gmf.editor.edit.parts.LargeItemEditPart;
import org.reuseware.application.taipan.gmf.editor.edit.parts.PortEditPart;
import org.reuseware.application.taipan.gmf.editor.edit.parts.PortSlotEditPart;
import org.reuseware.application.taipan.gmf.editor.edit.parts.ReliableRouteEditPart;
import org.reuseware.application.taipan.gmf.editor.edit.parts.RouteSlotEditPart;
import org.reuseware.application.taipan.gmf.editor.edit.parts.ShipEditPart;
import org.reuseware.application.taipan.gmf.editor.edit.parts.UnreliableRouteEditPart;
import org.reuseware.application.taipan.gmf.editor.edit.parts.WarshipEditPart;
import org.reuseware.application.taipan.gmf.editor.part.TaiPanDiagramUpdater;
import org.reuseware.application.taipan.gmf.editor.part.TaiPanLinkDescriptor;
import org.reuseware.application.taipan.gmf.editor.part.TaiPanNodeDescriptor;
import org.reuseware.application.taipan.gmf.editor.part.TaiPanVisualIDRegistry;
/**
* @generated
*/
public class AquatoryCanonicalEditPolicy extends CanonicalConnectionEditPolicy {
/**
* @generated
*/
Set myFeaturesToSynchronize;
/**
* @generated
*/
protected List getSemanticChildrenList() {
View viewObject = (View) getHost().getModel();
List result = new LinkedList();
for (Iterator it = TaiPanDiagramUpdater
.getAquatory_1000SemanticChildren(viewObject).iterator(); it
.hasNext();) {
result.add(((TaiPanNodeDescriptor) it.next()).getModelElement());
}
return result;
}
/**
* @generated
*/
protected boolean shouldDeleteView(View view) {
return true;
}
/**
* @generated
*/
protected boolean isOrphaned(Collection semanticChildren, final View view) {
if (view.getEAnnotation("Shortcut") != null) {//$NON-NLS-1$
return TaiPanDiagramUpdater.isShortcutOrphaned(view);
}
int visualID = TaiPanVisualIDRegistry.getVisualID(view);
switch (visualID) {
case PortEditPart.VISUAL_ID:
case ShipEditPart.VISUAL_ID:
case WarshipEditPart.VISUAL_ID:
case PortSlotEditPart.VISUAL_ID:
case RouteSlotEditPart.VISUAL_ID:
if (!semanticChildren.contains(view.getElement())) {
return true;
}
}
return false;
}
/**
* @generated
*/
protected String getDefaultFactoryHint() {
return null;
}
/**
* @generated
*/
protected Set getFeaturesToSynchronize() {
if (myFeaturesToSynchronize == null) {
myFeaturesToSynchronize = new HashSet();
myFeaturesToSynchronize.add(TaiPanPackage.eINSTANCE
.getAquatory_Ports());
myFeaturesToSynchronize.add(TaiPanPackage.eINSTANCE
.getAquatory_Ships());
myFeaturesToSynchronize.add(TaiPanPackage.eINSTANCE
.getAquatory_Routes());
}
return myFeaturesToSynchronize;
}
/**
* @generated
*/
protected List getSemanticConnectionsList() {
return Collections.EMPTY_LIST;
}
/**
* @generated
*/
protected EObject getSourceElement(EObject relationship) {
return null;
}
/**
* @generated
*/
protected EObject getTargetElement(EObject relationship) {
return null;
}
/**
* @generated
*/
protected boolean shouldIncludeConnection(Edge connector,
Collection children) {
return false;
}
/**
* @generated
*/
protected void refreshSemantic() {
List createdViews = new LinkedList();
createdViews.addAll(refreshSemanticChildren());
List createdConnectionViews = new LinkedList();
createdConnectionViews.addAll(refreshSemanticConnections());
createdConnectionViews.addAll(refreshConnections());
if (createdViews.size() > 1) {
// perform a layout of the container
DeferredLayoutCommand layoutCmd = new DeferredLayoutCommand(host()
.getEditingDomain(), createdViews, host());
executeCommand(new ICommandProxy(layoutCmd));
}
createdViews.addAll(createdConnectionViews);
makeViewsImmutable(createdViews);
}
/**
* @generated
*/
private Diagram getDiagram() {
return ((View) getHost().getModel()).getDiagram();
}
/**
* @generated
*/
private Collection refreshConnections() {
Map domain2NotationMap = new HashMap();
Collection linkDescriptors = collectAllLinks(getDiagram(),
domain2NotationMap);
Collection existingLinks = new LinkedList(getDiagram().getEdges());
for (Iterator linksIterator = existingLinks.iterator(); linksIterator
.hasNext();) {
Edge nextDiagramLink = (Edge) linksIterator.next();
int diagramLinkVisualID = TaiPanVisualIDRegistry
.getVisualID(nextDiagramLink);
if (diagramLinkVisualID == -1) {
if (nextDiagramLink.getSource() != null
&& nextDiagramLink.getTarget() != null) {
linksIterator.remove();
}
continue;
}
EObject diagramLinkObject = nextDiagramLink.getElement();
EObject diagramLinkSrc = nextDiagramLink.getSource().getElement();
EObject diagramLinkDst = nextDiagramLink.getTarget().getElement();
for (Iterator LinkDescriptorsIterator = linkDescriptors.iterator(); LinkDescriptorsIterator
.hasNext();) {
TaiPanLinkDescriptor nextLinkDescriptor = (TaiPanLinkDescriptor) LinkDescriptorsIterator
.next();
if (diagramLinkObject == nextLinkDescriptor.getModelElement()
&& diagramLinkSrc == nextLinkDescriptor.getSource()
&& diagramLinkDst == nextLinkDescriptor
.getDestination()
&& diagramLinkVisualID == nextLinkDescriptor
.getVisualID()) {
linksIterator.remove();
LinkDescriptorsIterator.remove();
}
}
}
deleteViews(existingLinks.iterator());
return createConnections(linkDescriptors, domain2NotationMap);
}
/**
* @generated
*/
private Collection collectAllLinks(View view, Map domain2NotationMap) {
if (!AquatoryEditPart.MODEL_ID.equals(TaiPanVisualIDRegistry
.getModelID(view))) {
return Collections.EMPTY_LIST;
}
Collection result = new LinkedList();
switch (TaiPanVisualIDRegistry.getVisualID(view)) {
case AquatoryEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getAquatory_1000ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
case PortEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getPort_2001ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
case ShipEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getShip_2002ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
case WarshipEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getWarship_2003ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
case PortSlotEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getPortSlot_2004ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
case RouteSlotEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getRouteSlot_2005ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
case LargeItemEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getLargeItem_3002ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
case EmptyBoxEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getEmptyBox_3003ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
case ItemHookEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getLargeItemHook_3004ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
case ReliableRouteEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getRoute_4002ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
case UnreliableRouteEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getRoute_4003ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
case BesiegePortOrderEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getBesiegePortOrder_4005ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
case EscortShipsOrderEditPart.VISUAL_ID: {
if (!domain2NotationMap.containsKey(view.getElement())) {
result.addAll(TaiPanDiagramUpdater
.getEscortShipsOrder_4006ContainedLinks(view));
}
if (!domain2NotationMap.containsKey(view.getElement())
|| view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
domain2NotationMap.put(view.getElement(), view);
}
break;
}
}
for (Iterator children = view.getChildren().iterator(); children
.hasNext();) {
result.addAll(collectAllLinks((View) children.next(),
domain2NotationMap));
}
for (Iterator edges = view.getSourceEdges().iterator(); edges.hasNext();) {
result.addAll(collectAllLinks((View) edges.next(),
domain2NotationMap));
}
return result;
}
/**
* @generated
*/
private Collection createConnections(Collection linkDescriptors,
Map domain2NotationMap) {
List adapters = new LinkedList();
for (Iterator linkDescriptorsIterator = linkDescriptors.iterator(); linkDescriptorsIterator
.hasNext();) {
final TaiPanLinkDescriptor nextLinkDescriptor = (TaiPanLinkDescriptor) linkDescriptorsIterator
.next();
EditPart sourceEditPart = getEditPart(nextLinkDescriptor
.getSource(), domain2NotationMap);
EditPart targetEditPart = getEditPart(nextLinkDescriptor
.getDestination(), domain2NotationMap);
if (sourceEditPart == null || targetEditPart == null) {
continue;
}
CreateConnectionViewRequest.ConnectionViewDescriptor descriptor = new CreateConnectionViewRequest.ConnectionViewDescriptor(
nextLinkDescriptor.getSemanticAdapter(), null,
ViewUtil.APPEND, false, ((IGraphicalEditPart) getHost())
.getDiagramPreferencesHint());
CreateConnectionViewRequest ccr = new CreateConnectionViewRequest(
descriptor);
ccr.setType(RequestConstants.REQ_CONNECTION_START);
ccr.setSourceEditPart(sourceEditPart);
sourceEditPart.getCommand(ccr);
ccr.setTargetEditPart(targetEditPart);
ccr.setType(RequestConstants.REQ_CONNECTION_END);
Command cmd = targetEditPart.getCommand(ccr);
if (cmd != null && cmd.canExecute()) {
executeCommand(cmd);
IAdaptable viewAdapter = (IAdaptable) ccr.getNewObject();
if (viewAdapter != null) {
adapters.add(viewAdapter);
}
}
}
return adapters;
}
/**
* @generated
*/
private EditPart getEditPart(EObject domainModelElement,
Map domain2NotationMap) {
View view = (View) domain2NotationMap.get(domainModelElement);
if (view != null) {
return (EditPart) getHost().getViewer().getEditPartRegistry().get(
view);
}
return null;
}
}