Example usage for org.eclipse.swt.widgets Display map

List of usage examples for org.eclipse.swt.widgets Display map

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Display map.

Prototype

public Point map(Control from, Control to, int x, int y) 

Source Link

Document

Maps a point from one coordinate system to another.

Usage

From source file:org.eclipse.swt.snippets.Snippet142.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 142");
    final Button button = new Button(shell, SWT.NONE);
    button.setSize(100, 100);/*w  ww  .  j  ava2s .  co m*/
    button.setText("Click");
    shell.pack();
    shell.open();
    button.addListener(SWT.MouseDown,
            e -> System.out.println("Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")"));
    final Point pt = display.map(shell, null, 50, 50);
    new Thread() {
        Event event;

        @Override
        public void run() {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event = new Event();
            event.type = SWT.MouseMove;
            event.x = pt.x;
            event.y = pt.y;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event.type = SWT.MouseDown;
            event.button = 1;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event.type = SWT.MouseUp;
            display.post(event);
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:MouseEventPost.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    final Button button = new Button(shell, SWT.NONE);
    button.setSize(100, 100);//  ww  w.  ja  v  a  2s . c o  m
    button.setText("Click");
    shell.pack();
    shell.open();
    button.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event e) {
            System.out.println("Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")");
        }
    });
    final Point pt = display.map(shell, null, 50, 50);
    new Thread() {
        Event event;

        public void run() {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event = new Event();
            event.type = SWT.MouseMove;
            event.x = pt.x;
            event.y = pt.y;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet142.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    final Button button = new Button(shell, SWT.NONE);
    button.setSize(100, 100);//from   www. jav  a2 s . c o m
    button.setText("Click");
    shell.pack();
    shell.open();
    button.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event e) {
            System.out.println("Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")");
        }
    });
    final Point pt = display.map(shell, null, 50, 50);
    new Thread() {
        Event event;

        public void run() {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event = new Event();
            event.type = SWT.MouseMove;
            event.x = pt.x;
            event.y = pt.y;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event.type = SWT.MouseDown;
            event.button = 1;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event.type = SWT.MouseUp;
            display.post(event);
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:RingShell.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
    shell.setBackground(display.getSystemColor(SWT.COLOR_DARK_MAGENTA));

    Region region = new Region();
    region.add(createCircle(100, 100, 100));
    region.subtract(createCircle(50, 100, 100));
    shell.setRegion(region);//w w  w  .j  av  a  2s .c  o m

    // Make the shell movable by using the mouse pointer. 
    shell.addMouseListener(new MouseListener() {
        public void mouseDoubleClick(MouseEvent e) {
            shell.dispose(); // Double click to dispose the shell.
        }

        public void mouseDown(MouseEvent e) {
            originalPosition = new Point(e.x, e.y);
        }

        public void mouseUp(MouseEvent e) {
            originalPosition = null;
        }
    });

    shell.addMouseMoveListener(new MouseMoveListener() {
        public void mouseMove(MouseEvent e) {
            if (originalPosition == null)
                return;
            Point point = display.map(shell, null, e.x, e.y);
            shell.setLocation(point.x - originalPosition.x, point.y - originalPosition.y);
            System.out.println("Moved from: " + originalPosition + " to " + point);
        }
    });

    Rectangle regionBounds = region.getBounds();
    shell.setSize(regionBounds.width, regionBounds.height);
    shell.open();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }
    display.dispose();
    region.dispose();

}

From source file:Snippet134.java

