Example ruby RESTful calls to IGIndex

Spent a few cycles figuring out a proof of concept script for working with IG Index’s RESTful API on their demo system. This example logs in, checks the FTSE prices, places a sell order 100 points above, then iterates all open orders and deletes them. Use with care! Note it relies on some environment variables …. will tidy it up soon to simplify the param hashes etc.

#!/usr/bin/env ruby

require 'json'
require 'rest_client'
host = 'https://demo-api.ig.com'
user = ENV["IG_USER"]
pass = ENV["IG_PASS"]
apikey = ENV["IG_API_KEY"]


  ## Login
  request_body_map = {
     :'identifier' => "#{user}",
     :'password' => "#{pass}"

  response = RestClient.post("#{host}/gateway/deal/session",
                        :content_type => 'application/json; charset=UTF-8',
                        :accept => 'application/json; charset=UTF-8',
                        :'X-IG-API-KEY' => "#{apikey}"
                } )

  puts "Login response status: #{response.code}"

  cst = response.headers[:'cst']
  sectoken = response.headers[:'x_security_token']

  # puts response.body

  ## Get FTSE100 DFB current price
  response = RestClient.get("#{host}/gateway/deal/markets/IX.D.FTSE.DAILY.IP",
                        :content_type => 'application/json; charset=UTF-8',
                        :accept => 'application/json; charset=UTF-8',
                        :'X-IG-API-KEY' => "#{apikey}",
                        :'X-SECURITY-TOKEN' => "#{sectoken}",
                        :'CST' => "#{cst}"
                } )

  puts "FTSE market response status: #{response.code}"

  ftse = JSON.parse(response.body)
  bid = ftse["snapshot"]["bid"]
  offer = ftse["snapshot"]["offer"]

  puts "FTSE 100 buy #{offer} sell #{bid}"

  ## place sell order 100 points above with contingent stop and limit 100 points either side
  sellprice = offer+100
  limit_sell_map = {
      :"epic" => "IX.D.FTSE.DAILY.IP",
     :"expiry" => "DFB",
      :"direction" => "SELL",
      :"size" => "2",
      :"level" => "#{sellprice}",
      :"type" => "LIMIT",
      :"currencyCode" => "GBP",
      :"timeInForce" => "GOOD_TILL_CANCELLED",
      :"guaranteedStop" => "false",
      :"stopDistance" => "100",
      :"limitDistance" => "100"

  response = RestClient.post("#{host}/gateway/deal/workingorders/otc",
                        :content_type => 'application/json; charset=UTF-8',
                        :accept => 'application/json; charset=UTF-8',
                        :'X-IG-API-KEY' => "#{apikey}",
                        :'X-SECURITY-TOKEN' => "#{sectoken}",
                        :'CST' => "#{cst}"
                } )

  puts "FTSE sell limit order response status: #{response.code}"
  # puts response.body
  orderresp = JSON.parse(response.body)
  dealref = orderresp["dealReference"]
  puts "Placed order with ref #{dealref}"

  response = RestClient.get("#{host}/gateway/deal/confirms/#{dealref}",
                        :content_type => 'application/json; charset=UTF-8',
                        :accept => 'application/json; charset=UTF-8',
                        :'X-IG-API-KEY' => "#{apikey}",
                        :'X-SECURITY-TOKEN' => "#{sectoken}",
                        :'CST' => "#{cst}"
                } )
  puts "Confirm orders status #{response.code}"
  oconfbody = JSON.parse(response.body)
  puts "Confirm dealStatus #{oconfbody['dealStatus']} reason #{oconfbody['reason']} status #{oconfbody['status']}"

  ## list working orders
  response = RestClient.get("#{host}/gateway/deal/workingorders",
                        :content_type => 'application/json; charset=UTF-8',
                        :accept => 'application/json; charset=UTF-8',
                        :'X-IG-API-KEY' => "#{apikey}",
                        :'X-SECURITY-TOKEN' => "#{sectoken}",
                        :'CST' => "#{cst}"
                } )

  puts "List active orders response status: #{response.code}"
  orders = JSON.parse(response.body)

  orders['workingOrders'].each do |child|
    dealid = child['workingOrderData']['dealId']
    puts "Deleting dealId #{dealid}"

    response = RestClient.post("#{host}/gateway/deal/workingorders/otc/#{dealid}", JSON.generate({}),
                        :content_type => 'application/json; charset=UTF-8',
                        :accept => 'application/json; charset=UTF-8',
                        :'X-IG-API-KEY' => "#{apikey}",
                        :'X-SECURITY-TOKEN' => "#{sectoken}",
                        :'CST' => "#{cst}",
                        "_method" => "DELETE"
                } )
    puts "Delete status #{response.code}"
    delbody = JSON.parse(response.body)
    dealreference = delbody["dealReference"]
    puts "Check status of delete #{dealreference}"
    response = RestClient.get("#{host}/gateway/deal/confirms/#{dealreference}",
                        :content_type => 'application/json; charset=UTF-8',
                        :accept => 'application/json; charset=UTF-8',
                        :'X-IG-API-KEY' => "#{apikey}",
                        :'X-SECURITY-TOKEN' => "#{sectoken}",
                        :'CST' => "#{cst}"
                } )
    puts "Confirms status #{response.code}"
    confbody = JSON.parse(response.body)
    puts "Confirm dealStatus #{confbody['dealStatus']} reason #{confbody['reason']} status #{confbody['status']}"

  rescue => e
    puts "ERROR: #{e}"



