diff --git a/java/webclient/classes_spec/org/mozilla/webclient/impl/wrapper_native/CurrentPageImpl.java b/java/webclient/classes_spec/org/mozilla/webclient/impl/wrapper_native/CurrentPageImpl.java index cd1d6e905da..2c8cfdeafd9 100644 --- a/java/webclient/classes_spec/org/mozilla/webclient/impl/wrapper_native/CurrentPageImpl.java +++ b/java/webclient/classes_spec/org/mozilla/webclient/impl/wrapper_native/CurrentPageImpl.java @@ -119,70 +119,77 @@ public Selection getSelection() { public void highlightSelection(Selection selection) { if (selection != null && selection.isValid()) { - Node startContainer = selection.getStartContainer(); - Node endContainer = selection.getEndContainer(); - int startOffset = selection.getStartOffset(); - int endOffset = selection.getEndOffset(); + final Node startContainer = selection.getStartContainer(); + final Node endContainer = selection.getEndContainer(); + final int startOffset = selection.getStartOffset(); + final int endOffset = selection.getEndOffset(); - getWrapperFactory().verifyInitialized(); - Assert.assert_it(-1 != getNativeBrowserControl()); - synchronized(getBrowserControl()) { - nativeHighlightSelection(getNativeBrowserControl(), startContainer, endContainer, startOffset, endOffset); + NativeEventThread.instance.pushBlockingWCRunnable(new WCRunnable() { + public Object run() { + nativeHighlightSelection(CurrentPageImpl.this.getNativeBrowserControl(), + startContainer, endContainer, + startOffset, endOffset); + return null; + } + }); } } -} public void clearAllSelections() { - getWrapperFactory().verifyInitialized(); - Assert.assert_it(-1 != getNativeBrowserControl()); - synchronized(getBrowserControl()) { - nativeClearAllSelections(getNativeBrowserControl()); + NativeEventThread.instance.pushBlockingWCRunnable(new WCRunnable() { + public Object run() { + nativeClearAllSelections(CurrentPageImpl.this.getNativeBrowserControl()); + return null; + } + }); } -} - public void findInPage(String stringToFind, boolean forward, boolean matchCase) { find(stringToFind, forward, matchCase); } -public boolean find(String stringToFind, boolean forward, boolean matchCase) +public boolean find(String toFind, boolean dir, boolean doCase) { + final String stringToFind = toFind; + final boolean forward = dir; + final boolean matchCase = doCase; ParameterCheck.nonNull(stringToFind); - getWrapperFactory().verifyInitialized(); - boolean result = false; + Boolean result = Boolean.FALSE; - synchronized(getBrowserControl()) { - result = nativeFind(getNativeBrowserControl(), stringToFind, forward, - matchCase); - } - return result; + result = (Boolean) + NativeEventThread.instance.pushBlockingWCRunnable(new WCRunnable(){ + public Object run() { + boolean rc = nativeFind(CurrentPageImpl.this.getNativeBrowserControl(), + stringToFind, forward, matchCase); + return rc ? Boolean.TRUE : Boolean.FALSE; + } + }); + return result.booleanValue(); } public void findNextInPage() { - getWrapperFactory().verifyInitialized(); - - synchronized(getBrowserControl()) { - nativeFindNext(getNativeBrowserControl()); - } + findNext(); } public boolean findNext() { - getWrapperFactory().verifyInitialized(); - boolean result = false; - - synchronized(getBrowserControl()) { - result = nativeFindNext(getNativeBrowserControl()); - } - return result; + Boolean result = Boolean.FALSE; + + result = (Boolean) + NativeEventThread.instance.pushBlockingWCRunnable(new WCRunnable(){ + public Object run() { + boolean rc = nativeFindNext(CurrentPageImpl.this.getNativeBrowserControl()); + return rc ? Boolean.TRUE : Boolean.FALSE; + } + }); + return result.booleanValue(); } public String getCurrentURL() { String result = null; - getWrapperFactory().verifyInitialized(); synchronized(getBrowserControl()) { result = nativeGetCurrentURL(getNativeBrowserControl()); @@ -348,21 +355,4 @@ native public void nativePrint(int webShellPtr); native public void nativePrintPreview(int webShellPtr, boolean preview); -// ----VERTIGO_TEST_START - -// -// Test methods -// - -public static void main(String [] args) -{ - Assert.setEnabled(true); - Log.setApplicationName("CurrentPageImpl"); - Log.setApplicationVersion("0.0"); - Log.setApplicationVersionDate("$Id: CurrentPageImpl.java,v 1.8 2005-02-10 04:20:50 edburns%acm.org Exp $"); - -} - -// ----VERTIGO_TEST_END - } // end of class CurrentPageImpl diff --git a/java/webclient/classes_spec/org/mozilla/webclient/impl/wrapper_native/SelectionImpl.java b/java/webclient/classes_spec/org/mozilla/webclient/impl/wrapper_native/SelectionImpl.java index d7f95456218..28324a96029 100644 --- a/java/webclient/classes_spec/org/mozilla/webclient/impl/wrapper_native/SelectionImpl.java +++ b/java/webclient/classes_spec/org/mozilla/webclient/impl/wrapper_native/SelectionImpl.java @@ -51,6 +51,9 @@ public class SelectionImpl extends Object implements Selection { * Get the text representation of this Selection object. */ public String toString() { + if (null == _selection) { + return ""; + } return (_selection); } diff --git a/java/webclient/src_moz/CurrentPageImpl.cpp b/java/webclient/src_moz/CurrentPageImpl.cpp index fde5fb51612..6eac3a98c5e 100644 --- a/java/webclient/src_moz/CurrentPageImpl.cpp +++ b/java/webclient/src_moz/CurrentPageImpl.cpp @@ -41,6 +41,9 @@ #include "nsIWebBrowserFind.h" #include "nsIDOMWindow.h" #include "nsIDOMDocument.h" +#include "nsIDOMRange.h" +#include "nsISelection.h" +#include "nsIDOMDocumentRange.h" #include "nsIHistoryEntry.h" #include "nsIInterfaceRequestor.h" #include "nsIInterfaceRequestorUtils.h" @@ -90,24 +93,104 @@ JNIEXPORT void JNICALL Java_org_mozilla_webclient_impl_wrapper_1native_CurrentPa } -#if 0 // convenience - JNIEXPORT void JNICALL Java_org_mozilla_webclient_impl_wrapper_1native_CurrentPageImpl_nativeHighlightSelection (JNIEnv *env, jobject obj, jint nativeBCPtr, jobject startContainer, jobject endContainer, jint startOffset, jint endOffset) { NativeBrowserControl *nativeBrowserControl = (NativeBrowserControl *) nativeBCPtr; - + if (nativeBrowserControl == nsnull) { ::util_ThrowExceptionToJava(env, "Exception: null nativeBCPtr passed to nativeHighlightSelection"); return; } + + if (env != nsnull && startContainer != nsnull && endContainer != nsnull && + startOffset > -1 && endOffset > -1) { + nsresult rv = nsnull; + + // resolve ptrs to the nodes + jclass nodeClass = env->FindClass("org/mozilla/dom/NodeImpl"); + if (!nodeClass) { + ::util_ThrowExceptionToJava(env, "Exception: nativeHighlightSelection"); + return; + } + jfieldID nodePtrFID = env->GetFieldID(nodeClass, "p_nsIDOMNode", "J"); + if (!nodePtrFID) { + ::util_ThrowExceptionToJava(env, "Exception: nativeHighlightSelection"); + return; + } + + // get the nsIDOMNode representation of the start and end containers + nsIDOMNode* node1 = (nsIDOMNode*) + env->GetLongField(startContainer, nodePtrFID); - PR_ASSERT(nativeBrowserControl->initComplete); + nsIDOMNode* node2 = (nsIDOMNode*) + env->GetLongField(endContainer, nodePtrFID); + + if (!node1 || !node2) { + ::util_ThrowExceptionToJava(env, "Exception: nativeHighlightSelection"); + return; + } + + // Get the DOM window and document + nsCOMPtr domWindow; + nsCOMPtr webBrowser; + nsCOMPtr domDocument; + nativeBrowserControl->mWindow->GetWebBrowser(getter_AddRefs(webBrowser)); - wsHighlightSelectionEvent *actionEvent = new wsHighlightSelectionEvent(env, nativeBrowserControl, startContainer, endContainer, (PRInt32) startOffset, (PRInt32) endOffset); - PLEvent *event = (PLEvent *) *actionEvent; - ::util_PostSynchronousEvent(nativeBrowserControl, event); -} + if (NS_FAILED(rv) || !webBrowser) { + ::util_ThrowExceptionToJava(env, "Exception: nativeHighlightSelection"); + return; + } + + // get the content DOM window for that web browser + rv = webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow)); + + if (NS_FAILED(rv) || !domWindow ) { + ::util_ThrowExceptionToJava(env, "Exception: nativeHighlightSelection"); + return; + } + + rv = domWindow->GetDocument(getter_AddRefs(domDocument)); + if (NS_FAILED(rv) || !domDocument ) { + ::util_ThrowExceptionToJava(env, "Exception: nativeHighlightSelection"); + return; + } + + // Get the selection object of the DOM window + nsCOMPtr selection; + rv = domWindow->GetSelection(getter_AddRefs(selection)); + if (NS_FAILED(rv) || selection == nsnull) { + ::util_ThrowExceptionToJava(env, "Exception: nativeHighlightSelection"); + return; + } + + nsCOMPtr docRange(do_QueryInterface(domDocument)); + if (docRange) { + nsCOMPtr range; + rv = docRange->CreateRange(getter_AddRefs(range)); + + if (range) { + rv = range->SetStart(node1, startOffset); + if (NS_FAILED(rv)) { + ::util_ThrowExceptionToJava(env, "Exception: nativeHighlightSelection"); + return; + } + + rv = range->SetEnd(node2, endOffset); + if (NS_FAILED(rv)) { + ::util_ThrowExceptionToJava(env, "Exception: nativeHighlightSelection"); + return; + } + + rv = selection->AddRange(range); + if (NS_FAILED(rv)) { + ::util_ThrowExceptionToJava(env, "Exception: nativeHighlightSelection"); + return; + } + } + } + } +} JNIEXPORT void JNICALL Java_org_mozilla_webclient_impl_wrapper_1native_CurrentPageImpl_nativeClearAllSelections (JNIEnv *env, jobject obj, jint nativeBCPtr) @@ -118,17 +201,41 @@ JNIEXPORT void JNICALL Java_org_mozilla_webclient_impl_wrapper_1native_CurrentPa ::util_ThrowExceptionToJava(env, "Exception: null nativeBCPtr passed to nativeClearAllSelections"); return; } + nsresult rv; - PR_ASSERT(nativeBrowserControl->initComplete); + // Get the DOM window and document + nsCOMPtr domWindow; + nsCOMPtr webBrowser; + rv = nativeBrowserControl->mWindow->GetWebBrowser(getter_AddRefs(webBrowser)); - wsClearAllSelectionEvent *actionEvent = new wsClearAllSelectionEvent(nativeBrowserControl); + if (NS_FAILED(rv) || !webBrowser) { + ::util_ThrowExceptionToJava(env, "Exception: nativeClearAllSelections"); + return; + } - PLEvent *event = (PLEvent *) *actionEvent; - ::util_PostSynchronousEvent(nativeBrowserControl, event); + // get the content DOM window for that web browser + rv = webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow)); + + if (NS_FAILED(rv) || !domWindow ) { + ::util_ThrowExceptionToJava(env, "Exception: nativeClearAllSelections"); + return; + } + + nsCOMPtr selection; + rv = domWindow->GetSelection(getter_AddRefs(selection)); + if (NS_FAILED(rv) || !selection) { + ::util_ThrowExceptionToJava(env, "Exception: nativeClearAllSelections"); + return; + } + + rv = selection->RemoveAllRanges(); + if (NS_FAILED(rv)) { + ::util_ThrowExceptionToJava(env, "Exception: nativeClearAllSelections"); + return; + } + } -#endif - /* * Class: org_mozilla_webclient_impl_wrapper_0005fnative_CurrentPageImpl * Method: nativeFind diff --git a/java/webclient/test/automated/src/classes/org/mozilla/webclient/CurrentPageTest.java b/java/webclient/test/automated/src/classes/org/mozilla/webclient/CurrentPageTest.java index 4938921548c..8f3e7b1e247 100644 --- a/java/webclient/test/automated/src/classes/org/mozilla/webclient/CurrentPageTest.java +++ b/java/webclient/test/automated/src/classes/org/mozilla/webclient/CurrentPageTest.java @@ -1,5 +1,5 @@ /* - * $Id: CurrentPageTest.java,v 1.7 2005-02-12 21:29:47 edburns%acm.org Exp $ + * $Id: CurrentPageTest.java,v 1.8 2005-02-14 02:16:18 edburns%acm.org Exp $ */ /* @@ -44,6 +44,9 @@ import java.io.File; import java.io.FileInputStream; import java.io.BufferedReader; +import org.w3c.dom.Document; +import org.w3c.dom.Node; + // CurrentPageTest.java public class CurrentPageTest extends WebclientTestCase implements ClipboardOwner { @@ -301,5 +304,81 @@ public class CurrentPageTest extends WebclientTestCase implements ClipboardOwner BrowserControlFactory.deleteBrowserControl(firstBrowserControl); } + public void testHighlightDomRegion() throws Exception { + BrowserControl firstBrowserControl = null; + DocumentLoadListenerImpl listener = null; + Selection selection = null; + firstBrowserControl = BrowserControlFactory.newBrowserControl(); + assertNotNull(firstBrowserControl); + BrowserControlCanvas canvas = (BrowserControlCanvas) + firstBrowserControl.queryInterface(BrowserControl.BROWSER_CONTROL_CANVAS_NAME); + eventRegistration = (EventRegistration2) + firstBrowserControl.queryInterface(BrowserControl.EVENT_REGISTRATION_NAME); + + assertNotNull(canvas); + Frame frame = new Frame(); + frame.setUndecorated(true); + frame.setBounds(0, 0, 640, 480); + frame.add(canvas, BorderLayout.CENTER); + frame.setVisible(true); + canvas.setVisible(true); + + Navigation2 nav = (Navigation2) + firstBrowserControl.queryInterface(BrowserControl.NAVIGATION_NAME); + assertNotNull(nav); + currentPage = (CurrentPage2) + firstBrowserControl.queryInterface(BrowserControl.CURRENT_PAGE_NAME); + + assertNotNull(currentPage); + + eventRegistration.addDocumentLoadListener(listener = new DocumentLoadListenerImpl() { + public void doEndCheck() { + CurrentPageTest.keepWaiting = false; + } + }); + + Thread.currentThread().sleep(3000); + + + CurrentPageTest.keepWaiting = true; + + nav.loadURL("http://localhost:5243/DOMSelectionTest.html"); + + // keep waiting until the previous load completes + while (CurrentPageTest.keepWaiting) { + Thread.currentThread().sleep(1000); + } + + Document dom = currentPage.getDOM(); + assertNotNull(dom); + + Node + start = dom.getElementById("p2"), + end = dom.getElementById("p4"); + assertNotNull(start); + assertNotNull(end); + + selection = currentPage.getSelection(); + selection.init("", start, end, 0, 0); + // select Paragraphs 2 - 4 exclusive + currentPage.highlightSelection(selection); + + Thread.currentThread().sleep(3000); // PENDING remove + + selection = currentPage.getSelection(); + assertTrue(-1 == selection.toString().indexOf("Paragraph 1")); + assertTrue(-1 != selection.toString().indexOf("Paragraph 2")); + assertTrue(-1 != selection.toString().indexOf("Paragraph 3")); + assertTrue(-1 == selection.toString().indexOf("Paragraph 4")); + assertTrue(-1 == selection.toString().indexOf("Paragraph 5")); + + currentPage.clearAllSelections(); + selection = currentPage.getSelection(); + assertEquals(0, selection.toString().length()); + + + frame.setVisible(false); + BrowserControlFactory.deleteBrowserControl(firstBrowserControl); + } } diff --git a/java/webclient/test/automated/src/classes/org/mozilla/webclient/DOMTest.java b/java/webclient/test/automated/src/classes/org/mozilla/webclient/DOMTest.java index ea4b3779bfd..b214c8cd08d 100644 --- a/java/webclient/test/automated/src/classes/org/mozilla/webclient/DOMTest.java +++ b/java/webclient/test/automated/src/classes/org/mozilla/webclient/DOMTest.java @@ -1,5 +1,5 @@ /* - * $Id: DOMTest.java,v 1.1 2005-02-04 15:43:47 edburns%acm.org Exp $ + * $Id: DOMTest.java,v 1.2 2005-02-14 02:16:18 edburns%acm.org Exp $ */ /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- @@ -73,7 +73,7 @@ public class DOMTest extends WebclientTestCase { // Testcases // - public void testHttpLoad() throws Exception { + public void testGetDOM() throws Exception { BrowserControl firstBrowserControl = null; DocumentLoadListenerImpl listener = null; Selection selection = null; diff --git a/java/webclient/test/automated/src/test/DOMSelectionTest.html b/java/webclient/test/automated/src/test/DOMSelectionTest.html new file mode 100644 index 00000000000..3b3960925de --- /dev/null +++ b/java/webclient/test/automated/src/test/DOMSelectionTest.html @@ -0,0 +1,22 @@ + + + + DOM Selection Test + + + +

DOM Selection Test

+ +

Paragraph 1

+

Paragraph 2

+

Paragraph 3

+

Paragraph 4

+

Paragraph 5

+ +
+ + +Last modified: Sun Feb 13 11:21:35 Eastern Standard Time 2005 + + +