Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Coordinate translation in nested figures(Need help with coordinate translation)
Coordinate translation in nested figures [message #694032] Thu, 07 July 2011 12:52 Go to next message
Kazan  is currently offline Kazan
Messages: 5
Registered: July 2009
Junior Member
I have nested figures, and I added a mouse listener to one of the nested figures.

When I click on the figure, and print the location of event.getLocation().x, X is relative to the top level figure.

In the example class below, when you click the top left corner of the green rectangle, I am trying to make it so it prints an X location of 0.

Instead, it prints an X location of 30.

I have been experimenting with the the _layer.translateToRelave(..), _layer.translateToAbsolute(..), _layer.translateFromParent(..), and _layer.translateToParent(..).

Nothing I try works. I read the documentation, but I am not able to understand what I am doing wrong.

Can someone, please, please, please, please help me out?

Here is my test class:
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.GridData;
import org.eclipse.draw2d.GridLayout;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.ScalableLayeredPane;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test2{
  private ScalableLayeredPane _pane;
  private Figure _rowController;
  private Figure _columnController;
  private Figure _root;
  private FreeformLayer _layer1;
  private FreeformLayer _layer2;
  
  public Test2(Shell shell){
    shell.setLayout(new FillLayout(SWT.VERTICAL));
    final Canvas canvas = new Canvas(shell, SWT.NONE);
    canvas.setSize(500, 500);
    LightweightSystem lws = new LightweightSystem(canvas);
    
    _root = new Figure();
    _root.setLayoutManager(new GridLayout(2, false));

    _pane = new ScalableLayeredPane();
    
    lws.setContents(_root);

    _layer1 = new FreeformLayer();
    _layer2 = new FreeformLayer();
    
    _pane.add(_layer1, new Rectangle(0, 0, 200, 200), 0);
    _pane.add(_layer2, new Rectangle(0, 0, 200, 200), 1);

    _layer1.setLayoutManager(new XYLayout());
    _layer2.setLayoutManager(new XYLayout());

    RectangleFigure fig1 = new RectangleFigure();
    fig1.setBackgroundColor(ColorConstants.lightGreen);
    
    RectangleFigure fig2 = new RectangleFigure();
    fig2.setBackgroundColor(ColorConstants.lightGray);

    _layer1.add(fig1, new Rectangle(  0,   0, 100, 100));
    _layer2.add(fig2, new Rectangle(200, 200, 100, 100));

    _pane.setScale(1);
    
    Figure pnlNW = new Figure();
    pnlNW.setBackgroundColor(ColorConstants.gray);
    pnlNW.setOpaque(true);
    GridData dataNW = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
    dataNW.heightHint = 20;
    dataNW.widthHint = 20;
    _root.add(pnlNW, dataNW);

    _columnController = new Figure();
    _columnController.setBackgroundColor(ColorConstants.gray);
    _columnController.setOpaque(true);
    GridData dataNorth = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
    dataNorth.heightHint = 20;
    _root.add(_columnController, dataNorth);

    _rowController = new Figure();
    _rowController.setBackgroundColor(ColorConstants.gray);
    _rowController.setOpaque(true);
    GridData dataWest = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
    dataWest.widthHint = 20;
    _root.add(_rowController, dataWest);

    GridData dataCenter = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    _root.add(_pane, dataCenter);
    
    _layer1.addMouseListener(new MouseListener.Stub(){
      public void mousePressed(MouseEvent event){
        Point p = event.getLocation().getCopy();
        System.out.println("p.x=" + p.x);
      }
    });
  }
  
  public static void main(String args[]){
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.V_SCROLL | SWT.H_SCROLL);
    shell.setBounds(0, 0, 400, 300);
    
    new Test2(shell);
    
    shell.open();
    while(!shell.isDisposed()){
      if(!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}
Re: Coordinate translation in nested figures [message #701032 is a reply to message #694032] Sun, 24 July 2011 09:12 Go to previous messageGo to next message
Ivar Refsdal is currently offline Ivar Refsdal
Messages: 24
Registered: May 2011
Junior Member
Hi Kazan.

You can use IFigure's getBounds() to get the absolute position of the
figure.. (setBounds() is called by the layoutmanager.)
And so you can use Point's getDifference to get the relative offset..

> _root.addMouseListener(new MouseListener.Stub() {
>
> public void mousePressed(MouseEvent event) {
> Point p = event.getLocation().getCopy();
> IFigure figure = _root.findFigureAt(p);
> if (figure==null) {
> System.out.println("nothing at " + p);
> return;
> }
> Dimension offset = p.getDifference(figure.getBounds().getTopLeft());
> System.out.println("mousePress: figure is " + figure + " and offset is " + offset);
> event.consume();
> }
> });

Hope this helps,
Best,
Ivar


Kazan wrote, on 07/07/2011 06:52 PM:
> I have nested figures, and I added a mouse listener to one of the nested
> figures.
>
> When I click on the figure, and print the location of
> event.getLocation().x, X is relative to the top level figure.
>
> In the example class below, when you click the top left corner of the
> green rectangle, I am trying to make it so it prints an X location of 0.
>
> Instead, it prints an X location of 30.
>
> I have been experimenting with the the _layer.translateToRelave(..),
> _layer.translateToAbsolute(..), _layer.translateFromParent(..), and
> _layer.translateToParent(..).
>
> Nothing I try works. I read the documentation, but I am not able to
> understand what I am doing wrong.
>
> Can someone, please, please, please, please help me out?
>
> Here is my test class:
>
> import org.eclipse.draw2d.ColorConstants;
> import org.eclipse.draw2d.Figure;
> import org.eclipse.draw2d.FreeformLayer;
> import org.eclipse.draw2d.GridData;
> import org.eclipse.draw2d.GridLayout;
> import org.eclipse.draw2d.LightweightSystem;
> import org.eclipse.draw2d.MouseEvent;
> import org.eclipse.draw2d.MouseListener;
> import org.eclipse.draw2d.RectangleFigure;
> import org.eclipse.draw2d.ScalableLayeredPane;
> import org.eclipse.draw2d.XYLayout;
> import org.eclipse.draw2d.geometry.Point;
> import org.eclipse.draw2d.geometry.Rectangle;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Canvas;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
>
> public class Test2{
> private ScalableLayeredPane _pane;
> private Figure _rowController;
> private Figure _columnController;
> private Figure _root;
> private FreeformLayer _layer1;
> private FreeformLayer _layer2;
>
> public Test2(Shell shell){
> shell.setLayout(new FillLayout(SWT.VERTICAL));
> final Canvas canvas = new Canvas(shell, SWT.NONE);
> canvas.setSize(500, 500);
> LightweightSystem lws = new LightweightSystem(canvas);
> _root = new Figure();
> _root.setLayoutManager(new GridLayout(2, false));
>
> _pane = new ScalableLayeredPane();
> lws.setContents(_root);
>
> _layer1 = new FreeformLayer();
> _layer2 = new FreeformLayer();
> _pane.add(_layer1, new Rectangle(0, 0, 200, 200), 0);
> _pane.add(_layer2, new Rectangle(0, 0, 200, 200), 1);
>
> _layer1.setLayoutManager(new XYLayout());
> _layer2.setLayoutManager(new XYLayout());
>
> RectangleFigure fig1 = new RectangleFigure();
> fig1.setBackgroundColor(ColorConstants.lightGreen);
> RectangleFigure fig2 = new RectangleFigure();
> fig2.setBackgroundColor(ColorConstants.lightGray);
>
> _layer1.add(fig1, new Rectangle( 0, 0, 100, 100));
> _layer2.add(fig2, new Rectangle(200, 200, 100, 100));
>
> _pane.setScale(1);
> Figure pnlNW = new Figure();
> pnlNW.setBackgroundColor(ColorConstants.gray);
> pnlNW.setOpaque(true);
> GridData dataNW = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
> dataNW.heightHint = 20;
> dataNW.widthHint = 20;
> _root.add(pnlNW, dataNW);
>
> _columnController = new Figure();
> _columnController.setBackgroundColor(ColorConstants.gray);
> _columnController.setOpaque(true);
> GridData dataNorth = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
> dataNorth.heightHint = 20;
> _root.add(_columnController, dataNorth);
>
> _rowController = new Figure();
> _rowController.setBackgroundColor(ColorConstants.gray);
> _rowController.setOpaque(true);
> GridData dataWest = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
> dataWest.widthHint = 20;
> _root.add(_rowController, dataWest);
>
> GridData dataCenter = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
> _root.add(_pane, dataCenter);
> _layer1.addMouseListener(new MouseListener.Stub(){
> public void mousePressed(MouseEvent event){
> Point p = event.getLocation().getCopy();
> System.out.println("p.x=" + p.x);
> }
> });
> }
>
> public static void main(String args[]){
> Display display = new Display();
> final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.V_SCROLL |
> SWT.H_SCROLL);
> shell.setBounds(0, 0, 400, 300);
> new Test2(shell);
> shell.open();
> while(!shell.isDisposed()){
> if(!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
> }
>
Re: Coordinate translation in nested figures [message #702812 is a reply to message #701032] Tue, 26 July 2011 16:48 Go to previous message
Ivar Refsdal is currently offline Ivar Refsdal
Messages: 24
Registered: May 2011
Junior Member
Hi again.

Actually, I'm a little confused by relative/absolute coordinates.
getBounds() seems to return the relative position of the figure.
However, so does the MouseEvent.
Can anybody explain this a little further?

I'd be very happy to see more comments in the javadoc about
whether absolute or relative coordinates is used for methods such
as getBounds().

Thanks,
Ivar

Ivar Refsdal wrote, on 07/24/2011 03:12 PM:
> Hi Kazan.
>
> You can use IFigure's getBounds() to get the absolute position of the
> figure.. (setBounds() is called by the layoutmanager.)
> And so you can use Point's getDifference to get the relative offset..
>
>> _root.addMouseListener(new MouseListener.Stub() {
>>
>> public void mousePressed(MouseEvent event) {
>> Point p = event.getLocation().getCopy();
>> IFigure figure = _root.findFigureAt(p);
>> if (figure==null) {
>> System.out.println("nothing at " + p);
>> return;
>> }
>> Dimension offset = p.getDifference(figure.getBounds().getTopLeft());
>> System.out.println("mousePress: figure is " + figure + " and offset is
>> " + offset);
>> event.consume();
>> }
>> });
>
> Hope this helps,
> Best,
> Ivar
>
>
> Kazan wrote, on 07/07/2011 06:52 PM:
>> I have nested figures, and I added a mouse listener to one of the nested
>> figures.
>>
>> When I click on the figure, and print the location of
>> event.getLocation().x, X is relative to the top level figure.
>>
>> In the example class below, when you click the top left corner of the
>> green rectangle, I am trying to make it so it prints an X location of 0.
>>
>> Instead, it prints an X location of 30.
>>
>> I have been experimenting with the the _layer.translateToRelave(..),
>> _layer.translateToAbsolute(..), _layer.translateFromParent(..), and
>> _layer.translateToParent(..).
>>
>> Nothing I try works. I read the documentation, but I am not able to
>> understand what I am doing wrong.
>>
>> Can someone, please, please, please, please help me out?
>>
>> Here is my test class:
>>
>> import org.eclipse.draw2d.ColorConstants;
>> import org.eclipse.draw2d.Figure;
>> import org.eclipse.draw2d.FreeformLayer;
>> import org.eclipse.draw2d.GridData;
>> import org.eclipse.draw2d.GridLayout;
>> import org.eclipse.draw2d.LightweightSystem;
>> import org.eclipse.draw2d.MouseEvent;
>> import org.eclipse.draw2d.MouseListener;
>> import org.eclipse.draw2d.RectangleFigure;
>> import org.eclipse.draw2d.ScalableLayeredPane;
>> import org.eclipse.draw2d.XYLayout;
>> import org.eclipse.draw2d.geometry.Point;
>> import org.eclipse.draw2d.geometry.Rectangle;
>> import org.eclipse.swt.SWT;
>> import org.eclipse.swt.layout.FillLayout;
>> import org.eclipse.swt.widgets.Canvas;
>> import org.eclipse.swt.widgets.Display;
>> import org.eclipse.swt.widgets.Shell;
>>
>> public class Test2{
>> private ScalableLayeredPane _pane;
>> private Figure _rowController;
>> private Figure _columnController;
>> private Figure _root;
>> private FreeformLayer _layer1;
>> private FreeformLayer _layer2;
>>
>> public Test2(Shell shell){
>> shell.setLayout(new FillLayout(SWT.VERTICAL));
>> final Canvas canvas = new Canvas(shell, SWT.NONE);
>> canvas.setSize(500, 500);
>> LightweightSystem lws = new LightweightSystem(canvas);
>> _root = new Figure();
>> _root.setLayoutManager(new GridLayout(2, false));
>>
>> _pane = new ScalableLayeredPane();
>> lws.setContents(_root);
>>
>> _layer1 = new FreeformLayer();
>> _layer2 = new FreeformLayer();
>> _pane.add(_layer1, new Rectangle(0, 0, 200, 200), 0);
>> _pane.add(_layer2, new Rectangle(0, 0, 200, 200), 1);
>>
>> _layer1.setLayoutManager(new XYLayout());
>> _layer2.setLayoutManager(new XYLayout());
>>
>> RectangleFigure fig1 = new RectangleFigure();
>> fig1.setBackgroundColor(ColorConstants.lightGreen);
>> RectangleFigure fig2 = new RectangleFigure();
>> fig2.setBackgroundColor(ColorConstants.lightGray);
>>
>> _layer1.add(fig1, new Rectangle( 0, 0, 100, 100));
>> _layer2.add(fig2, new Rectangle(200, 200, 100, 100));
>>
>> _pane.setScale(1);
>> Figure pnlNW = new Figure();
>> pnlNW.setBackgroundColor(ColorConstants.gray);
>> pnlNW.setOpaque(true);
>> GridData dataNW = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
>> dataNW.heightHint = 20;
>> dataNW.widthHint = 20;
>> _root.add(pnlNW, dataNW);
>>
>> _columnController = new Figure();
>> _columnController.setBackgroundColor(ColorConstants.gray);
>> _columnController.setOpaque(true);
>> GridData dataNorth = new GridData(SWT.FILL, SWT.CENTER, true, false,
>> 1, 1);
>> dataNorth.heightHint = 20;
>> _root.add(_columnController, dataNorth);
>>
>> _rowController = new Figure();
>> _rowController.setBackgroundColor(ColorConstants.gray);
>> _rowController.setOpaque(true);
>> GridData dataWest = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
>> dataWest.widthHint = 20;
>> _root.add(_rowController, dataWest);
>>
>> GridData dataCenter = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
>> _root.add(_pane, dataCenter);
>> _layer1.addMouseListener(new MouseListener.Stub(){
>> public void mousePressed(MouseEvent event){
>> Point p = event.getLocation().getCopy();
>> System.out.println("p.x=" + p.x);
>> }
>> });
>> }
>>
>> public static void main(String args[]){
>> Display display = new Display();
>> final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.V_SCROLL |
>> SWT.H_SCROLL);
>> shell.setBounds(0, 0, 400, 300);
>> new Test2(shell);
>> shell.open();
>> while(!shell.isDisposed()){
>> if(!display.readAndDispatch())
>> display.sleep();
>> }
>> display.dispose();
>> }
>> }
>>
>
Previous Topic:Default connection length
Next Topic:How to set Text to a IEditorInput used in MultiPageEditor
Goto Forum:
  


Current Time: Fri Jul 25 13:43:43 EDT 2014

Powered by FUDForum. Page generated in 0.03077 seconds