public static void main(String[] args) {
    final Display display = new Display();
    // Shell must be created with style SWT.NO_TRIM
    final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
    shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
    // define a region that looks like a key hole
    Region region = new Region();
    region.add(circle(67, 67, 67));//from   ww  w. j  a v a2 s.  c  o  m
    region.subtract(circle(20, 67, 50));
    region.subtract(new int[] { 67, 50, 55, 105, 79, 105 });
    // define the shape of the shell using setRegion
    shell.setRegion(region);
    Rectangle size = region.getBounds();
    shell.setSize(size.width, size.height);
    // add ability to move shell around
    Listener l = new Listener() {
        Point origin;

        public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.MouseDown:
                origin = new Point(e.x, e.y);
                break;
            case SWT.MouseUp:
                origin = null;
                break;
            case SWT.MouseMove:
                if (origin != null) {
                    Point p = display.map(shell, null, e.x, e.y);
                    shell.setLocation(p.x - origin.x, p.y - origin.y);
                }
                break;
            }
        }
    };
    shell.addListener(SWT.MouseDown, l);
    shell.addListener(SWT.MouseUp, l);
    shell.addListener(SWT.MouseMove, l);
    // add ability to close shell
    Button b = new Button(shell, SWT.PUSH);
    b.setBackground(shell.getBackground());
    b.setText("close");
    b.pack();
    b.setLocation(10, 68);
    b.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            shell.close();
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    region.dispose();
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet310.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 310");
    shell.setLayout(new GridLayout());
    final Spinner spinner = new Spinner(shell, SWT.BORDER);
    spinner.setValues(0, -100, 100, 0, 1, 10);
    spinner.setLayoutData(new GridData(200, SWT.DEFAULT));
    final ToolTip toolTip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_WARNING);
    spinner.addModifyListener(e -> {//from  w  w w  .j av a2 s  .c  o m
        String string = spinner.getText();
        String message = null;
        try {
            int value = Integer.parseInt(string);
            int maximum = spinner.getMaximum();
            int minimum = spinner.getMinimum();
            if (value > maximum) {
                message = "Current input is greater than the maximum limit (" + maximum + ")";
            } else if (value < minimum) {
                message = "Current input is less than the minimum limit (" + minimum + ")";
            }
        } catch (Exception ex) {
            message = "Current input is not numeric";
        }
        if (message != null) {
            spinner.setForeground(display.getSystemColor(SWT.COLOR_RED));
            Rectangle rect = spinner.getBounds();
            GC gc = new GC(spinner);
            Point pt = gc.textExtent(string);
            gc.dispose();
            toolTip.setLocation(display.map(shell, null, rect.x + pt.x, rect.y + rect.height));
            toolTip.setMessage(message);
            toolTip.setVisible(true);
        } else {
            toolTip.setVisible(false);
            spinner.setForeground(null);
        }
    });
    shell.setSize(300, 100);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet134.java

public static void main(String[] args) {
    final Display display = new Display();
    //Shell must be created with style SWT.NO_TRIM
    final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
    shell.setText("Snippet 134");
    shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
    //define a region that looks like a key hole
    Region region = new Region();
    region.add(circle(67, 67, 67));/*  w w  w  . ja va  2  s.  c  om*/
    region.subtract(circle(20, 67, 50));
    region.subtract(new int[] { 67, 50, 55, 105, 79, 105 });
    //define the shape of the shell using setRegion
    shell.setRegion(region);
    Rectangle size = region.getBounds();
    shell.setSize(size.width, size.height);
    //add ability to move shell around
    Listener l = new Listener() {
        /** The x/y of the MouseDown, relative to top-left of the shell. */
        Point origin;

        @Override
        public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.MouseDown:
                Point point = shell.toDisplay(e.x, e.y);
                Point loc = shell.getLocation();
                origin = new Point(point.x - loc.x, point.y - loc.y);
                break;
            case SWT.MouseUp:
                origin = null;
                break;
            case SWT.MouseMove:
                if (origin != null) {
                    Point p = display.map(shell, null, e.x, e.y);
                    shell.setLocation(p.x - origin.x, p.y - origin.y);
                }
                break;
            }
        }
    };
    shell.addListener(SWT.MouseDown, l);
    shell.addListener(SWT.MouseUp, l);
    shell.addListener(SWT.MouseMove, l);
    //add ability to close shell
    Button b = new Button(shell, SWT.PUSH);
    b.setBackground(shell.getBackground());
    b.setText("close");
    b.pack();
    b.setLocation(10, 68);
    b.addListener(SWT.Selection, e -> shell.close());
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    region.dispose();
    display.dispose();
}

From source file:DragTreeLeaf.java

