blob: eec83783ba85baf10f2eff1e74f28c832d85ea85 [file] [log] [blame]
/******************************************************************************
* Copyright (c) 2002, 2007 IBM Corporation and others.
* 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:
* IBM Corporation - initial API and implementation
****************************************************************************/
package org.eclipse.gmf.runtime.diagram.ui.editparts;
import java.util.Iterator;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.requests.SelectionRequest;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ComponentEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ContainerEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.PopupBarEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ResizableShapeEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramColorRegistry;
import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
/**
* the base controler for shapes
* @author mmostafa, crevells
*
*/
public abstract class ShapeEditPart extends TopGraphicEditPart implements IPrimaryEditPart {
/**
* copnstructor
* @param view the view controlled by this edit part
*/
public ShapeEditPart(View view) {
super(view);
}
protected void createDefaultEditPolicies() {
super.createDefaultEditPolicies();
installEditPolicy(EditPolicy.CONTAINER_ROLE, new ContainerEditPolicy());
installEditPolicy(EditPolicy.COMPONENT_ROLE, new ComponentEditPolicy());
installEditPolicy(EditPolicyRoles.POPUPBAR_ROLE, new PopupBarEditPolicy());
}
/**
* gets the location of this edit part's Figure
* @return <code>Point</code>
*/
final public Point getLocation() {
return getFigure().getBounds().getLocation();
}
/**
* gets the size of this edit part's Figure
* @return <code>Dimension</code>
*/
final public Dimension getSize() {
return getFigure().getBounds().getSize();
}
protected void handleNotificationEvent(Notification notification) {
Object feature = notification.getFeature();
if (NotationPackage.eINSTANCE.getSize_Width().equals(feature)
|| NotationPackage.eINSTANCE.getSize_Height().equals(feature)
|| NotationPackage.eINSTANCE.getLocation_X().equals(feature)
|| NotationPackage.eINSTANCE.getLocation_Y().equals(feature)) {
refreshBounds();
}
else if (NotationPackage.eINSTANCE.getFillStyle_FillColor().equals(feature)) {
Integer c = (Integer) notification.getNewValue();
setBackgroundColor(DiagramColorRegistry.getInstance().getColor(c));
}
else if (NotationPackage.eINSTANCE.getLineStyle_LineColor().equals(feature)) {
Integer c = (Integer) notification.getNewValue();
setForegroundColor(DiagramColorRegistry.getInstance().getColor(c));
}
else if (NotationPackage.eINSTANCE.getFontStyle().isInstance(notification.getNotifier()))
refreshFont();
else if (notification.getFeature() == NotationPackage.eINSTANCE.getView_Element()
&& ((EObject)notification.getNotifier())== getNotationView())
handleMajorSemanticChange();
else if (notification.getEventType() == EventType.UNRESOLVE && hasNotationView()){
// make sure we refresh if the unresolved element is the edit
// part's semantic element the comparison should be id based not
// instance based, since get element will resolve the element
// and resolving the element will result in returning a different
// instance than the proxy we had as a notifier
EObject notifier = (EObject) notification.getNotifier();
EObject viewElement = getNotationView().getElement();
if (viewElement!=null){
String id1 = EMFCoreUtil.getProxyID(notifier);
String id2 = EMFCoreUtil.getProxyID(viewElement);
if (id1.equals(id2)) {
handleMajorSemanticChange();
}
}
}
else
super.handleNotificationEvent(notification);
}
/**
* refresh the bounds
*/
protected void refreshBounds() {
int width = ((Integer) getStructuralFeatureValue(NotationPackage.eINSTANCE.getSize_Width())).intValue();
int height = ((Integer) getStructuralFeatureValue(NotationPackage.eINSTANCE.getSize_Height())).intValue();
Dimension size = new Dimension(width, height);
int x = ((Integer) getStructuralFeatureValue(NotationPackage.eINSTANCE.getLocation_X())).intValue();
int y = ((Integer) getStructuralFeatureValue(NotationPackage.eINSTANCE.getLocation_Y())).intValue();
Point loc = new Point(x, y);
((GraphicalEditPart) getParent()).setLayoutConstraint(
this,
getFigure(),
new Rectangle(loc, size));
}
protected void refreshVisuals() {
super.refreshVisuals();
refreshBounds();
refreshBackgroundColor();
refreshForegroundColor();
refreshFont();
}
/**
* Return the editpolicy to be installed as an <code>EditPolicy#PRIMARY_DRAG_ROLE</code>
* role. This method is typically called by <code>LayoutEditPolicy#createChildEditPolicy()</code>
* @return EditPolicy
*/
public EditPolicy getPrimaryDragEditPolicy() {
return new ResizableShapeEditPolicy();
}
public EditPart getTargetEditPart(Request request) {
if (RequestConstants.REQ_SELECTION == request.getType()
&& getParent() instanceof GroupEditPart) {
// If the shape is already selected then do not give up selection to
// the group.
if (getSelected() != SELECTED_NONE) {
return super.getTargetEditPart(request);
}
GroupEditPart groupEP = (GroupEditPart) getParent();
// Normally when a shape is not selected, the right-mouse button
// will cause the shape to be selected and the context menu to show.
// If the shape is in a group, we do not want this behavior as we
// want the context menu of the group to show.
if (getSelected() == SELECTED_NONE
&& (request instanceof SelectionRequest)
&& ((SelectionRequest) request).getLastButtonPressed() == 3) {
return groupEP.getTargetEditPart(request);
}
// If the group is currently selected, then this is the second click
// then the shape should be selected.
if (groupEP.getSelected() != SELECTED_NONE) {
return super.getTargetEditPart(request);
}
// If any of the group's children are currently selected then the
// selection of another child of the group will result in the child
// being selected and not the group.
for (Iterator iter = groupEP.getChildren().iterator(); iter
.hasNext();) {
EditPart childEP = (EditPart) iter.next();
if (childEP.getSelected() != SELECTED_NONE) {
return super.getTargetEditPart(request);
}
}
// otherwise we want the group to get selected
return groupEP.getTargetEditPart(request);
}
return super.getTargetEditPart(request);
}
}