Java Desktop App Packaging Automation

Tonight I have 2 hours free. It’s time to pull together the various snippets of info and manual tasks I’ve been following to build my app that runs on Mac OS X and Windows desktops and engineer a script I can just run each time.

The tools: first javafxpackager. https://docs.oracle.com/javafx/2/deployment/packager.htm

I’ve used this partially to build the windows executable but am using manually built ant script for Mac OS and manually signing everything in the bundle messy. Let’s sort that first.

Mac OS X

My compile / edit / test cycle is in Eclipse, and when I’m happy I export a runnable jar with the required Apache libs embedded, so my starting point for packaging is the jar. Might be nice one day to have a task in Eclipse that just does the packaging, anyway no distractions tonight.

I’m using jdk 7 still on the Mac and javafxpackager is nowhere to be found. First hurdle. The hunt begins, firstly Oracle tell me it’s included in the version of the jdk I’m running, 7.0.6. Aha it’s been renamed javapackager – nope that’s not there either. Sod it, let’s update to the latest jdk 7 – not ready to go to 8 yet. Hmm, still no packager. Pokes about a bit and sees in /Library/Java/JavaVirtualMachines there’s a few versions of the sdk now including 7u71, the most recent. Checks it out and the 7u6 I think I’m running and lo and behold there’s javafxpackager in both. OK so where is Mac OS X picking the SDK up from, or more importantly where _should_ it be picking it up from.

And voila more learnings! Short answer is ensure JAVA_HOME is set preferably using /usr/libexec/java_home to choose. Now javac reports it’s using the jdk I just installed. OK but still no javafxpackager in the default path. Henceforth it shall be known as $JAVA_HOME/bin/javafxpackager ….

Back to the job in hand, how do I go from a runnable jar to a signed .app with an embedded jre and a suitably signed installer pkg? javafxpackager -deploy appears to be the way, or it’s equivalent using ant, which might be more extensible and usable going forward. $JAVA_HOME/lib/ant-javafx.jar is your friend at this point. 30 minutes reading and researching later I think the 2 hours tonight isn’t going to be enough …..

Prototyping using the command line, the following generated a usable .app

javafxpackager -deploy -srcfiles ./DrumScoreEditor-Community-1.00.jar -outdir ./outdir -outfile DrumScoreEditor -native image -appclass org.whiteware.DrumScoreEditor -name Drum\ Score\ Editor

Seemed to strip out the Quicktime libraries that Apple reject your app store submission for, but can’t find a way to get it to sign everything for me yet, nor produce a .pkg (signed or otherwise). Using -native installer produced a well put together dmg with a typical ‘drag to /Applications’ image – nice but doesn’t help for the app store.

More when I have another 2 hours …..

Home Lab – Ruby on Rails Web Server on Ubuntu 14.04.1

Objective is to end up with a reusable template of a VM that can be cloned for testing ruby on rails app deployment, upgrades etc. This post builds Ubuntu 14.04 LTS, nginx as the web server, passenger as the app server and mysql for the database. This is more of a memo for me, hence not explaining every single command.


