Java SE 6.0 desktop API Programming

Source: Internet
Author: User
Tags java se
In terms of the default GUI appearance, printing and running performance, the Java platform has been striving to narrow the gap between local applications and Java applications. With the advent of Java SE 6 (code named Mustang), some new features have been added, including new system tray functions, better printing support and desktop APIs (Java. AWT. desktop API) to further narrow the gap. The new desktop APIs described in this article allow Java applications to interact with default applications of specific file types on the host platform. To describe these Apis more effectively, this article will also show you a simple example application topdemo.

  I. Desktop Overview

This new feature is provided by the java. AWT. Desktop class. This API comes from the jdesktop integration component (JDIC) project. The purpose of this project is to make Java-based applications become "first-class citizens" on the desktop platform and achieve seamless integration with desktop APIs. Specifically, this new desktop API allows your Java applications to implement the following functions:

· Use a specific Uniform Resource Identifier (URI) to start the default browser of the host system

· Start the default email client of the host system

· Start a specific application to open, edit, or print associated files

These desktop APIs use File Associations of your host's operating system to start applications associated with specific file types. For example, if the document text (. ODT) the file extension is associated with the OpenOffice writer application, so your Java application can start the OpenOffice writer to open, edit, or print files related to this association. Depending on your host system, different applications may be associated with different behaviors.

  2. Run the topdemo Application

Topdemo is a simple Java application that uses the Mustang desktop API. The application provides a main window that allows you to implement the following three functions:

1. Start the default browser with a specific URI.

2. Start the default email client with one email recipient.

3. Start an associated application to open, edit, or print files.

Figure 1 shows the user interface (UI ).


Figure 1: Consumer topdemo user interface

You can run the application by downloading the application source code and related jar files-change the Active Directory of your console to the Dist directory of the application project, run the following command with a Mustang JDK:

Java-jar producer topdemo. Jar

  Iii. Determine whether desktop APIs are supported

Before starting a browser, email client, or any application, topdemo must determine whether your platform supports this API. However, topdemo first disables all graphical text fields and buttons. The program enables these graphical components only after the platform supports them.

After instantiating these UIS, the app constructor quickly disables a few components of the app, as shown in the following code:

Public writable topdemo (){
// Initialize all gui components.
Initcomponents ();
// Disable the buttons for starting the browser and email client
// Disable the open, edit, and print button
Disableactions ();
...
}
/**
* Stop all graphical components until we know
* Whether their functions are supported.
*/
Private void disableactions (){
Txtbrowseruri. setenabled (false );
Btnlaunchbrowser. setenabled (false );

Txtmailto. setenabled (false );
Btnlaunchemail. setenabled (false );
Rbedit. setenabled (false );
Rbopen. setenabled (false );
Rbprint. setenabled (false );
Txtfile. setenabled (false );
Btnlaunchapplication. setenabled (false );
}
...
Public javax. Swing. jtextfield txtbrowseruri;
Public javax. Swing. jbutton btnlaunchbrowser;
Public javax. Swing. jtextfield txtmailto;
Public javax. Swing. jbutton btnlaunchemail;
Public javax. Swing. jradiobutton rbedit;
Public javax. Swing. jradiobutton rbopen;
Public javax. Swing. jradiobutton rbprint;
Public javax. Swing. jtextfield txtfile;
Public javax. Swing. jbutton btnlaunchapplication;

Use the desktop. isdesktopsupported () method to determine whether the desktop API is available. On the Solaris operating system and Linux platform, such APIs depend on the gnome library. If these libraries are unavailable, this method returns false. After you confirm that this API is supported (that is, isdesktopsupported () returns true), the application can use the static method getdesktop () to retrieve a desktop instance.

