Home » Modeling » Graphiti » Problem with overdrawing
Problem with overdrawing [message #773831] |
Mon, 02 January 2012 16:58 |
Jens Missing name Messages: 27 Registered: November 2011 |
Junior Member |
|
|
hi,
I wanted to create a petri net implmentation.
In a petri net there are Places (ellipse), Tokens and Arcs..
The token has to be in the front (of the places)
Here is my code. I hope you can help me out.
public class AddTokenFeature extends AbstractAddShapeFeature {
private static final IColorConstant CLASS_TEXT_FOREGROUND = new ColorConstant(
51, 51, 153);
public AddTokenFeature(IFeatureProvider fp) {
super(fp);
}
public boolean canAdd(IAddContext context) {
// check if user wants to add a Place
if (context.getNewObject() instanceof Token) {
// check if user wants to add to a diagram
if (context.getTargetContainer() instanceof Diagram) {
return true;
}
}
return false;
}
public PictogramElement add(IAddContext context) {
Token token = (Token) context.getNewObject();
Diagram targetDiagram = (Diagram) context.getTargetContainer();
IPeCreateService peCreateService = Graphiti.getPeCreateService();
ContainerShape containerShape = peCreateService.createContainerShape(
targetDiagram, true);
IGaService gaService = Graphiti.getGaService();
int width = 10;
int height = 10;
Ellipse ellipse = gaService.createEllipse(containerShape);
ellipse.setHeight(height);
ellipse.setWidth(width);
ellipse.setBackground(manageColor(IColorConstant.BLACK));
gaService.setLocationAndSize(ellipse, context.getX(), context.getY(),
width, height);
if (token.eResource() == null) {
getDiagram().eResource().getContents().add(token);
}
peCreateService.createChopboxAnchor(containerShape);
layoutPictogramElement(containerShape);
link(containerShape, token);
// SHAPE WITH TEXT
{
// create shape for text
Shape shape = peCreateService.createShape(containerShape, false);
// create and set text graphics algorithm
Text text = gaService
.createDefaultText(getDiagram(), shape, "test");
text.setForeground(manageColor(CLASS_TEXT_FOREGROUND));
text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER);
text.setVerticalAlignment(Orientation.ALIGNMENT_CENTER);
gaService.setLocationAndSize(text, 0, 0, width, 20);
// add a chopbox anchor to the shape
peCreateService.createChopboxAnchor(containerShape);
// call the layout feature
layoutPictogramElement(containerShape);
// create link and wire it
link(shape, token);
}
return containerShape;
}
}
public class CreateTokenFeature extends AbstractCreateFeature {
private Place p;
public CreateTokenFeature(IFeatureProvider fp) {
super(fp, "Token", "Create Token");
}
public boolean canCreate(ICreateContext context) {
EList<Anchor> anchors = context.getTargetContainer().getAnchors();
for (Anchor a : anchors) {
Object obj = getBusinessObjectForPictogramElement(a.getParent());
if(obj instanceof Place) {
p = (Place)obj;
return true;
}
}
return false;
}
@Override
public Object[] create(ICreateContext context) {
Token token = PetriFactory.eINSTANCE.createToken();
context.getTargetContainer().eResource().getContents().add(token);
addGraphicalRepresentation(context, token);
return new Object[] { token };
}
}
regards,
jens.
[Updated on: Mon, 02 January 2012 16:59] Report message to a moderator
|
|
| | | |
Re: Problem with overdrawing [message #775159 is a reply to message #774585] |
Thu, 05 January 2012 13:22 |
Jens Missing name Messages: 27 Registered: November 2011 |
Junior Member |
|
|
hi michael,
sorry, I don't get it working.
Could you please give me an example code?
I guess I have to use the move methode.
In the canAdd Methode I cannot check for "instance of Diagram".
So tried with "Shape". Is that correct?
public boolean canAdd(IAddContext context) {
ContainerShape c = context.getTargetContainer();
boolean bCheck = (context.getNewObject() instanceof Token && c instanceof Shape);
return bCheck;
}
public PictogramElement add(IAddContext context) {
Token token = (Token) context.getNewObject();
Diagram targetDiagram = (Diagram) context.getTargetContainer();
IPeCreateService peCreateService = Graphiti.getPeCreateService();
ContainerShape containerShape = peCreateService.createContainerShape(
targetDiagram, true);
IGaService gaService = Graphiti.getGaService();
PropertyUtil.setEClassShape(containerShape);
int width = 10;
int height = 10;
Ellipse ellipse = gaService.createEllipse(containerShape);
ellipse.setHeight(height);
ellipse.setWidth(width);
ellipse.setBackground(manageColor(IColorConstant.BLACK));
gaService.setLocationAndSize(ellipse, context.getX(), context.getY(),
width, height);
if (token.eResource() == null) {
getDiagram().eResource().getContents().add(token);
}
peCreateService.createChopboxAnchor(containerShape);
layoutPictogramElement(containerShape);
link(containerShape, token);
// SHAPE WITH TEXT
{
// create shape for text
Shape shape = peCreateService.createShape(containerShape, false);
// create and set text graphics algorithm
Text text = gaService
.createDefaultText(getDiagram(), shape, "test");
text.setForeground(manageColor(CLASS_TEXT_FOREGROUND));
text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER);
text.setVerticalAlignment(Orientation.ALIGNMENT_CENTER);
gaService.setLocationAndSize(text, 0, 0, width, 20);
// add a chopbox anchor to the shape
peCreateService.createChopboxAnchor(containerShape);
// call the layout feature
layoutPictogramElement(containerShape);
// create link and wire it
link(shape, token);
}
System.out.println("Children in List: " + containerShape.getChildren().size());
for (Shape shape : containerShape.getChildren()) {
System.out.println(shape.getClass());
}
EList<Shape> shapes = containerShape.getChildren();
//containerShape.getChildren().
//containerShape.getChildren().move(newPosition, oldPosition);
return containerShape;
}
regards,
jens.
|
|
| |
Re: Problem with overdrawing [message #778174 is a reply to message #777375] |
Wed, 11 January 2012 14:25 |
Jens Missing name Messages: 27 Registered: November 2011 |
Junior Member |
|
|
Hi,
I solved the problem after all..
Places-Code
public PictogramElement add(IAddContext context) {
IGaLayoutService layoutService = Graphiti.getGaLayoutService();
ICreateService createService = Graphiti.getCreateService();
String description = ExampleUtil.askString(TITLE, USER_QUESTION, "");
if (description == null || description.trim().length() == 0) {
return null;
}
Place place = (Place) context.getNewObject();
IPeCreateService peCreateService = Graphiti.getPeCreateService();
ContainerShape outerContainerShape = createService.createContainerShape(getDiagram(), true);
Rectangle outerRectangle = createService.createRectangle(outerContainerShape);
outerRectangle.setTransparency(0.99d);
layoutService.setLocationAndSize(outerRectangle, context.getX(), context.getY(), 100, 100) ;
int width = 70;
int height = 70;
IGaService gaService = Graphiti.getGaService();
ContainerShape ellipseShape = createService.createContainerShape(outerContainerShape, true);
Ellipse ellipse = gaService.createEllipse(ellipseShape);
ellipse.setHeight(height);
ellipse.setWidth(width);
ellipse.setBackground(manageColor(IColorConstant.WHITE));
Text textRectangle = createService.createText(outerRectangle);
setLayoutForBorderTexts(layoutService, textRectangle);
layoutService.setLocationAndSize(textRectangle, 0, 0, width, 20);
textRectangle.setValue( description);
gaService.setLocationAndSize(ellipse, ellipse.getX() + 10, ellipse.getY() + 15, 70, 70);
if (place.eResource() == null) {
getDiagram().eResource().getContents().add(place);
}
peCreateService.createChopboxAnchor(ellipseShape);
link(ellipseShape, place);
return outerContainerShape;
}
Token-Code
public PictogramElement add(IAddContext context) {
int width = 10;
int height = 10;
ICreateService createService = Graphiti.getCreateService();
IGaLayoutService layoutService = Graphiti.getGaLayoutService();
IGaService gaService = Graphiti.getGaService();
Token token = (Token) context.getNewObject();
ContainerShape ellipseShape = createService.createContainerShape(context.getTargetContainer(), true);
Ellipse ellipse = createService.createEllipse(ellipseShape);
ellipse.setHeight(height);
ellipse.setWidth(width);
ellipse.setBackground(manageColor(IColorConstant.BLACK));
gaService.setLocationAndSize(ellipse, context.getX(), context.getY(), 15, 15);
link(ellipseShape, token);
createService.createChopboxAnchor(ellipseShape);
return ellipseShape;
}
regards,
jens.
|
|
|
Goto Forum:
Current Time: Fri Sep 20 11:52:37 GMT 2024
Powered by FUDForum. Page generated in 0.03869 seconds
|