Download server image from Ubuntu, http://www.ubuntu.com/download/server and set up your VM according to cloud providers process, or local vmware / fusion etc.

Get yourself ssh access https://help.ubuntu.com/community/SSH/OpenSSH/Configuring to install the ssh daemon, then copy your public key file to authorized_keys on the new server. Much easier than console access only from this point!

sudo apt-get update
sudo apt-get install openssh-server
sudo apt-get install avahi-daemon avahi-discover

Name Services

If you’re using Fusion or some such local virtual machine technology that allocates IP addresses using DHCP, get some naming going to make it easier to connect to your VM. Enable bonjour protocols using sudo apt-get install avahi-daemon avahi-discover. Change the hostname, sudo vi /etc/hostname and sudo vi /etc/hosts to change entry then reboot, eg use a single hostname such as labweb1

BROKEN – cannot set up multiple local virtual hosts using avahi, mimicking how this is done in regular DNS to allow nginx server blocks (or apache virtual hosts) to work. Airtonix has scripts but couldn’t get them to work

SSH Access

With name services now set up,  Get yourself ssh access  – copy your public key file to .ssh/authorized_keys on the new server. Much easier than console access only from this point!

Web Server

Install nginx https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-14-04-lts

sudo apt-get install nginx


sudo apt-get install git
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc

restart your shell

git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
sudo apt-get install build-essential libssl-dev
rbenv install 2.1.3
rbenv global 2.1.3
echo "gem: --no-document" >> ~/.gemrc

App Server

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
sudo apt-get install apt-transport-https ca-certificates

create file /etc/apt/sources.list.d/passenger.list and insert deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main

sudo chown root /etc/apt/sources.list.d/passenger.list
sudo chmod 600 /etc/apt/sources.list.d/passenger.list
sudo apt-get update
sudo apt-get install nginx-extras passenger

Edit /etc/nginx/nginx.conf and uncomment passenger_root and passenger_ruby

Note this will mean passenger launches with the default ruby installed by ubuntu in /usr/bin/ruby – this is OK!

Edit /etc/nginx/nginx.conf to look similar to this:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/<INSERT YOUR RAILS APP NAME>/current/public;
    passenger_ruby /Users/alanwhite/.rbenv/versions/2.1.3/bin/ruby
    passenger_enabled on;
    passenger_set_cgi_param SECRET_KEY_BASE "blah whatever it was in your ruby on rails app";

This will ensure passenger uses your installed ruby version and not the system installed one. There’s still a flaw here in that to change ruby version you have to edit this file as root.


Going to implement a mysql database here too, it’s really a web server, but one of the application use cases I need to experiment with relies on a mysql database local to the web server, rather than on a separate (virtual or otherwise) machine.

sudo apt-get install mysql-server

I use the password labsql1 usually for the mysql root user. This is a transient lab that gets torn down.

sudo mysql_install_db

Might be a transient lab but let’s get rid of the defaults.

sudo mysql_secure_installation

Now we have nginx, passenger and mysql running but haven’t deployed an app to it. Probably a good time to stop and use your virtualisation managers clone or snapshot facility, whatever works best, to create a known starting point for testing apps.

One of the areas I want to investigate further – needing all the dev libraries to install gems – seems wrong

sudo apt-get install libmysqlclient-dev

Prepare For Deployment

I’d like to deploy my test app to /var/www/my_app_name. Unfortunately Ubuntu 14 LTS doesn’t allow package deployers to point anything there so the installed nginx points to /usr/share/nginx/html

sudo mkdir /var/www
sudo chgrp www-data /var/www
sudo chmod 775 /var/www

Don’t forget to put you git/bitbucket credentials for deploy on the new server if you’re using a capistrano recipe that pulls down from one of those repo hosts.

Ruby on Rails Installation on Ubuntu 12.04.5

Objective is to have a server where multiple ruby or rails projects could be hosted, and the app could be simply installed from github without any faffing about.

What do I mean by faffing about? Not needing root access to the server in any way in order to deploy and run your app. Sure, for some baseline system packages like apache and plugins, and to define the virtual host to apache you may need sudo but once configured, deploying your app, updates and even changing ruby and rails versions should require no system privileges.

Prepping the server