Desktop desktop = NULL;
// Before using more desktop APIs, check
// Whether this API is supported by a special virtual machine on the specified host.
If (desktop. isdesktopsupported ()){
Desktop = desktop. getdesktop ();
...

If your application does not use isdesktopsupported () for API support check before calling getdesktop, then it must be prepared to capture an unsupportedoperationexception-this exception will be thrown when your application requests a desktop instance on a platform that does not support this feature. In addition, if your application runs in a non-keyboard, mouse, or monitor environment, the getdesktop () method throws a java. AWT. headlessexception exception.

Once the search is complete, this desktop instance allows your applications to browse, mail, open, edit, or even print a file or Uri, however, only the retrieved desktop instance supports these activities. Each of these activities is called an action, and each action is described as a desktop. Action enumeration instance:

· Browse-describes a browsing behavior performed by the default browser of the host.

· Mail-describe a mail behavior executed by the default email client of the host

· Open-describes the opening behavior of an application associated with opening a specific file type.

· Edit-describes the editing action performed by an application associated with editing a specific file type.

· Print-Describe the printing behavior performed by an application associated with printing a specific file type

Before calling any of these actions, an application must determine whether the desktop instance supports them. This is different from determining whether a desktop instance is available. The desktop. isdesktopsupported () method tells you whether an instance can be created. Once you obtain a desktop object, you can query the object to determine which types of behaviors are supported. If the desktop object does not support specific behaviors, or if the desktop API itself is not supported, toptopdemo simply disables the affected graphic components. As shown in figure 2, you cannot use these components to call the desktop features when the feature is disabled.


Figure 2: graphical components are disabled when desktop APIs are not supported.

By using a new desktop instance, the following code checks whether desktop. Action is supported and enables appropriate graphical components:

Public writable topdemo (){
...
// Before using more desktop APIs, check
// Whether this API is supported by a special virtual machine on the specified host.
If (desktop. isdesktopsupported ()){
Desktop = desktop. getdesktop ();
// Now, enable the button to implement the supported Behavior
Enablesupportedactions ();
}
...
}
/**
* Enables behaviors supported on the host.
* These actions are: Open the browser,
* Open the email client, and use the applications associated with them to open, edit, and print files.
*/
Private void enablesupportedactions (){
If (desktop. issupported (desktop. Action. Browse )){
Txtbrowseruri. setenabled (true );
Btnlaunchbrowser. setenabled (true );
}

If (desktop. issupported (desktop. Action. Mail )){
Txtmailto. setenabled (true );
Btnlaunchemail. setenabled (true );
}
If (desktop. issupported (desktop. Action. Open )){
Rbopen. setenabled (true );
}
If (desktop. issupported (desktop. Action. Edit )){
Rbedit. setenabled (true );
}
If (desktop. issupported (desktop. Action. Print )){
Rbprint. setenabled (true );
}

If (rbedit. isenabled () | rbopen. isenabled () | rbprint. isenabled ()){
Txtfile. setenabled (true );
Btnlaunchapplication. setenabled (true );
}
}

Once the application determines the supported behavior, it enables the appropriate graphical components. If all components are enabled, the corresponding UI should look like 3.


Figure 3: Enable components when desktop APIs are supported.

  4. Open a browser

The following instance method will open the default browser of your host:

Public void browse (URI) throws ioexception

Because only the associated desktop is supported. the UI component is enabled only when actiondesktopdemo is used. Therefore, before calling the Browse () method, this simple demo application does not need to perform behavior support checks. However, checking behavior before each call supports adding program robustness in practice:

If (desktop. issupported (desktop. Action. Browse )){
// Start the browser
...
}

Desktopdemo adds a java. AWT. event. actionlistener to each button. When enabled, the "launch Browser" button uses its actionlistener to call the following methods:

Private void onlaunchbrowser (Java. AWT. event. actionevent EVT ){
Uri uri = NULL;
Try {
Uri = new uri (txtbrowseruri. gettext ());
Desktop. Browse (URI );
}
Catch (ioexception IOE ){
IOE. printstacktrace ();
}
Catch (urisyntaxexception use ){
Use. printstacktrace ();
}
...
}

This browse () method may throw various types of exceptions, including: an nullpointerexception exception is thrown when the URI is null; If browse behavior is not supported, an unsupportedoperationexception is thrown; if a default browser or application cannot be found or started, an ioexception is thrown. If a security manager denies one call, a securityexception exception is thrown.

However, if everything goes well, the listener retrieves text from the text domain associated with figure 4, creates a URI, and calls the Browse () method. The above code starts the default browser of your system and instructs the browser to load the URI, as shown in Figure 5.


Figure 4: Start the default browser with a specific URI.

Figure 5: Use the desktop API to start the default browser.

  5. send an email

If this behavior is supported, the application can start the default email client of the Host-by calling this desktop instance method:

Public void mail (URI) throws ioexception
Topdemo provides an actionlistener for the "launch mail" button. In this case, the listener calls the following methods:
Private void onlaunchmail (Java. AWT. event. actionevent EVT ){
String mailto = txtmailto. gettext ();
Uri urimailto = NULL;
Try {
If (mailto. Length ()> 0 ){
Urimailto = new uri ("mailto", mailto, null );
Desktop. Mail (urimailto );
} Else {
Desktop. Mail ();
}
}
Catch (ioexception IOE ){
IOE. printstacktrace ();
}
Catch (urisyntaxexception use ){
Use. printstacktrace ();
}
...
}

This onlaunchmail () method retrieves Email recipients from the relevant text domain, creates a URI Using A mailto mode parameter when there is a receiver, and then calls the mail () method. This mail () method is overloaded, so that you can use (or do not use) a URI describing its mailto receiver (see Figure 6) to call this method.


Figure 6: use an email recipient to start the default email client.

When creating this URI, you can use multiple Email recipients. This mailto mode supports CC, BCC, subject, and body fields. For example, you can use the following text to create a mailto URI:

Mailto: duke@sun.com? Subject = Happy New Year! & Body = Happy New Year, Duke!

Figure 7 shows the corresponding results.


Figure 7: The desktop API uses multiple mailto parameters to start the default email client.

Of course, you can also call mail () without parameters (). In this case, your email client will start a new email window without a specified recipient, subject, or body.

  6. Open, edit, and print files

Java applications can use the open (), edit (), and print () methods of a desktop object to open a desktop object from the associated application, edit and print files (see figure 8 ). Similarly, topdemo allows these actions only when the desktop instance supports them. Therefore, in this application environment, you do not have to perform such support checks again.


Figure 8: start the application associated with a specific file type.

Each single-choice button in topdemo also has its own actionlistener. In this case, each single-choice button sets an instance variable to describe the associated desktop. Action of the recently selected button:

Desktop. Action action;
Private void onprintaction (Java. AWT. event. actionevent EVT ){
Action = desktop. Action. Print;
}
Private void oneditaction (Java. AWT. event. actionevent EVT ){
Action = desktop. Action. Edit;
}
Private void onopenaction (Java. AWT. event. actionevent EVT ){
Action = desktop. Action. open;
}

When you press the "launch Default Application" button, it calls its own listener-this will call the following methods:

Private void onlaunchdefaultapplication (Java. AWT. event. actionevent EVT ){
String filename = txtfile. gettext ();
File file = new file (filename );
Try {
Switch (Action ){
Case open:
Desktop. Open (File );
Break;
Case EDIT:
Desktop. Edit (File );
Break;
Case print:
Desktop. Print (File );
Break;
}
}
Catch (ioexception IOE ){
IOE. printstacktrace ();
}
...
}

This method determines which desktop. Action to select and calls the appropriate desktop instance method-open (), edit () or print (). Each method requires a file parameter-it is used to execute the required behavior.

Interestingly, different applications can register for these different behaviors on the same file type. For example, you can use the open action to start the Firefox browser, use the edit action to start Emacs, or even use the print action to start different applications. The association between your host desktop is used to determine what applications should be called.

Note that using existing desktop APIs in Mustang to operate desktop file associations is impossible. Currently, you can only use tools on the platform to create or change these associations.

  VII. Summary

Desktop integration is an important topic of Mustang. One way Mustang supports this topic is to provide a set of Java. AWT. Desktop APIs. This API allows Java applications to start the default browser and email client of the host. In addition, Java applications can start applications associated with specific file types to open, edit, and print files. Although Java applications cannot operate, create, or change file associations, these desktop APIs are sure to allow Java applications to start the default associated applications.

Source: Tianji original link: http://soft.yesky.com/360/2408860.shtml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.