Reputation: 1353
I am trying to create a line segment which has trough and crust as shown below :
EDIT : Now I'm getting the line segment figure in outline window but not in the diagram editor
However I'm not getting exactly what i wanted . I have to create this line segment only using x , y value and width/height from Rectangle class
.
Below is the code : see outline shape method :
package pipenet.diagram.edit.parts;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
public class Line3 extends Shape {
public Line3() {
this.setAntialias(SWT.ON);
}
@Override
protected void fillShape(Graphics graphics) {
graphics.pushState();
graphics.setFillRule(SWT.FILL_WINDING);
graphics.popState();
}
@Override
protected void outlineShape(Graphics graphics) {
Rectangle r = getBounds().getCopy();
// v 1
graphics.drawLine(r.x, r.y+r.height, r.x, r.y);
graphics.drawLine(r.x, r.y, r.x+r.width/2, r.y);
graphics.drawLine(r.x+r.width/2, r.y, r.x+r.width/2, r.y+r.height);
graphics.drawLine(r.x+r.width/2, r.y+r.height, r.x+r.width, r.y+r.height);
graphics.drawLine(r.x+r.width, r.y+r.height, r.x+r.width, r.y);
}
@Override
public void paintFigure(Graphics graphics) {
// TODO Auto-generated method stub
super.paintFigure(graphics);
}
}
EDIT PART CLASS :
package pipenet.diagram.edit.parts;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.handles.MoveHandle;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderedShapeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IBorderItemEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.BorderItemSelectionEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.FlowLayoutEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ResizableShapeEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.figures.BorderItemLocator;
import org.eclipse.gmf.runtime.draw2d.ui.figures.PolylineConnectionEx;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
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;
import pipenet.diagram.edit.policies.TransitionItemSemanticEditPolicy;
import pipenet.diagram.part.PipenetVisualIDRegistry;
import pipenet.diagram.providers.PipenetElementTypes;
/**
* @generated
*/
public class TransitionEditPart extends AbstractBorderedShapeEditPart {
/**
* @generated
*/
public static final int VISUAL_ID = 2001;
/**
* @generated
*/
protected IFigure contentPane;
/**
* @generated
*/
protected IFigure primaryShape;
/**
* @generated
*/
public TransitionEditPart(View view) {
super(view);
}
/**
* @generated
*/
protected void createDefaultEditPolicies() {
super.createDefaultEditPolicies();
installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
new TransitionItemSemanticEditPolicy());
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() {
FlowLayoutEditPolicy lep = new FlowLayoutEditPolicy() {
protected EditPolicy createChildEditPolicy(EditPart child) {
View childView = (View) child.getModel();
switch (PipenetVisualIDRegistry.getVisualID(childView)) {
case TransitionNameEditPart.VISUAL_ID:
return new BorderItemSelectionEditPolicy() {
protected List createSelectionHandles() {
MoveHandle mh = new MoveHandle(
(GraphicalEditPart) getHost());
mh.setBackgroundColor(new Color(null, 20 ,80 , 90));
mh.getBackgroundColor();
System.out.print(mh.getBackgroundColor());
mh.setBorder(null);
return Collections.singletonList(mh);
}
};
}
EditPolicy result = child
.getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE);
if (result == null) {
result = new NonResizableEditPolicy();
}
return result;
//return super.createChildEditPolicy(child);
}
protected Command createAddCommand(EditPart child, EditPart after) {
return null;
}
protected Command createMoveChildCommand(EditPart child,
EditPart after) {
return null;
}
protected Command getCreateCommand(CreateRequest request) {
return null;
}
};
return lep;
}
/**
* @generated
*/
protected IFigure createNodeShape() {
return primaryShape = new Line3();
}
/**
* @generated
*
*
*/
// public TransitionFigure getPrimaryShape() {
// return (TransitionFigure) primaryShape;
// }
/**
* @generated
*/
protected void addBorderItem(NodeFigure borderItemContainer,
IBorderItemEditPart borderItemEditPart) {
if (borderItemEditPart instanceof TransitionNameEditPart) {
BorderItemLocator locator = new BorderItemLocator(getMainFigure(),
PositionConstants.BOTTOM);
locator.setBorderItemOffset(new Dimension(-2, -2));
borderItemContainer.add(borderItemEditPart.getFigure(), locator);
} else {
super.addBorderItem(borderItemContainer, borderItemEditPart);
}
}
/**
* @generated
// */
protected NodeFigure createNodePlate() {
DefaultSizeNodeFigure result = new DefaultSizeNodeFigure(240, 240);
result.setBackgroundColor(new Color(null , 90 , 90 , 90));
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 createMainFigure() {
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) {
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(PipenetVisualIDRegistry
.getType(TransitionNameEditPart.VISUAL_ID));
}
/**
* @generated
*/
public List/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/getMARelTypesOnSource() {
ArrayList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/types = new ArrayList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/(
1);
types.add(PipenetElementTypes.Arch_4001);
return types;
}
/**
* @generated
*/
public List/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/getMARelTypesOnSourceAndTarget(
IGraphicalEditPart targetEditPart) {
LinkedList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/types = new LinkedList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/();
if (targetEditPart instanceof pipenet.diagram.edit.parts.TransitionEditPart) {
types.add(PipenetElementTypes.Arch_4001);
}
if (targetEditPart instanceof PlaceEditPart) {
types.add(PipenetElementTypes.Arch_4001);
}
return types;
}
/**
* @generated
*/
public List/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/getMATypesForTarget(
IElementType relationshipType) {
LinkedList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/types = new LinkedList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/();
if (relationshipType == PipenetElementTypes.Arch_4001) {
types.add(PipenetElementTypes.Transition_2001);
types.add(PipenetElementTypes.Place_2002);
}
return types;
}
/**
* @generated
*/
public List/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/getMARelTypesOnTarget() {
ArrayList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/types = new ArrayList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/(
1);
types.add(PipenetElementTypes.Arch_4001);
return types;
}
/**
* @generated
*/
public List/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/getMATypesForSource(
IElementType relationshipType) {
LinkedList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/types = new LinkedList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/();
if (relationshipType == PipenetElementTypes.Arch_4001) {
types.add(PipenetElementTypes.Transition_2001);
types.add(PipenetElementTypes.Place_2002);
}
return types;
}
// @Override
// public EditPolicy getPrimaryDragEditPolicy() {
// ResizableShapeEditPolicy editPolicy = new ResizableShapeEditPolicy();
// editPolicy.setResizeDirections(PositionConstants.SOUTH);
// editPolicy.setResizeDirections(PositionConstants.NORTH);
// //editPolicy.setDragAllowed(false);
//
// return editPolicy;
// }
/**
* @generated
*/
public class TransitionFigure extends Line3{
/**
* @generated
*/
public TransitionFigure() {
FlowLayout layoutThis = new FlowLayout();
layoutThis.setStretchMinorAxis(true);
layoutThis.setMinorAlignment(FlowLayout.ALIGN_LEFTTOP);
layoutThis.setMajorAlignment(FlowLayout.ALIGN_LEFTTOP);
layoutThis.setMajorSpacing(15);
layoutThis.setMinorSpacing(15);
layoutThis.setHorizontal(true);
this.setLayoutManager(layoutThis);
}
}
}
Upvotes: 1
Views: 853
Reputation: 1353
Here it's the complete solution.
@mikeyq6's solution:
graphics.drawLine(r.x, r.y+r.height, r.x, r.y);
graphics.drawLine(r.x, r.y, r.x+r.width/2, r.y);
graphics.drawLine(r.x+r.width/2, r.y, r.x+r.width/2, r.y+r.height);
graphics.drawLine(r.x+r.width/2, r.y+r.height, r.x+r.width, r.y+r.height);
graphics.drawLine(r.x+r.width, r.y+r.height, r.x+r.width, r.y);
Rectangle r = getBounds().getShrinked(100, 80);
Upvotes: 0
Reputation: 1148
There are 5 lines in that figure, but you're only drawing four. Plus you're adding the width to the y-value, which would draw the line too tall. You should be using half of the width to reach the middle of the rectangle
Since each line is connected to the previous one, you can draw the lines one by one, starting from where the previous one finished.
How about this:
graphics.drawLine(r.x, r.y+r.height, r.x, r.y);
graphics.drawLine(r.x, r.y, r.x+r.width/2, r.y);
graphics.drawLine(r.x+r.width/2, r.y, r.x+r.width/2, r.y+r.height);
graphics.drawLine(r.x+r.width/2, r.y+r.height, r.x+r.width, r.y+r.height);
graphics.drawLine(r.x+r.width, r.y+r.height, r.x+r.width, r.y);
Upvotes: 1