[jbosstools-commits] JBoss Tools SVN: r22604 - in trunk: jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042_6229 and 6 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Mon Jun 7 13:01:28 EDT 2010


Author: yradtsevich
Date: 2010-06-07 13:01:26 -0400 (Mon, 07 Jun 2010)
New Revision: 22604

Added:
   trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042_6229/
   trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042_6229/JBIDE-5042-6229.html
   trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/DnD_JBIDE5042_JBIDE6229_Test.java
   trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/AbstractDraggableFragment.java
   trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DraggableElement.java
   trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DraggableTextSelection.java
   trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/IDraggableFragment.java
Removed:
   trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042/
   trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042_6229/JBIDE-5042.html
   trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/DnD_JBIDE5042Test.java
Modified:
   trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/JsfAllTests.java
   trunk/vpe/plugins/org.jboss.tools.vpe.xulrunner/src/org/jboss/tools/vpe/xulrunner/util/XPCOM.java
   trunk/vpe/plugins/org.jboss.tools.vpe.xulrunner/src/org/jboss/tools/vpe/xulrunner/util/XulRunnerVpeUtils.java
   trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DraggablePattern.java
   trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/VpeDnD.java
   trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/VpeController.java
   trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/SelectionManager.java
Log:
https://jira.jboss.org/browse/JBIDE-6229
Implement DnD support for selected text in VPE
- DnD for selected text has been implemented
- JUnits have been added
- Method XPCOM.printAllContractIDs has been created

Copied: trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042_6229 (from rev 22595, trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042)

Copied: trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042_6229/JBIDE-5042-6229.html (from rev 22595, trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042/JBIDE-5042.html)
===================================================================
--- trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042_6229/JBIDE-5042-6229.html	                        (rev 0)
+++ trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042_6229/JBIDE-5042-6229.html	2010-06-07 17:01:26 UTC (rev 22604)
@@ -0,0 +1,33 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head id="head-1">
+<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
+<title>Insert title here</title>
+<style type="text/css">
+table {
+	background-color: black;
+	table-layout: fixed;
+}
+td {
+	background-color: white;
+}
+td div {
+	width: 100px;
+	height: 100px;
+}
+</style>
+</head>
+<body>
+	<table>
+		<tr>
+			<td><div><input id="draggableButton" type="button" value="DraggableBn" style=" width : 85px;"><span id="draggableTextContainer">Draggable Text</span></div></td><td><div id="cell_01">dddddd</div></td><td><div>dddddd</div></td><td><div>dddddd</div></td>
+		</tr>
+		<tr>
+			<td><div>dddddd</div></td><td><div>dddddd</div></td><td><div>dddddd</div></td><td><div>dddddd</div></td>
+		</tr>
+		<tr>
+			<td><div>dddddd</div></td><td><div>dddddd</div></td><td><div id="dropable	">Dropable</div></td><td><div>dddddd</div></td>
+		</tr>
+	</table>
+</body>
+</html>
\ No newline at end of file

Deleted: trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042_6229/JBIDE-5042.html
===================================================================
--- trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042/JBIDE-5042.html	2010-06-07 08:44:31 UTC (rev 22595)
+++ trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/5042_6229/JBIDE-5042.html	2010-06-07 17:01:26 UTC (rev 22604)
@@ -1,33 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-<html>
-<head id="head-1">
-<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
-<title>Insert title here</title>
-<style type="text/css">
-table {
-	background-color: black;
-	table-layout: fixed;
-}
-td {
-	background-color: white;
-}
-td div {
-	width: 100px;
-	height: 100px;
-}
-</style>
-</head>
-<body>
-	<table>
-		<tr>
-			<td><div><input id="draggable" type="button" value="Draggable" style=" width : 85px;">dddddd</div></td><td><div id="cell_01">dddddd</div></td><td><div>dddddd</div></td><td><div>dddddd</div></td>
-		</tr>
-		<tr>
-			<td><div>dddddd</div></td><td><div>dddddd</div></td><td><div>dddddd</div></td><td><div>dddddd</div></td>
-		</tr>
-		<tr>
-			<td><div>dddddd</div></td><td><div>dddddd</div></td><td><div id="dropable	">Dropable</div></td><td><div>dddddd</div></td>
-		</tr>
-	</table>
-</body>
-</html>
\ No newline at end of file

Modified: trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/JsfAllTests.java
===================================================================
--- trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/JsfAllTests.java	2010-06-07 15:08:21 UTC (rev 22603)
+++ trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/JsfAllTests.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -15,7 +15,7 @@
 
 import org.jboss.tools.jsf.vpe.jsf.test.jbide.ChangeMessageBundleTest_JBIDE5818;
 import org.jboss.tools.jsf.vpe.jsf.test.jbide.ContextMenuDoubleInsertionTest_JBIDE3888;
-import org.jboss.tools.jsf.vpe.jsf.test.jbide.DnD_JBIDE5042Test;
+import org.jboss.tools.jsf.vpe.jsf.test.jbide.DnD_JBIDE5042_JBIDE6229_Test;
 import org.jboss.tools.jsf.vpe.jsf.test.jbide.EditFontFamilyTest_JBIDE5872;
 import org.jboss.tools.jsf.vpe.jsf.test.jbide.ExceptionInVPEComments_JBIDE5143;
 import org.jboss.tools.jsf.vpe.jsf.test.jbide.FacetProcessingTest;
@@ -125,7 +125,7 @@
 
 		TestSuite suite = new TestSuite("Tests for Vpe Jsf components"); //$NON-NLS-1$
 		// $JUnit-BEGIN$
-		suite.addTestSuite(DnD_JBIDE5042Test.class);
+		suite.addTestSuite(DnD_JBIDE5042_JBIDE6229_Test.class);
 		suite.addTestSuite(UnclosedELExpressionTest.class);
 		suite.addTestSuite(TestContextPathResolution.class);
 		suite.addTestSuite(JBIDE5920Test.class);

