Author: yradtsevich
Date: 2010-06-02 12:02:31 -0400 (Wed, 02 Jun 2010)
New Revision: 22483
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/XulRunnerVpeUtils.java
Log:
https://jira.jboss.org/browse/JBIDE-6229
Implement DnD support for selected text in VPE
- A method to get bounds of the selected text has been created:
XulRunnerVpeUtils.getTextSelectionBounds .
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-02
14:58:31 UTC (rev 22482)
+++
trunk/vpe/plugins/org.jboss.tools.vpe.xulrunner/src/org/jboss/tools/vpe/xulrunner/util/XPCOM.java 2010-06-02
16:02:31 UTC (rev 22483)
@@ -36,6 +36,7 @@
public static final String NS_PREFSERVICE_CONTRACTID =
"(a)mozilla.org/preferences-service;1"; //$NON-NLS-1$
public static final String NS_SUPPORTSSTRING_CONTRACTID =
"(a)mozilla.org/supports-string;1"; //$NON-NLS-1$
public static final String NS_SUPPORTSARRAY_CONTRACTID =
"(a)mozilla.org/supports-array;1"; //$NON-NLS-1$
+ public static final String NS_ACCESSIBILITYSERVICE_CONTRACTID =
"(a)mozilla.org/accessibilityService;1"; //$NON-NLS-1$
public static final String IN_FLASHER_CONTRACTID =
"(a)mozilla.org/inspector/flasher;1"; //$NON-NLS-1$
/**
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-02
14:58:31 UTC (rev 22482)
+++
trunk/vpe/plugins/org.jboss.tools.vpe.xulrunner/src/org/jboss/tools/vpe/xulrunner/util/XulRunnerVpeUtils.java 2010-06-02
16:02:31 UTC (rev 22483)
@@ -15,6 +15,11 @@
import org.eclipse.swt.graphics.Rectangle;
import org.jboss.tools.vpe.xulrunner.BrowserPlugin;
+import org.mozilla.interfaces.nsIAccessNode;
+import org.mozilla.interfaces.nsIAccessible;
+import org.mozilla.interfaces.nsIAccessibleCoordinateType;
+import org.mozilla.interfaces.nsIAccessibleRetrieval;
+import org.mozilla.interfaces.nsIAccessibleText;
import org.mozilla.interfaces.nsIBoxObject;
import org.mozilla.interfaces.nsIDOMDocument;
import org.mozilla.interfaces.nsIDOMElement;
@@ -22,6 +27,8 @@
import org.mozilla.interfaces.nsIDOMNSElement;
import org.mozilla.interfaces.nsIDOMNSHTMLElement;
import org.mozilla.interfaces.nsIDOMNode;
+import org.mozilla.interfaces.nsIDOMText;
+import org.mozilla.xpcom.Mozilla;
import org.mozilla.xpcom.XPCOMException;
/**
@@ -106,4 +113,69 @@
return new Rectangle(0, 0, 0, 0);
}
}
+
+ /**
+ * Returns the bounds of the selected text in given
+ * {@code selectionContainer}.
+ */
+ public static Rectangle getTextSelectionBounds(nsIDOMText slectionContainer) {
+ nsIAccessibleText accessibleTextAncestor =
getAccessibleTextAncestor(slectionContainer);
+ if (accessibleTextAncestor == null) {
+ // cannot get selection bounds
+ return null;
+ }
+ if (accessibleTextAncestor.getSelectionCount() == 0) {
+ // no text selected
+ return null;
+ }
+
+ int[] startOffset = new int[1];
+ int[] endOffset = new int[1];
+ accessibleTextAncestor.getSelectionBounds(0, startOffset, endOffset);
+
+ int[] x = new int[1];
+ int[] y = new int[1];
+ int[] width = new int[1];
+ int[] height = new int[1];
+ accessibleTextAncestor.getRangeExtents(startOffset[0], endOffset[0],
+ x, y, width, height, nsIAccessibleCoordinateType.COORDTYPE_PARENT_RELATIVE);
+
+ Rectangle ancestorBounds = getElementBounds(
+ queryInterface(accessibleTextAncestor, nsIAccessNode.class)
+ .getDOMNode());
+
+ return new Rectangle(ancestorBounds.x + x[0], ancestorBounds.y + y[0],
+ width[0], height[0]);
+ }
+
+ /**
+ * Returns the nearest ancestor of given {@code node} which supports
+ * {@link nsIAccessibleText}.
+ */
+ private static nsIAccessibleText getAccessibleTextAncestor(nsIDOMNode node) {
+ nsIAccessibleText accessibleTextAncestor = null;
+ nsIDOMNode ancestor = node;
+ while (accessibleTextAncestor == null && ancestor != null) {
+ ancestor = ancestor.getParentNode();
+ try {
+ nsIAccessible accessibleAncestor = getAccessible(ancestor);
+ accessibleTextAncestor = queryInterface(accessibleAncestor,
nsIAccessibleText.class);
+ } catch (XPCOMException e) {
+ // it's OK, accessibleTextAncestor still = null
+ }
+ }
+
+ return accessibleTextAncestor;
+ }
+
+ /**
+ * Returns {@link nsIAccessible} interface for given {@code node}.
+ */
+ private static nsIAccessible getAccessible(nsIDOMNode node) {
+ return ((nsIAccessibleRetrieval)
+ Mozilla.getInstance().getServiceManager().getServiceByContractID(
+ XPCOM.NS_ACCESSIBILITYSERVICE_CONTRACTID,
+ nsIAccessibleRetrieval.NS_IACCESSIBLERETRIEVAL_IID))
+ .getAccessibleFor(node);
+ }
}