It’s been about a while, a few aborted attempts to install various things linger. First step is to tidy up. My preference these days is rbenv for ruby version management, and bundler for gem mgmt, with gems local to each app, not system installed.

RVM was already installed from a previous adventure, time to remove it

Removing rvm

The process was to run rvm implode, and remove references in the startup scripts and make sure the gem was gone. Please note I only removed the .z* files as all they had in them were the rvm references. Check yours before doing that. Also removed all references to rvm in .bashrc and .bash_profile.

Install Ruby Environment Manager

Next install rbenv from github, avoiding the linux distro’s package manager. NB If you’re behind an enterprise firewall that spoofs certificates so ssh/ssl doesn’t work, you’ll have to use one of the other documented methods for cloning from github, e.g. http and plain text user/passwords – frustrating when experts do this in the name of security and end up with plain text credentials traversing the internet.


Exit the shell and start a new one so the rbenv command is available, then install the ruby-build plugin which allows you to build new ruby versions when needed.


Install Ruby

Then use rbenv to install ruby!


Set the default ruby version for this user to the newly installed one, e.g. rbenv global 2.1.3

This process is directly from the rbenv github readme, which contains all the commands in a ready to cut’n’paste format.

Install Rails

Installing rails now becomes as simple as gem install rails but you may wish to tell gem to not bother installing all the documentation if you’re intending this install to be a server only, in which case enter echo "gem: --no-document" >> ~/.gemrc beforehand.

Web & Application Server

Next we need a web server hosting an application server that knows how to run ruby on rails apps. I’d usually go for nginx as a web server, but as this linux VM is already running apache2 with a bunch of other web sites we’ll stick with that. Still need to add the application server – for this we’re installing Phusion Passenger following their instructions.

First we establish the authentication of the Phusion repos.


Then we add them to the list of repos Ubuntu uses.


Then a quick sudo apt-get update and ubuntu has the latest phusion passenger software ready to install.


OK, now it’s all installed, the software needs enabled in apache. Works as per the Passenger install guide using sudo a2enmod passenger and then restart apache sudo service apache2 restart

Apache Virtual Host

You’ll need a virtual host for your web site.

<VirtualHost *:80>
    ServerName blog.blahblah.org
    DocumentRoot /var/www/blog/current/public
    PassengerRuby /home/arw/.rbenv/shims/ruby
    SetEnv SECRET_KEY_BASE 1389....
    <Directory /var/www/blog/current/public>
        Allow from All
        Options -MultiViews

Note the use of the PassengerRuby directive or it will try to use the system installed ruby, bundler etc and it will all go horribly wrong!

Next Steps

If you’ve developed your ruby on rails app, followed all the capistrano guides and deployed it, in this case to /var/www/blog, and remembered to set up your production database of choice, and ran the rake db:migrate on production ….. it just works! Honest.

Distributing an app on Mac OS X without Xcode, outside the Mac App Store

I have a java app, built with eclipse, packaged in a DMG. As it’s not “signed” in any way so Mac users have to set their Gatekeeper to allow software downloaded from anywhere to run. I kinda get Apple’s desire to control how apps are sourced to their platforms, for stability, quality etc, but unless you want to have your apps users exposed (according to Apple) you have to pay some money and add more complexity to your application packaging process.

Naturally there’s no great documentation for exactly what I want to do here, not that I can find anyway, happy to be corrected! As always, I’ll credit any articles I find as I work through this.

Apple have a documented process for taking an app built with Xcode to the point of being signed. At a high level it looks like this:

  1. Get an apple developer id (costs money btw)
  2. Request developer ID certificates
  3. Sign your application using the Developer ID Application certificate
  4. Sign the installer using the Developer ID Installer certificate

Sounds easy at this level, huh? When you look into the detail, it’s all geared toward Xcode. What follows is my adventures in trying to do this without Xcode.

Step 1 – Get an Apple Developer ID.

No problem here, you just have to pay money at https://developer.apple.com/register/index.action

Step 2 – Request Developer ID Certificates

On the developer portal there’s a Member Center page, upon which you select the Certificates, Identifiers & Profiles link.

Following the Certificates link you get a page where, after fair warning that the easiest way to get certificates is via Xcode, you find a link to upload a Certificate Signing Request from your Mac.

Now we get asked what type of certificate do we need. Apple split this into 2 categories, development and production. Looking through the brief description of all the options, I chose the Production Developer ID certificates.

