Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Problem with overdrawing
Problem with overdrawing [message #773831] Mon, 02 January 2012 16:58 Go to next message
Jens Missing name is currently offline 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 #774125 is a reply to message #773831] Tue, 03 January 2012 09:59 Go to previous messageGo to next message
Michael Wenz is currently offline Michael Wenz
Messages: 1612
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Jens,

Graphiti draws all shapes in the order in which they appear in the children
relation (it's ordered). So, in your case Tokens would need to be sorted in
at the end of the list.

HTH,
Michael
Re: Problem with overdrawing [message #774135 is a reply to message #774125] Tue, 03 January 2012 10:33 Go to previous messageGo to next message
Jens Missing name is currently offline Jens Missing name
Messages: 27
Registered: November 2011
Junior Member
hi Michael,

could you please give me an hint, how I can accomplish this.
Which of the collections do I have to resort?

regards,

jens.

[Updated on: Tue, 03 January 2012 10:34]

Report message to a moderator

Re: Problem with overdrawing [message #774585 is a reply to message #774135] Wed, 04 January 2012 08:31 Go to previous messageGo to next message
Michael Wenz is currently offline Michael Wenz
Messages: 1612
Registered: July 2009
Location: Walldorf, Germany
Senior Member
The Children releation of the ContainerShape containing the stuff. Note that
the Diagram is also a ContainerShape.

Michael


"Jens Mising name" schrieb im Newsbeitrag
news:jdulic$otf$1@news.eclipse.org...

hi Michael,

can you give me an hint how I can accomplish this.
Which of the collections do I have to resort?

regards,

jens.
Re: Problem with overdrawing [message #775159 is a reply to message #774585] Thu, 05 January 2012 13:22 Go to previous messageGo to next message
Jens Missing name is currently offline 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 #777375 is a reply to message #775159] Tue, 10 January 2012 11:10 Go to previous messageGo to next message
Michael Wenz is currently offline Michael Wenz
Messages: 1612
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Have a look at the EMF EList interface. There are several index based
methods you can use (add, remove, also move). The elements with the highest
index will be drawn last.

Sorry, I don't have any example coding at hand now...

Michael
Re: Problem with overdrawing [message #778174 is a reply to message #777375] Wed, 11 January 2012 14:25 Go to previous message
Jens Missing name is currently offline 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.
Previous Topic:FocusListener for Diagrams?
Next Topic:Marquee vs Select all behavior
Goto Forum:
  


Current Time: Tue Oct 21 07:21:15 GMT 2014

Powered by FUDForum. Page generated in 0.03074 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software