/*
* Copyright (c) 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.eclipse.gmf.examples.taipan.port.diagram.edit.parts;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gmf.examples.taipan.TaiPanPackage;
import org.eclipse.gmf.examples.taipan.figures.BuildingShape;
import org.eclipse.gmf.examples.taipan.port.diagram.edit.policies.BuildingItemSemanticEditPolicy;
import org.eclipse.gmf.examples.taipan.port.diagram.part.TaiPanVisualIDRegistry;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ResizableShapeEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.requests.ArrangeRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants;
import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.swt.graphics.Color;
/**
* @generated
*/
public class BuildingEditPart extends ShapeNodeEditPart {
/**
* @generated
*/
public static final int VISUAL_ID = 2001;
/**
* @generated
*/
protected IFigure contentPane;
/**
* @generated
*/
protected IFigure primaryShape;
/**
* @generated
*/
public BuildingEditPart(View view) {
super(view);
}
/**
* @generated
*/
protected void createDefaultEditPolicies() {
super.createDefaultEditPolicies();
installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE, new BuildingItemSemanticEditPolicy());
installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy());
// XXX need an SCR to runtime to have another abstract superclass that would let children add reasonable editpolicies
// removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles.CONNECTION_HANDLES_ROLE);
}
/**
* @generated
*/
protected LayoutEditPolicy createLayoutEditPolicy() {
org.eclipse.gmf.runtime.diagram.ui.editpolicies.LayoutEditPolicy lep = new org.eclipse.gmf.runtime.diagram.ui.editpolicies.LayoutEditPolicy() {
protected EditPolicy createChildEditPolicy(EditPart child) {
EditPolicy result = child.getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE);
if (result == null) {
result = new NonResizableEditPolicy();
}
return result;
}
protected Command getMoveChildrenCommand(Request request) {
return null;
}
protected Command getCreateCommand(CreateRequest request) {
return null;
}
};
return lep;
}
public EditPolicy getPrimaryDragEditPolicy() {
return new ResizableShapeEditPolicy() {
protected Command getAutoSizeCommand(Request request) {
Command command = super.getAutoSizeCommand(request);
ArrangeRequest layoutRequest = new ArrangeRequest(RequestConstants.REQ_ARRANGE_DEFERRED);
List editParts = new ArrayList(getParent().getChildren());
layoutRequest.setViewAdaptersToArrange(editParts);
Command layoutCommand = getParent().getCommand(layoutRequest);
if (layoutCommand != null) {
command = command.chain(layoutCommand);
}
return command;
}
};
}
public Command getCommand(Request request) {
Command command = super.getCommand(request);
if (request.getType().equals(REQ_DELETE)) {
ArrangeRequest layoutRequest = new ArrangeRequest(RequestConstants.REQ_ARRANGE_DEFERRED);
List editParts = new ArrayList(getParent().getChildren());
editParts.remove(this);
layoutRequest.setViewAdaptersToArrange(editParts);
Command layoutCommand = getParent().getCommand(layoutRequest);
if (layoutCommand != null) {
command = command.chain(layoutCommand);
}
}
return command;
}
@Override
protected void handleNotificationEvent(Notification notification) {
if (notification.getFeature() == TaiPanPackage.eINSTANCE.getBuilding_Street() || notification.getFeature() == TaiPanPackage.eINSTANCE.getBuilding_Info()) {
ArrangeRequest layoutRequest = new ArrangeRequest(RequestConstants.REQ_ARRANGE_DEFERRED);
Command layoutCommand = getParent().getCommand(layoutRequest);
if (layoutCommand != null && layoutCommand.canExecute()) {
getDiagramEditDomain().getDiagramCommandStack().execute(layoutCommand);
}
}
super.handleNotificationEvent(notification);
}
/**
* @generated
*/
protected IFigure createNodeShapeGen() {
return primaryShape = new BuildingFigure();
}
protected IFigure createNodeShape() {
IFigure shape = createNodeShapeGen();
ToolbarLayout layout = new ToolbarLayout();
layout.setMinorAlignment(ToolbarLayout.ALIGN_CENTER);
layout.setStretchMinorAxis(false);
layout.setSpacing(getMapMode().DPtoLP(5));
shape.setLayoutManager(layout);
shape.setBorder(new MarginBorder(getMapMode().DPtoLP(5)));
return shape;
}
/**
* @generated
*/
public BuildingFigure getPrimaryShape() {
return (BuildingFigure) primaryShape;
}
/**
* @generated
*/
protected boolean addFixedChild(EditPart childEditPart) {
if (childEditPart instanceof BuildingInfoEditPart) {
((BuildingInfoEditPart) childEditPart).setLabel(getPrimaryShape().getFigureBuildingInfoLabel());
return true;
}
return false;
}
/**
* @generated
*/
protected boolean removeFixedChild(EditPart childEditPart) {
if (childEditPart instanceof BuildingInfoEditPart) {
return true;
}
return false;
}
/**
* @generated
*/
protected void addChildVisual(EditPart childEditPart, int index) {
if (addFixedChild(childEditPart)) {
return;
}
super.addChildVisual(childEditPart, -1);
}
/**
* @generated
*/
protected void removeChildVisual(EditPart childEditPart) {
if (removeFixedChild(childEditPart)) {
return;
}
super.removeChildVisual(childEditPart);
}
/**
* @generated
*/
protected IFigure getContentPaneFor(IGraphicalEditPart editPart) {
return getContentPane();
}
/**
* @generated
*/
protected NodeFigure createNodePlate() {
DefaultSizeNodeFigure result = new DefaultSizeNodeFigure(40, 40);
return result;
}
/**
* Creates figure for this edit part.
*
* Body of this method does not depend on settings in generation model
* so you may safely remove <i>generated</i> tag and modify it.
*
* @generated
*/
protected NodeFigure createNodeFigure() {
NodeFigure figure = createNodePlate();
figure.setLayoutManager(new StackLayout());
IFigure shape = createNodeShape();
figure.add(shape);
contentPane = setupContentPane(shape);
return figure;
}
/**
* Default implementation treats passed figure as content pane.
* Respects layout one may have set for generated figure.
* @param nodeShape instance of generated figure class
* @generated
*/
protected IFigure setupContentPane(IFigure nodeShape) {
if (nodeShape.getLayoutManager() == null) {
ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout();
layout.setSpacing(5);
nodeShape.setLayoutManager(layout);
}
return nodeShape; // use nodeShape itself as contentPane
}
/**
* @generated
*/
public IFigure getContentPane() {
if (contentPane != null) {
return contentPane;
}
return super.getContentPane();
}
/**
* @generated
*/
protected void setForegroundColor(Color color) {
if (primaryShape != null) {
primaryShape.setForegroundColor(color);
}
}
/**
* @generated
*/
protected void setBackgroundColor(Color color) {
if (primaryShape != null) {
primaryShape.setBackgroundColor(color);
}
}
/**
* @generated
*/
protected void setLineWidth(int width) {
if (primaryShape instanceof Shape) {
((Shape) primaryShape).setLineWidth(width);
}
}
/**
* @generated
*/
protected void setLineType(int style) {
if (primaryShape instanceof Shape) {
((Shape) primaryShape).setLineStyle(style);
}
}
/**
* @generated
*/
public EditPart getPrimaryChildEditPart() {
return getChildBySemanticHint(TaiPanVisualIDRegistry.getType(BuildingInfoEditPart.VISUAL_ID));
}
/**
* @generated
*/
public class BuildingFigure extends BuildingShape {
/**
* @generated
*/
private WrappingLabel fFigureBuildingInfoLabel;
/**
* @generated
*/
public BuildingFigure() {
createContents();
}
/**
* @generated
*/
private void createContents() {
fFigureBuildingInfoLabel = new WrappingLabel();
fFigureBuildingInfoLabel.setText("");
this.add(fFigureBuildingInfoLabel);
}
/**
* @generated
*/
public WrappingLabel getFigureBuildingInfoLabel() {
return fFigureBuildingInfoLabel;
}
}
}