<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>doOriented.com</title>
	<atom:link href="http://www.dooriented.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dooriented.com/blog</link>
	<description>Less words, more code</description>
	<pubDate>Fri, 09 May 2008 17:52:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Wicketing Javascript through AJAX</title>
		<link>http://www.dooriented.com/blog/2008/05/09/wicketing-javascript-through-ajax/</link>
		<comments>http://www.dooriented.com/blog/2008/05/09/wicketing-javascript-through-ajax/#comments</comments>
		<pubDate>Fri, 09 May 2008 17:52:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[web development]]></category>

		<category><![CDATA[wicket]]></category>

		<guid isPermaLink="false">http://www.dooriented.com/blog/?p=9</guid>
		<description><![CDATA[Communication between Java and Javascript cannot be done easier than with Wicket framework, by far the best helper if you&#8217;re in Java based web development. 
html


&#60;!DOCTYPE html PUBLIC &#34;-//W3C//DTD XHTML 1.1//EN&#34; &#34;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&#34;&#62;
&#60;html xmlns:wicket&#62;
&#60;head&#62;
&#60;script type=&#34;text/javascript&#34; &#62;
function callWicket() {
	wicketAjaxGet(callback + '&#38;parameter=value', function() {}, function() {});
}
&#60;/script&#62;
&#60;/head&#62;

&#60;body&#62;
	&#60;div wicket:id = &#34;div&#34;&#62;&#60;/div&#62;
	&#60;span wicket:id = &#34;wicketAnswer&#34;&#62;[]&#60;/span&#62;
&#60;/body&#62;
&#60;/html&#62;


Java - or should I say the [...]]]></description>
			<content:encoded><![CDATA[<p>Communication between Java and Javascript cannot be done easier than with Wicket framework, by far the best helper if you&#8217;re in Java based web development. </p>
<p><b>html</b></p>
<pre>
<pre class="syntax-highlight:html">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.1//EN&quot; &quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt;
&lt;html xmlns:wicket&gt;
&lt;head&gt;
&lt;script type=&quot;text/javascript&quot; &gt;
function callWicket() {
	wicketAjaxGet(callback + '&amp;parameter=value', function() {}, function() {});
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
	&lt;div wicket:id = &quot;div&quot;&gt;&lt;/div&gt;
	&lt;span wicket:id = &quot;wicketAnswer&quot;&gt;[]&lt;/span&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
</pre>
<p><b>Java - or should I say the magic of Wicket?</b></p>
<pre>
<pre class="syntax-highlight:java">
package com.somepackage;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.util.time.Duration;

@SuppressWarnings(&quot;serial&quot;)
public class BasePage extends WebPage {
	private WebMarkupContainer dummyDiv;
	private Label label;

	public BasePage() {
		this(null);
	}

	public BasePage(final PageParameters parameters) {
		//handle js call to wicket
		final AbstractDefaultAjaxBehavior behavior = new AbstractDefaultAjaxBehavior() {
		    protected void respond(final AjaxRequestTarget target) {
		    	label.setModelObject(&quot;Yeah I was just called from Javascript!&quot;);
		        target.addComponent(label);
		    }
		};
		add(dummyDiv = new MyWebMarkupContainer(&quot;div&quot;, behavior));
		//handle wicket call to js
		dummyDiv.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(1)) {
			@Override
			protected void onPostProcessTarget(AjaxRequestTarget target) {
				target.appendJavascript(&quot;callWicket();&quot;);
			}
		});
		dummyDiv.add(behavior);

    	add(label = new Label(&quot;wicketAnswer&quot;, &quot;Look here to see when wicket is called from javascript&quot;));
    	label.setOutputMarkupId(true);
	}
}

@SuppressWarnings(&quot;serial&quot;)
class MyWebMarkupContainer extends WebMarkupContainer implements IHeaderContributor {
	AbstractDefaultAjaxBehavior behavior;

	public MyWebMarkupContainer(String id, AbstractDefaultAjaxBehavior behavior) {
		super(id);
		this.behavior = behavior;
	}

	@Override
	public void renderHead(IHeaderResponse response) {
		response.renderJavascript(&quot;var callback = '&quot; + behavior.getCallbackUrl() + &quot;';&quot;, &quot;insertedjavascript&quot;);
	}
}
</pre>
</pre>
<p>I don&#8217;t think anyone needs explanation about how this works since it&#8217;s THAT easy. Only one more thing to add:</p>
<pre>
<pre class="syntax-highlight:java">
Map map = ((WebRequestCycle) RequestCycle.get()).getRequest().getParameterMap();
</pre>
</pre>
<p>- this to get the parameters map.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dooriented.com/blog/2008/05/09/wicketing-javascript-through-ajax/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Javascript inheritance</title>
		<link>http://www.dooriented.com/blog/2008/05/09/javascript-inheritance/</link>
		<comments>http://www.dooriented.com/blog/2008/05/09/javascript-inheritance/#comments</comments>
		<pubDate>Fri, 09 May 2008 17:12:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.dooriented.com/blog/?p=8</guid>
		<description><![CDATA[When it comes to web development, Javascript is the best client-side programming language. It has come a long way in the last years. It&#8217;s no longer just a simple scripting language. These days a lot of Javascript libraries are available to be used freely and they are incredibly powerful: Script.aculo.us, DOJO, YUI, jQuery, Prototype, to [...]]]></description>
			<content:encoded><![CDATA[<p>When it comes to web development, Javascript is the best client-side programming language. It has come a long way in the last years. It&#8217;s no longer just a simple scripting language. These days a lot of Javascript libraries are available to be used freely and they are incredibly powerful: Script.aculo.us, DOJO, YUI, jQuery, Prototype, to name just a few. </p>
<p>I am personally not very found of Javascript because of its loosely typed nature. I consider it harder to develop a large framework in Javascript compared to say, Java, because the compiler/interpreter cannot  always come to your rescue as you&#8217;d expect if you&#8217;re coming from a strongly typed language. </p>
<p>But a lot of its strength comes from the fact that it&#8217;s loosely typed. You also feel the sense of freedom that only these kind of languages (like Ruby) can offer. Types help you a lot to avoid errors and to give you important structure, but also constrain your ability to do more with your code. </p>
<p>Javascript is (or can act like) a(n almost) full fledged object oriented language. Let&#8217;s try inheritance:</p>
<p>This is the method that simulates inheritance in Javascript:</p>
<pre>
<pre class="syntax-highlight:javascript">
function inherit(child, parent) {
    var sConstructor = parent.toString();
    var aMatch = sConstructor.match( /\s*function (.*)\(/ );
    if ( aMatch != null ) { child.prototype[aMatch[1]] = parent; }
    for (var m in parent.prototype) {
        child.prototype[m] = parent.prototype[m];
    }
};
</pre>
</pre>
<p>Let&#8217;s define some Javascript objects:</p>
<pre>
<pre class="syntax-highlight:javascript">
function Element() {
	this.location = null;
	this.type = &quot;Element&quot;;
	this.selected = false;
	this.zIndex = 0;
}
Element.prototype.isInside = function(rectangle){
        dosomething;
}

function Item() {
	this.Element(); //calling the constructor of the base class

	this.lastValidLocation = null; //a rectangle
	this.type = &quot;Item&quot;;
	this.selectorItem = null;
	this.zIndex = 1;
}
inherit(Item, Element);
Item.prototype.getSelectorItem = function() {
	return this.selectorItem;
}
Item.prototype.draw = function() {
       dosomething;
}
Item.prototype.deleteItem = function() {
       dosomething;
}

function SelectorItem() {
	this.Element();

	this.items = new Array();
	this.type = &quot;SelectorItem&quot;;
	this.zIndex = 0;
}
inherit(SelectorItem, Element);
SelectorItem.prototype.getSelectorItem = function() {
	return this;
}
SelectorItem.prototype.createNewItem = function(onItem) {
       dosomething;
}
SelectorItem.prototype.draw = function() {
       dosomething;
}
</pre>
</pre>
<p>It is <b>very important</b> when you call &#8220;inherit&#8221;. That&#8217;s because Javascript does not support method override/overload, but you can also simulate this. When executing &#8220;inherit&#8221;, the prototype of the base class will be copied to the child. Then, if needed, redefine some methods so that when you call them, the new ones will get executed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dooriented.com/blog/2008/05/09/javascript-inheritance/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using a static image map with Wicket</title>
		<link>http://www.dooriented.com/blog/2008/05/09/using-a-static-image-map-with-wicket/</link>
		<comments>http://www.dooriented.com/blog/2008/05/09/using-a-static-image-map-with-wicket/#comments</comments>
		<pubDate>Fri, 09 May 2008 16:20:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[web development]]></category>

		<category><![CDATA[wicket]]></category>

		<guid isPermaLink="false">http://www.dooriented.com/blog/?p=7</guid>
		<description><![CDATA[On one of my projects I had to implement an image and link the image area to wicket.
This is how I did it:
html


&#60;div&#62;
   &#60;map name=&#34;themap&#34;&#62;
       &#60;area shape=&#34;rect&#34; coords=&#34;a1,b1,c1,d1&#34; href=&#34;#&#34;
           wicket:id=&#34;wicket1&#34; title=&#34;Title1&#34; /&#62;
      [...]]]></description>
			<content:encoded><![CDATA[<p>On one of my projects I had to implement an image and link the image area to wicket.</p>
<p>This is how I did it:</p>
<p><b>html</b></p>
<pre>
<pre class="syntax-highlight:html">
&lt;div&gt;
   &lt;map name=&quot;themap&quot;&gt;
       &lt;area shape=&quot;rect&quot; coords=&quot;a1,b1,c1,d1&quot; href=&quot;#&quot;
           wicket:id=&quot;wicket1&quot; title=&quot;Title1&quot; /&gt;
       &lt;area shape=&quot;rect&quot; coords=&quot;a2,b2,c2,d2&quot; href=&quot;#&quot;
           wicket:id=&quot;wicket2&quot; title=&quot;Title2&quot; /&gt;
   &lt;/map&gt;
   &lt;br/&gt;
   Some text here
&lt;/div&gt;
</pre>
</pre>
<p><b>Java</b></p>
<pre>
<pre class="syntax-highlight:html">
       add(new PageLink(&quot;wicket1&quot;, SomeWicketClass1.class));
       add(new PageLink(&quot;wicket2&quot;, SomeWicketClass2.class));
</pre>
</pre>
<p>Wicket is the best framework for web development.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dooriented.com/blog/2008/05/09/using-a-static-image-map-with-wicket/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Javascript - how to make something happen after some time</title>
		<link>http://www.dooriented.com/blog/2008/05/09/javascript-how-to-make-something-happen-after-some-time/</link>
		<comments>http://www.dooriented.com/blog/2008/05/09/javascript-how-to-make-something-happen-after-some-time/#comments</comments>
		<pubDate>Fri, 09 May 2008 15:51:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.dooriented.com/blog/?p=5</guid>
		<description><![CDATA[I&#8217;m writing this post because I&#8217;ve been asked a million times about this and it&#8217;s easier to point someone to a link than to explain it again  .
In Javascript, executing something after a specified time couldn&#8217;t be easier - you&#8217;ve got the setTimeout method. Just be careful to call it the right way. It [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m writing this post because I&#8217;ve been asked a million times about this and it&#8217;s easier to point someone to a link than to explain it again <img src='http://www.dooriented.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>In Javascript, executing something after a specified time couldn&#8217;t be easier - you&#8217;ve got the setTimeout method. Just be careful to call it the right way. It takes two arguments:</p>
<p>-> the <i>method</i> to be called after the specified time passed. I repeat, a <u>method</u>, not a method call.<br />
-> the amount of time, specified in milliseconds. </p>
<p>For example, using script.aculo.us, I can make the &#8220;feedback&#8221; div fade away in 10 seconds after rendering like this:</p>
<p>window.setTimeout(function() {Effect.Fade(&#8217;feedback&#8217;);}, 10000);</p>
<p>(and NOT window.setTimeout(Effect.Fade(&#8217;feedback&#8217;), 10000}; - also keep in mind Javascript is case sensitive)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dooriented.com/blog/2008/05/09/javascript-how-to-make-something-happen-after-some-time/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Javascript variables scope</title>
		<link>http://www.dooriented.com/blog/2008/04/27/javascript-variables-scope/</link>
		<comments>http://www.dooriented.com/blog/2008/04/27/javascript-variables-scope/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 20:55:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.dooriented.com/blog/?p=4</guid>
		<description><![CDATA[A variable can be defined as a segment of memory which holds some kind of value. The programmer can reference that segment of memory by using the name of the variable in order to assign it a value or to read it and use it.
In Javascript, the scope of the variable is the area of [...]]]></description>
			<content:encoded><![CDATA[<p>A variable can be defined as a segment of memory which holds some kind of value. The programmer can reference that segment of memory by using the name of the variable in order to assign it a value or to read it and use it.</p>
<p>In Javascript, the scope of the variable is the area of the script where that variable is valid to be used. The scope of the global variable is the entire script. That means you can access it anywhere between the &lt;script&gt; and &lt;/script&gt; tags and in other scripts that are executed <i>after</i> the variable definition. </p>
<p>A global variable is a variable defined in the main part of the script (not inside a function for instance). It makes no difference if you define it using the keyword var or without it. It does make all the difference in the world if you use it inside a function. </p>
<p>A local variable is a variable that is defined inside a function. It is only available to be used inside that function. Javascript allows you to use the same name for a global variable and for a local one, but when used inside that function, the local variable will be evaluated. A local variable is defined <i>only</i> by using the keyword var. If the keyword is omitted, the variable is defined as global. </p>
<p>Local variables are temporary. Global variables are permanent. Permanent variables exist throughout the execution of <i>all</i> the scripts, until being discarded when the page unloads. Temporary variables are allocated on the <i>stack</i> every time the function is called and deallocated when the execution of the function ends. Large local chunk of data causes the stack to overflow, so data structures like large arrays declared inside a function (as local) or passed to the function (as arguments) should be avoided. Use global variables for such tasks. </p>
<p>Variables in Javascript can only be of 4 types: number (there is no distinction between integer and real-valued numbers), string, Boolean and null values. Remember that Javascript is a &#8220;loosely typed language&#8221;, which means the type of the variable is not declared explicitly. Javascript implicitly determines the type of the variable based on the initial value that is assigned to it. </p>
<p>The null value is automatically converted to the initial values of other data types once that variable is initialized to a different type - when used as a number, it becomes 0, when used as a string it becomes &#8220;&#8221; and when used as a boolean it becomes false. </p>
<p>Data types are automatically converted as needed throughout the execution of the script. For instance, when an expression containing a number and a string (like a sum) is evaluated, the value returned is a string. Also, keep in mind that in Javascript evaluation is done from left to right and only paranthesys can change the order of the evaluation:<br />
10 + 10 will return 20<br />
10 + &#8220;10&#8243; will return &#8220;1010&#8243;<br />
10 + 10 + &#8220;10&#8243; will return &#8220;2010&#8243;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dooriented.com/blog/2008/04/27/javascript-variables-scope/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Wicket - Ajax like file upload on a modal window</title>
		<link>http://www.dooriented.com/blog/2008/04/23/wicket-ajax-like-file-upload-on-a-modal-window/</link>
		<comments>http://www.dooriented.com/blog/2008/04/23/wicket-ajax-like-file-upload-on-a-modal-window/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 20:52:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[web development]]></category>

		<category><![CDATA[wicket]]></category>

		<guid isPermaLink="false">http://www.dooriented.com/blog/?p=3</guid>
		<description><![CDATA[A lot of people seem to be asking how to do a file upload using AJAX. The short answer to this is that it cannot be done! That&#8217;s because it is not supported by today&#8217;s HTML / browser specs.
What you can do is just fake it  .
I&#8217;ve been using Wicket (Java Web Framework) regularly [...]]]></description>
			<content:encoded><![CDATA[<p>A lot of people seem to be asking how to do a file upload using AJAX. The short answer to this is that it cannot be done! That&#8217;s because it is not supported by today&#8217;s HTML / browser specs.</p>
<p>What you can do is just fake it <img src='http://www.dooriented.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>I&#8217;ve been using <a title="Wicket" href="http://wicket.apache.com" target="_blank">Wicket</a> (Java Web Framework) regularly for web development. Recently I needed to be able to do a file upload inside a modal window. File uploading needs form submission in order to work so I faced a few problems:</p>
<ul>
<li>My modal window was implemented as a panel. I could not do a regular form submission as that would close (submit) my modal window. I&#8217;ve been suggested to implement the modal window as a page instead of the panel so that I could use Ajax submit but I really needed it the way it was.</li>
<li>Ajax and multipart don&#8217;t cope.</li>
<li>Whatever I tried, without regular submission, the file uploading field would not return the file name so it was impossible to get.</li>
</ul>
<p>I searched the web and I finally found and implemented the solution. It involves using an iframe, but it&#8217;s pretty simple. Here&#8217;s the code if anyone else needs this:</p>
<ul>
<li>UploadIFrame.java</li>
</ul>
<pre>
<pre class="syntax-highlight:java">
public abstract class UploadIFrame extends WebPage {

    private boolean uploaded = false;
    private FileUploadField uploadField;
    private String newFileUrl;

    public UploadIFrame() {
        add(new UploadForm(&quot;form&quot;));
        addOnUploadedCallback();
    }

    /**
     * return the callback url when upload is finished
     * @return callback url when upload is finished
     */
    protected abstract String getOnUploadedCallback();

    /**
     * Called when the input stream has been uploaded and when it is available
     * on server side
     * return the url of the uploaded file
     * @param upload fileUpload
     */
    protected abstract String manageInputSream(FileUpload upload);

    private class UploadForm extends Form {
    	public UploadForm(String id) {
            super(id);
            uploadField = new FileUploadField(&quot;file&quot;);
            add(uploadField);
            add(new AjaxLink(&quot;submit&quot;){
            	@Override
            	public void onClick(AjaxRequestTarget target) {
            		target.appendJavascript(&quot;showProgressWheel()&quot;);
            	}
            });
        }

    	@Override
        public void onSubmit() {
            FileUpload upload = uploadField.getFileUpload();
            newFileUrl = manageInputSream(upload);
            //file is now uploaded, and the IFrame will be reloaded, during
            //reload we need to run the callback
            uploaded = true;
        }

    }

    private void addOnUploadedCallback() {
    	//a hacked component to run the callback on the parent
        add(new WebComponent(&quot;onUploaded&quot;) {
        	@Override
            protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                if (uploaded) {
                	if (uploadField.getFileUpload() != null){
	                    replaceComponentTagBody(markupStream, openTag,
	                            &quot;window.parent.&quot; + getOnUploadedCallback() + &quot;('&quot; +
	                            uploadField.getFileUpload().getClientFileName() + &quot;','&quot; +
	                            newFileUrl +&quot;')&quot;);
                	}
                    uploaded = false;
                }
            }
        });
    }
}
</pre>
</pre>
<ul>
<li>UploadIFrame.html</li>
</ul>
<pre>
<pre class="syntax-highlight:html">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.1//EN&quot; &quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt;
&lt;html xmlns:wicket&gt;
&lt;head&gt;
&lt;link href = &quot;../counters/css/style.css&quot; type = &quot;text/css&quot; rel = &quot;stylesheet&quot;/&gt;
&lt;!--[if IE]&gt;
&lt;link href = &quot;../counters/css/ie.css&quot; type = &quot;text/css&quot; rel = &quot;stylesheet&quot;/&gt;
&lt;![endif]&#8211;&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	function showProgressWheel() {
		document.images[0].style.display = &#8216;block&#8217;;
	  	// delay the wheel a bit so it can locally shine
	  	setTimeout(function() { document.forms[0].submit() }, 800);
	  	return false;
	}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
	&lt;form wicket:id=&quot;form&quot;&gt;
		&lt;table style=&quot;width:500px; margin-top:0px;&quot;&gt;
	  		&lt;tr&gt;
	  			&lt;td&gt;
	    			&lt;input style=&quot;width:auto;&quot; wicket:id=&quot;file&quot; type=&quot;file&quot;/&gt;
	    		&lt;/td&gt;
	    		&lt;td&gt;
					&lt;a wicket:id=&quot;submit&quot;&gt;Change Avatar&lt;/a&gt;
				&lt;/td&gt;
				&lt;td&gt;
	    			&lt;img src=&quot;images/indicator.gif&quot; style=&quot;display:none&quot;/&gt;
	    		&lt;/td&gt;
	    	&lt;/tr&gt;
	    &lt;/table&gt;
  &lt;/form&gt;

  &lt;script wicket:id=&quot;onUploaded&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
</pre>
<ul>
<li>UploadPanel.java</li>
</ul>
<pre>
<pre class="syntax-highlight:java">
public abstract class UploadPanel extends Panel {

    private InlineFrame uploadIFrame = null;

    public UploadPanel(String id) {
        super(id);
        addOnUploadedCallback();
        setOutputMarkupId(true);
    }

    /**
     * Called when the upload load is uploaded and ready to be used
     * Return the url of the new uploaded resource
     * @param upload {@link FileUpload}
     */
    public abstract String onFileUploaded(FileUpload upload);

    /**
     * Called once the upload is finished and the traitment of the
     * {@link FileUpload} has been done in {@link UploadPanel#onFileUploaded}
     * @param target an {@link AjaxRequestTarget}
     * @param fileName name of the file on the client side
     * @param newFileUrl Url of the uploaded file
     */
    public abstract void onUploadFinished(AjaxRequestTarget target, String filename, String newFileUrl);

    @Override
    protected void onBeforeRender() {
        super.onBeforeRender();
        if (uploadIFrame == null) {
            // the iframe should be attached to a page to be able to get its pagemap,
            // that's why i'm adding it in onBeforRender
            addUploadIFrame();
        }
    }

    /**
     * Create the iframe containing the upload widget
     *
     */
    private void addUploadIFrame() {
        IPageLink iFrameLink = new IPageLink() {
        	@Override
            public Page getPage() {
                return new UploadIFrame() {
                	@Override
                    protected String getOnUploadedCallback() {
                        return &quot;onUpload_&quot; + UploadPanel.this.getMarkupId();
                    }

                    @Override
                    protected String manageInputSream(FileUpload upload) {
                    	return UploadPanel.this.onFileUploaded(upload);
                    }
                };
            }
        	@Override
            public Class&lt;UploadIFrame&gt; getPageIdentity() {
                return UploadIFrame.class;
            }
        };
        uploadIFrame = new InlineFrame(&quot;upload&quot;, getPage().getPageMap(), iFrameLink);
        add(uploadIFrame);
    }

    /**
     * Hackie method allowing to add a javascript in the page defining the
     * callback called by the innerIframe
     *
     */
    private void addOnUploadedCallback() {
        final OnUploadedBehavior onUploadBehavior = new OnUploadedBehavior();
        add(onUploadBehavior);
        add(new WebComponent(&quot;onUploaded&quot;) {
        	@Override
            protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                // calling it through setTimeout we ensure that the callback is called
                // in the proper execution context, that is the parent frame
                replaceComponentTagBody(markupStream, openTag,
                        &quot;function onUpload_&quot; + UploadPanel.this.getMarkupId() +
                        &quot;(clientFileName, newFileUrl) {window.setTimeout(function() { &quot; +
                        onUploadBehavior.getCallback() + &quot; }, 0 )}&quot;);
            }
        });
    }

    private class OnUploadedBehavior extends AbstractDefaultAjaxBehavior {
        public String getCallback() {
            return generateCallbackScript(
                    &quot;wicketAjaxGet('&quot; + getCallbackUrl(false) +
                    &quot;&amp;amp;amp;newFileUrl=' + encodeURIComponent(newFileUrl)&quot; +
                    &quot; + '&amp;amp;amp;clientFileName=' + encodeURIComponent(clientFileName)&quot;).toString();
        }
        @Override
        protected void respond(AjaxRequestTarget target) {
        	UploadPanel.this.onUploadFinished(target, getRequest().getParameter(&quot;clientFileName&quot;), getRequest().getParameter(&quot;newFileUrl&quot;));
        }
    };
}
</pre>
</pre>
<ul>
<li>UploadPanel.html</li>
</ul>
<pre>
<pre class="syntax-highlight:html">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.1//EN&quot; &quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt;
&lt;html xmlns:wicket&gt;
&lt;wicket:panel&gt;
  &lt;!-- I put the callback snippet at the body so that is rendered for each panel instead of once --&gt;
  &lt;script wicket:id=&quot;onUploaded&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
  &lt;iframe wicket:id=&quot;upload&quot; frameborder=&quot;0&quot; style=&quot;height:60px; width:500px; overflow:hidden&quot;&gt;&lt;/iframe&gt;
&lt;/wicket:panel&gt;
&lt;/html&gt;
</pre>
</pre>
<ul>
<li> This is how to use it:</li>
</ul>
<pre>
<pre class="syntax-highlight:java">
		add(new UploadPanel(&quot;myUpload&quot;){
			@Override
			public String onFileUploaded(FileUpload upload) {
				if (upload != null){
					if(upload.getSize() &gt; 1000000) {
						return &quot;The Picture Is Too Big !&quot;;
					} else {
				        try {
							jdbcSignUp.changeAvatarFor(((WebSession)getSession()).getUser().getUsername(), upload.getInputStream(), (int)upload.getSize());
				        	//save on server side here
				        	//and return the url of the saved file
	                        return &quot;The Picture Has Been Uploaded&quot;;
				        } catch (IOException e) {
							log.error(&quot;Exception:&quot;, e);
						}
					}
				}
				return &quot;&quot;;
			}

			@Override
			public void onUploadFinished(AjaxRequestTarget target, String filename, String newFileUrl) {
				//when upload is finished, will be called
				messageLabel.setModelObject(newFileUrl);
				target.addComponent(messageLabel);

				avatarImage.setImageResource(new MyBlobImageResource(((WebSession)getSession()).getUser().getUsername()));
				target.addComponent(avatarImage);
				((BasePage)parent).onAvatarChanged(target);
			}
		});
</pre>
</pre>
<p>Most credit should go to <a title="Vincent Demay" href="http://www.demay-fr.net/blog" target="_blank">Vincent Demay</a>, although his code contains errors.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dooriented.com/blog/2008/04/23/wicket-ajax-like-file-upload-on-a-modal-window/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
