Desktop integration with Java 6

Java 6 tries hard to make Java applications easier to integrate in the desktop environment of various platforms. One of such welcome attempts is the new java.awt.Desktop class adapted from JDIC (JDesktop Integration Components). This API allows access to this functionality:

  • Start the host’s default web browser and load a specified URI
  • Start the default email client with preloaded details like the destination, subject and body
  • Open a file in the default application for that file type
  • Edit a file in the default editor for that file type
  • Print a file using the default configuration the the host

For this tutorial I am going to reuse the com.littletutorials.console.Shell class defined in my older tutorial “Console applications with Java 6“. We only need to modify the com.littletutorials.Command enumeration to define our new commands targeting the Desktop API:

package com.littletutorials.console;

import java.io.*;
import java.awt.Desktop;
import java.net.URI;

public enum Command
{
    BROWSE(true, new Action()
    {
        @Override
        public void exec(Console c, String[] params) throws Exception
        {
            Desktop dt = Desktop.getDesktop();
            dt.browse(new URI(params[0]));
        }
    }),
    MAIL(true, new Action()
    {
        @Override
        public void exec(Console c, String[] params) throws Exception
        {
            Desktop dt = Desktop.getDesktop();
            dt.mail(new URI("mailto", params[0], ""));
        }
    }),
    OPEN(true, new Action()
    {
        @Override
        public void exec(Console c, String[] params) throws Exception
        {
            Desktop dt = Desktop.getDesktop();
            dt.open(new File(params[0]));
        }
    }),
    EDIT(true, new Action()
    {
        @Override
        public void exec(Console c, String[] params) throws Exception
        {
            Desktop dt = Desktop.getDesktop();
            dt.edit(new File(params[0]));
        }
    }),
    PRINT(true, new Action()
    {
        @Override
        public void exec(Console c, String[] params) throws Exception
        {
            Desktop dt = Desktop.getDesktop();
            dt.print(new File(params[0]));
        }
    }),
    BYE(false, new Action()
    {
        @Override
        public void exec(Console c, String[] params)
        {
            c.printf("Bye%n");
            System.exit(0);
        }
    });

    private interface Action
    {
        public void exec(Console c, String[] params) throws Exception;
    }

    public interface Listener
    {
        public void exception(Exception e);
    }

    private Action action;
    private boolean desktopCmd;

    private Command(boolean desktopCmd, Action a)
    {
        this.desktopCmd = desktopCmd;
        this.action = a;
    }

    public void exec(final Console c, final String[] params, final Listener l)
    {
        Runnable r = new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    action.exec(c, params);
                }
                catch (Exception e)
                {
                    l.exception(e);
                }
            }
        };

        if (desktopCmd)
        {
            if (! Desktop.isDesktopSupported())
            {
                c.printf("Desktop features not supported on %1$s%n",
                    System.getProperty("os.name"));
                return;
            }

            Desktop.Action desktopAction =
                Enum.valueOf(Desktop.Action.class, this.name());
            Desktop dt = Desktop.getDesktop();
            if (! dt.isSupported(desktopAction))
            {
                c.printf("Desktop action not supported: %1$s%n", desktopAction);
                return;
            }

            new Thread(r).start();
        }
        else
        {
            r.run();
        }
    }
}

The Command enumeration defines commands for each of the Desktop API’s capabilities listed above.
For each desktop command we check if the desktop API as a whole and also the specific desktop action is supported on the current host (lines 103 and 113). We also execute desktop commands on a separate thread.

The specific calls to the Desktop API are located in the Action.exec methods of the anonymous Action instances passed to enumeration elements on construction (lines 15, 24, 33, 42, 51).
The application has to be executed from a console/command prompt with a command like this:

java -cp . com.littletutorials.console.Shell


Now a session with the shell might look like this:

Welcome to the System. Please login.
01:40:28 User: john
01:40:31 Password [john]: 
Access granted
01:40:33 $ open /test.pdf
01:41:21 $ browse http://www.littletutorials.com
01:42:10 $ mail friend@mail.com?SUBJECT=Hello&BODY=Greetings
01:43:17 $ edit /test.txt
Desktop action not supported: EDIT
01:43:50 $ print /test.txt
Desktop action not supported: PRINT
01:44:07 $ bye
Bye


While this API is nice in itself, it is worth the effort of checking the status of the parent project at JDIC since more goodies are already available there as I write these words.

3 thoughts on “Desktop integration with Java 6”

  1. Pingback: java-talk.com

Comments are closed.