18 Jan 2012, 10:50am
gwt java:

leave a comment




  • 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.

    18 Feb 2011, 1:27pm
    gwt:

    1 comment




  • 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.

    7 Feb 2011, 2:58pm
    java:

    leave a comment




  • 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.

    4 Feb 2011, 10:10pm
    gwt:

    leave a comment




  • 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) {
    		...
    2 Feb 2011, 11:45pm
    gwt jobs:

    leave a comment




  • 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) {“.

    2 Feb 2011, 11:41pm
    java projects:

    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!

    3 Dec 2010, 4:02pm
    gwt:

    leave a comment




  • 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.

    16 Sep 2010, 6:02pm
    jobs projects:

    3 comments




  • 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! :)

    25 Aug 2010, 10:41am
    jobs projects:

    leave a comment




  • 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.

    2 Feb 2010, 7:13pm
    jobs projects:

    leave a comment




  • 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!

    5 Aug 2009, 1:12pm
    php:

    leave a comment




  • 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.

    Put the whole code and markup in a file with file extension “.php”.
    In my case, every user ending up at “www.svenbuschbeck.net” should to be forwarded to “www.svenbuschbeck.net/wordpress/home”. So I adjusted the script below like “$url = ‘wordpress/home’”, named the file “index.php” and put it in the root folder. Done :) .
    <?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>
    24 Jun 2009, 3:03pm
    jobs projects:

    2 comments




  • 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" />
    16 Apr 2009, 5:46pm
    jobs:

    3 comments




  • 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.