There’s some verbiage at the bottom of the page about needing some Intermediate Certificates that Xcode automagically installs. Further reading says these are certificates that need to be installed in your keychain as part of the trust chain for the certs you’re about to receive. Clicking them results in a downloaded file which if you click open Keychain Access and prompts to install. Do this for each of them.

At the next screen, you get another warning that Xcode is the preferred method for requesting and installing these certificates on a Mac. Onwards we go boldly ignoring what has now escalated from being an “easier”, to the “preferred” way of doing this.

First thing to note is you can’t request both the Application and the Installer certificates at the same time.

So what this does, when pressing continue, is take you to screen that tells you how to use Keychain Access tool that comes with Mac OS X. The instructions are complete and after this step we have a Certificate Signing Request (CSR) on the Desktop, and a couple of new entries in the keychain as shown below.

Back on the developer website, selecting continue on the Next step is to upload that CSR and ask Apple to generate the certificate. Fairly painlessly this happens and the certificate is downloaded in the browser. Double-clicking prompts Keychain Access to ask which keychain to install the certificate in. Bad move, I have no idea. Options seem to be login (default), iCloud or System. Kudos to this page, as it goes through the exact process I’ve just been through but for iOS development, looks very similar and shows the cert installed in the login keychain. So let’s accept the default and crack on.

This works fine and examining the login keychain you can see the certificate is there with a 5 year lifetime.

At this stage I choose Add Another, from the options at the foot of the page the cert was downloaded from, as we still need the Installer cert.

The process is identical to the above, but choosing Installer instead of Developer cert when asked. Notably, once this is in the keychain you can see it’s only got a 1 year lifetime. Guess this means you have to revisit your installer package once a year at a minimum – not an unreasonable position to take.

OK so we now have some certs for signing our app installed in the keychain on the Mac. A quick backup now invoked on Time Machine to ensure we have a copy somewhere.

Step 3 – Sign Your Application

Now what? All the docs appear to talk about Xcode packages, archives and other terminology, then I found the Code Signing Guide in the Apple developer library.

Cutting through all the words, if you’re not looking to sandbox your app (although that’s also best practise these days), signing the app appears to work with:

$ export SIGNER="Developer ID Application: Alan White (NMZ5U4C57W)"
$ codesign -v -f -s $SIGNER Drum\ Score\ Editor.app
Drum Score Editor.app: signed bundle with Mach-O thin (x86_64) [org.whiteware.DrumScoreEditor]

Point to note: if you’ve embedded a jre inside the app, you’ll need to sign that before signing the app as a whole.

$ codesign -v -f -s $SIGNER /Users/alanwhite/Development/export/builder/Drum\ Score\ Editor.app/Contents/PlugIns/jdk1.7.0_60.jdk
/Users/alanwhite/Development/export/builder/Drum Score Editor.app/Contents/PlugIns/jdk1.7.0_60.jdk: signed bundle [com.oracle.java.7u60.jdk]
$ codesign -v -f -s $SIGNER Drum\ Score\ Editor.app
Drum Score Editor.app: signed bundle with Mach-O thin (x86_64) [org.whiteware.DrumScoreEditor]

Step 4 – Sign Your Installer

So how about building an installer and signing the install package?

$ export INSTALLSIG="3rd Party Mac Developer Installer: Alan White (NMZ5U4C57W)"
$ productbuild --component Drum\ Score\ Editor.app /Applications --sign $INSTALLSIG Drum\ Score\ Editor.pkg
productbuild: Adding component at /Users/alanwhite/Development/Drum Score/Drum Score Editor.app
productbuild: Signing product with identity "3rd Party Mac Developer Installer: Alan White (NMZ5U4C57W)" from keychain /Users/alanwhite/Library/Keychains/login.keychain
productbuild: Adding certificate "Apple Worldwide Developer Relations Certification Authority"
productbuild: Adding certificate "Apple Root CA"
productbuild: Wrote product to Drum Score Editor.pkg

Last step after this is to create the DMG to hold the installer package, as most people are used to getting software in a dmg.

hdiutil create -volname DSEditorInstaller -srcfolder Drum\ Score -ov -format UDZO DrumScore.dmg


