Bug 237479 - [Zest] Graph.getFigureAt does not work with scaled layers
Summary: [Zest] Graph.getFigureAt does not work with scaled layers
Status: NEW
Alias: None
Product: GEF
Classification: Tools
Component: GEF-Legacy Zest (show other bugs)
Version: 3.4   Edit
Hardware: All All
: P3 normal with 3 votes (vote)
Target Milestone: ---   Edit
Assignee: gef-inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-06-17 11:16 EDT by Zviki Cohen CLA
Modified: 2015-01-22 02:44 EST (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Zviki Cohen CLA 2008-06-17 11:16:45 EDT
There's a problem with the "find the item under the mouse" snippet. It will work, but not with scaled layouts. 
The coordinates need to be translated. 

Moreover, why should I care about the figure? In most cases, I need the node. There's no simple way of reaching the node from the figure anyway, since the figures mapping is private.

Let me suggest the followin method for the Graph class:

   public GraphItem getItemAt(final int x, final int y) {
       GraphItem result = null;
       final Point point = new Point(x, y);
       getRootLayer().translateToRelative(point);
       final IFigure figureAtPoint = getFigureAt(point.x, point.y);
       if (figureAtPoint != null) {
           result = getGraphItem(figureAtPoint);
       }
       return result;
   }
Comment 1 Jeff Norris CLA 2009-03-12 14:52:35 EDT
I second this suggestion.  The getFigureAt method isn't useful because I just end up with a GraphLabel when I want the GraphNode and there's no way to efficiently get the GraphNode since the figure2ItemMap is private.  

Please either add the getItemAt method as described above or make the getGraphItem(IFigure figure) method public API.


Comment 2 m.schrey CLA 2010-08-12 10:18:08 EDT
This problem drove me crazy too. Because of the private Method I had no chance to get my Nested Objects. But I really want to use Zest so here is a small workaround for those who are contaminated such as me ;)

For example I want to get the "node" at mouse location when a button is released:

graph.addMouseListener(new MouseListener() {
  public void mouseUp(MouseEvent e) {
    //this just gives us a GraphLabel etc. like postet above.
    IFigure figure = graph.getFigureAt(e.x, e.y);
    ...
  }
  ...
}

Now I do the following using reflections (sorry for that but it works)

// first the same as above (still in the mouseUp-method)
IFigure figure = graph.getFigureAt(e.x, e.y);
GraphItem item = null;
// now the bad things begin...
try {
  // getting private field
  Field f = Graph.class.getDeclaredField("figure2ItemMap");
  // set field public
  f.setAccessible(true);
  // get field
  HashMap figure2ItemMap = (HashMap)f.get(graph);
  f.setAccessible(false);
  // use field to get the GraphItem
  item = (GraphItem) figure2ItemMap.get(figure);
}
catch (Exception e1) {
  // this should not happen
  e1.printStackTrace();
}

Hope I could help someone. Btw. I first tried it with the "getGraphItem" method but this didn't work. Maybe because it is neither declared as private nor than public.

Nevertheless the bug should be fixed sometime. Its just a little public in front of the "getGraphItem" method. :P
Comment 3 Sebastian Bauer CLA 2011-04-15 09:23:51 EDT
Any news on that bug? I would also find an official getGraphItemAt() or similar method very handy.
Comment 4 Alexander Nyßen CLA 2015-01-22 02:44:23 EST
Re-assigning back to inbox, as Ian is no longer active committer.