Saturday, 21 June 2008

JPopupMenu and ActionEvent's getSource( ) Method

When handling an event from a Java Swing component you would most likely implement the ActionListener interface and be able to get the source of the event from the ActionEvent's getSource() property. Not so if the event was triggered by a JPopupMenu. The type of the source of an ActionEvent is actually JPopupMenu$1 which seems to be a private inner class of JPopupMenu. The hack round this is pretty simple but not obvious because JPopupmenu$1 isn't documented anywhere.

JPopupMenu$1 extends Component and its parent is the JPopupMenu that you wanted in the first place so all you have to do is cast it and call getParent().

Thursday, 19 June 2008

COM Scripting in Groovy

Today I needed to test a COM component to make sure it worked as I expected, and to learn a bit about it at the same time. This isn't as easy as it used to be! COM components have fallen seriously out of favour (and for good reason), the effect of this seems to be that the components live on but the consuming languages aren't easily available. I could have created a wrapper using .net but this always seems an ugly and overweight solution.

Groovy has a far more elegant way of handling COM, involving no compilation (apart from what is done in memory) and everything you need comes with the Groovy SDK. In hardly took any time to get this small example of how to send an SMS using the Esendex COM component.


import org.codehaus.groovy.scriptom.*

Scriptom.inApartment {
   def service = new ActiveXObject("Esendex.SendService2");
   service.Initialise("MyUserName", "MyPassword", "MyAccount");
   service.SendMessage("441234567890", "Hello", 1);
}


The above code is a mashup of the examples on the Scriptom Page and the Esendex VB SDK Page. Its mostly self explanatory, the inApartment method takes a closure containing your COM scripting. Note that the third argument in the SendMessage call, 1, tells the COM object that the SMS is a text message. Remember to install the COM dll using regsvr32 and replace the username, password and account info with your own.

Advantages over VB6 and VBA? Its really easy to thread, and you don't have to find that decade old installation disc :)

Wednesday, 11 June 2008

A quick PHP testing tip for my first post. PHP's configuration file typically called php.ini is almost as important as the source code of a project in my experience. PHP code can give different results depending on how the environment is set up which can be frustrating when you begin deploying to a different environment and things start going wrong. In my case, after developing the first pass of the Esendex PHP ReST SDK I gave it to a colleague to review and it threw warnings. The offender was the following...

if(!defined(ESENDEX_HOME))
{
define('ESENDEX_HOME', '');
}

You might have seen the problem straight away, the defined function requires a string and not a constant name. The error_reporting property in my php.ini file was set quite low (not including E_NOTICE) so it didn't throw a warning. The quick way of catching all these syntax errors during development is to set the error_reporting property to E_ALL which will throw warnings over any mistake, this can be done with the error_reporting function.

error_reporting(E_ALL);

Now you get to spend the next half hour going through your project fixing all the bugs you never knew you had!