public static void main(String[] args) {

    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 3; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("item " + i);
        for (int j = 0; j < 3; j++) {
            TreeItem subItem = new TreeItem(item, SWT.NONE);
            subItem.setText("item " + i + " " + j);
            for (int k = 0; k < 3; k++) {
                TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
                subsubItem.setText("item " + i + " " + j + " " + k);
            }/*ww w. j  a  va2 s. c  om*/
        }
    }

    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;

    final DragSource source = new DragSource(tree, operations);
    source.setTransfer(types);
    final TreeItem[] dragSourceItem = new TreeItem[1];
    source.addDragListener(new DragSourceListener() {
        public void dragStart(DragSourceEvent event) {
            TreeItem[] selection = tree.getSelection();
            if (selection.length > 0 && selection[0].getItemCount() == 0) {
                event.doit = true;
                dragSourceItem[0] = selection[0];
            } else {
                event.doit = false;
            }
        };

        public void dragSetData(DragSourceEvent event) {
            event.data = dragSourceItem[0].getText();
        }

        public void dragFinished(DragSourceEvent event) {
            if (event.detail == DND.DROP_MOVE)
                dragSourceItem[0].dispose();
            dragSourceItem[0] = null;
        }
    });

    DropTarget target = new DropTarget(tree, operations);
    target.setTransfer(types);
    target.addDropListener(new DropTargetAdapter() {
        public void dragOver(DropTargetEvent event) {
            event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
            if (event.item != null) {
                TreeItem item = (TreeItem) event.item;
                Point pt = display.map(null, tree, event.x, event.y);
                Rectangle bounds = item.getBounds();
                if (pt.y < bounds.y + bounds.height / 3) {
                    event.feedback |= DND.FEEDBACK_INSERT_BEFORE;
                } else if (pt.y > bounds.y + 2 * bounds.height / 3) {
                    event.feedback |= DND.FEEDBACK_INSERT_AFTER;
                } else {
                    event.feedback |= DND.FEEDBACK_SELECT;
                }
            }
        }

        public void drop(DropTargetEvent event) {
            if (event.data == null) {
                event.detail = DND.DROP_NONE;
                return;
            }
            String text = (String) event.data;
            if (event.item == null) {
                TreeItem item = new TreeItem(tree, SWT.NONE);
                item.setText(text);
            } else {
                TreeItem item = (TreeItem) event.item;
                Point pt = display.map(null, tree, event.x, event.y);
                Rectangle bounds = item.getBounds();
                TreeItem parent = item.getParentItem();
                if (parent != null) {
                    TreeItem[] items = parent.getItems();
                    int index = 0;
                    for (int i = 0; i < items.length; i++) {
                        if (items[i] == item) {
                            index = i;
                            break;
                        }
                    }
                    if (pt.y < bounds.y + bounds.height / 3) {
                        TreeItem newItem = new TreeItem(parent, SWT.NONE, index);
                        newItem.setText(text);
                    } else if (pt.y > bounds.y + 2 * bounds.height / 3) {
                        TreeItem newItem = new TreeItem(parent, SWT.NONE, index + 1);
                        newItem.setText(text);
                    } else {
                        TreeItem newItem = new TreeItem(item, SWT.NONE);
                        newItem.setText(text);
                    }

                } else {
                    TreeItem[] items = tree.getItems();
                    int index = 0;
                    for (int i = 0; i < items.length; i++) {
                        if (items[i] == item) {
                            index = i;
                            break;
                        }
                    }
                    if (pt.y < bounds.y + bounds.height / 3) {
                        TreeItem newItem = new TreeItem(tree, SWT.NONE, index);
                        newItem.setText(text);
                    } else if (pt.y > bounds.y + 2 * bounds.height / 3) {
                        TreeItem newItem = new TreeItem(tree, SWT.NONE, index + 1);
                        newItem.setText(text);
                    } else {
                        TreeItem newItem = new TreeItem(item, SWT.NONE);
                        newItem.setText(text);
                    }
                }

            }
        }
    });

    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet91.java

public static void main(String[] args) {

    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 91");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 3; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("item " + i);
        for (int j = 0; j < 3; j++) {
            TreeItem subItem = new TreeItem(item, SWT.NONE);
            subItem.setText("item " + i + " " + j);
            for (int k = 0; k < 3; k++) {
                TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
                subsubItem.setText("item " + i + " " + j + " " + k);
            }/*from ww w  . j av a 2s. c o  m*/
        }
    }

    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;

    final DragSource source = new DragSource(tree, operations);
    source.setTransfer(types);
    final TreeItem[] dragSourceItem = new TreeItem[1];
    source.addDragListener(new DragSourceListener() {
        @Override
        public void dragStart(DragSourceEvent event) {
            TreeItem[] selection = tree.getSelection();
            if (selection.length > 0 && selection[0].getItemCount() == 0) {
                event.doit = true;
                dragSourceItem[0] = selection[0];
            } else {
                event.doit = false;
            }
        }

        @Override
        public void dragSetData(DragSourceEvent event) {
            event.data = dragSourceItem[0].getText();
        }

        @Override
        public void dragFinished(DragSourceEvent event) {
            if (event.detail == DND.DROP_MOVE)
                dragSourceItem[0].dispose();
            dragSourceItem[0] = null;
        }
    });

    DropTarget target = new DropTarget(tree, operations);
    target.setTransfer(types);
    target.addDropListener(new DropTargetAdapter() {
        @Override
        public void dragOver(DropTargetEvent event) {
            event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
            if (event.item != null) {
                TreeItem item = (TreeItem) event.item;
                Point pt = display.map(null, tree, event.x, event.y);
                Rectangle bounds = item.getBounds();
                if (pt.y < bounds.y + bounds.height / 3) {
                    event.feedback |= DND.FEEDBACK_INSERT_BEFORE;
                } else if (pt.y > bounds.y + 2 * bounds.height / 3) {
                    event.feedback |= DND.FEEDBACK_INSERT_AFTER;
                } else {
                    event.feedback |= DND.FEEDBACK_SELECT;
                }
            }
        }

        @Override
        public void drop(DropTargetEvent event) {
            if (event.data == null) {
                event.detail = DND.DROP_NONE;
                return;
            }
            String text = (String) event.data;
            if (event.item == null) {
                TreeItem item = new TreeItem(tree, SWT.NONE);
                item.setText(text);
            } else {
                TreeItem item = (TreeItem) event.item;
                Point pt = display.map(null, tree, event.x, event.y);
                Rectangle bounds = item.getBounds();
                TreeItem parent = item.getParentItem();
                if (parent != null) {
                    TreeItem[] items = parent.getItems();
                    int index = 0;
                    for (int i = 0; i < items.length; i++) {
                        if (items[i] == item) {
                            index = i;
                            break;
                        }
                    }
                    if (pt.y < bounds.y + bounds.height / 3) {
                        TreeItem newItem = new TreeItem(parent, SWT.NONE, index);
                        newItem.setText(text);
                    } else if (pt.y > bounds.y + 2 * bounds.height / 3) {
                        TreeItem newItem = new TreeItem(parent, SWT.NONE, index + 1);
                        newItem.setText(text);
                    } else {
                        TreeItem newItem = new TreeItem(item, SWT.NONE);
                        newItem.setText(text);
                    }

                } else {
                    TreeItem[] items = tree.getItems();
                    int index = 0;
                    for (int i = 0; i < items.length; i++) {
                        if (items[i] == item) {
                            index = i;
                            break;
                        }
                    }
                    if (pt.y < bounds.y + bounds.height / 3) {
                        TreeItem newItem = new TreeItem(tree, SWT.NONE, index);
                        newItem.setText(text);
                    } else if (pt.y > bounds.y + 2 * bounds.height / 3) {
                        TreeItem newItem = new TreeItem(tree, SWT.NONE, index + 1);
                        newItem.setText(text);
                    } else {
                        TreeItem newItem = new TreeItem(item, SWT.NONE);
                        newItem.setText(text);
                    }
                }

            }
        }
    });

    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}