CSS JQuery Progress Bar

Posted in Uncategorized on June 3rd, 2011 by admin

I wanted a simple progress bar to use on a website but didn’t want to have to download a load of images so I wrote a simple JQuery plug-in here it is.

<style type="text/css">
.progressOuter {
  border: 1px solid;
  width: 100px;
  height: 15px;
  position: absolute;
}

.progressInner {
  border: 1px solid;
  background-color: green;
  width: 0px;
  height: 15px;
  position: relative;
}
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" ></script>

<script type="text/javascript">

(function( $ ){
  $.fn.progressBar = function(settings) {
    this.append("<div class='progressOuter'></div><div class='progressInner'></div>");
    var progressBarWidth = $(".progressOuter").width();
    var incremetWidth = progressBarWidth / settings.steps;
    var progressInnerDiv = this.find(".progressInner");

    var progress = 0;
    settings.incrementFunction(function() {
    if (progress < settings.steps) {
      progress = progress + 1;
      var progressInnerWidth = incremetWidth * progress;
      progressInnerDiv.width(progressInnerWidth + "px");
    }
  });

  return this;
};
})( jQuery );

</script>

And it can be used with the following.


$(document).ready(function() {
  $("#div").progressBar({
    steps : 10,
    incrementFunction : function(incrementFunction) {
      //an event to trigger the next step in the progress bar
    }
  });
});

And here is an example of it all together. Right click and view source if you want to see how it all works.

Mocking Http Services

Posted in Uncategorized on March 9th, 2011 by admin

Java 1.6 included a light weight http server which can be found in the com.sun.net.httpserver package. Using this it is really simple to create mocks of system you connect to by http for testing. The following will allow you to bring up a http server which will respond with a given 200 response. It can be simply used with,


WebServer server = new WebServer("/", "8080", "Some response");

Don’t forget to call stop in your test code.


server.stop();


package com.chrisgrimble.webserver;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

public class WebServer {
    private HttpServer server;

    public WebServer(String response) {
        this("/", response);
    }

    public WebServer(String path, String response) {
        this(path, 8080, response);

    }

    public WebServer(String path, int port, String response) {
        try {
            InetSocketAddress address = new InetSocketAddress(port);

            server = HttpServer.create(address, 0);
            server.createContext(path, new CannedResponseHandler(response));
            server.setExecutor(Executors.newCachedThreadPool());

            server.start();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void stop() {
        server.stop(0);
    }

    private class CannedResponseHandler implements HttpHandler {

        private final String response;

        CannedResponseHandler(String response) {
            this.response = response;
        }

        public void handle(HttpExchange exchange) throws IOException {
            Headers responseHeaders = exchange.getResponseHeaders();
            responseHeaders.set("Content-Type", "text/plain");
            exchange.sendResponseHeaders(200, 0);

            OutputStream responseBody = exchange.getResponseBody();

            responseBody.write(response.getBytes());

            responseBody.close();
        }
    }
}

Sorting Strings Alphabetically in Java

Posted in Uncategorized on September 8th, 2009 by admin

The java collections library allows you to sort a List by the objects is contains natural order.

Collections.sort(aList);

However for Strings the natural order sorts any words which start with a capital before any words which do not. E.g. Bbb will come before aaa.

The sort method on Collections is overloaded to pass in a List to sort and a Comparator. To sort a List so aaa will come before Bbb we can write a Comparator which ignores case and then pass an instance of this into the sort method. Finally if two Strings to sort are the same ignoring case we want the String with a capital letter to come first. E.g. Bbb will come after aaa but before bbb.

public class AlphabeticComparator implements Comparator {
  public int compare(String arg0, String arg1) {
    if (arg0.equalsIgnoreCase(arg1)) {
      return arg0.compareTo(arg1);
    }
    return arg0.compareToIgnoreCase(arg1);
  }
}

This can then be used with the Collections sort method

Collections.sort(aList, new AlphabeticComparator());

Sure I’ve not handled null and probably missed some edge cases. However this is a very small amount of code compared to some examples I’ve seen for sorting Strings alphabetically.

Converting Subversion between operating systems

Posted in Uncategorized on June 20th, 2009 by admin

I recently got a mac after using windows for some time and wanted to move my Subversion repositories to my new computer. I copied the files that made up the repository onto my mac but when I tried to use them subversion returned an error saying the file system type was incorrect.

I found you could export and import repositories to a text file to get round this. So on my windows box I ran


svnadmin dump myreponame > myreponame.dump

I then copied the myreponame.dump file to my mac and ran


svnadmin create myreponame
svnadmin load myreponame < myreponame.dump

New Features for GPX File Generater

Posted in Uncategorized on May 19th, 2009 by admin

I’ve added loads of new features to my GPX file generator. These changes include.

  • New layout
  • Upload GPX files and display them on the map
  • Draw a line to show your route
  • Fix URL too long errors when generating GPX file
  • Resize map to fill window when the browser window is resized
  • Counter of number of points

If anyone knows how to drag markers with OpenLayers I’d love to know.