1

Can someone please explain to me how coordinate transformations work in draw2d? I have a hierarchical diagram where a figure can contain figures which also contain figures. At first I added internal figures by using the request's getLocation, fetching the host figure of the EditPolicy and appliying hostFigure.translateToRelative(location) but does not work! neither combinations of translateToParent and other things.

At the end I copied the implementation from the Logic example, which uses getConstraintFor, a method provided by the policy which does the translation itself. I checked this could but also could not understand how it works.

I read in a number of threads in the eclipse forums on this subject, but still don't understand why a simple method like translateToAbsolute does not behave as expected. Could anyone please explan? Thanks

vainolo
  • 6,907
  • 4
  • 24
  • 47
  • Could you be a little more specific than `does not work`? In what way does `translateToAbsolute(..)` not work as you expect it to? – Frettman Oct 18 '11 at 07:36
  • A concrete example: I am creating edit parts which can contain edit parts (recursively, with no limit). The request that is received when the creation edit policy is applied gives the coordinates in absolute coordinates, and I want to transform them to coordinates relative to the containing editpart (on which the policy was activated, can be at any level). I thought that getHostFigure().translateToRelative() on the request.getLocation() would give me the coordinates where the user clicked relative to the top-left corner of the editpart's figure, but this was not the case. – vainolo Oct 24 '11 at 21:00
  • Like I wrote in my answer, ´translateToRelative(..)´ will only change the location if the host figure actually has its own coordinate system. Most figures don't. So the location will not actually be relative to the host figure, but instead relative to the first figure with its own coordinate system when going up the figure tree. It's unexpected and maybe confusing at first, but doesn't really change how things work. – Frettman Oct 27 '11 at 08:46
  • The figures I am using have an XYLayout onto which I add new figures. I thought this means it has its own coordinate system... – vainolo Oct 28 '11 at 08:27
  • The `Rectangle`s you provide as layout constraints for the `XYLayout ` are expected to be relative to the parent figure. But the `XYLayout ` will offset those with `getClientArea().getLocation()`. So the final location assigned to the child figures may actually be absolute coordinates (or rather relative to your root figure). This basically means the layout managers only emulate a local coordinate system, so you don't have to care which figures in the hierarchy actually have their own coordinate system and which don't. I hope this explanation makes sense to you =) – Frettman Oct 28 '11 at 09:25
  • Try calling `isCoordinateSystem()` on your figures to see that most will *not* provide their own relative/local coordinate system. – Frettman Oct 28 '11 at 09:31
  • Sorry. Still not with you. I think the real problem is the naming of the functions in GEF and not my understanding of coordinate systems. Anyway, thanks for your time. – vainolo Nov 08 '11 at 09:58
  • `translateToRelative(..)` will only produce truly relative coordinates (where (0,0) is at the top-left corner of your figure) if the figure on which you are calling the method returns true from its `isCoordinateSystem()` method. So I can see why the name `translateToRelative` could be considered somewhat misleading or at least confusing. – Frettman Nov 08 '11 at 13:25
  • In other words, you are correct if you say that in your case the returned coordinates were not relative at all. But if you set the location of a child figure to those coordinates, the child will be painted at the correct position. So for the code you write it is irrelevant if the coordinates are truly relative or not. – Frettman Nov 08 '11 at 13:25

1 Answers1

1

Two pieces of information that might shed some light on your problem:

  1. Depending on the request type I would expect the location to already be in absolute coordinates.
  2. Unless explicitly implemented otherwise, Figures don't have a local coordinate system for their children. So converting a location up and down the Figure hierarchy does not necessarily change the coordinates.
Frettman
  • 2,251
  • 1
  • 13
  • 9