Que tal amigos, luego de un buen tiempo no POSTEANDO en mi BLOG, debido a falta de tiempo, hoy reaparesco posteando y mostrandoles el manejo de un clase muy util llamada: java.awt.Desktop. Esta clase brinda facilidades para poder abrir documentos, editarlos y mandarlos a imprimir facilmente.
En detalle esta clase brinda las siguientes facilidades, pero eso si trabaja con un JDK 1.6, tengan en cuenta eso:
- Desktop.Action.OPEN = Brinda facilidad para abrir un fichero desde JAVA, apoyándose en el software por defecto asigando a ese fichero.
- Desktop.Action.EDIT = Brinda facilidad para editar un fichero desde JAVA, apoyándose en el software por defecto asigando a ese fichero.
- Desktop.Action.PRINT = Brinda facilidad para impresion de un fichero desde JAVA, apoyándose en el software por defecto asigando a ese fichero.
- Desktop.Action.BROWSE = Brinda facilidad para acceder a un URL, apoyándose en el navegador por defecto asigando a ese fichero.
Aqui el ejemplo completo:
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
/**
* @author Cesar Ricardo.
* @clase: TestDesktop.java
* @descripción descripción de la clase.
* @author_web: http://frameworksjava2008.blogspot.com
http://viviendoconjavaynomoririntentandolo.blogspot.com
* @author_email: nombre del email del autor.
* @author_company: nombre de la compañía del autor.
* @fecha_de_creación: dd-mm-yyyy.
* @fecha_de_ultima_actualización: dd-mm-yyyy.
* @versión 1.0
*/
public class ManejoDesktopAWT{
private Desktop desktop = null;
private Desktop.Action action = null;
private File archivo = null;
/**
* main
* @param argumentos
* @throws IOException
*/
public static void main( String[] argumentos ){
ManejoDesktopAWT desktop = new ManejoDesktopAWT();
//Validar que existan en la ruta.
desktop.getUtilProcesarFicheros( "C:\\MI_FOTO.jpg", "ABRIR" );
//desktop.getUtilProcesarFicheros( "C:\\CIBERTEC.gif", "EDITAR" );
//desktop.getUtilProcesarFicheros( "C:\\DOCUMENTO.txt", "IMPRIMIR" );
//desktop.getLinkURL( "http://viviendoconjavaynomoririntentandolo.blogspot.com/" );
}
/**
* getUtilProcesarFicheros este método permite el 'ABRIR, EDITAR y IMPRIMIR' un archivo de cualquier formato en base al
* software por default que tenga asignado.
* 'ABRIR, EDITAR, IMPRIMIR'.
* @param rutaFichero
*/
public void getUtilProcesarFicheros( String rutaFichero, String procesoFichero ){
try{
System.out.println( "SOPORTADO: " + Desktop.isDesktopSupported() );
if( Desktop.isDesktopSupported() == true ){
desktop = Desktop.getDesktop();
archivo = new File( rutaFichero );
if( procesoFichero.equalsIgnoreCase( "ABRIR" ) ){
action = Desktop.Action.OPEN;
}
else if( procesoFichero.equalsIgnoreCase( "EDITAR" ) ){
action = Desktop.Action.EDIT;
}
else if( procesoFichero.equalsIgnoreCase( "IMPRIMIR" ) ){
action = Desktop.Action.PRINT;
}
switch( action ){
case OPEN :
System.out.println( "DENTRO DE OPCION: OPEN" );
desktop.open( archivo );
break;
case EDIT :
System.out.println( "DENTRO DE OPCION: EDIT" );
desktop.edit( archivo );
break;
case PRINT :
System.out.println( "DENTRO DE OPCION: PRINT" );
desktop.print( archivo );
break;
}
}
}
catch( IOException e ){
e.printStackTrace();
}
catch( Exception e ){
e.printStackTrace();
}
}
/**
* getLinkURL
* @param url
*/
public void getLinkURL( String url ){
desktop = Desktop.getDesktop();
try{
if( !(desktop.isSupported( Desktop.Action.BROWSE ) == true) ) {
System.err.println( "Desktop no soportar la accion del Navegador." );
System.exit( 1 );
}
else{
URI uri = new URI( url );
desktop.browse( uri );
}
}
catch( Exception e ) {
e.printStackTrace();
}
}
}
Espero les sea de utilidad.
Saludos.
Showing posts with label Desktop. Show all posts
Showing posts with label Desktop. Show all posts
Saturday, April 17, 2010
Subscribe to:
Posts (Atom)

