This is my proposal for an implementation of java.awt.Desktop (and its
support classes).

I would like to check this in today, if no there are no suggestions or
comments.

Let me know what do you think,
Mario

---- Changelog ---

2007-03-02  Mario Torre  <[EMAIL PROTECTED]>

        * java/awt/Desktop.java: new java 1.6 class.
        * java/awt/peer/DesktopPeer.java: new inteface.
        * gnu/java/awt/peer/GnomeDesktopPeer.java: new class.
        * gnu/java/awt/peer/ClasspathDesktopPeer.java: new class.
        * gnu/java/awt/peer/KDEDesktopPeer.java: new class.
        * java/awt/Toolkit.java (createDesktopPeer): new method to support the
        creation of DesktopPeer instances.
        * gnu/java/awt/peer/qt/QtToolkit.java: update copyright notice and
        organize import.
        * gnu/java/awt/ClasspathToolkit.java (createDesktopPeer): new method.


-- 
Lima Software - http://www.limasoftware.net/
GNU Classpath Developer - http://www.classpath.org/
Jabber: [EMAIL PROTECTED] - Profile: 
http://www.gtalkprofile.com/profile/9661.html
pgp key: http://subkeys.pgp.net/
PGP Key ID: 80F240CF
Fingerprint: BA39 9666 94EC 8B73 27FA  FC7C 4086 63E3 80F2 40CF

Please, support open standards:
http://opendocumentfellowship.org/petition/
http://www.nosoftwarepatents.com/
### Eclipse Workspace Patch 1.0
#P classpath
Index: gnu/java/awt/ClasspathToolkit.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/awt/ClasspathToolkit.java,v
retrieving revision 1.22
diff -u -r1.22 ClasspathToolkit.java
--- gnu/java/awt/ClasspathToolkit.java	12 Feb 2007 21:39:20 -0000	1.22
+++ gnu/java/awt/ClasspathToolkit.java	2 Mar 2007 17:10:03 -0000
@@ -38,17 +38,21 @@
 
 package gnu.java.awt;
 
+import gnu.java.awt.peer.ClasspathDesktopPeer;
 import gnu.java.awt.peer.ClasspathFontPeer;
 import gnu.java.awt.peer.EmbeddedWindowPeer;
 import gnu.java.security.action.SetAccessibleAction;
 
 import java.awt.AWTException;
+import java.awt.Desktop;
 import java.awt.Font;
 import java.awt.FontFormatException;
 import java.awt.GraphicsDevice;
 import java.awt.GraphicsEnvironment;
+import java.awt.HeadlessException;
 import java.awt.Toolkit;
 import java.awt.font.TextAttribute;
+import java.awt.peer.DesktopPeer;
 import java.awt.peer.RobotPeer;
 import java.io.IOException;
 import java.io.InputStream;
@@ -212,5 +216,18 @@
    {
      return -1;
    }
+   
+   /* (non-Javadoc)
+    * @see java.awt.Toolkit#createDesktopPeer(java.awt.Desktop)
+    */
+   protected DesktopPeer createDesktopPeer(Desktop target)
+     throws HeadlessException
+   {
+     if (GraphicsEnvironment.isHeadless())
+       throw new HeadlessException();
+     
+     return ClasspathDesktopPeer.getDesktop();
+   }
+
 }
 
