Faking a drag gesture

Hi Beni,

another question today: is there a possibility to fake a drag gesture to activate drag&drop of dockables?

I have a list of available dockables in my application menu, where the user can select from. Well, sometimes it is not so easy to choose the right location for adding the new dockable into my content-area. So, I would like to manually initiate a drag, after the user selects the menu item, such that the user can directly choose the location where to drop the new dockable.

Do you have any ideas?

Thanks a lot,

Andy

There is an interface for remotely dragging and dropping Dockables. It was originally intended to drag and drop Dockables that are already visible. If you can wait until the weekend I’ll modify the interface a bit in order to get it working with invisible Dockables as well.

Otherwise you would need to create some fake-visibility for the Dockable in order to get the interface working. For example adding the Dockable to some (invisible, temporary) StackDockStation should already be enough.

DockRelocator relocator = controller.getRelocator();
DirectRemoteRelocator remote = relocator.createDirectRemote( dockable ); // or cdockable.intern()

remote.init/drag/drop```

Hi Beni,

will this interface also provides the nice rectangles where the dockables can be placed?

I understood this interface in such a way, that I have to provide the coordinates by my own, where the dockable will be dropped. But I like to have a functionality, thats uses all the dnd possibilities of your framework, like highlighting possible drop locations etc.

Well, I can try to paint these things by my own to, e.g., the glasspane, but I think, I need some hints from you, how and where you solved this issue in your code. :wink:

Thanks again,

Andy

You provide the interface with the current position of the mouse on the screen, everything else works as if the mouse would really be there, including hover effects and preview windows. The “real” DnD mechanism and the interface share the same code.

This is an example how you could use the interface. It will work with the next release which I’ll upload within the next hour.


import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;

import bibliothek.gui.DockController;
import bibliothek.gui.dock.DefaultDockable;
import bibliothek.gui.dock.SplitDockStation;
import bibliothek.gui.dock.control.RemoteRelocator;

public class Dock47 {
	public static void main( String[] args ){
		JFrame frame = new JFrame( "test" );
		final DockController controller = new DockController();
		controller.setRootWindow( frame );

		JButton button = new JButton( "drag me" );
		frame.add( button, BorderLayout.NORTH );

		SplitDockStation station = new SplitDockStation();
		frame.add( station );
		controller.add( station );

		frame.setBounds( 20, 20, 400, 400 );
		frame.setVisible( true );

		MouseInputListener listener = new MouseInputAdapter(){
			private RemoteRelocator relocator;
			
			@Override
			public void mousePressed( MouseEvent e ){
				relocator = controller.getRelocator().createRemote( new DefaultDockable( "Test" ), true );
				Point mouse = e.getLocationOnScreen();
				
			    switch( relocator.init( mouse.x, mouse.y, 0, 0, e.getModifiersEx() ) ){
			    	case BREAK:
			    		relocator = null;
			    		break;
			    	case BREAK_CONSUMED:
			    		e.consume();
			    		relocator = null;
			    		break;
			    	case CONTINUE_CONSUMED:
			    		e.consume();
			    		break;
			    }
			}
			@Override
			public void mouseDragged( MouseEvent e ){
				if( relocator != null ){
					Point mouse = e.getLocationOnScreen();
					switch( relocator.drag( mouse.x, mouse.y, e.getModifiersEx() ) ){
					case BREAK:
			    		relocator = null;
			    		break;
			    	case BREAK_CONSUMED:
			    		e.consume();
			    		relocator = null;
			    		break;
			    	case CONTINUE_CONSUMED:
			    		e.consume();
			    		break;
					}
				}
			}
			
			@Override
			public void mouseReleased( MouseEvent e ){
				if( relocator != null ){
					Point mouse = e.getLocationOnScreen();
					switch( relocator.drop( mouse.x, mouse.y, e.getModifiersEx() ) ){
					case BREAK:
			    		relocator = null;
			    		break;
			    	case BREAK_CONSUMED:
			    		e.consume();
			    		relocator = null;
			    		break;
			    	case CONTINUE_CONSUMED:
			    		e.consume();
			    		break;
					}
				}
			}
		};
		button.addMouseListener( listener );
		button.addMouseMotionListener( listener );
	}
}

Great, great job, Beni!

It tooks a while until I recognized the forceDrag flag, but now it work fantastically. :smiley:

Thanks a lot!