Took my prior DMG containing the unsigned app to a vanilla installed Mac running 10.8, sure enough it wouldn’t run, citing preferences by default only allowing apps signed by a validated Apple Developer ID. Took my new DMG across there, double-clicked the installer package, ran straight through, installed the app fine. Double-clicked the app in /Applications and it burst into life …. after I downloaded and installed a java runtime, it still had the old Apple 1.6 version, but at least it gave me a nice dialog box telling me it couldn’t find a runtime!




Java Mac OS X App Bundle Machinations

Lost 6 hours of my life tracking down this issue, and of course in the end it turns out to be my user error, although as always as a developer you challenge whether it needed to be this hard to debug.


What was I trying to achieve was to create mac app bundle to put in a dmg to distribute my java desktop app. In Eclipse, generated a Mac app bundle using the export task.


Into Finder, double clicked the app and nothing happens. A quick search around Console, shows:



A quick google makes suggestions like there’s no main entry point defined in the jar file


This however is a rat-hole, there is indeed no main entry point defined in the manifest but a few more searches reveal the package isn’t invoked using java -jar xx.jar, it’s invoked using java -cp xx.jar mainEntryPoint. So we check the Info.plist in the app bundle, and find the main class is defined. So what’s going wrong?

This article http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/packagingAppsForMac.html seems to imply  a tool is needed at Java 7, so yep, great but why wouldn’t that be in eclipse already, i.e. why would eclipse claim to produce an app bundle if it did it the wrong way? Is it?  Time to go build one manually and see.

Manually creating an App Bundle, Java 7 (1.7.0_45-b18), Mac OS X 10.9.2

Downloaded appbundler-1.0.jar from https://java.net/projects/appbundler/downloads, as described in the Oracle article above and put it in a test directory.4

Set up the build.xml file from the various entries on the java.net and Oracle articles referenced above and running ant causes a no such file exception. It’s looking for an Info.plist file to be in the build directory. Odd, this isn’t mentioned anywhere!


Helpful: http://supunmali-myexperience.blogspot.co.uk/2012/10/jar-bundler-mac-os-x.html, resolved by setting JAVA_HOME but then I hit another problem:


If you’re not an experience Ant user (and I am not!), what this means is I forgot a target statement in the build.xml file, here’s the file I used at this stage which worked ….. or so I thought, it certainly allowed Ant to build the app bundle.

<?xml version="1.0" encoding="UTF-8"?>
<project name="DrumScore" default="default" basedir=".">

  <property environment="env"/>

  <taskdef name="bundleapp"

  <target name="default">
    <bundleapp outputdirectory="."
        name="Drum Score Editor"
        displayname="Drum Score Editor"
      <runtime dir="${env.JAVA_HOME}"/>

And this gave me:


Frustratingly, trying to launch the generated .app bundle, I got exactly the same errors. What was going on here? I checked the Mac OS X Gatekeeper security level, it’s set to allow apps downloaded from anywhere so this should work!


There are many articles on various things which cause the 10810 error, from process table full to permissions and missing libraries.

What a huge diversion all these ratholes are you can go down. This is all so sketchy, returning errors meaning “unknown error”, meaningless “return code 1” in the syslog.

Bottom line is: do not try and follow the examples, work stuff out. Mine was failing because ……. I had assumed the main method name needed specific in the mainclassname statement, as is shown in the Oracle example. As soon as I made it the class name, and not the method it worked. i.e.

<?xml version="1.0" encoding="UTF-8"?>
<project name="DrumScore" default="default" basedir=".">

  <property environment="env"/>

  <taskdef name="bundleapp"

  <target name="default">
    <bundleapp outputdirectory="."
        name="Drum Score Editor"
        displayname="Drum Score Editor"

Should I post this? After all it was my error in specifying it wrongly. Yes, because if anyone else is searching for answers in this murky, poorly documented space, made messy by Apple divorcing Java without a properly focused handing of the baton to Oracle….. this might just help!

I’m now scared … Mac, Sandbox, App Store: And the world just got infinitely more complicated …. http://www.intransitione.com/blog/take-java-to-app-store/

Java Drag and Drop: Part 3, visualising it


In the prior posts in this series we looked at creating an app using the AWT drag and drop functionality and a functionally identical app which uses the Swing TransferHandler approach.

