/*******************************************************************************
* Copyright (c) 2011 University of Mannheim: Chair for Software Engineering
* 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:
* Ralph Gerbig - initial API and implementation and initial documentation
*******************************************************************************/
package de.uni_mannheim.informatik.swt.models.plm.diagram.custom.editpolicies;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.editpolicies.AbstractEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IResizableCompartmentEditPart;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateUnspecifiedTypeRequest;
import org.eclipse.gmf.runtime.notation.BasicCompartment;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import de.uni_mannheim.informatik.swt.models.plm.diagram.edit.parts.Connection2EditPart;
import de.uni_mannheim.informatik.swt.models.plm.diagram.edit.parts.ConnectionEditPart;
import de.uni_mannheim.informatik.swt.models.plm.diagram.edit.parts.Entity2EditPart;
import de.uni_mannheim.informatik.swt.models.plm.diagram.edit.parts.EntityEditPart;
import de.uni_mannheim.informatik.swt.models.plm.diagram.providers.PLMElementTypes;
/**
* This edit policy is responsible for making compartments visible
* when hovering over an model element with a tool. E.g. if an
* attribute tool is selected while hovering the attributes compartment
* will become visible.
*
*/
public class EntityConnectionSelectionEditPolicy extends AbstractEditPolicy{
@Override
public void showTargetFeedback(Request request) {
//Code shall only be executed when a tool is selected which can add a child to entity
if (! (request instanceof CreateUnspecifiedTypeRequest))
return;
CreateUnspecifiedTypeRequest req = (CreateUnspecifiedTypeRequest)request;
if( !(
req.getElementTypes().contains(PLMElementTypes.Attribute_3061)
||req.getElementTypes().contains(PLMElementTypes.Attribute_3070)
||req.getElementTypes().contains(PLMElementTypes.Method_3062)
||req.getElementTypes().contains(PLMElementTypes.Method_3071)
||req.getElementTypes().contains(PLMElementTypes.Entity_3069)
||req.getElementTypes().contains(PLMElementTypes.Connection_3060)
)
)
return;
AbstractClabjectEditPart clabjectEditPart = new AbstractClabjectEditPart(getHost());
//The editing domain
TransactionalEditingDomain domain = (TransactionalEditingDomain)clabjectEditPart.getEditingDomain();
//Show all compartments so that their edit parts are created for identifieng
//a views compartment.
for (View v : (List<View>)clabjectEditPart.getNotationView().getChildren())
if (v instanceof BasicCompartment){
BasicCompartment b = (BasicCompartment)v;
domain.getCommandStack().execute(SetCommand.create(domain, v, NotationPackage.eINSTANCE.getView_Visible(), true));
}
de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject self = (de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject) clabjectEditPart.resolveSemanticElement();
//Attribute not selected -> Hide it
if (!(req.getElementTypes().contains(PLMElementTypes.Attribute_3061))
||!(req.getElementTypes().contains(PLMElementTypes.Attribute_3070))
)
//Attributes
if (self.getEigenAttributes().size() == 0 && clabjectEditPart.getAttributesCompartment() != null)
domain.getCommandStack().execute(SetCommand.create(domain, clabjectEditPart.getAttributesCompartment().getNotationView(), NotationPackage.eINSTANCE.getView_Visible(), false));
//Method not selected -> Hide it
if( !(req.getElementTypes().contains(PLMElementTypes.Method_3062))
||!(req.getElementTypes().contains(PLMElementTypes.Method_3071))
)
//Methods
if (self.getEigenMethods().size() == 0 && clabjectEditPart.getMethodsCompartment() != null)
domain.getCommandStack().execute(SetCommand.create(domain, clabjectEditPart.getMethodsCompartment().getNotationView(), NotationPackage.eINSTANCE.getView_Visible(), false));
//Content not selected -> Hide it
if( !(req.getElementTypes().contains(PLMElementTypes.Entity_3069))
&& !(req.getElementTypes().contains(PLMElementTypes.Connection_3060))
)
//Content
if (self.getContent().size() == 0 && clabjectEditPart.getContentsCompartment() != null)
domain.getCommandStack().execute(SetCommand.create(domain, clabjectEditPart.getContentsCompartment().getNotationView(), NotationPackage.eINSTANCE.getView_Visible(), false));
super.showTargetFeedback(request);
}
@Override
public void eraseTargetFeedback(Request request) {
boolean elementCreated = false;
//Search in the stack if this code is called from an element creation via a tool
for (StackTraceElement element : Thread.currentThread().getStackTrace())
if (element.getClassName().equals("org.eclipse.gef.tools.CreationTool")
&& element.getMethodName().equals("handleButtonUp")){
elementCreated = true;
break;
}
//Compartment visibility shall not be changed back after
//an element was created
if (!elementCreated){
AbstractClabjectEditPart clabjectEditPart = new AbstractClabjectEditPart(getHost());
clabjectEditPart.manageCompartmentVisibility();
}
super.eraseTargetFeedback(request);
}
private class AbstractClabjectEditPart{
private EntityEditPart entityEditPart = null;
private Entity2EditPart entity2EditPart = null;
private ConnectionEditPart connectionEditPart = null;
private Connection2EditPart connection2EditPart = null;
public AbstractClabjectEditPart(EditPart clabjectEditPart){
if (clabjectEditPart instanceof EntityEditPart)
entityEditPart = (EntityEditPart)clabjectEditPart;
else if (clabjectEditPart instanceof Entity2EditPart)
entity2EditPart = (Entity2EditPart)clabjectEditPart;
else if (clabjectEditPart instanceof ConnectionEditPart)
connectionEditPart = (ConnectionEditPart)clabjectEditPart;
else if (clabjectEditPart instanceof Connection2EditPart)
connection2EditPart = (Connection2EditPart)clabjectEditPart;
}
public void manageCompartmentVisibility() {
if (entityEditPart != null)
entityEditPart.manageCompartmentVisibility();
else if (entity2EditPart != null)
entity2EditPart.manageCompartmentVisibility();
else if (connectionEditPart != null)
connectionEditPart.manageCompartmentVisibility();
else if (connection2EditPart != null)
connection2EditPart.manageCompartmentVisibility();
}
public IResizableCompartmentEditPart getMethodsCompartment(){
if (entityEditPart != null)
return entityEditPart.getMethodsCompartment();
else if (entity2EditPart != null)
return entity2EditPart.getMethodsCompartment();
else if (connectionEditPart != null)
return connectionEditPart.getMethodsCompartment();
else if (connection2EditPart != null)
return connection2EditPart.getMethodsCompartment();
throw new RuntimeException();
}
public IResizableCompartmentEditPart getContentsCompartment() {
if (entityEditPart != null)
return entityEditPart.getContentsCompartment();
else if (entity2EditPart != null)
return entity2EditPart.getContentsCompartment();
else if (connectionEditPart != null)
return connectionEditPart.getContentsCompartment();
else if (connection2EditPart != null)
return connection2EditPart.getContentsCompartment();
throw new RuntimeException();
}
public TransactionalEditingDomain getEditingDomain(){
if (entityEditPart != null)
return entityEditPart.getEditingDomain();
else if (entity2EditPart != null)
return entity2EditPart.getEditingDomain();
else if (connectionEditPart != null)
return connectionEditPart.getEditingDomain();
else if (connection2EditPart != null)
return connection2EditPart.getEditingDomain();
throw new RuntimeException();
}
public EObject resolveSemanticElement(){
if (entityEditPart != null)
return entityEditPart.resolveSemanticElement();
else if (entity2EditPart != null)
return entity2EditPart.resolveSemanticElement();
else if (connectionEditPart != null)
return connectionEditPart.resolveSemanticElement();
else if (connection2EditPart != null)
return connection2EditPart.resolveSemanticElement();
throw new RuntimeException();
}
public View getNotationView(){
if (entityEditPart != null)
return entityEditPart.getNotationView();
else if (entity2EditPart != null)
return entity2EditPart.getNotationView();
else if (connectionEditPart != null)
return connectionEditPart.getNotationView();
else if (connection2EditPart != null)
return connection2EditPart.getNotationView();
throw new RuntimeException();
}
public IResizableCompartmentEditPart getAttributesCompartment(){
if (entityEditPart != null)
return entityEditPart.getAttributesCompartment();
else if (entity2EditPart != null)
return entity2EditPart.getAttributesCompartment();
else if (connectionEditPart != null)
return connectionEditPart.getAttributesCompartment();
else if (connection2EditPart != null)
return connection2EditPart.getAttributesCompartment();
throw new RuntimeException();
}
}
}