Deleted: trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/DnD_JBIDE5042Test.java
===================================================================
--- trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/DnD_JBIDE5042Test.java	2010-06-07 15:08:21 UTC (rev 22603)
+++ trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/DnD_JBIDE5042Test.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -1,198 +0,0 @@
-/*******************************************************************************
-  * Copyright (c) 2007-2009 Red Hat, Inc.
-  * Distributed under license by Red Hat, Inc. All rights reserved.
-  * This program is made available under the terms of the
-  * Eclipse Public License v1.0 which accompanies this distribution,
-  * and is available at http://www.eclipse.org/legal/epl-v10.html
-  *
-  * Contributor:
-  *     Red Hat, Inc. - initial API and implementation
-  ******************************************************************************/
-package org.jboss.tools.jsf.vpe.jsf.test.jbide;
-
-import java.io.IOException;
-import java.lang.reflect.Field;
-
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.swt.graphics.Point;
-import org.eclipse.ui.IEditorInput;
-import org.eclipse.ui.PartInitException;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.part.FileEditorInput;
-import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
-import org.jboss.tools.jsf.vpe.jsf.test.JsfAllTests;
-import org.jboss.tools.jst.jsp.jspeditor.JSPMultiPageEditor;
-import org.jboss.tools.vpe.dnd.VpeDnD;
-import org.jboss.tools.vpe.editor.VpeController;
-import org.jboss.tools.vpe.editor.VpeEditorPart;
-import org.jboss.tools.vpe.editor.mozilla.MozillaEditor;
-import org.jboss.tools.vpe.editor.mozilla.MozillaEventAdapter;
-import org.jboss.tools.vpe.ui.test.TestUtil;
-import org.jboss.tools.vpe.ui.test.VpeTest;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
-import org.jmock.api.Invocation;
-import org.jmock.lib.action.CustomAction;
-import org.mozilla.interfaces.nsIDOMElement;
-import org.mozilla.interfaces.nsIDOMEventTarget;
-import org.mozilla.interfaces.nsIDOMMouseEvent;
-import org.mozilla.interfaces.nsIDOMNSUIEvent;
-import org.mozilla.interfaces.nsIDOMNode;
-import org.mozilla.interfaces.nsIDragService;
-import org.mozilla.interfaces.nsIDragSession;
-import org.mozilla.interfaces.nsIScriptableRegion;
-import org.mozilla.interfaces.nsISupportsArray;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-
-/**
- * Tests Drag&Drop functionality of the VPE.
- * 
- * @see JIRA Issue JBIDE-5042 ( https://jira.jboss.org/jira/browse/JBIDE-5042 ):
- * "Enhance DnD support in VPE"
- * 
- * @author yradtsevich
- */
-public class DnD_JBIDE5042Test  extends VpeTest {
-	private static final String DROP_CONTAINER_ID = "cell_01";
-	private static final String DRAG_ICON_ID = "dragIcon";
-	private static final String DRAGGABLE_ID = "draggable"; //$NON-NLS-1$
-	private static final String TEST_PAGE_NAME = "JBIDE/5042/JBIDE-5042.html"; //$NON-NLS-1$
-	private static final Point DRAG_POINT = new Point(0, 0);
-	/**Cells in the table are 100x100px. Thus this point means 'top of the second cell'*/
-	private static final Point DROP_POINT = new Point(150, 10);
-	private Mockery context = new Mockery();
-
-	public DnD_JBIDE5042Test(String name) {
-		super(name);
-	}
-	
-	/**
-	 * Try to open two pages in VPE and refresh them n times.
-	 */
-	public void testDnDWithMocks() throws Throwable {
-		setException(null);
-
-		JSPMultiPageEditor editor = openPageInVpe(TEST_PAGE_NAME);
-		final MozillaEditor visualEditor = ((VpeEditorPart) editor.getVisualEditor())
-				.getVisualEditor();
-		VpeController controller = TestUtil.getVpeController(editor);
-		TestUtil.waitForJobs();
-		
-		final nsIDragService dragService = mock(nsIDragService.class);
-		final nsIDragSession dragSession = mock(nsIDragSession.class);
-		checking(new Expectations() {{
-			allowing(dragService).getCurrentSession(); will(returnValue(dragSession));
-			allowing(dragSession).getSourceDocument(); will(returnValue(visualEditor.getDomDocument()));
-			allowing(dragSession).setCanDrop(with(any(Boolean.TYPE)));
-		}});
-		replaceDragService(controller.getVpeDnD(), dragService);
-
-		Element draggable = findSourceElementById(controller, DRAGGABLE_ID);
-		setSelectedNode(controller, draggable);
-		TestUtil.waitForJobs();
-
-		final nsIDOMElement dragIcon = controller.getXulRunnerEditor()
-				.getDOMDocument().getElementById(DRAG_ICON_ID);
-
-		final nsIDOMMouseEvent mouseDownEvent = createMockMouseEvent(
-				DRAG_POINT, "mousedown", dragIcon, "mouseDown");
-		final nsIDOMMouseEvent dragOverMouseEvent = createMockMouseEvent(
-				DROP_POINT, "dragover", null, "dragover");
-		final nsIDOMMouseEvent dragDropMouseEvent = createMockMouseEvent(
-				DROP_POINT, "dragdrop", null, "dragdrop");
-
-		final MozillaEventAdapter eventListener = visualEditor.getMozillaEventAdapter();
-		checking(new Expectations() {{
-			allowing(dragService).invokeDragSession(
-					with(any(nsIDOMNode.class)), with(any(nsISupportsArray.class)),
-					with(any(nsIScriptableRegion.class)), with(any(Long.TYPE)));
-			will(new CustomAction("invokeDragSession") {
-				public Object invoke(Invocation invocation) throws Throwable {
-					eventListener.handleEvent(dragOverMouseEvent);
-					TestUtil.waitForJobs();
-					eventListener.handleEvent(dragDropMouseEvent);
-					TestUtil.waitForJobs();
-					return null;
-				}
-			});
-		}});
-					
-		eventListener.handleEvent(mouseDownEvent);
-		TestUtil.waitForJobs();
-		
-		draggable = findSourceElementById(controller, DRAGGABLE_ID);
-		assertEquals(DROP_CONTAINER_ID, ((Element)draggable.getParentNode()).getAttribute("id"));
-
-		if (getException() != null) {
-			throw getException();
-		}
-	}
-
-	private nsIDOMMouseEvent createMockMouseEvent(final Point mousePos,
-			final String type, final nsIDOMElement targetElement, String name) {
-		final nsIDOMMouseEvent mouseEvent = mock(nsIDOMMouseEvent.class, name + "_nsIDOMMouseEvent");
-		final nsIDOMEventTarget mouseEventTarget = mock(nsIDOMEventTarget.class, name + "_nsIDOMEventTarget");
-		final nsIDOMNSUIEvent mouseNsUIEvent = mock(nsIDOMNSUIEvent.class, name + "_nsIDOMNSUIEvent");
-		
-		checking(new Expectations() {{
-			allowing(mouseEvent).getType(); will(returnValue(type));
-			allowing(mouseEvent).getButton(); will(returnValue(VpeController.LEFT_BUTTON));
-			allowing(mouseEvent).getTarget(); will(returnValue(mouseEventTarget));
-			allowing(mouseEvent).queryInterface(nsIDOMMouseEvent.NS_IDOMMOUSEEVENT_IID); will(returnValue(mouseEvent));
-			allowing(mouseEventTarget).queryInterface(nsIDOMElement.NS_IDOMELEMENT_IID); will(returnValue(targetElement));
-			allowing(mouseEvent).queryInterface(nsIDOMNSUIEvent.NS_IDOMNSUIEVENT_IID); will(returnValue(mouseNsUIEvent));
-			allowing(mouseEvent).getClientX(); will(returnValue(mousePos.x));
-			allowing(mouseEvent).getClientY(); will(returnValue(mousePos.y));
-			allowing(mouseNsUIEvent).getPageX(); will(returnValue(mousePos.x));
-			allowing(mouseNsUIEvent).getPageY(); will(returnValue(mousePos.y));
-			allowing(mouseEvent).stopPropagation();
-			allowing(mouseEvent).preventDefault();
-		}});
-
-		return mouseEvent;
-	}
-
-	private void replaceDragService(VpeDnD vpeDnD, nsIDragService dragService) throws Throwable {
-		Field dragServiceField = vpeDnD.getClass().getDeclaredField("dragService");
-		dragServiceField.setAccessible(true);
-		dragServiceField.set(vpeDnD, dragService);
-	}
-
-	private JSPMultiPageEditor openPageInVpe(final String pageName) throws CoreException,
-			PartInitException, IOException {
-		IFile elementPageFile = (IFile) TestUtil.getComponentPath(
-				pageName, JsfAllTests.IMPORT_PROJECT_NAME);
-		IEditorInput input = new FileEditorInput(elementPageFile);
-
-		JSPMultiPageEditor editor = (JSPMultiPageEditor) PlatformUI.getWorkbench()
-				.getActiveWorkbenchWindow().getActivePage().openEditor(input,
-						EDITOR_ID, true);
-
-		return editor;
-	}
-	
-	private void setSelectedNode(VpeController controller, Node node) {
-		IndexedRegion sourceNodeBounds = ((IndexedRegion)node);
-		
-		controller.getPageContext().getSourceBuilder().getStructuredTextViewer()
-				.setSelectedRange(sourceNodeBounds.getStartOffset(),
-						sourceNodeBounds.getEndOffset() - sourceNodeBounds.getStartOffset());
-	}
-
-	/** @see org.jmock.Mockery#mock(java.lang.Class, java.lang.String) */
-	public <T> T mock(Class<T> typeToMock, String name) {
-		return context.mock(typeToMock, name);
-	}
-
-	/** @see org.jmock.Mockery#mock(java.lang.Class) */
-	public <T> T mock(Class<T> typeToMock) {
-		return context.mock(typeToMock);
-	}
-
-	/** @see org.jmock.Mockery#checking(Expectations) */
-	public void checking(Expectations expectations) {
-		context.checking(expectations);
-	}
-}

