Dojo checkboxes

I recently hit this snag when working with Dojo. Basically I wanted to set the checked status of a checkbox on a webpage programmatically. Simple you might think? Apparently not as easy as it should be.

Interaction with checkboxes has changed slightly in Dojo 1.1 (apparently) but myCheckbox.setValue(true) should be valid. When calling dojo.byId('my-checkbox-id').setValue(true), I was getting an error saying the method didn’t exist. The object was definitely the checkbox as I could determine the correct checked state from that object (myCheckbox.checked) so I was very confused. I then remembered another way to access objects with Dojo is using the HTML attribute ‘jsId‘. This creates a global javascript variable referring to that object in the DOM. So I set something like jsId='myCheckbox' and then called myCheckbox.setValue(true) and it worked!

Very odd behaviour. I can only guess that the javascript object created by Dojo using jsId and dojo.byId() is a different bit of code and creates an object pointing to the DOM checkbox object in a different way. Very strange but at least there’s the workaround above…

Replacing Python on a Battlefield 2 server

Lately I’ve been doing some work (I promise it is real work) with Battlefield 2. The server part of the game has a cut-down version of Python built in so that you can interact with it, modifying aspects of the game and reacting to events etc. More details on this can be found on this wiki.

Anyway, the Python version it uses is pretty old (2.3.4, current version is 2.5.2) and it’s had functionality cut, e.g. no threading support. For the work I’m doing I needed threading support and a few other things so wanted to swap out the version of Python for another - turns out this wasn’t obvious so here are the steps:

  1. Replace the Battlefield 2 Python library (e.g. ‘bf2/bin/ia-32/libdice_py.so’) with the version of your choice. I just linked to my locally installed version ‘/usr/lib/libpython2.5.so’.
  2. Replace the Python library path at runtime to use the new libraries, e.g. edit ‘bf2/python/bf2/__init__.py’ and putting the equivalent of ’sys.path = ['/usr/lib/python2.5/', '/usr/lib/python2.5/lib-dynload/', 'python', 'mods/bf2/python', 'admin']‘ after the ‘import sys’ line.

I do get the following warning at runtime: ’sys:1: RuntimeWarning: Python C API version mismatch for module host: This Python has API version 1013, module host has version 1012.’ but it doesn’t seem to cause any problems (so far!).

Online tools outside and inside the firewall

The one thing that really frustrates me about most online tools is that I can’t use them for work - confidentiality and other boring reasons stand in the way of me using twitter to talk about confidential IBM things, Remember The Milk for managing TODO lists (I do anyway but keep any confidential stuff elsewhere), Google Reader for reading internal blogs etc etc. This means we end up creating copies of these tools internally, e.g. BlueTwit (like twitter), Beehive (like Facebook et al), Dogear (like del.icio.us), Spectacular! (like Google Reader)… the list goes on.

Now I understand we need these tools so we can keep certain information out of the public space but it’s frustrating that I have to use two different tools for the same job. A suggestion - why don’t we write the backend stuff to store information privately and a load of Greasemonkey scripts for each of the different apps to integrate both into the same user interface? Just a thought.

Having said all that, maybe it’s a good thing - maybe it keeps me in the right mindset for not disclosing things accidentally. When I’m using an internal tool I know what I can and should say and vice-versa for the external tools.

Increasing SPARQL performance

Recently I’ve been playing (for work, honest!) with the Semantic Web related technology SPARQL, a query language for RDF. I ended up creating some very complex queries for my OWL ontologies that were being executed through Jena, a Java framework for building Semantic Web apps. These queries were running so incredibly slowly that I thought we might have taken the completely wrong direction for the project and would have to start from scratch. Actually it turns out I was doing something wrong which is always good to hear…

After looking at some code by someone else in my department that had done some similar work before, we realised that they were setting a specification that defines the language and reasoner to use when creating the ontology model. Now they had no idea why they were setting that but doing the same in my code resulted in a 700% performance enhancement! Quite a productive day :D .

So if you’re ever using SPARQL and Jena and having some performance issues, remember to set the appropriate specification (in my case OWL_DL_MEM).

Am I online?

I’ve recently changed my broadband provider and wondered how reliable they’d be. I figured I needed a way for my house to let me know whenever the connection had dropped and for how long for etc. Remembering my post a little while ago about
publishing to Twitter about local Bluetooth devices
, I thought I could do something similar. So now I’ve written a little script (twitter-netcheck) that notices whenever my internet connection is dropped and then publishes to Twitter whenever it comes back. It also sends me a direct message so I get alerted by SMS or IM immediately. Quite handy I think - try it out for yourself

SWT browser widgets and local plugin files

Ok, getting very technical from my normal postings but I found this really difficult to track down so thought it was worth posting somewhere. I’ve creating an application inside the Eclipse framework and wanted to write some HTML and display inside the SWT browser widget. It’s pretty easy to do this with files elsewhere, you simply give the browser widget a URL. However, if you want to load stuff from a local project it turns out to be a bit less obvious (technically this applies to getting access to any files from within an Eclipse project at runtime). So… in great anticipation I’m sure - here’s the code:

Browser browser = new Browser(parent, SWT.NONE);
Bundle plugin = Activator.getDefault().getBundle(); // Where Activator is my org.eclipse.core.runtime.Plugin
IPath relativePagePath = new Path("html/index.html");
URL fileInPlugin = FileLocator.find(plugin, relativePagePath, null);
URL pageUrl = FileLocator.toFileURL(fileInPlugin);
browser.setUrl(pageUrl.toString());

Easy when you know how.