Swing applet JDialog result to wicket page - servlets

I call an applet contained html from wicket page, based on the applet's JDialaog confirmation, I need to get back to wicket page, If user does not confirm he needs to stay in applet only.
I know setResponsePage in wicket can invoke the html but it needs to be directed only when user confirms from applets jdialog. any ideas please..basically need to invoke wicket page from applet, i tried to set the param in applet URL but somehow my getAppetContext() is returning null.

You have several options. I think the nicest will be using ajax.
The design will be as follows:
WicketPage
-- javascript
-- java-applet
You have an AbstractAjaxBehaviour that renders javascript, that in turn renders your Java applet.
You pass on the behavhiour's getCallbackUrl() to the javascript function. This can be done in the renderHead() of the AbstractAjaxBehaviour usign a response.render( OnDomReadyHeaderItem.forScript( js ) );
You can call javascript from your Applet. So when the confirmation has taken place, the applet will call some javascript function, which in turn call a Wicket.ajax.get.
This Wicket.ajax.get can use the callbackUrl from the behaviour, and you end up in the server-side Java again, in the body on the onRequest of the AjaxBehaviour
See also these references:
https://docs.oracle.com/javase/tutorial/deployment/applet/invokingJavaScriptFromApplet.html
http://wickedsource.org/2013/01/07/rolling-your-own-ajax-behavior-with-wicket/
http://wicket.apache.org/guide/guide/ajax.html

try {
if(appletCtx != null){
camera.endLiveView();
camera.closeSession();
CanonCamera.close();
getAppletContext().showDocument(new URL(LOCAL_URI+"?wicket:bookmarkablePage=%3Acom.xyz.app.tla.wickola.page.takingViewingPictures.UploadEmployeePhotoPage"),"_parent");
System.exit( 0 ); // a separate jvm for my photo applet
}
} catch (MalformedURLException e) {
e.printStackTrace();
}

Related

how to locate elements on a different webpage?

