How to create a file association for Java Applet (in Windows)?

Everybody says that Java applets are dead (and there are talks to revive them with JavaFX 2). But it seems they are still not dead, at least from my point of view - since I maintain an applet for one of my customers.

Basically, that applet processes .svg files. And it would be convenient for user just to do double-click on a file and get the applet to start with that file.

I solved this problem as follows: applet accepts a parameter in it's launch url, and file association in Windows launches Internet Explorer with the file name as parameter in that url. Here's the code to establish such file association:
assoc .svg=SvgGraphicsFile
ftype SvgGraphicsFile=C:\Program Files\Internet Explorer\iexplore.exe "www.someplace.ru/index.html?file=" "%1"
And this is how I extracted that file name from applet url:
// first, in our Applet class, we get the url, from which app was launched
val docBase = getDocumentBase()
 
// then, we parse it
object Loader {
  def load(documentBase: String): Boolean = {
    val pattern = "file="
    val i = documentBase.indexOf(pattern)
    if (i > 0) {
      val f = decode(documentBase.drop(i + pattern.size).stripPrefix("\"%20\""))
      println("loading file: %s" format f)
      // load your file
      true
    } else false
  }
  def decode(l: String): String = {
    var a = l.getBytes
    var i = a.indexOf(37)
    while (i != -1) {
      val x = List(a(i+1).toChar,a(i+2).toChar).mkString
      val p = List(int2ubyte(java.lang.Integer.parseInt(x,16)))
      a = patch(a, i, p, 3)
      i = a.indexOf(37)
    }
    new String(a)
  }
  /** Because default Seq's patch method hangs Proguard. And I do not know why. */
  def patch[A : Manifest](s:Array[A], from: Int, patch: Seq[A], replaced: Int): Array[A] = {
    (s.take(from) ++ patch ++ s.drop(from + replaced)).toArray
  }
  def int2ubyte(i:Int) = if (i > 127) (i - 256).toByte else i.toByte
}
That's it. Hope this helps somebody :)

Comments

Popular posts from this blog

How to create your own simple 3D render engine in pure Java

Solving quadruple dependency injection problem in Angular

Configuration objects in Scallop