Copied: trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/DnD_JBIDE5042_JBIDE6229_Test.java (from rev 22450, trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/DnD_JBIDE5042Test.java)
===================================================================
--- trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/DnD_JBIDE5042_JBIDE6229_Test.java	                        (rev 0)
+++ trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/DnD_JBIDE5042_JBIDE6229_Test.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -0,0 +1,242 @@
+/*******************************************************************************
+  * Copyright (c) 2007-2009 Red Hat, Inc.
+  * Distributed under license by Red Hat, Inc. All rights reserved.
+  * This program is made available under the terms of the
+  * Eclipse Public License v1.0 which accompanies this distribution,
+  * and is available at http://www.eclipse.org/legal/epl-v10.html
+  *
+  * Contributor:
+  *     Red Hat, Inc. - initial API and implementation
+  ******************************************************************************/
+package org.jboss.tools.jsf.vpe.jsf.test.jbide;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.part.FileEditorInput;
+import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
+import org.jboss.tools.jsf.vpe.jsf.test.JsfAllTests;
+import org.jboss.tools.jst.jsp.jspeditor.JSPMultiPageEditor;
+import org.jboss.tools.vpe.dnd.VpeDnD;
+import org.jboss.tools.vpe.editor.VpeController;
+import org.jboss.tools.vpe.editor.VpeEditorPart;
+import org.jboss.tools.vpe.editor.mozilla.MozillaEditor;
+import org.jboss.tools.vpe.editor.mozilla.MozillaEventAdapter;
+import org.jboss.tools.vpe.ui.test.TestUtil;
+import org.jboss.tools.vpe.ui.test.VpeTest;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.api.Invocation;
+import org.jmock.lib.action.CustomAction;
+import org.mozilla.interfaces.nsIDOMElement;
+import org.mozilla.interfaces.nsIDOMEventTarget;
+import org.mozilla.interfaces.nsIDOMMouseEvent;
+import org.mozilla.interfaces.nsIDOMNSUIEvent;
+import org.mozilla.interfaces.nsIDOMNode;
+import org.mozilla.interfaces.nsIDragService;
+import org.mozilla.interfaces.nsIDragSession;
+import org.mozilla.interfaces.nsIScriptableRegion;
+import org.mozilla.interfaces.nsISupportsArray;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+
+/**
+ * Tests Drag&Drop functionality of the VPE.
+ * 
+ * @see JIRA Issue JBIDE-5042 ( https://jira.jboss.org/jira/browse/JBIDE-5042 ):
+ * "Enhance DnD support in VPE"
+ * 
+ * @author yradtsevich
+ */
+ at SuppressWarnings("nls")
+public class DnD_JBIDE5042_JBIDE6229_Test  extends VpeTest {
+	private static final String DROP_CONTAINER_ID = "cell_01";
+	private static final String DRAG_ICON_ID = "dragIcon";
+	private static final String DRAGGABLE_BUTTON_ID = "draggableButton";
+	private static final String DRAGGABLE_TEXT_CONTAINER_ID = "draggableTextContainer";
+	private static final String TEST_PAGE_NAME = "JBIDE/5042_6229/JBIDE-5042-6229.html";
+	private static final String DND_TEXT = "Text";
+	private static final Point DRAG_POINT = new Point(0, 0);
+	/**Cells in the table are 100x100px. Thus this point means 'top of the second cell'*/
+	private static final Point DROP_POINT = new Point(150, 10);
+	private Mockery context = new Mockery();
+
+	public DnD_JBIDE5042_JBIDE6229_Test(String name) {
+		super(name);
+	}
+	
+	/**
+	 * Try to open two pages in VPE and refresh them n times.
+	 */
+	public void testElementDnDWithMocks() throws Throwable {
+		setException(null);
+
+		JSPMultiPageEditor editor = openPageInVpe(TEST_PAGE_NAME);
+		final MozillaEditor visualEditor = ((VpeEditorPart) editor.getVisualEditor())
+				.getVisualEditor();
+		VpeController controller = TestUtil.getVpeController(editor);
+		TestUtil.waitForJobs();
+		
+		Element draggable = findSourceElementById(controller, DRAGGABLE_BUTTON_ID);
+		IndexedRegion region = (IndexedRegion) draggable;
+		setSelectedRange(controller, region.getStartOffset(),
+				region.getEndOffset() - region.getStartOffset());
+		TestUtil.waitForJobs();
+		
+		executeSelectionDragAndDropToSecondCell(visualEditor, controller);
+		
+		draggable = findSourceElementById(controller, DRAGGABLE_BUTTON_ID);
+		assertEquals(DROP_CONTAINER_ID, ((Element)draggable.getParentNode()).getAttribute("id"));
+
+		if (getException() != null) {
+			throw getException();
+		}
+	}
+
+	public void testTextDnDWithMocks() throws Throwable {
+		setException(null);
+	
+		JSPMultiPageEditor editor = openPageInVpe(TEST_PAGE_NAME);
+		final MozillaEditor visualEditor = ((VpeEditorPart) editor.getVisualEditor())
+				.getVisualEditor();
+		VpeController controller = TestUtil.getVpeController(editor);
+		TestUtil.waitForJobs();
+		
+		Element draggableTextContainer = findSourceElementById(controller, DRAGGABLE_TEXT_CONTAINER_ID);
+		Text draggableTextNode = (Text) draggableTextContainer.getChildNodes().item(0);
+		IndexedRegion draggableTextRegion = (IndexedRegion)draggableTextNode;
+		setSelectedRange(controller,
+				draggableTextRegion.getEndOffset() - DND_TEXT.length(), DND_TEXT.length());
+		TestUtil.waitForJobs();
+		
+		executeSelectionDragAndDropToSecondCell(visualEditor, controller);
+		
+		Element dropContainer = findSourceElementById(controller, DROP_CONTAINER_ID);
+		NodeList dropContainerChildren = dropContainer.getChildNodes();
+		assertTrue(dropContainerChildren.getLength() == 1);
+		assertTrue(dropContainerChildren.item(0) instanceof Text);
+		
+		String dropContainerContent = ((Text)dropContainerChildren.item(0)).getNodeValue();
+
+		assertEquals(DND_TEXT + "dddddd", dropContainerContent);
+
+		if (getException() != null) {
+			throw getException();
+		}
+	}
+
+	private void executeSelectionDragAndDropToSecondCell(
+			final MozillaEditor visualEditor, VpeController controller)
+			throws Throwable {
+		final nsIDragService dragService = mock(nsIDragService.class);
+		final nsIDragSession dragSession = mock(nsIDragSession.class);
+		checking(new Expectations() {{
+			allowing(dragService).getCurrentSession(); will(returnValue(dragSession));
+			allowing(dragSession).getSourceDocument(); will(returnValue(visualEditor.getDomDocument()));
+			allowing(dragSession).setCanDrop(with(any(Boolean.TYPE)));
+		}});
+		replaceDragService(controller.getVpeDnD(), dragService);
+	
+		final nsIDOMElement dragIcon = controller.getXulRunnerEditor()
+				.getDOMDocument().getElementById(DRAG_ICON_ID);
+	
+		final nsIDOMMouseEvent mouseDownEvent = createMockMouseEvent(
+				DRAG_POINT, "mousedown", dragIcon, "mouseDown");
+		final nsIDOMMouseEvent dragOverMouseEvent = createMockMouseEvent(
+				DROP_POINT, "dragover", null, "dragover");
+		final nsIDOMMouseEvent dragDropMouseEvent = createMockMouseEvent(
+				DROP_POINT, "dragdrop", null, "dragdrop");
+	
+		final MozillaEventAdapter eventListener = visualEditor.getMozillaEventAdapter();
+		checking(new Expectations() {{
+			allowing(dragService).invokeDragSession(
+					with(any(nsIDOMNode.class)), with(any(nsISupportsArray.class)),
+					with(any(nsIScriptableRegion.class)), with(any(Long.TYPE)));
+			will(new CustomAction("invokeDragSession") {
+				public Object invoke(Invocation invocation) throws Throwable {
+					eventListener.handleEvent(dragOverMouseEvent);
+					TestUtil.waitForJobs();
+					eventListener.handleEvent(dragDropMouseEvent);
+					TestUtil.waitForJobs();
+					return null;
+				}
+			});
+		}});
+					
+		eventListener.handleEvent(mouseDownEvent);
+		TestUtil.waitForJobs();
+		TestUtil.delay(100);
+		TestUtil.waitForJobs();
+	}
+
+	private nsIDOMMouseEvent createMockMouseEvent(final Point mousePos,
+			final String type, final nsIDOMElement targetElement, String name) {
+		final nsIDOMMouseEvent mouseEvent = mock(nsIDOMMouseEvent.class, name + "_nsIDOMMouseEvent");
+		final nsIDOMEventTarget mouseEventTarget = mock(nsIDOMEventTarget.class, name + "_nsIDOMEventTarget");
+		final nsIDOMNSUIEvent mouseNsUIEvent = mock(nsIDOMNSUIEvent.class, name + "_nsIDOMNSUIEvent");
+		
+		checking(new Expectations() {{
+			allowing(mouseEvent).getType(); will(returnValue(type));
+			allowing(mouseEvent).getButton(); will(returnValue(VpeController.LEFT_BUTTON));
+			allowing(mouseEvent).getTarget(); will(returnValue(mouseEventTarget));
+			allowing(mouseEvent).queryInterface(nsIDOMMouseEvent.NS_IDOMMOUSEEVENT_IID); will(returnValue(mouseEvent));
+			allowing(mouseEventTarget).queryInterface(nsIDOMElement.NS_IDOMELEMENT_IID); will(returnValue(targetElement));
+			allowing(mouseEvent).queryInterface(nsIDOMNSUIEvent.NS_IDOMNSUIEVENT_IID); will(returnValue(mouseNsUIEvent));
+			allowing(mouseEvent).getClientX(); will(returnValue(mousePos.x));
+			allowing(mouseEvent).getClientY(); will(returnValue(mousePos.y));
+			allowing(mouseNsUIEvent).getPageX(); will(returnValue(mousePos.x));
+			allowing(mouseNsUIEvent).getPageY(); will(returnValue(mousePos.y));
+			allowing(mouseEvent).stopPropagation();
+			allowing(mouseEvent).preventDefault();
+		}});
+
+		return mouseEvent;
+	}
+
+	private void replaceDragService(VpeDnD vpeDnD, nsIDragService dragService) throws Throwable {
+		Field dragServiceField = vpeDnD.getClass().getDeclaredField("dragService");
+		dragServiceField.setAccessible(true);
+		dragServiceField.set(vpeDnD, dragService);
+	}
+
+	private JSPMultiPageEditor openPageInVpe(final String pageName) throws CoreException,
+			PartInitException, IOException {
+		IFile elementPageFile = (IFile) TestUtil.getComponentPath(
+				pageName, JsfAllTests.IMPORT_PROJECT_NAME);
+		IEditorInput input = new FileEditorInput(elementPageFile);
+
+		JSPMultiPageEditor editor = (JSPMultiPageEditor) PlatformUI.getWorkbench()
+				.getActiveWorkbenchWindow().getActivePage().openEditor(input,
+						EDITOR_ID, true);
+
+		return editor;
+	}
+	
+	private void setSelectedRange(VpeController controller, int offset, int length) {
+		controller.getPageContext().getSourceBuilder().getStructuredTextViewer()
+				.setSelectedRange(offset, length);
+	}
+
+	/** @see org.jmock.Mockery#mock(java.lang.Class, java.lang.String) */
+	public <T> T mock(Class<T> typeToMock, String name) {
+		return context.mock(typeToMock, name);
+	}
+
+	/** @see org.jmock.Mockery#mock(java.lang.Class) */
+	public <T> T mock(Class<T> typeToMock) {
+		return context.mock(typeToMock);
+	}
+
+	/** @see org.jmock.Mockery#checking(Expectations) */
+	public void checking(Expectations expectations) {
+		context.checking(expectations);
+	}
+}