We saw that leveraging the Swing approach reduces the amount of code involved, however patterns emerge in the code that show how Swing is encapsulating the underlying AWT routines, i.e. we see similar code moving from a generic controller to the TransferHandler. By using Swing containers rather than our own Model/View combination, we also reduce code.

Both the applications have limited functionality, what this article does is demonstrates how to add a visual icon of the widget being transferred, so the user can see exactly what the canvas would look like where they drop the widget.

Custom Component

When using the custom component (see DragginCustom2 in the github repo), the changes are all in the controller. When we’ve decided we’re starting a drag operation we create an image of the widget.

The next challenge is the user could grab the widget anywhere within it, so we need to work out the offset of the mouse pointer from the top of the widget, so the image can be displayed correctly without it jerking to the mouse pointer location.

This offset is also used to place the widget in the right place when the user releases the mouse and drops it in place. If we didn’t take the offset into account the widget would be added with it’s top left corner where the mouse pointer is, not where it is visually on the screen.


Point to note, in the dragGestureRecognized method, once we’ve identified the widget over which the mouse drag was started, we create a BufferedImage and ask the widget to paint itself in that buffer.

Notice how the bounds location of the widget are set to 0,0 temporarily. This is because we want it to be drawn starting at the top of the buffer, not the same distance in as it is from the canvas.

