gwt java: ant code code reuse eclipse example fix gwt library tutorial web application
leave a comment
Multiple Projects and GWT
When working on several large scale projects (in Eclipse), it’s convenient and of course more efficient to share and reuse code via common libraries. While those are in an early stage they need to be changed a lot and therefore it’s handy to link projects in instead of creating new jars each time the library has been updated.
Unfortunately, this standard approach for Java development in Eclipse does not work that straight forward as with plain old Java projects, it requires three steps in total:
- Link the library project to all relevant projects (“Project Preferences” -> “Java Build Path” -> “Projects” -> “Add…”)
- Then, add the client-side code of the library (by adding it as a module.) Therefore, edit the gwt.xml of your application and add for example
<inherits name="net.svenbuschbeck.gwt.lib.SuperLib "/>where SuperLib is the file name of the gwt.xml file in you library project and before that is the package it lies in. - Add the code to the project by linking a source folder. Unfortunately, this is required if you do not want to write an Ant script to compile your application. (If you prefer Ant check this out) I don’t like the idea of using such a script because if you forget to run it each time you make changes, you will end up in confusion—let’s go for the convenient, automatic way then.
- Add a folder to your application project; open the “advanced” section in the folder creation dialog, select “Link to alternate location” and pick the source folder (usually “src”) of your library project. (Hint: if you work within a team using a versioning system, you can create a variable for that folder and use this instead. This way, each of your colleagues can put the library project in a different folder and accommodate for that by adjusting the variable
) - Right click the folder, “Build Path” -> “Use as Source Folder”. Done.
- Add a folder to your application project; open the “advanced” section in the folder creation dialog, select “Link to alternate location” and pick the source folder (usually “src”) of your library project. (Hint: if you work within a team using a versioning system, you can create a variable for that folder and use this instead. This way, each of your colleagues can put the library project in a different folder and accommodate for that by adjusting the variable
Surprisingly, the GWT plugin for Eclipse does not honor the project linking, thus all the references need to be made explicit or you will end up with lots of the following: ClassNotFoundException.
gwt java javascript: bug code error fix gwt Java Javascript web application
leave a comment
“An invalid or illegal string was specified” Exception in GWT
While working with canvas (drawing stuff on it) in GWT, suddenly and in a seemingly unpredictable manner I got following error message now and then from within the GWT framework code:
com.google.gwt.core.client.JavaScriptException: (NS_ERROR_DOM_SYNTAX_ERR): An invalid or illegal string was specified;
Again, GWT tricked me into thinking I am writing Java code and made me forget about that it is going to be compiled into Javascript. And because of the latter, a division by zero does not throw an DevisionByZeroException, no, it returns NaN even for native data types (there is no differentiation between double and Double in Javascript — there is only the object-version of double, which can be of value Double.NaN).
But also GWT was not prepared to handle Double.NaN. When calling canvas.getContext().drawImage(image,x,y) and one of x and y or both were Double.NaN I got the error message shown above. If you got the same… you know what to do now: check all devisions in your code for potential devisions by zero!!
Sorting Lists in GWT
Quick one:
Lists.sort(list, comparator) is not implemented in the GAE JVM.
But, as a replacement/alternative, Collections.sort(list, comparator) is.
Disable an Anchor in GWT
Unexpectedly, calling setEnabled(false) does not prevent a link/anchor from being clicked. That means, the click events still get triggered.
The reason is more or less a bug in GWT as it updates the list of events that are going to be triggered only at the moment when it gets attached to the DOM (Btw, in GWT, this process is called to sink and to unsink events, where the former enables a specific event to be triggered and the latter disables it).
I found a workaround by creating my own Anchor class and forcing the underlying GWT Anchor to update the list of events to be sunk.
...
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Event;
public class Anchor extends com.google.gwt.user.client.ui.Anchor {
@Override public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
if (isAttached()) {
onDetach();
if (enabled) {
sinkEvents(Event.ONCLICK);
} else {
unsinkEvents(Event.ONCLICK);
}
onAttach();
}
}
@Override protected void onLoad() {
super.onLoad();
if (isEnabled()) {
sinkEvents(Event.ONCLICK);
} else {
unsinkEvents(Event.ONCLICK);
}
}
}
Note the onLoad method, it sets up the state when the widget gets attached to the DOM the first time. It is required because setEnabled() could have been called before the anchor got attached.
If you encountered the same issue, please vote for this bug report.
Java Cookies from the Future Past
While working with cookies in Java/GWT and thus—to set the expire date—with Date, I found a doubtful Java behavior.
My goal was to set a cookie to expire in about one month from today like this:
Date expires = new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 30);
Cookies.setCookie("myCookie", "myData", expires);
And kept wondering why the cookie never got stored.
And finally created a simple test case like this:
Date today = new Date(); Date tomorrow = new Date(today.getTime() + 1000 * 60 * 60 * 24); Date nextMonth = new Date(today.getTime() + 1000 * 60 * 60 * 24 * 30);
And got following dates:
today=Mon Feb 07 14:27:50 CET 2011 tomorrow=Tue Feb 08 14:27:50 CET 2011 nextMonth=Tue Jan 18 21:25:02 CET 2011
According to Java’s calculation, the cookie was expired already before even being set.
Took me a bit to understand why:
1000 * 60 * 60 * 24 * 30 = 2,592,000,000 = 0x9A7EC800
Thus, the first bit got set to one… a classical overflow causing the integer value to become negative — just try:
System.out.println(1000 * 60 * 60 * 24 * 30);
It will print out -1702967296.
Fix: Add a little L will solve the issue by forcing the compiler to calculate using the scope of long:
Date nextMonthLong = new Date(today.getTime() + 1000L * 60 * 60 * 24 * 30);
I guess I will fall for that one again sometime as the error is not obvious in my opinion — especially because getTime() returns a long and still, the compiler sticks with an int for the multiplication part.
Disable Context Menu in GWT
To make use of the right mouse button, it is necessary to disable the native browser context menu (the popup menu appearing on right click). This can be achieved like this:
RootLayoutPanel.get().addDomHandler(new ContextMenuHandler() {
@Override public void onContextMenu(ContextMenuEvent event) {
event.preventDefault();
event.stopPropagation();
}
}, ContextMenuEvent.getType());
Same should work for RootPanel.
After that, it is possible to make use of the right mouse button for example like this:
someWidget.addDomHandler(new MouseMoveHandler() {
@Override public void onMouseMove(MouseMoveEvent event) {
if (event.getNativeButton() == NativeEvent.BUTTON_RIGHT) {
...
GWT Error Message “Asked for attribute parser of no type”
Example:
public boolean isCollapsed() {
return panel.isVisible();
}
public void setCollapsed() {
panel.setVisible(false);
}
Seen it? Despite the complicated and cryptic error message, the mistake is rather simple: the setter method is missing a parameter, i.e. “public void setCollapsed(boolean collapsed) {“.
java projects: API code example flickr Java tutorial web application
leave a comment
Authentication with FlickrJ
When you know about username and password logins, the whole Flickr authentication process for web applications seems a little weird on first sight, nevertheless it is logical and necessary after you have done some reading (for example the official Flickr WebApp Auth HowTo).
To get started and into coding quickly (using FlickrJ and Java) I recommend this page. Especially the code example is excellent in my opinion! Thanks Andy Sacher!
Adding a New Service (GWT)
Adding a new servlet/service to you GWT application is quite straight forward, e.g. by copying the example “greetingService” or creating a new servlet. But it’s easy to overlook a required change/adjustment of your project’s configuration and you might end up with an error message like “Blocked attempt to access interface ‘some.package.SomeService’, which is not implemented by ‘some.other.package.SomeOtherServiceImpl’; this is either misconfiguration or a hack attempt”.
Check list (some should be replaced with whatever you want to call your new service):
- Copy or create files:
SomeService.java and SomeServiceAsync.java in client package
SomeServiceImpl.java in server package + change implementation statement to SomeService - Adjust web.xml:
<servlet> <servlet-name>someServlet</servlet-name> <servlet-class>some.package.SomeServiceImpl</servlet-class> </servlet> <servlet-mapping> <servlet-name>someServlet</servlet-name> <url-pattern>/[copy base directory name from other service declaration]/some</url-pattern> </servlet-mapping>
- Annotate interface SomeService.java:
@RemoteServiceRelativePath("some") - Connect to your new service in the client:
private final SomeServiceAsync someService = GWT.create(SomeService.class);
Definitely some possibilities to make a mistakes or miss something here.
GWT FileUpload: Adding Widgets to a FormPanel
If you build your first GWT form, for example something like that:
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent"> <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder" xmlns:g="urn:import:com.google.gwt.user.client.ui"> <g:HTMLPanel> <g:FormPanel ui:field="form"> <g:FileUpload ui:field="uploadField" name="file"/> <g:SubmitButton ui:field="submitButton">Upload</g:SubmitButton> </g:FormPanel> </g:HTMLPanel> </ui:UiBinder>
And your console keeps telling you during runtime something like this: “java.lang.IllegalStateException: SimplePanel can only contain one child widget”. Instead of writing a long page of explanations and complaints like I did before, it’s simply like that:
“Just put all your widgets in a panel (like HorizontalPanel) and add that panel to the FormPanel.” (Jake − cf. comment below)
Thanks Jake!
First GWT Steps
Just started to work with GWT – a pretty interesting approach for web development compared to PHP or JSF. The whole Application engine is quite impressive especially allowing you to quickly test your applications locally by supporting automatic hot deployment after each code update.
One thing that took me a while was one of that “[ERROR] Unable to find ‘[some-file].xml’ on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?” errors. If you are sure the file is in place, I realized restarting the App Engine or Eclipse mostly solves that problem.
How to Make Jquerymenu for Drupal Keep its State on Page Reload
While setting up the website for my new project “Glocal” www.glocal-project.eu), I came across the problem of finding a proper menu module. Something easy to use, stable and efficient in the same time for the complex intranet structure (therefore, sorry, but you will not be able to see my solution there unless you are a project member). Something with a high usability in the end. Activemenu is still quite buggy and DHTML Menue requires a double click to actually open a page — unbearable in a non-doulbe-click environment like the Internet — who is supposed to guess, that this menu requires a double click?? Leaving me with JQuerymenu.
First impression: perfect! Open and close branches by clicking (+) or (-) — view page by clicking menu item label. Even the few styling issues could be fixed easily by using CSS. But as soon, as someone clicks a label, the menu collapses to its default status. It does not remember its last status after loading a different page with the same menu.
Is this it? All module have critical downsides like this? I was quite disappointed!
But I taught JQuerymenu to remember!
As it is quite some code, I will not post it here directly, but added it to the tracker page for this “feature request” or you can download the two updated files (jquerymenu.js and jquerymenu.module) here and replace the once in your /sites/all/modules/jquerymenu folder.
But please be careful, it should be considered an alpha version, there are quite some weaknesses (see tracker page). Any feedback or suggestions are very welcome!
Simple redirection using PHP and HTML meta tag
Something everyone needs once in a while… maybe you moved a file on your server or you did not install wordpress in the root folder of your webserver — like I did neither
— a redirection or forwarding can be an easy answer.
The script is rather simple — it redirects in three ways to provide a reasonable fall back if any of the automatic redirecting fails. To use it on your server, just enter the URL/address/file name/folder you would like the user to be redirected to in the second line of the page/script.
<?php
$url = 'put your URL between the parenthesis';
header("Location: $url");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta http-equiv="refresh" content="0;url=<?php echo $url; ?>"/>
<title>You will be redirected... </title>
</head>
<body>
If you are not redirected automatically, please click <a href="<?php echo $url; ?>">here</a>.
</body>
</html>
fixing flex VideoDisplay CuePointManager
I was creating a Flex application to show slides and a presentation video of previously recorded presentations. Accordingly, each slide should appear at a certain point of time in the video — calls for cue points!
As all slides and there appearance are stored in a text file in my case, I started adding cue points with ActionScript. But as soon as the video can be controlled with a slider, allowing to shift for– and backwards, the event handler “cuePoint” was not triggered any more. Thus, the slides where not changed correctly as the user jumps ahead, as the cue points between the previous and the new position did not cause and cue point event.
Finally I wrote AdvCuePointManager, inheriting from CuePointManager, but which can deal with jumping back– and forward.
Copy it to “<your source folder>/net/svenbuschbeck/flex/video” and use it as follows:
<mx:VideoDisplay cuePointManagerClass="net.svenbuschbeck.flex.video.AdvCuePointManager" />
how to install memchached 1.2.2 from source
I have installed an instance of memchached version 1.2.2 on one of our servers (Debian etch) today and to keep you from spending a whole afternoon, see my everything-step-by-step instruction below.
Memchached is a distributed hash map, which can be used for example to speed up any kind of web application, see website for details. In our case, we want to use it as temporary data store. I will report about the experiences in a latter post.
introduction
Always refer to this page for details, but I created a version with less text but including steps to really start from scratch.
All lines starting with # are command lines, i.e. you need to type into a linux shell.
Output of any kind is always surrounded by ” even if it is multiline output.
content
a. get libevent (needed to install memcached)
b. get memcached and verify installation
c. use and test memcached within Java with junit/ant
a. installing libevent 1.3
a.1. check for current version of libevent
a.1a.
Log in as root or get super user rights by calling su
# updatedb
# locate libevent
If there is output including “libevent1” and/or “libevent-1″ (ignore package files like *.deb) -> a.1b, otherwise a.2
a.1b. removing old libevent version
# apt-get remove –purge libevent1
# Y
# updatedb
# locate libevent
Should now return nothing or package files only, i.e. you are ready for installation
a.2. installing libevent 1.3
a.2a downloading and unpacking
# cd /usr/local/src
# wget http://monkey.org/~provos/libevent-1.3b.tar.gz
# tar zxvf libevent-1.3b.tar.gz
# cd libevent-1.3b
a.2b. configuring
# ./configure
check the output, if it contains something like “configure: error: no acceptable C compiler found in $PATH” -> a.2c. otherwise a.2d.
a.2c. compiling
# apt-get install gcc
Redo a.2b.
I got output like “C compiler cannot create executables”, reading file ‘config.log’ did not help me at all. Googling finally did, as I found a forum entry, pointing out a missing lib.
So try this:
# apt-get install libc-dev
Redo a.2b.
If that did not solve it… sorry … google on, there is no sense in going on without solving this issue. :-/
a.2d. make it!
# make && make install
If you get something like “-bash: make: command not found” -> A.2e, otherwise A.3.
a.2e.
# apt-get install make
Redo a.2d.
a.3. configuration
Press the Esc key as you read [esc] in the commands below.
# vim /etc/ld.so.conf.d/libevent-i386.conf
# i/usr/local/lib/[esc]:wq
# ldconfig
b. install memchached and verify installation
b.1. download, unpack and install memchached
# cd /usr/local/src
# wget http://danga.com/memcached/dist/memcached-1.2.2.tar.gz
# tar zxvf memcached-1.2.2.tar.gz
# cd memcached-1.2.2
# ./configure
# make && make install
After installing gcc and libc-dev in section a, this one went easily for me — if you skipped section a and run in problems here, please install gcc and libc-dev (see a.2c).
b.2. verify installation of memchached
b.2a. start memchached server
# memcached –u www-data –vv
Output should end with line “<3 server listening”. Perfect!
b.2b test server
I will refer to this shell in front of you as server shell below. Now, open another shell on the same machine, I will refer to it a client shell.
# telnet localhost 11211
You should see something like “<7 new client connection” on the server shell, switch back to client shell.
# set test1 1 10000 1
# a
You should see “STORED” on client shell and the two following lines on server shell
”<7 set test1 1 10000 1
>7 STORED”
Perfect!
You did it, your memcached is up and running
c. memcached and Java
I wrote a little test package using a Java client library for memcached from here, together with junit and ant. You can download it to have a look how simple using memcache is and to verify your installation with an included junit test, automated with an ant build file.