Added: trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/AbstractDraggableFragment.java
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/AbstractDraggableFragment.java	                        (rev 0)
+++ trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/AbstractDraggableFragment.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -0,0 +1,45 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2009 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ *     Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.vpe.dnd;
+
+import org.jboss.tools.vpe.editor.util.HTML;
+import org.jboss.tools.vpe.editor.util.VpeStyleUtil;
+import org.mozilla.interfaces.nsIDOMCSSStyleDeclaration;
+import org.mozilla.interfaces.nsIDOMElement;
+
+/**
+ * @author Yahor Radtsevich (yradtsevich)
+ *
+ */
+public abstract class AbstractDraggableFragment implements IDraggableFragment {
+	private static final String DRAGGING_OPACITY = "0.5";		  //$NON-NLS-1$
+
+	/* (non-Javadoc)
+	 * @see org.jboss.tools.vpe.dnd.IDraggableFragment#createRepresentation()
+	 */
+	public final nsIDOMElement createRepresentation() {
+		nsIDOMElement representation = cloneFragmentAsElement();
+		
+		DndUtil.setTemporaryDndElement(representation, true);
+
+		nsIDOMCSSStyleDeclaration representationStyle
+				= VpeStyleUtil.getStyle(representation);
+		representationStyle.setProperty(HTML.STYLE_PARAMETER_POSITION,
+				HTML.STYLE_VALUE_ABSOLUTE, HTML.STYLE_PRIORITY_IMPORTANT);
+		representationStyle.setProperty(HTML.STYLE_PARAMETER_OPACITY,
+				DRAGGING_OPACITY, HTML.STYLE_PRIORITY_IMPORTANT);
+		VpeStyleUtil.setElementVisible(representation, false);
+		
+		return representation;
+	}
+	
+	public abstract nsIDOMElement cloneFragmentAsElement();
+}