Index: gnu/java/awt/peer/qt/QtToolkit.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/awt/peer/qt/QtToolkit.java,v
retrieving revision 1.11
diff -u -r1.11 QtToolkit.java
--- gnu/java/awt/peer/qt/QtToolkit.java	21 Dec 2006 13:02:50 -0000	1.11
+++ gnu/java/awt/peer/qt/QtToolkit.java	2 Mar 2007 17:10:04 -0000
@@ -1,5 +1,5 @@
 /* QtToolkit.java --
-   Copyright (C)  2005, 2006  Free Software Foundation, Inc.
+   Copyright (C)  2005, 2006, 2007  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -37,6 +37,7 @@
 
 package gnu.java.awt.peer.qt;
 
+import gnu.java.awt.ClasspathToolkit;
 import gnu.java.awt.EmbeddedWindow;
 import gnu.java.awt.peer.ClasspathFontPeer;
 import gnu.java.awt.peer.EmbeddedWindowPeer;
@@ -50,56 +51,56 @@
 import java.awt.Dialog;
 import java.awt.Dimension;
 import java.awt.EventQueue;
+import java.awt.FileDialog;
 import java.awt.Font;
 import java.awt.FontMetrics;
 import java.awt.Frame;
+import java.awt.GraphicsDevice;
+import java.awt.GraphicsEnvironment;
 import java.awt.Image;
 import java.awt.Label;
 import java.awt.List;
-import java.awt.MenuBar;
 import java.awt.Menu;
+import java.awt.MenuBar;
 import java.awt.MenuItem;
 import java.awt.Panel;
-import java.awt.TextArea;
-import java.awt.TextField;
-import java.awt.FileDialog;
-import java.awt.GraphicsDevice;
-import java.awt.GraphicsEnvironment;
 import java.awt.PopupMenu;
 import java.awt.PrintJob;
-import java.awt.Scrollbar;
 import java.awt.ScrollPane;
+import java.awt.Scrollbar;
+import java.awt.TextArea;
+import java.awt.TextField;
 import java.awt.Window;
 import java.awt.datatransfer.Clipboard;
 import java.awt.dnd.DragGestureEvent;
 import java.awt.dnd.peer.DragSourceContextPeer;
 import java.awt.event.AWTEventListener;
+import java.awt.im.InputMethodHighlight;
 import java.awt.image.ColorModel;
 import java.awt.image.DirectColorModel;
 import java.awt.image.ImageObserver;
 import java.awt.image.ImageProducer;
-import java.awt.im.InputMethodHighlight;
 import java.awt.peer.ButtonPeer;
-import java.awt.peer.FontPeer;
-import java.awt.peer.PanelPeer;
 import java.awt.peer.CanvasPeer;
-import java.awt.peer.FramePeer;
-import java.awt.peer.PopupMenuPeer;
 import java.awt.peer.CheckboxMenuItemPeer;
-import java.awt.peer.LabelPeer;
-import java.awt.peer.RobotPeer;
 import java.awt.peer.CheckboxPeer;
-import java.awt.peer.ScrollPanePeer;
 import java.awt.peer.ChoicePeer;
+import java.awt.peer.DialogPeer;
+import java.awt.peer.FileDialogPeer;
+import java.awt.peer.FontPeer;
+import java.awt.peer.FramePeer;
+import java.awt.peer.LabelPeer;
 import java.awt.peer.ListPeer;
-import java.awt.peer.ScrollbarPeer;
 import java.awt.peer.MenuBarPeer;
-import java.awt.peer.TextAreaPeer;
-import java.awt.peer.DialogPeer;
 import java.awt.peer.MenuItemPeer;
-import java.awt.peer.TextFieldPeer;
-import java.awt.peer.FileDialogPeer;
 import java.awt.peer.MenuPeer;
+import java.awt.peer.PanelPeer;
+import java.awt.peer.PopupMenuPeer;
+import java.awt.peer.RobotPeer;
+import java.awt.peer.ScrollPanePeer;
+import java.awt.peer.ScrollbarPeer;
+import java.awt.peer.TextAreaPeer;
+import java.awt.peer.TextFieldPeer;
 import java.awt.peer.WindowPeer;
 import java.io.InputStream;
 import java.net.URL;
@@ -107,8 +108,6 @@
 import java.util.Map;
 import java.util.Properties;
 
-import gnu.java.awt.ClasspathToolkit;
-
 public class QtToolkit extends ClasspathToolkit
 {
   public static EventQueue eventQueue = null; // the native event queue
Index: java/awt/Toolkit.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/Toolkit.java,v
retrieving revision 1.49
diff -u -r1.49 Toolkit.java
--- java/awt/Toolkit.java	3 Jan 2007 22:51:07 -0000	1.49
+++ java/awt/Toolkit.java	2 Mar 2007 17:10:04 -0000
@@ -63,6 +63,7 @@
 import java.awt.peer.CheckboxMenuItemPeer;
 import java.awt.peer.CheckboxPeer;
 import java.awt.peer.ChoicePeer;
+import java.awt.peer.DesktopPeer;
 import java.awt.peer.DialogPeer;
 import java.awt.peer.FileDialogPeer;
 import java.awt.peer.FontPeer;
@@ -149,6 +150,15 @@
   }
 
   /**
+   * 
+   * @param target
+   * @return
+   * @throws HeadlessException
+   */
+  protected abstract DesktopPeer createDesktopPeer(Desktop target)
+    throws HeadlessException;
+  
+  /**
    * Creates a peer object for the specified <code>Button</code>.
    *
    * @param target The <code>Button</code> to create the peer for.
Index: gnu/java/awt/peer/KDEDesktopPeer.java
===================================================================
RCS file: gnu/java/awt/peer/KDEDesktopPeer.java
diff -N gnu/java/awt/peer/KDEDesktopPeer.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/java/awt/peer/KDEDesktopPeer.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,142 @@
+/* GnomeDesktopPeer.java -- Offers a KDE Desktop peer for DesktopPeer
+ Copyright (C) 2006, 2007 Free Software Foundation, Inc.
+
+ This file is part of GNU Classpath.
+
+ GNU Classpath is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ GNU Classpath is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Classpath; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301 USA.
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library.  Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library with independent modules to produce an
+ executable, regardless of the license terms of these independent
+ modules, and to copy and distribute the resulting executable under
+ terms of your choice, provided that you also meet, for each linked
+ independent module, the terms and conditions of the license of that
+ module.  An independent module is a module which is not derived from
+ or based on this library.  If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so.  If you do not wish to do so, delete this
+ exception statement from your version. */
+
+package gnu.java.awt.peer;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+
+/**
+ * @author Mario Torre <[EMAIL PROTECTED]>
+ */
+public class KDEDesktopPeer
+  extends ClasspathDesktopPeer
+{
+  /**
+   * Query string to use if a GNOME desktop is detected to get the name of the
+   * default browser. This requires gconftool-2 (part of GNOME).
+   */
+  private static final String BROWSER_QUERY_GNOME =
+    "gconftool-2 -g /desktop/gnome/url-handlers/http/command";
+
+  @Override
+  protected String getCommand(String action)
+  {   
+    // check if a command already exists
+    String command = super.getCommand(action);
+    
+    if (command == null)
+      {
+        try
+          {
+            if (action == _MAIL)
+              { 
+                command = "kfmclient exec";
+              }
+            else if (action == _PRINT)
+              {
+                command = null;
+              }
+            else
+              {
+                command = "kfmclient openURL";
+              }
+          }
+        catch (Exception e)
+          {
+            command = null;
+          }
+      }
+    
+    return command;
+  }
+     
+  @Override
+  protected boolean supportCommand(String check)
+  {
+    if (check == _PRINT)
+      { 
+        return super.supportCommand(check);
+      }
+    
+    return true;
+  }
+  
+  @Override
+  public void mail() throws IOException
+  {
+    checkPermissions();
+    
+    String mail = getCommand(_MAIL);
+    
+    if (mail == null)
+      throw new UnsupportedOperationException();
+    
+    Runtime.getRuntime().exec(mail + " mailto:");
+  }
+  
+  protected String execQuery(String command) throws IOException
+  {
+    InputStream in = null;
+    StringBuilder output = new StringBuilder();
+
+    try
+      {
+        Process process = Runtime.getRuntime().exec(command);
+
+        // Get the input stream and read from it
+        in = process.getInputStream();
+        int c;
+        while ((c = in.read()) != - 1)
+          {
+            output.append((char) c);
+          }
+      }
+    finally
+      {
+        if (in != null)
+          in.close();
+      }
+
+    // remove %s from the string, leave only the command line
+    int index = output.indexOf("%s");
+    output.delete(index, index + 1);
+    
+    return output.toString().trim();
+  }
+}
Index: gnu/java/awt/peer/ClasspathDesktopPeer.java
===================================================================
RCS file: gnu/java/awt/peer/ClasspathDesktopPeer.java
diff -N gnu/java/awt/peer/ClasspathDesktopPeer.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/java/awt/peer/ClasspathDesktopPeer.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,301 @@
+/* ClasspathDesktopPeer.java -- Offers a concrete implementation for DesktopPeer
+ Copyright (C) 2006, 2007 Free Software Foundation, Inc.
+
+ This file is part of GNU Classpath.
+
+ GNU Classpath is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ GNU Classpath is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Classpath; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301 USA.
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library.  Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library with independent modules to produce an
+ executable, regardless of the license terms of these independent
+ modules, and to copy and distribute the resulting executable under
+ terms of your choice, provided that you also meet, for each linked
+ independent module, the terms and conditions of the license of that
+ module.  An independent module is a module which is not derived from
+ or based on this library.  If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so.  If you do not wish to do so, delete this
+ exception statement from your version. */
+
+package gnu.java.awt.peer;
+
+import java.awt.AWTPermission;
+import java.awt.Desktop.Action;
+import java.awt.peer.DesktopPeer;
+
+import java.io.File;
+import java.io.IOException;
+
+import java.net.URI;
+
+import java.util.prefs.Preferences;
+
+/**
+ * Offers a common implementation for the Desktop peers, that enables
+ * access to default system application within java processes.
+ * 
+ * @author Mario Torre <[EMAIL PROTECTED]>
+ */
+public class ClasspathDesktopPeer
+    implements DesktopPeer
+{
+  /** This is the fallback browser, if no desktop was detected. */
+  protected static final String _DEFAULT_BROWSER = "firefox";
+  
+  /** gnu.java.awt.peer.Desktop.html.command */
+  protected static final String _BROWSE = "html";
+  
+  /** gnu.java.awt.peer.Desktop.mail.command */
+  protected static final String _MAIL = "mail";
+  
+  /** gnu.java.awt.peer.Desktop.edit.command */
+  protected static final String _EDIT = "edit";
+  
+  /** gnu.java.awt.peer.Desktop.print.command */
+  protected static final String _PRINT = "print";
+  
+  /** gnu.java.awt.peer.Desktop.open.command */
+  protected static final String _OPEN = "open";
+  
+  /** */
+  protected static final KDEDesktopPeer kde = new KDEDesktopPeer();
+  
+  /** */
+  protected static final GnomeDesktopPeer gnome = new GnomeDesktopPeer();
+  
+  /** */
+  protected static final ClasspathDesktopPeer classpath =
+    new ClasspathDesktopPeer(); 
+  
+  /**
+   * Preference subsystem. Packagers and users can override the default
+   * behaviour of this class via preferences and system properties.
+   */
+  protected Preferences prefs =
+    Preferences.userNodeForPackage(ClasspathDesktopPeer.class).node("Desktop");
+  
+  /**
+   * @param target
+   */
+  protected ClasspathDesktopPeer()
+  {
+    /* nothing to do */
+  }
+
+  public boolean isSupported(Action action)
+  {
+    String check = null;
+    
+    switch(action)
+    {
+      case BROWSE:
+        check = _BROWSE;
+        break;
+        
+      case MAIL:
+        check = _MAIL;
+        break;
+        
+      case EDIT:
+        check = _EDIT;
+        break;
+        
+      case PRINT:
+        check = _PRINT;
+        break;
+      
+      case OPEN: default:
+        check = _OPEN;
+        break;
+    }
+    
+    return this.supportCommand(check);
+  }
+
+  public void browse(URI url) throws IOException
+  {
+    checkPermissions();
+    
+    String browser = getCommand(_BROWSE);
+    
+    if (browser == null)
+      throw new UnsupportedOperationException();
+    
+    browser = browser + " " + url.toString();
+    
+    Runtime.getRuntime().exec(browser);
+  }
+
+  public void edit(File file) throws IOException
+  {
+    checkPermissions(file, false);
+    
+    String edit = getCommand(_EDIT);
+    
+    if (edit == null)
+      throw new UnsupportedOperationException();
+    
+    edit = edit + " " + file.getAbsolutePath(); 
+    Runtime.getRuntime().exec(edit);
+  }
+
+  public void mail(URI mailtoURL) throws IOException
+  {
+    checkPermissions();
+    
+    String scheme = mailtoURL.getScheme();
+    if (scheme == null || !scheme.equalsIgnoreCase("mailto"))
+      throw new IllegalArgumentException("URI Scheme not of type mailto");
+    
+    String mail = getCommand(_MAIL);
+    
+    if (mail == null)
+      throw new UnsupportedOperationException();
+    
+    mail = mail + " " + mailtoURL.toString();
+    
+    Runtime.getRuntime().exec(mail);
+  }
+
+  public void mail() throws IOException
+  {
+    checkPermissions();
+    
+    String mail = getCommand(_MAIL);
+    
+    if (mail == null)
+      throw new UnsupportedOperationException();
+    
+    Runtime.getRuntime().exec(mail);
+  }
+  
+  public void open(File file) throws IOException
+  {
+    checkPermissions(file, true);
+    
+    String open = getCommand(_OPEN);
+    
+    if (open == null)
+      throw new UnsupportedOperationException();
+    
+    open = open + " " + file.getAbsolutePath(); 
+    Runtime.getRuntime().exec(open);
+  }
+
+  public void print(File file) throws IOException
+  {
+    checkPrintPermissions(file);
+    
+    String print = getCommand(_PRINT);
+    
+    if (print == null)
+      throw new UnsupportedOperationException();
+    
+    print = print + " " + file.getAbsolutePath(); 
+    Runtime.getRuntime().exec(print);
+  }
+  
+  protected String getCommand(String action)
+  {
+    // check if a system property exist
+    String command =
+      System.getProperty("gnu.java.awt.peer.Desktop." + action + ".command");
+    
+    // otherwise, get it from preferences, if any
+    if (command == null)
+      {
+        command = prefs.node(action).get("command", null);
+      }
+    
+    return command;
+  }
+  
+  /**
+   * Note: Checks for AWTPermission("showWindowWithoutWarningBanner") only.
+   */
+  protected void checkPermissions()
+  {
+    SecurityManager sm = System.getSecurityManager();
+    if (sm != null) {
+        sm.checkPermission(new AWTPermission("showWindowWithoutWarningBanner"));
+    }
+  }
+  
+  /**
+   * Calls checkPermissions() and checks for SecurityManager.checkRead() 
+   * and, if readOnly is false, for SecurityManager.checkWrite() 
+   */
+  protected void checkPermissions(File file, boolean readOnly)
+  {
+    checkPermissions();
+    
+    SecurityManager sm = System.getSecurityManager();
+    if (sm != null) {
+        sm.checkRead(file.toString());
+        if (!readOnly) sm.checkWrite(file.toString());
+    }
+  }
+  
+  /**
+   * Calls checkPermissions(file, true) and checks for
+   * SecurityManager.checkPrintJobAccess()
+   */
+  protected void checkPrintPermissions(File file)
+  {
+    checkPermissions(file, true);
+    
+    SecurityManager sm = System.getSecurityManager();
+    if (sm != null) {
+      sm.checkPrintJobAccess();
+    }  
+  }
+  
+  /**
+   * @param check
+   * @return
+   */
+  protected boolean supportCommand(String check)
+  {
+    return ((this.getCommand(check) != null) ?  true : false);
+  }
+  
+  /**
+   * @return
+   */
+  public static DesktopPeer getDesktop()
+  {
+    //  check if we are under Gnome or KDE or anything else
+    String desktopSession = System.getenv("GNOME_DESKTOP_SESSION_ID");
+    if (desktopSession == null)
+      { 
+        desktopSession = System.getenv("KDE_FULL_SESSION");
+        if (desktopSession != null)
+          return kde;
+      }
+    else
+      {
+        return gnome;
+      }
+    
+    // revert to this class for default values
+    return classpath;
+  }
+}
Index: gnu/java/awt/peer/GnomeDesktopPeer.java
===================================================================
RCS file: gnu/java/awt/peer/GnomeDesktopPeer.java
diff -N gnu/java/awt/peer/GnomeDesktopPeer.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/java/awt/peer/GnomeDesktopPeer.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,157 @@
+/* GnomeDesktopPeer.java -- Offers a GNOME Desktop peer for DesktopPeer
+ Copyright (C) 2006, 2007 Free Software Foundation, Inc.
+
+ This file is part of GNU Classpath.
+
+ GNU Classpath is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ GNU Classpath is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Classpath; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301 USA.
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library.  Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library with independent modules to produce an
+ executable, regardless of the license terms of these independent
+ modules, and to copy and distribute the resulting executable under
+ terms of your choice, provided that you also meet, for each linked
+ independent module, the terms and conditions of the license of that
+ module.  An independent module is a module which is not derived from
+ or based on this library.  If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so.  If you do not wish to do so, delete this
+ exception statement from your version. */
+
+package gnu.java.awt.peer;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+
+/**
+ * @author Mario Torre <[EMAIL PROTECTED]>
+ */
+public class GnomeDesktopPeer
+    extends ClasspathDesktopPeer
+{
+  /**
+   * Query string to use if a GNOME desktop is detected to get the name of the
+   * default browser. This requires gconftool-2 (part of GNOME).
+   */
+  private static final String BROWSER_QUERY_GNOME =
+    "gconftool-2 -g /desktop/gnome/url-handlers/http/command";
+
+  @Override
+  protected String getCommand(String action)
+  {
+    // check if a command already exists
+    String command = super.getCommand(action);
+    
+    if (command == null)
+      {
+        try
+          {
+            if (action == _BROWSE)
+              { 
+                command = execQuery(BROWSER_QUERY_GNOME);
+              }
+            else if (action == _PRINT)
+              {
+                command = null;
+              }
+            else
+              {
+                command = "gnome-open";
+              }
+          }
+        catch (Exception e)
+          {
+            command = null;
+          }
+      }
+    
+    return command;
+  }
+  
+  @Override
+  public void browse(URI url) throws IOException
+  {
+    checkPermissions();
+    
+    String browser = getCommand(_BROWSE);
+    
+    if (browser == null)
+      throw new UnsupportedOperationException();
+    
+    browser = browser + " " + url.toString();
+    
+    Runtime.getRuntime().exec(browser);
+  }
+    
+  @Override
+  protected boolean supportCommand(String check)
+  {
+    if (check == _PRINT)
+      { 
+        return super.supportCommand(check);
+      }
+    
+    return true;
+  }
+  
+  @Override
+  public void mail() throws IOException
+  {
+    checkPermissions();
+    
+    String mail = getCommand(_MAIL);
+    
+    if (mail == null)
+      throw new UnsupportedOperationException();
+    
+    Runtime.getRuntime().exec(mail + " mailto:");
+  }
+  
+  protected String execQuery(String command) throws IOException
+  {
+    InputStream in = null;
+    StringBuilder output = new StringBuilder();
+
+    try
+      {
+        Process process = Runtime.getRuntime().exec(command);
+
+        // Get the input stream and read from it
+        in = process.getInputStream();
+        int c;
+        while ((c = in.read()) != - 1)
+          {
+            output.append((char) c);
+          }
+      }
+    finally
+      {
+        if (in != null)
+          in.close();
+      }
+
+    // remove %s from the string, leave only the command line
+    int index = output.indexOf("%s");
+    output.delete(index, index + 1);
+    
+    return output.toString().trim();
+  }
+}
Index: java/awt/peer/DesktopPeer.java
===================================================================
RCS file: java/awt/peer/DesktopPeer.java
diff -N java/awt/peer/DesktopPeer.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ java/awt/peer/DesktopPeer.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,64 @@
+/* DesktopPeer.java -- Interface to enable access to common applications
+ Copyright (C) 2006, 2007 Free Software Foundation, Inc.
+
+ This file is part of GNU Classpath.
+
+ GNU Classpath is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ GNU Classpath is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Classpath; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301 USA.
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library.  Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library with independent modules to produce an
+ executable, regardless of the license terms of these independent
+ modules, and to copy and distribute the resulting executable under
+ terms of your choice, provided that you also meet, for each linked
+ independent module, the terms and conditions of the license of that
+ module.  An independent module is a module which is not derived from
+ or based on this library.  If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so.  If you do not wish to do so, delete this
+ exception statement from your version. */
+
+package java.awt.peer;
+
+import java.awt.Desktop.Action;
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+
+/**
+ * @author Mario Torre <[EMAIL PROTECTED]>
+ *
+ */
+public interface DesktopPeer
+{
+  public void browse(URI url) throws IOException;
+
+  public void edit(File file) throws IOException;
+
+  public boolean isSupported(Action action);
+
+  public void mail(URI mailtoURL) throws IOException;
+  
+  public void mail() throws IOException;
+
+  public void open(File file) throws IOException;
+  
+  public void print(File file) throws IOException;
+}
Index: java/awt/Desktop.java
===================================================================
RCS file: java/awt/Desktop.java
diff -N java/awt/Desktop.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ java/awt/Desktop.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,268 @@
+/* Desktop.java -- enable desktop integration between java programs and system
+ programs.
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+ This file is part of GNU Classpath.
+
+ GNU Classpath is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ GNU Classpath is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Classpath; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301 USA.
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library.  Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library with independent modules to produce an
+ executable, regardless of the license terms of these independent
+ modules, and to copy and distribute the resulting executable under
+ terms of your choice, provided that you also meet, for each linked
+ independent module, the terms and conditions of the license of that
+ module.  An independent module is a module which is not derived from
+ or based on this library.  If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so.  If you do not wish to do so, delete this
+ exception statement from your version. */
+
+
+package java.awt;
+
+import java.awt.peer.DesktopPeer;
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+
+/**
+ * This class enables Java application to access system commands to perform
+ * desktop oriented operations, like writing and sending emails, or surfing
+ * webpages with the system browser or editing/printing files with a default
+ * editor. Methods are provided to handle these common operations, plus an
+ * <code>open</code> command selects a default registered application for the
+ * specified file type. For example, opening an odf file results in launching
+ * OpenOffice. If an operation is not supported, or the application fails to
+ * launch, an exception is generated.
+ * 
+ * <strong>Implementation note: </strong>As this class is used to manage Desktop
+ * integration, we provide some extension to configure the behaviour of this
+ * class depending on the type of dektop that is detected.<br />
+ * 
+ * First of all, we support 5 system properties that can be used to define
+ * the application to launch in any given case. These properties are:<br />
+ * <br />
+ * <code>gnu.java.awt.peer.Desktop.html.command</code><br />
+ * <code>gnu.java.awt.peer.Desktop.mail.command</code><br />
+ * <code>gnu.java.awt.peer.Desktop.edit.command</code><br />
+ * <code>gnu.java.awt.peer.Desktop.print.command</code><br />
+ * <code>gnu.java.awt.peer.Desktop.open.command</code><br />
+ * <br />
+ * <br />
+ * These can be specified from the command line and have priority on every
+ * other setting.<br />
+ * <br />
+ * The second method supported is defining a Java preference.<br />
+ * The main preference node is a <strong>user node</strong> relative to the
+ * class <code>gnu.java.awt.peer.ClasspathDesktopPeer</code>. This node
+ * contains a child for each supported operation. The key for each type is
+ * always <code>command</code>:
+ * <br /><br />
+ * <code>gnu.java.awt.peer.Desktop.html.command</code><br />
+ * <code>gnu.java.awt.peer.Desktop.mail.command</code><br />
+ * <code>gnu.java.awt.peer.Desktop.edit.command</code><br />
+ * <code>gnu.java.awt.peer.Desktop.print.command</code><br />
+ * <code>gnu.java.awt.peer.Desktop.open.command</code><br />
+ * <br />
+ * <br />
+ * The access to these keys is done with the Preference API or, if outside
+ * of the java scope, is done in a backend dependent way. For example,
+ * with the GConf backend, you can access these properties
+ * with (may not be accurate on your system):
+ * <br /><br />
+ * <code>
+ * gconftool-2 -g /apps/classpath/gnu/java/awt/peer/Desktop/html/command
+ * </code> 
+ * 
+ * @author Mario Torre <[EMAIL PROTECTED]>
+ * @since 1.6
+ */
+public class Desktop
+{
+  /**
+   * Represents an action type supported by a platform.
+   * 
+   * To determine if a given action is supported by the platform,
+   * use the <code>Desktop.isSupported(java.awt.Desktop.Action)</code>
+   * method.
+   * 
+   * @author Mario Torre <[EMAIL PROTECTED]>
+   */
+  public enum Action
+  {
+    BROWSE, EDIT, MAIL, OPEN, PRINT
+  }
+
+  private DesktopPeer peer;
+
+  private Desktop()
+  {
+    /* nothing to be done */
+  }
+  
+  /**
+   * Returns an istance of the Desktop Class.
+   * 
+   * If this implementation does not support Desktop, an 
+   * UnsupportedOperationException will be thrown.
+   * Also, an HeadlessException will be generated if
+   * GraphicsEnvironment.isHeadless() returns true.
+   * 
+   * @throws UnsupportedOperationException
+   * @throws HeadlessException
+   */
+  public static Desktop getDesktop() throws UnsupportedOperationException,
+      HeadlessException
+  {
+    if (GraphicsEnvironment.isHeadless())
+      throw new HeadlessException();
+    
+    if (!Desktop.isDesktopSupported())
+      throw new UnsupportedOperationException();
+    
+    Desktop desktop = new Desktop();
+    desktop.peer = Toolkit.getDefaultToolkit().createDesktopPeer(desktop);
+    
+    return desktop;
+  }
+
+  /**
+   * Check if this implementation supports Desktop.
+   * If true, use getDesktop to get an instance of this class.
+   * 
+   * This implementation will return false when GraphicsEnvironment.isHeadless
+   * returns true.
+   * 
+   * @return true if this class is supported on the current platform;
+   * false otherwise
+   */
+  private static boolean isDesktopSupported()
+  {
+    if (GraphicsEnvironment.isHeadless())
+      return false;
+    
+    return true;
+  }
+  
+  /**
+   * Check if the given Action is supported by this implementation.
+   * 
+   * @param action
+   * @return
+   */
+  public boolean isSupported(Desktop.Action action)
+  {
+    return peer.isSupported(action);
+  }
+  
+  /**
+   * Launches the Desktop default browser to open the given <code>uri</code>.
+   * 
+   * If a security manager exists and denies
+   * AWTPermission("showWindowWithoutWarningBanner"),a SecurityException will
+   * be generated.
+   * 
+   * @param uri
+   * @throws IOException
+   */
+  public void browse(URI uri)
+    throws IOException
+  {
+    peer.browse(uri);
+  }
+  
+  /**
+   * Launch the edit command to edit this file.
+   * File should already exist before the editing starts. 
+   * 
+   * If a security manager exists and
+   * SecurityManager.checkRead(java.lang.String) method denies read access to
+   * the file, or SecurityManager.checkPrintJobAccess() method denies the
+   * permission to print the file, a SecurityException will be generated.
+   * 
+   * @param file
+   * @throws IOException
+   */
+  public void edit(File file)
+    throws IOException
+  {
+    peer.edit(file);
+  }
+  
+  /**
+   * Launches the Desktop default mailer.
+   * 
+   * If a security manager exists and denies
+   * AWTPermission("showWindowWithoutWarningBanner"), a SecurityException will
+   * be generated.
+   *
+   * @throws IOException
+   */
+  public void mail()
+    throws IOException
+  {
+    peer.mail();
+  }
+  
+  /**
+   * Launches the Desktop default mailer, with the given mailtoURI
+   * as agrument. The <code>mailtoURI</code> must conform to the
+   * [EMAIL PROTECTED] http://www.ietf.org/rfc/rfc2368.txt The mailto URL scheme (RFC 2368)} 
+   * 
+   * If a security manager exists and denies
+   * AWTPermission("showWindowWithoutWarningBanner"), a SecurityException will
+   * be generated.
+   * 
+   * @param mailtoURI
+   * @throws IOException
+   */
+  public void mail(URI mailtoURI)
+    throws IOException
+  {
+    peer.mail(mailtoURI);
+  }
+  
+  /**
+   * Launches the Desktop default application to open the given File.
+   * If <code>file</code> is a directory, a file manager is launched.
+   * 
+   * @param file
+   * @throws IOException
+   */
+  public void open(File file)
+    throws IOException
+  {
+    peer.open(file);
+  }
+
+  /**
+   * Launch the print program to print this file.
+   * 
+   * @param file
+   * @throws IOException
+   */
+  public void print(File file)
+    throws IOException
+  {
+    peer.print(file);
+  }
+}

Reply via email to