I'm new to java and webdriver. My web-applications adds some data to a table on a webpage. If the addition is successful, a new web page is opened and the success message is displayed on the new page. If the addition is not successful, a javascript alert is thrown. After accepting the alertHow do I check the presence of an the message on the new webpage using webdriver?
If it is opening in new window you need to switch the control to new window first
Find the logic here to switch the control between windows
After switching the control to new window you can verify whatever you want. Either element or text.
isElementPresent? method logic here .
isTextPresent? method logic here.
If I understand the question correctly, after sending the data to the table, if the sending is successful then the window is loaded with a new webpage else an alert appears once you accept the alert the window is loaded.
After sending the data, check for the presence of the alert, if the alert is present then accept it. Next is verifying whether a text is present in the newly loaded webpage or not.
public void isAlertPresent(){
try {
driver.switchTo.alert().accept();
}
catch ( NoAlertPresentException e ){
}
System.out.println(driver.findElement(By.tagName("body")).getText().contains("Expected Message"));
If the location where the message appears is static then I would suggest you to use a better approach than the above like if an element has that text
WebElement element = driver.findElement(By.id("elementID"));
System.out.println(element.getText().trim().equals("Expected Message"));

IE9 refresh after Ajax request returns

I have a JavaScript class that displays a partially-opaque div over top of the content of another div when an Ajax request is sent to the server.
When the request returns, the JavaScript class hides the partially-opaque div....it works great...sort of.
Right now, in IE9, when the Ajax request is complete, the partial-opacity is only hidden if the user moves their mouse.
So, my question is, how do I force the browser to do what it's supposed to do?
This is my extremely simple function that is called after the request returns to the browser:
_hideBlockingDiv: function() {
if (this.get_blockingDivClientID()) {
var blockingElement = $get(this.get_blockingDivClientID());
if (blockingElement != null) {
blockingElement.style.display = 'none';
//I know that this method is executing correctly because I "hi" showed
//up properly...but the element remained visible:
blockingElement.innerHTML = 'hi';
}
//if I add the alert then everything works fine in IE9
//if I don't then the page will remain the same until the user moves their mose
//alert("done");
}
}
Please note that I am not using JQuery.
I am using the AJAX.NET library since I am a .NET developer (and JQuery didn't become popular until years after I implemented my Ajax-enabled server controls)
Thanks
-Frinny
How and where do you call the _hideBlockingDiv function from? Since you are using MS Ajax library, you might want to have a page loaded handler on client side and call this function from within that handler. So basically
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function(){
_hideBlockingDiv();
});
Hope this helps!
It turns out that the problem only exists in the beta version of IE9 that I was using at the time. This problem went away once the full version of IE9 was released.

ajaxSubmit and Other Code. Can someone help me determine what this code is doing?

I've inherited some code that I need to debug. It isn't working at present. My task is to get it to work. No other requirements have been given to me. No, this isn't homework, this is a maintenance nightmare job.
ASP.Net (Framework 3.5), C#, jQuery 1.4.2. This project makes heavy use of jQuery and AJAX. There is a drop down on a page that, when an item is chosen, is supposed to add that item (it's a user) to an object in the database.
To accomplish this, the previous programmer first, on page load, dynamically loads the entire page through AJAX. To do this, he's got 5 div's, and each one is loaded from a jQuery call to a different full page in the website.
Somehow, the HTML and BODY and all the other stuff is stripped out and the contents of the div are loaded with the content of the aspx page. Which seems incredibly wrong to me since it relies on the browser to magically strip out html, head, body, form tags and merge with the existing html head body form tags.
Also, as the "content" page is returned as a string, the previous programmer has this code running on it before it is appended to the div:
function CleanupResponseText(responseText, uniqueName) {
responseText = responseText.replace("theForm.submit();", "SubmitSubForm(theForm, $(theForm).parent());");
responseText = responseText.replace(new RegExp("theForm", "g"), uniqueName);
responseText = responseText.replace(new RegExp("doPostBack", "g"), "doPostBack" + uniqueName);
return responseText;
}
When the dropdown itself fires it's onchange event, here is the code that gets fired:
function SubmitSubForm(form, container) {
//ShowLoading(container);
$(form).ajaxSubmit( {
url: $(form).attr("action"),
success: function(responseText) {
$(container).html(CleanupResponseText(responseText, form.id));
$("form", container).css("margin-top", "0").css("padding-top", "0");
//HideLoading(container);
}
}
);
}
This blows up in IE, with the message that "Microsoft JScript runtime error: Object doesn't support this property or method" -- which, I think, has to be that $(form).ajaxSubmit method doesn't exist.
What is this code really trying to do? I am so turned around right now that I think my only option is to scrap everything and start over. But I'd rather not do that unless necessary.
Is this code good? Is it working against .Net, and is that why we are having issues?
A google search for
jquery ajax submit
reveals the jQuery Form Plugin. Given that, is that file included on your page where the other code will have access to the method? Does this code work in Firefox and not IE?
Seems like there was too much jQuery fun going on. I completely reworked the entire code block since it was poorly designed in the first place.

externalinterface - calling javascript from SWF

HI,
im trying to call a javascript function from my actionscript code but its not working;
as3:
if (ExternalInterface.available)
{
try
{
ExternalInterface.addCallback("changeDocumentTitle",null);
}
catch(error:Error)
js (inside velocity file using swfobject)
function changeDocumentTitle()
{
alert('call from SWF');
}
anyone know what could be happenin?
If you are trying to invoke a JS function from within your Flex app, you want to use ExternalInterface.call(...) and not ExternalInterface.addCallback(...). From the docs:
public static function call(functionName:String, ... arguments):*
Calls a function exposed by the Flash Player container, passing zero or more arguments. If the function is not available, the call returns null; otherwise it returns the value provided by the function. Recursion is not permitted on Opera or Netscape browsers; on these browsers a recursive call produces a null response. (Recursion is supported on Internet Explorer and Firefox browsers.)
If the container is an HTML page, this method invokes a JavaScript function in a script element.
http://livedocs.adobe.com/flex/3/langref/flash/external/ExternalInterface.html
addCallback() is used if you want to expose an ActionScript function from your Flash app to the HTML container so that it can be invoked via JavaScript.
On the local system, communication between the SWF and Javascript tends to be hampered by security issues. You can reconfigure your flash to allow some of these communications via the "settings manager".
It may also be an issue with "allowscriptacces" not being set where you embed the flash object.
Another problem may be that flash tries to call javascript before the javascript is loaded. The init order thing can be quite annoying.

Displaying Loading text while doing a WebRequest

I have a button on my webform. Clicking this button will do an HttpWebRequest during the onclick event handler. After the request we copy the response from the request into HttpContext.Current.Response and send that to the client.
This web request can take a while (up to 5 seconds, since it's generating a report). During this time the user has no indication that anything is going on, except for the browser progress bar and the spinning IE icon (if they're using IE). So I need a loading indicator while this is happening.
I've tried using javascript that fires during the button's onclick event (using OnClientClick) and while that works, I don't know how to find out when the web request is finished. Since we just send the response to the client, a full postback doesn't happen.
I've tried wrapping the button in an UpdatePanel and using the UpdateProgress, but when we send the response to HttpContext.Current.Response and call Response.End(), we get an error in the javascript, since the response isn't well formed (we're sending back an excel sheet for the user to download).
Since we're sending back a file for users to download, I don't want to pop-up a separate window, since then in IE they'd get the information bar blocking the download.
Any ideas here?
As an alternative to the Professional AJAX.NET library, jQuery has a really nice way of doing this.
Take a look at this example of using a .NET PageMethod (if possible in your scenario).
You define a page method call in jQuery, you can tack on your loading... message in a hidden div.
Say what callback you want to return on success (ie when your 5 second report is generated)
then hide the loading and handle the data.
Take a look at the javascript on my contact page for an example (view the source).
I have a a button on the page, add the jQuery onClick.
When clicked that shows a hidden loading div, makes an ajax call to a page method that takes the parameters of the form.
The page method does emailing etc then returns to the form in the onSuccess javascript method I have there.
The onSuccess hides the loading div.
A simple trick i have used in the past is to redirect to an intermediate page with an animated progress bar (gif) and then have that page do the REAL post of the data.
(or even pop-up a layer with the animation on it and a polite message asking the user to wait a minute or two)
The simple feedback of the animated gif creates the illusion to the end user that the app is not stalled and they will be more patient.
Another approach is to hand the data off to a worker thread and return immediately with a message stating that the report will be emailed or made available in the "reports" section of the site when it is ready. This approach lacks the benefit of instant notification when the report is completed though.
Here is my solution :
Download and examine the samples of free Professional AJAX.NET library.
Write a AjaxMethod that creates your file and returns file location as a parameter.
Write your Client-Side function to call method at Step 2. When this method called show an indicator.
Write a client-side callback method to hide indicator and show/download file that user requested.
Add your client-side function calls yo your button element.
When your method at server-side ends your callback will be called.
Hope this helps !
The solution I'm presenting here is aimed to show a method to let a "Loading..." box to appear while you're server-side processing and to disappear when server-side processing is complete.
I'll do this with the very basic AJAX machinery (tested on FF, but IE should be ok either), i.e. not using a framework like Prototype or jQuery or Dojo, as you didn't specify your knowledge about them.
To let you better understand the trick, the following is just a small example and doesn't pretend to be an out-of-the-box solution. I tend not to be superficial, but I think a clearer example can explain better than many words.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>First Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
.hidden {
display: none;
}
.loadingInProgress {
color: #FFFFFF;
width: 75px;
background-color: #FF0000;
}
</style>
<script type="text/javascript">
var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
httpRequest.overrideMimeType('text/xml');
} else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
}
httpRequest.onreadystatechange = function(){
switch (httpRequest.readyState) {
case 1: // Loading
document.getElementById('loading').className = "loadingInProgress";
break;
case 4: // Complete
document.getElementById('loading').className = "hidden";
if (httpRequest.status == 200) {
// perfect!
} else {
// there was a problem with the request,
// for example the response may be a 404 (Not Found)
// or 500 (Internal Server Error) response codes
}
break;
}
};
function go() {
httpRequest.open('GET', document.getElementById('form1').action, true);
httpRequest.send('');
}
</script>
</head>
<body>
<div id="loading" class="hidden">Loading...</div>
<form id="form1" name="form1" action="doSomething.php">
<input type="button" value="Click to submit:" onclick="go()" />
</form>
</body>
</html>
As you can see, there's a <div> which holds the "Loading..." message.
The principle is to show/hide the <div> depending on the XMLHttpRequest object's readyState.
I've used the onreadystatechange handler of the XMLHttpRequest to trigger the readyState change.
The back-end php script I use (declared as the form's action) does just a sleep(5), to let the "Loading..." message appear for 5 secs.
<?php
sleep(5);
header('Cache-Control: no-cache');
echo "OK";
?>
The Cache-control: no-cache header is necessary, since usually if you don't set it the browser will cache the response avoiding to resubmit the request if you should need to.
A good source for "getting started" AJAX documentation is Mozilla MDC.
The whole thing could be much more gently handled by a Javascript framework like Prototype, taking advantage of its browser-safe approach, saving you hours of debug.
Edit:
I chose php 'cause I don't know ASP.NET nor ASP, sorry about that.

Resources