Also we need to store locally the dragImageOffset when the drag is initiated. That value is needed later in the drop method but the AWT system doesn’t store it anywhere, you have to take care of that.

	class CanvasDragController implements DragGestureListener, DragSourceListener, DropTargetListener {
		private Point dragImageOffset = new Point();
		public CanvasDragController(Canvas canvas) {
			// to allow drags to be initiated on the canvas
			DragSource source = DragSource.getDefaultDragSource();
			source.createDefaultDragGestureRecognizer(canvas, DnDConstants.ACTION_MOVE, this);
			// to allow drops to happen on the canvas
			DropTarget target = new DropTarget(canvas,this);

		// DragGestureListener
		public void dragGestureRecognized(DragGestureEvent dge) {
			DragSource source = DragSource.getDefaultDragSource();
			Canvas canvas = (Canvas) dge.getComponent();
			CanvasModel canvasModel = canvas.getCanvasModel();

			CanvasWidget widget = canvasModel.getWidgetAt(dge.getDragOrigin());
			if ( widget != null ) {
				Rectangle widgetBounds = widget.getBounds();
				BufferedImage dragImage = new BufferedImage(widgetBounds.width,widgetBounds.height,BufferedImage.TYPE_INT_ARGB);
				Graphics g = dragImage.getGraphics();
				Point widgetSavedLocation = widget.getBounds().getLocation();
				widgetBounds.setLocation(0, 0);
				dragImageOffset = new Point(widget.getBounds().x - dge.getDragOrigin().x, widget.getBounds().y - dge.getDragOrigin().y);
				CanvasWidgetTransferable transferablePackage = new CanvasWidgetTransferable(widget);
				source.startDrag(dge, DragSource.DefaultMoveDrop, 
						transferablePackage, this);


		// DragSourceListener
		public void dragEnter(DragSourceDragEvent dsde) {}
		public void dragOver(DragSourceDragEvent dsde) {}
		public void dropActionChanged(DragSourceDragEvent dsde) {}
		public void dragExit(DragSourceEvent dse) {}

		public void dragDropEnd(DragSourceDropEvent dsde) {
			Canvas canvas = (Canvas) dsde.getDragSourceContext().getComponent();
			CanvasModel canvasModel = canvas.getCanvasModel();
			Rectangle bounds = null;
			try {
				bounds = (Rectangle) dsde.getDragSourceContext().getTransferable().getTransferData(widgetFlavor);
			} catch (UnsupportedFlavorException e) {
			} catch (IOException e) {

			if ( dsde.getDropSuccess() ) {
				if ( dsde.getDropAction() == DnDConstants.ACTION_MOVE) {
					// we need to remove the source element now
					Point p = new Point(bounds.x,bounds.y);
					CanvasWidget widget = null;
					for ( CanvasWidget searchWidget : canvasModel.widgetList ) {
						if ( searchWidget.getBounds().getLocation().equals(p) )
							widget = searchWidget;
					if ( widget != null)
			} else {
				// we need to mark it as no longer moving
				Point p = new Point(bounds.x,bounds.y);
				CanvasWidget widget = canvasModel.getWidgetAt(p);

		// DropTargetListener
		public void dragEnter(DropTargetDragEvent dtde) {}
		public void dragOver(DropTargetDragEvent dtde) {}
		public void dropActionChanged(DropTargetDragEvent dtde) {}
		public void dragExit(DropTargetEvent dte) {}

		public void drop(DropTargetDropEvent dtde) {
			if (!dtde.isDataFlavorSupported(widgetFlavor)) {
			Canvas canvas = (Canvas) dtde.getDropTargetContext().getComponent();
			CanvasModel canvasModel = canvas.getCanvasModel();
			Rectangle bounds = new Rectangle(0,0,10,10);
			try {
				bounds = (Rectangle) dtde.getTransferable().getTransferData(widgetFlavor);
			} catch (UnsupportedFlavorException e) {
			} catch (IOException e) {
			CanvasWidget widget = new CanvasWidget();
			Point mouseLocation = dtde.getLocation();
			Point realDropLocation = new Point(mouseLocation.x + dragImageOffset.x,mouseLocation.y + dragImageOffset.y);

Swing Component

The changes happen in the TransferHandler, full source is available in the DragSwing2 app in the github repo. Pretty much identical logic as the custom example above but just in different places.

The same issues apply about ensuring we work out the offset from where the mouse was dragged to the top left of the component, and using that when dropping the widget on the canvas.


There are some nuances to using a drag image with this approach, particularly where you can call the setDragImage method and accompanying setDragImageOffset. We call it to set the image when the transferable is created, the only other option is when Swing asks for the drag options supported by the canvas, this didn’t seem a logical choice.

Although the TransferHandler approach allows the dragImageOffset to be retrieved when the drop is taking place, i.e. in the importData method, it bizarrely has no ability to tell us where the mouse was when get a chance to specify the drag offset! So we override the exportAsDrag method to squirrel away the mouse location at the time, so it can be used later.

	class CanvasTransferHandler extends TransferHandler {	
		private Point dragStart = new Point();
		public int getSourceActions(JComponent c) {
			return TransferHandler.MOVE;

		protected Transferable createTransferable(JComponent c) {
			for ( Component comp : c.getComponents() ) {
				if ( comp instanceof CanvasWidget ) {
					CanvasWidget canvasWidget = (CanvasWidget) comp;
					if ( canvasWidget.isMoving() ) {
						BufferedImage widgetImage = new BufferedImage(canvasWidget.getWidth(),
						Graphics g = widgetImage.getGraphics();
						setDragImageOffset(new Point(canvasWidget.getX() - dragStart.x, canvasWidget.getY() - dragStart.y));
						return new CanvasWidgetTransferable(canvasWidget);
			return null;

		protected void exportDone(JComponent source, Transferable data,
				int action) {
			if ( action == TransferHandler.MOVE ) {
				Canvas canvas = (Canvas) source;
				for ( Component comp : source.getComponents() ) {
					if ( comp instanceof CanvasWidget ) {
						CanvasWidget canvasWidget = (CanvasWidget) comp;
						if ( canvasWidget.isMoving() ) {

		public boolean importData(TransferSupport support) {
			if ( !canImport(support) )
				return false;

			Canvas canvas = (Canvas) support.getComponent();
			Rectangle bounds = new Rectangle(0,0,10,10);
			try {
				bounds = (Rectangle) support.getTransferable().getTransferData(widgetFlavor);
			} catch (Exception e) {
				return false;
			CanvasWidget widget = new CanvasWidget();
			Point mouseLocation = support.getDropLocation().getDropPoint();
			Point realDropLocation = new Point(mouseLocation.x + getDragImageOffset().x,mouseLocation.y + getDragImageOffset().y);
			return true;

		public boolean canImport(TransferSupport support) {
			if ( support.isDataFlavorSupported(widgetFlavor) )
				return true;
			return false;

		public void exportAsDrag(JComponent comp, InputEvent e, int action) {
			dragStart = ((MouseEvent) e).getPoint();
			super.exportAsDrag(comp, e, action);



Full source to all example applications available at http://github.com/alanwhite/drag-artist


You deserve it for reading this far, thank you for that. If this has been any use or if you think it could be made better or clearer, please either comment or tweet and let me know!