Paid for writing

In case you like my writing and would like me to write for your website, then please leave a comment to any of my blog article, mentioning your Email Id and I will reply back. Thanks

Monday, February 9, 2009

Implementing Desktop Class in Java

/*The Desktop Class in Java can be used to carry out the following tasks.
1. Open the Default Web Browser
2. Open a file for editing, reading.
3. Open the default mail Client.
4. Print a file.
*/

//DesktopAction.java

import java.net.URI;
import java.awt.Desktop;
import java.io.IOException;
import java.io.File;
import java.io.Console;

public class DesktopAction
{
int choice;
Desktop desk;
Console console;

public DesktopAction()
{
choice=1;
if(Desktop.isDesktopSupported())
{
desk=Desktop.getDesktop();
}

console=System.console();
}

public static void main(String args[]) throws Exception
{
DesktopAction da=new DesktopAction();
da.menu();

}


public void menu() throws Exception
{
do
{
System.out.println("1. Browse a Website ");
System.out.println("2. Edit a File ");
System.out.println("3. Open Mail Client ");
System.out.println("4. Print a File ");
System.out.println("5. Open a File ");
System.out.println("6. Exit");
System.out.print("Enter choice : ");
choice=Integer.parseInt(console.readLine());

switch(choice)
{
case 1:
browseWeb();
break;

case 2:
editFile();
break;

case 3:
openMail();
break;


case 4:
printFile();
break;

case 5:
openFile();
break;

case 6:
System.exit(0);


}


}while(choice!=6);
}

void browseWeb() throws Exception
{

if(desk.isSupported(Desktop.Action.BROWSE))
{
System.out.print("Enter the website: ");
String site=console.readLine();

desk.browse(new URI(site));
}
}

void editFile() throws Exception
{
if(desk.isSupported(Desktop.Action.EDIT))
{
System.out.print("Enter the file name: ");
String fileName=console.readLine();

desk.edit(new File(fileName));
}
}

void openMail() throws Exception
{
if(desk.isSupported(Desktop.Action.MAIL))
{

System.out.print("Do you want to enter the Mail To for Email Client : ");
String option=console.readLine();
if(option.equals("Y")||option.equals("y"))
{
System.out.print("Enter mail to : ");
String mailTo=console.readLine();
desk.mail(new URI(mailTo));
}
else
{
desk.mail();
}
}
}

void printFile() throws Exception
{
if(desk.isSupported(Desktop.Action.PRINT))
{
System.out.print("Enter the file name: ");
String fileName=console.readLine();

desk.print(new File(fileName));
}
}

void openFile() throws Exception
{
if(desk.isSupported(Desktop.Action.OPEN))
{
System.out.print("Enter the file name: ");
String fileName=console.readLine();

desk.open(new File(fileName));
}
}
}


//Explanation

1. The source code implements the Desktop Class.
2. The class DesktopAction has a main() which creates an object of the class DesktopAction.
3. It calls the menu() function which displays the user menu.
4. The menu() provides the option to browse a website, edit a website, open a mail client, and open a file for reading.
5. The console object of the Console class is used to read the user input.
6. Depending upon the user input the menu() calls one of the other functions within the class
7. The constructor of the class creates an object of the Desktop called desk, which is used by other functions.
8. Inside each of the functions there is an isSupported() function. The isSupported function checks whether the Desktop action, which is to be executed below, is supported or not.
9. So in browseWeb() the Desktop.Action.BROWSE is checked, if supported then the user enters the website name and the browse() function opens the default browser with the url mentioned by the user.
10. Similarly the editFile() function reads the file name from the user and opens the file for editing using the edit() function.
11. The openMail() uses the mail() function to open the default mail client. In case the user wants to enter the name of the Mail To, then he can do so and the mail client will open with that address embedded in MailTo:
12. The printFile() similar to editFile() prints the file.
13. The openFile() opens the file. Its functioning is similar to editFile() and printFile().

0 comments: