[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: SWT stops processing events when dragging starts

... here is the missing snippet:

maxx schrieb:
Hy there,

when a drag-operation starts, it seems that SWT stops processing events until either the mouse is moved or the button is released.

To reproduce the behavior, start the test given below and position your mouse-pointer above a tree-item. As soon as you push down the left mouse-button the counter will stop ...

Is this expected behavior ?

I work on an application which shows quite a lot of real-time updated information. As soon as the mouse-button is pressed on any of the drag-drop enabled components *everything* in the GUI freezes, and SWT pollutes its async-queue with a ton of events. These events will then be "post" processed, ie. they get processed as soon as the mouse moves/releases.

Thanks in advance
maxx

import org.eclipse.swt.SWT; import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.events.DisposeEvent; import org.eclipse.swt.dnd.DND; import org.eclipse.swt.dnd.DragSource; import org.eclipse.swt.dnd.DragSourceAdapter; import org.eclipse.swt.dnd.DragSourceEvent; import org.eclipse.swt.dnd.DropTarget; import org.eclipse.swt.dnd.DropTargetAdapter; import org.eclipse.swt.dnd.DropTargetEvent; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem;

public class DragNUpdateTest
{

    static Button button;
    static boolean running = false;


public static void main(String[] args) { Shell shell = new Shell(); shell.setLayout(new FillLayout()); shell.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent event) { running = false; } });

        // Create the tree and some tree items
        final Tree tree = new Tree(shell, SWT.NONE);
        TreeItem item1 = new TreeItem(tree, SWT.NONE);
        item1.setText("Item 1");
        TreeItem item2 = new TreeItem(tree, SWT.NONE);
        item2.setText("Item 2");
        TreeItem item3 = new TreeItem(tree, SWT.NONE);
        item3.setText("Item 3");
        TreeItem item4 = new TreeItem(tree, SWT.NONE);
        item4.setText("Item 4");

        // Create the drag source on the tree
        DragSource ds = new DragSource(tree, DND.DROP_MOVE);
        ds.setTransfer(new Transfer[]{TextTransfer.getInstance()});
        ds.addDragListener(new DragSourceAdapter()
        {
            public void dragSetData(DragSourceEvent event)
            {
                // Set the data to be the first selected item's text
                event.data = tree.getSelection()[0].getText();
            }
        });

        // Create the button
        button = new Button(shell, SWT.FLAT);
        button.setText("Button");
        button.setAlignment(SWT.CENTER);
        button.setData(0);

        // Create the drop target on the button
        DropTarget dt = new DropTarget(button, DND.DROP_MOVE);
        dt.setTransfer(new Transfer[]{TextTransfer.getInstance()});
        dt.addDropListener(new DropTargetAdapter()
        {
            public void drop(DropTargetEvent event)
            {
                // Set the buttons text to be the text being dropped
                button.setText((String) event.data);
            }
        });

        shell.pack();
        shell.open();

        startUpdater();

        Display display = Display.getDefault();
        while (!shell.isDisposed())
            if (!display.readAndDispatch())
                display.sleep();
        display.dispose();
    }

    private static void startUpdater()
    {
        Thread updateThread = new Thread(new Runnable()
        {
            public void run()
            {
                while (running)
                {
                    Display.getDefault().asyncExec(new Runnable()
                    {
                        public void run()
                        {
                            if (!running)
                                return;
                            int val = (Integer) button.getData() + 1;
                            button.setData(val);
                            button.setText(String.valueOf(val));
                        }
                    });

                    try
                    {
                        Thread.sleep(200);
                    }
                    catch (InterruptedException e)
                    {
                    }
                }
            }
        });

        running = true;
        updateThread.start();
    }


}