28 Feb 2011, 11:10pm
gwt java:

leave a comment




  • How to Execute Code When the GWT Application Is Going Down

    My goal was to store the UI state of my application just before it gets terminated to be able to restore it next time the way the user left it the other day.

    I tried to add an addAttachHandler to the RootPanel to get informed about the root panel getting detached from the DOM so that I can finalize my application. Surprisingly, that does not work in Chrome (tested Chrome and Firefox only).

    But besides that this sounds like a bug to me, I found the “proper” way of doing things before the application ends:

    Window.addWindowClosingHandler(new Window.ClosingHandler() {
    	@Override public void onWindowClosing(ClosingEvent event) { ... }
    });

    In the end, I think something like Document.addUnloadHandler would be more suggestive… closing the window or reloading a page is both exiting the application by unloading the DOM – not closing the window.

    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.

    14 Feb 2011, 9:26pm
    linux:

    leave a comment




  • Logitech Communicator STX Webcam vs. Ubuntu (64bit) & Skype 2.1

    There are many tutorials out there solving the problem by making Skype use Video for Linux version 1 drivers instead of version 2, as that particular webcam does not seem to get along with the newer version. It boils down to the following lines:

    Create a file in /usr/local/bin/skype and insert

    #!/bin/bash
    LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so /usr/bin/skype

    Finally, make it executable by sudo chmod a+x /usr/local/bin/skype
    Use this file to start Skype from now on. Done.

    But not for me. Skype refused to eat it: ERROR: ld.so: object '/usr/lib/libv4l/v4l1compat.so' from LD_PRELOAD cannot be preloaded: ignored.
    Until I finally found this one here.

    Just to cut a long story short, here is why: all the other solutions work for 32bit Linux only — but hey, I do not have any of my old laptop’s potential to be wasted — I am running 64bit Ubuntu (tested with version 9.10 and 10.10 64bit). And with a minor tweak, the fix will work for you, too. :)

    Install the video4linux libraries:

     sudo apt-get install lib32v4l-0

    And change /usr/local/bin/skype to

    LD_PRELOAD=/usr/lib32/libv4l/v4l1compat.so /usr/bin/skype

    Its just about the “32″ — and one good example more of why you should put meaningful error messages in whatever software!

    Thanks Eoin Murphy.

    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.

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

    14 Dec 2010, 11:49am
    java:

    leave a comment




  • Casting collections in Java

    Assuming two classes A and B as

    class A {}
    class B extends A {}

    It is just logical that casting B to A works fine:

    A anObjectOfClassA = new B();

    But when it comes to collections of As and Bs, a strange phenomenon appears in Java:

    Collection<B> collectionOfBs = new LinkedList<B>();
    Collection<A> collectionOfAs = (Collection<A>)collectionOfBs; // this line does not compile!

    That is, though the collection wrapper (the java.util.Collection class) is the same and B extends A, casting a collection of objects of class B to a collection of objects of class A throws an compile-time error.

    But it works using generics (the whole class for the sake of completeness and reusability):

    import java.util.Collection;
    import java.util.LinkedList;
    
    public class CollectionCastingExample {
    
      class A {}
      class B extends A {}
    
      Collection<B> collectionOfBs = new LinkedList<B>();
    //  Collection<A> collectionOfAs = (Collection<A>)collectionOfBs;
    
      Collection<A> collectionOfAs = downCastCollection(collectionOfBs, A.class);
    
      /**
       * Casts a collection of objects of class B where B extends A to a collection of objects of class A.
       *
       * @param <T> Base class
       * @param collection Collection of objects of a class extending T
       * @param aClass Representation of T
       * @return Collection of objects of T casted from given collection of objects of a class extending T.
       */
      @SuppressWarnings("unchecked")
      public static <T> Collection<T> downCastCollection(Collection<? extends T> collection,
          Class<T> aClass) {
        return (Collection<T>) collection;
      }
    }

    Despite the fact that a @SuppressWarnings("unchecked") is required, it avoids iterating over the whole collection of Bs and casting each of them from B to A plus adding them to a new collection of As.

    17 Sep 2010, 3:49pm
    gwt java:

    leave a comment




  • Uploading File to Server: Access Denied

    Simple task: upload a file to the server. Achieved so far: upload form works and the file gets transferred to the server.
    But as soon as the file is written, I get one of that:
    “java.security.AccessControlException: access denied (java.io.FilePermission /some/folder/upload__71c20601_12b1b66bc39__7ffa_00000000.tmp write)”
    I am amazed how much searching is required to find some information about how to modify the file permissions for the local app engine! And in the end, it turns out that there is an issue with the GAE on Mac but without a way to configure the local Jetty server to allow write access. :(
    As soon as I deploy the app to a Tomcat on Linux, it works like a charm!

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

    9 Sep 2010, 4:48pm
    jobs projects:

    1 comment




  • GWT Does Not Load Module in Local AppEngine

    The issue arose after I renamed the module file (ending with .gwt.xml) to better represent the module functionality. I also updated all relevant files in the project (search for files containing the old name to find them) accordingly.

    Starting the application after that modifications ended up in an error (“[ERROR] Unable to find ‘<old module name>.gwt.xml’ on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?”) as the AppEngine tried loading the module by its old name.

    Solution: Delete the launch profile for the project (by choosing “Run As…” -> “Run Configurations…” from the context menu).

    Obviously the GWT does not check nor update the automatically generated launch profile thus you need to delete it to force the GWT to create a new profile from scratch taking the project changes into account. You might also adjust the profile according to the changes made, but deleting it is the safe and easy way.

    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.

    30 Apr 2010, 11:04pm
    linux:

    leave a comment




  • Flickering Screen After Upgrade to Kubuntu 10.04

    First of all, I was amazed how smoothly the upgrade went. (K)Ubuntu and I guess other distributions as well have gone quite a far way to become platforms for everybody — with a lot of software but even more tools, helpers and automatic background services — like the nice upgrade service.

    There was only one problem I ran into and I want to share the solution.

    I am running Kubuntu 10.04 (just upgraded today) on a Lenovo IBM ThinkPad R60 (Yes, one of those with both brands on them ;) ) with an ATI Radeon X1400 graphics device. But since upgraded, my display started flickering — not permanently but unbearable.

    The solution that worked for me (at least partially — see below — and this is of cause depending on the graphics device type) was to install the proprietary ATI drivers. I know, it is not a good solution, I do not like to use them neither and if anyone out there is reading this with a better solution in mind, let me know it!! :)
    Edit 2011-02-14: Added a new post with an additional, alternative solution here.

    So what you need to do is  to get your favorite package management tool (e.g. Synaptic or KPackageKit) — just hit Alt+F2 and type “package” and pick KPackageKit from the list (it will ask you for the super user password). Then, search for “fglrx”. In KPackageKit, you will find a result titled “Video driver for the ATI graphics accelerator”, and something below the title like “fglrx – 2:8.723.1-0ubuntu3″ but the version number should not matter. Click the arrow to the right, hit “Apply” and after rebooting, everything should perfectly without any configuration.

    Good luck. ;)

    Problems noticed so far: The driver causes “Segmentation Fault” error messages, e.g. when trying to open up the display settings. That’s ugly, I know. But you can read in several forum and blog posts, that the support for the former nicely working driver for the ATI X1400 has been discontinued since 10.04, hence, it’s just good luck that the fglrx works somehow — good to know *after* upgrading, right. But still, the flickering was unbearable for me, thus, I accept the issues for now. I will keep my eyes peeled hoping for a proper solution in the future.

    18 Feb 2010, 7:10pm
    jobs projects:

    leave a comment




  • Getting Drupal’s Access Control Module to Work Properly

    After setting up some content types — some public, some internal. I installed the Access Control module, set up internal content not to be visible to anonymous users — but without any effect.

    After some research, but without success, I realized the *Advanced* section at the bottom of the Access Control tab for each content type. And now the magic trick: Increase the weight and you are done. So I guess the build in access management was fighting the Access Control module, so it is up to you to make your favorite module stronger by giving it more weight. — I doubt this is intuitive. Additionally, it is for sure difficult to simply find the tiny little select box down there in a section, which is by default folded.

    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!

    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" />