Desktop.java 8.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/* 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.
56
 *
57 58 59
 * <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 />
60
 *
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
 * 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
94 95
 * </code>
 *
96 97 98 99 100 101 102
 * @author Mario Torre <neugens@limasoftware.net>
 * @since 1.6
 */
public class Desktop
{
  /**
   * Represents an action type supported by a platform.
103
   *
104 105 106
   * To determine if a given action is supported by the platform,
   * use the <code>Desktop.isSupported(java.awt.Desktop.Action)</code>
   * method.
107
   *
108 109 110 111 112 113 114 115 116 117 118 119 120
   * @author Mario Torre <neugens@limasoftware.net>
   */
  public enum Action
  {
    BROWSE, EDIT, MAIL, OPEN, PRINT
  }

  private DesktopPeer peer;

  private Desktop()
  {
    /* nothing to be done */
  }
121

122
  /**
123
   * Returns an instance of the Desktop Class.
124 125
   *
   * If this implementation does not support Desktop, an
126 127 128
   * UnsupportedOperationException will be thrown.
   * Also, an HeadlessException will be generated if
   * GraphicsEnvironment.isHeadless() returns true.
129
   *
130 131 132 133 134 135 136 137
   * @throws UnsupportedOperationException
   * @throws HeadlessException
   */
  public static Desktop getDesktop() throws UnsupportedOperationException,
      HeadlessException
  {
    if (GraphicsEnvironment.isHeadless())
      throw new HeadlessException();
138

139 140
    if (!Desktop.isDesktopSupported())
      throw new UnsupportedOperationException();
141

142 143
    Desktop desktop = new Desktop();
    desktop.peer = Toolkit.getDefaultToolkit().createDesktopPeer(desktop);
144

145 146 147 148 149 150
    return desktop;
  }

  /**
   * Check if this implementation supports Desktop.
   * If true, use getDesktop to get an instance of this class.
151
   *
152 153
   * This implementation will return false when GraphicsEnvironment.isHeadless
   * returns true.
154
   *
155 156 157
   * @return true if this class is supported on the current platform;
   * false otherwise
   */
158
  public static boolean isDesktopSupported()
159 160 161
  {
    if (GraphicsEnvironment.isHeadless())
      return false;
162

163 164
    return true;
  }
165

166 167
  /**
   * Check if the given Action is supported by this implementation.
168
   *
169 170 171 172 173 174 175
   * @param action
   * @return
   */
  public boolean isSupported(Desktop.Action action)
  {
    return peer.isSupported(action);
  }
176

177 178
  /**
   * Launches the Desktop default browser to open the given <code>uri</code>.
179
   *
180 181 182
   * If a security manager exists and denies
   * AWTPermission("showWindowWithoutWarningBanner"),a SecurityException will
   * be generated.
183
   *
184 185 186 187 188 189 190 191
   * @param uri
   * @throws IOException
   */
  public void browse(URI uri)
    throws IOException
  {
    peer.browse(uri);
  }
192

193 194
  /**
   * Launch the edit command to edit this file.
195 196
   * File should already exist before the editing starts.
   *
197 198 199 200
   * 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.
201
   *
202 203 204 205 206 207 208 209
   * @param file
   * @throws IOException
   */
  public void edit(File file)
    throws IOException
  {
    peer.edit(file);
  }
210

211 212
  /**
   * Launches the Desktop default mailer.
213
   *
214 215 216 217 218 219 220 221 222 223 224
   * If a security manager exists and denies
   * AWTPermission("showWindowWithoutWarningBanner"), a SecurityException will
   * be generated.
   *
   * @throws IOException
   */
  public void mail()
    throws IOException
  {
    peer.mail();
  }
225

226 227 228
  /**
   * Launches the Desktop default mailer, with the given mailtoURI
   * as agrument. The <code>mailtoURI</code> must conform to the
229 230
   * {@link http://www.ietf.org/rfc/rfc2368.txt The mailto URL scheme (RFC 2368)}
   *
231 232 233
   * If a security manager exists and denies
   * AWTPermission("showWindowWithoutWarningBanner"), a SecurityException will
   * be generated.
234
   *
235 236 237 238 239 240 241 242
   * @param mailtoURI
   * @throws IOException
   */
  public void mail(URI mailtoURI)
    throws IOException
  {
    peer.mail(mailtoURI);
  }
243

244 245 246
  /**
   * Launches the Desktop default application to open the given File.
   * If <code>file</code> is a directory, a file manager is launched.
247
   *
248 249 250 251 252 253 254 255 256 257 258
   * @param file
   * @throws IOException
   */
  public void open(File file)
    throws IOException
  {
    peer.open(file);
  }

  /**
   * Launch the print program to print this file.
259
   *
260 261 262 263 264 265 266 267 268
   * @param file
   * @throws IOException
   */
  public void print(File file)
    throws IOException
  {
    peer.print(file);
  }
}