Added: trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DraggableElement.java
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DraggableElement.java	                        (rev 0)
+++ trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DraggableElement.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2009 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ *     Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.vpe.dnd;
+
+import static org.jboss.tools.vpe.xulrunner.util.XPCOM.queryInterface;
+
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
+import org.jboss.tools.vpe.xulrunner.util.XulRunnerVpeUtils;
+import org.mozilla.interfaces.nsIDOMElement;
+
+/**
+ * @author Yahor Radtsevich (yradtsevich)
+ *
+ */
+public class DraggableElement extends AbstractDraggableFragment {
+	private final nsIDOMElement element;
+
+	public DraggableElement(nsIDOMElement element) {
+		this.element = element;
+	}
+	
+	/* (non-Javadoc)
+	 * @see org.jboss.tools.vpe.dnd.IDraggableFragment#createRepresentation()
+	 */
+	public nsIDOMElement cloneFragmentAsElement() {
+		nsIDOMElement representation
+				= queryInterface(element.cloneNode(true), nsIDOMElement.class);
+		element.getParentNode().appendChild(representation);
+
+		return representation;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.jboss.tools.vpe.dnd.IDraggableFragment#getPosition()
+	 */
+	public Point getPosition() {
+		Rectangle bounds = XulRunnerVpeUtils.getElementBounds(element);
+		return new Point(bounds.x, bounds.y);
+	}
+
+}

Modified: trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DraggablePattern.java
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DraggablePattern.java	2010-06-07 15:08:21 UTC (rev 22603)
+++ trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DraggablePattern.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -12,6 +12,7 @@
 
 import static org.jboss.tools.vpe.xulrunner.util.XPCOM.queryInterface;
 
+import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.graphics.Rectangle;
 import org.jboss.tools.vpe.editor.mozilla.MozillaEditor;
 import org.jboss.tools.vpe.editor.util.HTML;
@@ -28,12 +29,9 @@
  * @author Yahor Radtsevich (yradtsevich)
  */
 public class DraggablePattern {
-	private static final String DRAGGING_OPACITY = "0.5";		  //$NON-NLS-1$
 	private static final int ICON_HEIGHT = 20;
 	private static final String DRAG_ICON_ID = "dragIcon";		  //$NON-NLS-1$
 	private static final String DRAG_ICON_FILE = "dragIcon.gif";  //$NON-NLS-1$
-	private static final String ABSOLUTE_POSITION = "absolute";	  //$NON-NLS-1$
-	private static final String IMPORTANT_PRIORITY = "important"; //$NON-NLS-1$
 	
 	/** @see #getTransparentDiv() */
 	private static final String TRANSPARENT_DIV_ID = "transparentDragDiv"; //$NON-NLS-1$
@@ -53,8 +51,8 @@
 	private int offsetX;
 	private int offsetY;
 	private boolean sessionStarted;
-	private nsIDOMElement nodeCopy;
-	private nsIDOMElement node;
+	private nsIDOMElement draggableFragmentRepresentation;
+	private IDraggableFragment draggableFragment;
 	private final MozillaEditor mozillaEditor;
 
 	public DraggablePattern(MozillaEditor mozillaEditor) {
@@ -62,17 +60,17 @@
 		this.mozillaEditor = mozillaEditor;
 	}
 
-	public void showDragIcon(nsIDOMElement element) {
-		this.node = element;
-		Rectangle bounds = XulRunnerVpeUtils.getElementBounds(element);
+	public void showDragIcon(IDraggableFragment draggableFragment) {
+		this.draggableFragment = draggableFragment;
+		Point position = draggableFragment.getPosition();
 		nsIDOMElement dragIcon = getDragIcon();
 
 		VpeStyleUtil.setElementVisible(dragIcon, true);
-		VpeStyleUtil.moveElementTo(dragIcon, bounds.x, bounds.y - ICON_HEIGHT);
+		VpeStyleUtil.moveElementTo(dragIcon, position.x, position.y - ICON_HEIGHT);
 	}
 
 	public void hideDragIcon() {
-		this.node = null;
+		this.draggableFragment = null;
 		VpeStyleUtil.setElementVisible(getDragIcon(), false);
 	}
 
@@ -124,40 +122,24 @@
 		}
 	}
 
-	public nsIDOMNode getNode() {
-		return node;
-	}
-
 	public void startSession(int mouseStartX, int mouseStartY) {
 		if (sessionStarted) {
 			new IllegalStateException(
 					"Session is already started."); //$NON-NLS-1$
 		}
-		if (node == null) {
+		if (draggableFragment == null) {
 			new IllegalStateException(
 			"No node to drag."); //$NON-NLS-1$
 		}
 
-		Rectangle nodeBounds = XulRunnerVpeUtils.getElementBounds(node);
-		offsetX = nodeBounds.x - mouseStartX;
-		offsetY = nodeBounds.y - mouseStartY;
+		Point fragmentPosition = draggableFragment.getPosition();
+		offsetX = fragmentPosition.x - mouseStartX;
+		offsetY = fragmentPosition.y - mouseStartY;
 		
-		nodeCopy = queryInterface(node.cloneNode(true), nsIDOMElement.class);
-		if (nodeCopy.getNodeType() == nsIDOMNode.ELEMENT_NODE) {
-			nsIDOMElement elementCopy = queryInterface(nodeCopy, nsIDOMElement.class);
-			DndUtil.setTemporaryDndElement(elementCopy, true);
-		}
+		draggableFragmentRepresentation = draggableFragment.createRepresentation();
 
-		nsIDOMCSSStyleDeclaration nodeCopyStyle
-				= VpeStyleUtil.getStyle(nodeCopy);
-
-		nodeCopyStyle.setProperty(HTML.STYLE_PARAMETER_POSITION,
-				ABSOLUTE_POSITION, IMPORTANT_PRIORITY);
-		nodeCopyStyle.setProperty(HTML.STYLE_PARAMETER_OPACITY,
-				DRAGGING_OPACITY, IMPORTANT_PRIORITY);
-
-		setVisible(false);
-		node.getParentNode().appendChild(nodeCopy);
+		//TODO: remove the following line
+		//setVisible(false);
 		moveTo(mouseStartX, mouseStartY);
 		setVisible(true);
 		sessionStarted = true;
@@ -166,25 +148,25 @@
 	public void closeSession() {
 		if (sessionStarted) {
 			setVisible(false);
-			nsIDOMNode parent = nodeCopy.getParentNode();
+			nsIDOMNode parent = draggableFragmentRepresentation.getParentNode();
 			if (parent != null) {
-				parent.removeChild(nodeCopy);
+				parent.removeChild(draggableFragmentRepresentation);
 			}
 			
 			hideDragIcon();
-			nodeCopy = null;
+			draggableFragmentRepresentation = null;
 			sessionStarted = false;
 		}
 	}
 
 	public void setVisible(boolean visible) {
-		VpeStyleUtil.setElementVisible(nodeCopy, visible);
+		VpeStyleUtil.setElementVisible(draggableFragmentRepresentation, visible);
 		VpeStyleUtil.setElementVisible(getTransparentDiv(), visible);
 		VpeStyleUtil.setElementVisible(getDragIcon(), visible);
 	}
 
 	public void moveTo(int mouseX, int mouseY) {
-		VpeStyleUtil.moveElementTo(nodeCopy, offsetX + mouseX, offsetY + mouseY);
+		VpeStyleUtil.moveElementTo(draggableFragmentRepresentation, offsetX + mouseX, offsetY + mouseY);
 		VpeStyleUtil.moveElementTo(getTransparentDiv(),
 				offsetX + mouseX - TRANSPARENT_DIV_SIZE / 2,
 				offsetY + mouseY - TRANSPARENT_DIV_SIZE / 2);

Added: trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DraggableTextSelection.java
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DraggableTextSelection.java	                        (rev 0)
+++ trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DraggableTextSelection.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2009 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ *     Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.vpe.dnd;
+
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
+import org.jboss.tools.vpe.editor.util.VisualDomUtil;
+import org.jboss.tools.vpe.xulrunner.util.XulRunnerVpeUtils;
+import org.mozilla.interfaces.nsIDOMDocument;
+import org.mozilla.interfaces.nsIDOMElement;
+import org.mozilla.interfaces.nsIDOMNode;
+import org.mozilla.interfaces.nsIDOMText;
+
+/**
+ * @author Yahor Radtsevich (yradtsevich)
+ *
+ */
+public class DraggableTextSelection extends AbstractDraggableFragment {
+	private final nsIDOMText selectionContainer;
+	private final int startOffset;
+	private final int endOffset;
+
+	/**
+	 * NOTE: selectionContainer must contain really selected text. It will
+	 * not work if real selection range do not math offsets.
+	 */
+	public DraggableTextSelection(nsIDOMText selectionContainer,
+			int startOffset, int endOffset) {
+		this.selectionContainer = selectionContainer;
+		this.startOffset = startOffset;
+		this.endOffset = endOffset;
+	}
+	
+
+	/* (non-Javadoc)
+	 * @see org.jboss.tools.vpe.dnd.AbstractDraggableFragment#cloneFragmentAsElement()
+	 */
+	@Override
+	public nsIDOMElement cloneFragmentAsElement() {
+		nsIDOMDocument document = selectionContainer.getOwnerDocument();
+		
+		nsIDOMElement cloneContainer = VisualDomUtil.createBorderlessContainer(
+				document);
+		nsIDOMNode selectionContainerParent = selectionContainer.getParentNode();
+		selectionContainerParent.appendChild(cloneContainer);
+		String cloneText = selectionContainer.getData().substring(startOffset, endOffset);
+		nsIDOMText clone = document.createTextNode(cloneText);
+		cloneContainer.appendChild(clone);
+		
+		return cloneContainer;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.jboss.tools.vpe.dnd.IDraggableFragment#getPosition()
+	 */
+	public Point getPosition() {
+		Rectangle bounds = XulRunnerVpeUtils.getTextSelectionBounds(selectionContainer); 
+		return new Point(bounds.x, bounds.y);
+	}
+}

Added: trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/IDraggableFragment.java
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/IDraggableFragment.java	                        (rev 0)
+++ trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/IDraggableFragment.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -0,0 +1,23 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2009 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ *     Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.vpe.dnd;
+
+import org.eclipse.swt.graphics.Point;
+import org.mozilla.interfaces.nsIDOMElement;
+
+/**
+ * @author Yahor Radtsevich (yradtsevich)
+ *
+ */
+public interface IDraggableFragment {
+	public Point getPosition(); 
+	public nsIDOMElement createRepresentation();
+}

Modified: trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/VpeDnD.java
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/VpeDnD.java	2010-06-07 15:08:21 UTC (rev 22603)
+++ trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/VpeDnD.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -16,6 +16,7 @@
 import java.util.EnumSet;
 
 import org.eclipse.core.resources.IFile;
+import org.eclipse.swt.custom.StyledText;
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
@@ -40,6 +41,7 @@
 import org.jboss.tools.vpe.editor.mapping.VpeNodeMapping;
 import org.jboss.tools.vpe.editor.mozilla.MozillaEditor;
 import org.jboss.tools.vpe.editor.mozilla.listener.MozillaDndListener;
+import org.jboss.tools.vpe.editor.mozilla.listener.MozillaSelectionListener;
 import org.jboss.tools.vpe.editor.util.VisualDomUtil;
 import org.jboss.tools.vpe.editor.util.VpeDndUtil;
 import org.jboss.tools.vpe.xulrunner.editor.XulRunnerEditor;
@@ -51,8 +53,11 @@
 import org.mozilla.interfaces.nsIDOMMouseEvent;
 import org.mozilla.interfaces.nsIDOMNSUIEvent;
 import org.mozilla.interfaces.nsIDOMNode;
+import org.mozilla.interfaces.nsIDOMRange;
+import org.mozilla.interfaces.nsIDOMText;
 import org.mozilla.interfaces.nsIDragService;
 import org.mozilla.interfaces.nsIFile;
+import org.mozilla.interfaces.nsISelection;
 import org.mozilla.interfaces.nsIServiceManager;
 import org.mozilla.interfaces.nsISupports;
 import org.mozilla.interfaces.nsISupportsArray;
@@ -71,7 +76,7 @@
  */
 // NOTE: the code has been cleaned after SVN revision 21574, many methods
 // have been removed. To find the old code refer to older revisions.
-public class VpeDnD implements MozillaDndListener {
+public class VpeDnD implements MozillaDndListener, MozillaSelectionListener {
 	private static final String TAG_TAGLIB = "taglib"; //$NON-NLS-1$
 
 	/*
@@ -95,6 +100,11 @@
 
 	private VpeController vpeController;
 
+	/* TODO: yradtsevich: sourceInnerDragInfo is a state variable
+	 * that not always reflects actual state of the object
+	 * (e.g. during external Drag&Drop and text Drag&Drop).
+	 * It has to be removed from the class and corresponding
+	 * methods has to be refactored. */
 	private VpeSourceInnerDragInfo sourceInnerDragInfo = null;
 	private DraggablePattern draggablePattern;
 	private DropableArea dropableArea;
@@ -108,7 +118,7 @@
 		nsIDOMElement selectedElement = vpeController.getXulRunnerEditor()
 				.getLastSelectedElement();
 		// start drag sessionvpe-element
-		if (isDraggable(selectedElement)) {
+		if (isTextSelected(getVisualSelection()) || isDraggable(selectedElement)) {
 			Point pageCoords = getPageCoords(domEvent);
 			draggablePattern.startSession(pageCoords.x, pageCoords.y);
 			startDragSession(selectedElement);
@@ -129,7 +139,11 @@
 		
 		final DropResolver dropResolver;
 		if (isInnerDragSession()) {
-			dropResolver = getDropResolverForInternalDrop();
+			if (isTextSelected(getVisualSelection())) {
+				dropResolver = getDropResolverForNode(vpeController.getDomMapping().getNearSourceNode(getVisualSelection().getFocusNode()));
+			} else {
+				dropResolver = getDropResolverForInternalDrop();
+			}
 
 			Point mousePosition = getPageCoords(event);
 			draggablePattern.moveTo(mousePosition.x, mousePosition.y);
@@ -187,15 +201,59 @@
 	}
 
 	public void selectionChanged() {
-		nsIDOMElement selectedElement = vpeController.getXulRunnerEditor()
-				.getLastSelectedElement();
-		if (isDraggable(selectedElement)) {
-			draggablePattern.showDragIcon(selectedElement);
+		refreshDraggablePattern();
+	}
+
+	private void refreshDraggablePattern() {
+		nsISelection selection = getVisualSelection();
+		if (isTextSelected(selection)) {
+			nsIDOMRange range = selection.getRangeAt(0);
+			nsIDOMText textContainer = queryInterface(
+					range.getStartContainer(), nsIDOMText.class);
+			
+			draggablePattern.showDragIcon(new DraggableTextSelection(
+					textContainer, range.getStartOffset(), range.getEndOffset()));
 		} else {
-			draggablePattern.hideDragIcon();
+			nsIDOMElement selectedElement = vpeController.getXulRunnerEditor()
+					.getLastSelectedElement();
+
+			if (isDraggable(selectedElement)) {
+				draggablePattern.showDragIcon(new DraggableElement(selectedElement));
+			} else {
+				draggablePattern.hideDragIcon();
+			}
 		}
 	}
+	
+	private nsISelection getVisualSelection() {
+		return vpeController.getXulRunnerEditor().getWebBrowser()
+				.getContentDOMWindow().getSelection();
+	}
 
+	private boolean isTextSelected(nsISelection selection) {
+		if (selection.getRangeCount() == 0) {
+			// nothing selected
+			return false;
+		}
+
+		nsIDOMRange range = selection.getRangeAt(0);
+		nsIDOMNode container = range.getStartContainer();
+		if (!container.equals(range.getEndContainer())) {
+			// more than one node selected
+			return false;
+		}
+		if (container.getNodeType() != nsIDOMNode.TEXT_NODE) {
+			// not text node is selected
+			return false;
+		}
+		if (range.getStartOffset() == range.getEndOffset()) {
+			// no text selected
+			return false;
+		}
+		
+		return true;
+	}
+
 	public boolean isDragIconClicked(nsIDOMMouseEvent mouseEvent) {
 		return draggablePattern.isDragIconClicked(mouseEvent);
 	}
@@ -363,7 +421,7 @@
 		nsISupportsString transferData = (nsISupportsString) getComponentManager()
 		.createInstanceByContractID(XPCOM.NS_SUPPORTSSTRING_CONTRACTID, null,
 				nsISupportsString.NS_ISUPPORTSSTRING_IID);
-		String data=VPE_ELEMENT; 
+		String data=VPE_ELEMENT;
 		transferData.setData(data);
 		iTransferable.setTransferData(VpeController.MODEL_FLAVOR, transferData, data.length());
 		iTransferable.setTransferData("text/plain", transferData, data.length()); //$NON-NLS-1$
@@ -389,17 +447,20 @@
 
 	}
 
-	private void externalDropAny(final String flavor, final String data,
-			final int offset) {
-		StructuredTextEditor sourceEditor = vpeController.getSourceEditor();
-		if (flavor == null || flavor.length() == 0 
-				|| !(sourceEditor instanceof IDNDTextEditor)) {
-			return;
+	private void dropAny(final String flavor, final String data) {
+		VpeSourceDropInfo dropInfo = getDropInfo();
+		Point range = getSourceSelectionRange(dropInfo.getContainer(), dropInfo.getOffset());
+
+		if (dropInfo.getContainer() != null && data != null) {
+			StructuredTextEditor sourceEditor = vpeController.getSourceEditor();
+			if (flavor == null || flavor.length() == 0 
+					|| !(sourceEditor instanceof IDNDTextEditor)) {
+				return;
+			}
+			
+			sourceEditor.setHighlightRange(range.x, 0, true);
+			((IDNDTextEditor) sourceEditor).runDropCommand(flavor, data);
 		}
-		
-		sourceEditor.setHighlightRange(offset, 0, true);
-		((IDNDTextEditor) sourceEditor).runDropCommand(flavor, data);
-
 	}
 	
 	private boolean isInnerDragSession() {
@@ -465,8 +526,21 @@
 		if (VpeDebug.PRINT_VISUAL_INNER_DRAGDROP_EVENT) {
 			System.out.print("<<<<<< innerDrop"); //$NON-NLS-1$
 		}
-		if (sourceInnerDragInfo != null) {
-			VpeSourceDropInfo sourceDropInfo = getDropInfo(event);
+
+		/* TODO: yradtsevich: the code needs to be refactored.
+		 * See TODO comment for sourceInnerDragInfo */
+		if (isTextSelected(getVisualSelection())) {
+			// it is inner Drag&Drop of text
+			StyledText textWidget = vpeController.getSourceEditor()
+					.getTextViewer().getTextWidget();
+			String text = textWidget.getSelectionText();
+			
+			Point selectionRange = textWidget.getSelectionRange();
+			textWidget.replaceTextRange(selectionRange.x, selectionRange.y, ""); //$NON-NLS-1$
+			
+			dropAny(DndUtil.kUnicodeMime, text);
+		} else if (sourceInnerDragInfo != null) {
+			VpeSourceDropInfo sourceDropInfo = getDropInfo();
 			if (sourceDropInfo.getContainer() != null) {
 				if (VpeDebug.PRINT_VISUAL_INNER_DRAGDROP_EVENT) {
 					System.out
@@ -512,9 +586,7 @@
 	private void externalDrop(nsIDOMMouseEvent mouseEvent, String flavor, String data) {
 		vpeController.onHideTooltip();
 	
-		VpeSourceDropInfo dropInfo = getDropInfo(mouseEvent);
-		Point range = getSourceSelectionRange(
-				dropInfo.getContainer(), dropInfo.getOffset());
+
 	
 		final DragTransferData dragTransferData = DndUtil.getDragTransferData();
 		final nsISupports aValue = dragTransferData.getValue();
@@ -543,20 +615,7 @@
 			}
 		}
 
-		if (dropInfo.getContainer() != null && data != null) {
-			if (VpeDebug.PRINT_VISUAL_INNER_DRAGDROP_EVENT) {
-				System.out
-						.println("  drop!  container: " + dropInfo.getContainer().getNodeName()); //$NON-NLS-1$
-			}
-
-			externalDropAny(aFlavor, data, range.x);
-	
-			// TypedEvent tEvent = new TypedEvent(mouseEvent);
-			// tEvent.data = data;
-			// dropContext.setFlavor(aFlavor);
-			// dropContext.setMimeData(data);
-			// DnDUtil.fireDnDEvent(dropContext, textEditor, tEvent);
-		}
+		dropAny(aFlavor, data);
 	}
 
 	@SuppressWarnings("restriction")
@@ -607,9 +666,8 @@
 	private nsIComponentManager getComponentManager() {
 		
 		if(componentManager==null) {
-			
 			componentManager = Mozilla.getInstance()
-			.getComponentManager();
+					.getComponentManager();
 		}
 		return componentManager;
 	}
@@ -647,7 +705,7 @@
 		}
 	}
 	
-	private VpeSourceDropInfo getDropInfo(nsIDOMEvent event) {
+	private VpeSourceDropInfo getDropInfo() {
 		Node dropContainer = null;
 		int dropOffset = 0;
 		boolean canDrop = false;
@@ -685,6 +743,14 @@
 		
 		return new VpeSourceDropInfo(dropContainer, dropOffset, canDrop);
 	}
+	
+	/* (non-Javadoc)
+	 * @see org.jboss.tools.vpe.editor.mozilla.listener.MozillaSelectionListener#notifySelectionChanged(org.mozilla.interfaces.nsIDOMDocument, org.mozilla.interfaces.nsISelection, short)
+	 */
+	public void notifySelectionChanged(nsIDOMDocument domDocument,
+			nsISelection selection, short reason) {
+		refreshDraggablePattern();
+	}
 
 	private interface DropResolver {
 		public boolean canDrop(Node node);

Modified: trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/VpeController.java
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/VpeController.java	2010-06-07 15:08:21 UTC (rev 22603)
+++ trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/VpeController.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -460,6 +460,7 @@
 				mozillaEventAdapter.addKeyListener(this);
 				mozillaEventAdapter.addMouseListener(this);
 				mozillaEventAdapter.addSelectionListener(this);
+				mozillaEventAdapter.addSelectionListener(vpeDnD);
 				mozillaEventAdapter.addAfterPaintListener(this);
 				mozillaEventAdapter.addScrollListener(this);
 			}
@@ -479,6 +480,7 @@
 				mozillaEventAdapter.removeKeyListener(this);
 				mozillaEventAdapter.removeMouseListener(this);
 				mozillaEventAdapter.removeSelectionListener(this);
+				mozillaEventAdapter.removeSelectionListener(vpeDnD);
 				mozillaEventAdapter.removeAfterPaintListener(this);
 				mozillaEventAdapter.removeScrollListener(this);
 			}

Modified: trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/SelectionManager.java
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/SelectionManager.java	2010-06-07 15:08:21 UTC (rev 22603)
+++ trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/SelectionManager.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -234,10 +234,10 @@
 			selectionLength = NodesManagingUtil.getNodeLength(targetSourceNode);
 
 		}
-
+		
+		SelectionUtil.clearSelection(selectionController);
 		SelectionUtil.setSourceSelection(getPageContext(), targetSourceNode,
 				selectionOffset, selectionLength);
-
 		// paint selection rectangle
 		getPageContext().getVisualBuilder().setSelectionRectangle(
 				targetVisualNode);

Modified: trunk/vpe/plugins/org.jboss.tools.vpe.xulrunner/src/org/jboss/tools/vpe/xulrunner/util/XPCOM.java
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe.xulrunner/src/org/jboss/tools/vpe/xulrunner/util/XPCOM.java	2010-06-07 15:08:21 UTC (rev 22603)
+++ trunk/vpe/plugins/org.jboss.tools.vpe.xulrunner/src/org/jboss/tools/vpe/xulrunner/util/XPCOM.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -16,14 +16,22 @@
 import java.util.List;
 import java.util.Map;
 
+import org.mozilla.interfaces.nsIComponentManager;
+import org.mozilla.interfaces.nsIComponentRegistrar;
 import org.mozilla.interfaces.nsIDOMNode;
+import org.mozilla.interfaces.nsIServiceManager;
+import org.mozilla.interfaces.nsISimpleEnumerator;
 import org.mozilla.interfaces.nsISupports;
+import org.mozilla.interfaces.nsISupportsCString;
+import org.mozilla.xpcom.Mozilla;
 import org.mozilla.xpcom.XPCOMException;
 
 /**
  * @author Sergey Vasilyev (svasilyev at exadel.com): initial creation.
- * @author Yahor Radtsevich (yradtsevich): method {@code queryInterface} and
- * related stuff (JBIDE-6393).
+ * @author Yahor Radtsevich (yradtsevich): methods {@link #queryInterface},
+ * {@link #printAllContractIDs(Class)},
+ * {@link #printSupportedInterfaces(nsISupports, boolean)}
+ * and related stuff (JBIDE-6393).
  */
 public final class XPCOM {
 	private XPCOM() {}
@@ -79,7 +87,7 @@
 		String interfaceId = getInterfaceId(type);
 		return (T) object.queryInterface(interfaceId);
 	}
-
+	
 	/**
 	 * Returns XPCOM ID for the given {@code type}
 	 * <P>
@@ -163,7 +171,7 @@
 	  * 
 	  * @deprecated This method tries to query every known XPCOM interface
 	  * from given object and it performs very slow (seconds).
-	  * For debug/test purposes only. Do not use it in the production code.
+	  * For debug purposes only. Do not use it in the production code.
 	  */
 	public static void printSupportedInterfaces(nsISupports object, boolean printMethods) {
 		for (Class<? extends nsISupports> type : getSupportedInterfaces(object)) {
@@ -175,7 +183,57 @@
 			}
 		}
 	}
+	
+	/**
+	 * Prints all XPCOM interface ID for the given type
+	 * to the {@code System.out}.
+	 * 
+	 * @deprecated For debug purposes only.
+	 */
+	public static void printAllContractIDs(Class<? extends nsISupports> type) {
+		nsIComponentManager componentManager = Mozilla.getInstance().getComponentManager();
+		nsIServiceManager serviceManager = Mozilla.getInstance().getServiceManager();
+		
+		nsISimpleEnumerator contractIDsEnumerator
+				= queryInterface(componentManager, nsIComponentRegistrar.class)
+						.enumerateContractIDs();
 
+		List<String> contractIDs = new ArrayList<String>();
+		while(contractIDsEnumerator.hasMoreElements()) {
+			contractIDs.add( queryInterface(contractIDsEnumerator.getNext(),
+					nsISupportsCString.class).getData() );
+		}
+		java.util.Collections.sort(contractIDs);
+		for (String contractID : contractIDs) {
+			boolean hasComponent = false;
+			boolean hasService = false;
+			
+			try {
+				nsISupports component = componentManager.createInstanceByContractID(
+						contractID, null, nsISupports.NS_ISUPPORTS_IID);
+				queryInterface(component, type);
+				hasService = true;
+			} catch (Throwable e) {
+				// it's OK
+			}
+			
+			try {
+				nsISupports service = 
+					serviceManager.getServiceByContractID(contractID, nsISupports.NS_ISUPPORTS_IID);//createInstanceByContractID(
+								//contractID, null, nsISupports.NS_ISUPPORTS_IID);
+				queryInterface(service, type);
+				hasService = true;
+			} catch (Throwable e) {
+				// it's OK
+			}
+			
+			if (hasComponent || hasService) {
+				System.out.print(String.format("%s (hasComponent = %s, hasService = %s)",
+						contractID, hasComponent, hasService));
+			}
+		}
+	}
+
 	/**
 	 * Returns all XPCOM interfaces which extend nsISupports.
 	 * 

Modified: trunk/vpe/plugins/org.jboss.tools.vpe.xulrunner/src/org/jboss/tools/vpe/xulrunner/util/XulRunnerVpeUtils.java
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe.xulrunner/src/org/jboss/tools/vpe/xulrunner/util/XulRunnerVpeUtils.java	2010-06-07 15:08:21 UTC (rev 22603)
+++ trunk/vpe/plugins/org.jboss.tools.vpe.xulrunner/src/org/jboss/tools/vpe/xulrunner/util/XulRunnerVpeUtils.java	2010-06-07 17:01:26 UTC (rev 22604)
@@ -118,8 +118,8 @@
 	 * Returns the bounds of the selected text in given
 	 * {@code selectionContainer}.
 	 */
-	public static Rectangle getTextSelectionBounds(nsIDOMText slectionContainer) {
-		nsIAccessibleText accessibleTextAncestor = getAccessibleTextAncestor(slectionContainer);
+	public static Rectangle getTextSelectionBounds(nsIDOMText selectionContainer) {
+		nsIAccessibleText accessibleTextAncestor = getAccessibleTextAncestor(selectionContainer);
 		if (accessibleTextAncestor == null) {
 			// cannot get selection bounds
 			return null;
@@ -140,11 +140,16 @@
 		accessibleTextAncestor.getRangeExtents(startOffset[0], endOffset[0],
 				x, y, width, height, nsIAccessibleCoordinateType.COORDTYPE_PARENT_RELATIVE);
 		
-		Rectangle ancestorBounds = getElementBounds(
-				queryInterface(accessibleTextAncestor, nsIAccessNode.class)
-						.getDOMNode());
+		nsIDOMNode ancestorParent = queryInterface(accessibleTextAncestor, nsIAccessNode.class)
+				.getDOMNode().getParentNode();
+		Rectangle ancestorParentBounds;
+		if (ancestorParent != null) {
+			ancestorParentBounds = getElementBounds(ancestorParent);
+		} else {
+			ancestorParentBounds = new Rectangle(0, 0, 0, 0);
+		}
 		
-		return new Rectangle(ancestorBounds.x + x[0], ancestorBounds.y + y[0],
+		return new Rectangle(ancestorParentBounds.x + x[0], ancestorParentBounds.y + y[0],
 				width[0], height[0]);
 	}
 	



More information about the jbosstools-commits mailing list