segunda-feira, 18 de fevereiro de 2008

Como obter MAC, IP e nome da máquina.

Olá a todos!
Hoje trago para vocês uma classe que torna possível obter o endereço MAC, o IP e o nome da máquina!!
Aproveitem!


import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;

/**
* @author Dalí Freire Dias
* @version 03/07/2007
*/
public class FacadeNet
{

public static String getMAC()
{
InetAddress localHost;
NetworkInterface networkInterface;
String mac = "";

try {

localHost = InetAddress.getLocalHost();
networkInterface = NetworkInterface.getByInetAddress( localHost );
mac = getString( networkInterface.getHardwareAddress() );

} catch( UnknownHostException e ) {
e.printStackTrace();
JOptionPane.showMessageDialog( null, "Falha ao obter endereço MAC!", "", JOptionPane.ERROR_MESSAGE );
} catch( SocketException e ) {
e.printStackTrace();
JOptionPane.showMessageDialog( null, "Falha ao obter endereço MAC!", "", JOptionPane.ERROR_MESSAGE );
} catch( Exception e ) {
e.printStackTrace();
JOptionPane.showMessageDialog( null, "Falha ao obter endereço MAC!", "", JOptionPane.ERROR_MESSAGE );
}

return mac.toUpperCase();
}


public static String getIP()
{
InetAddress localHost;
String ip = "";

try {

localHost = InetAddress.getLocalHost();
ip = localHost.getHostAddress();

} catch( UnknownHostException e ) {
e.printStackTrace();
JOptionPane.showMessageDialog( null, "Falha ao obter IP!", "", JOptionPane.ERROR_MESSAGE );
} catch( Exception e ) {
e.printStackTrace();
JOptionPane.showMessageDialog( null, "Falha ao obter IP!", "", JOptionPane.ERROR_MESSAGE );
}

return ip;
}


public static String getNomeDaMaquina()
{
InetAddress localHost;
String ip = "";

try {

localHost = InetAddress.getLocalHost();
ip = localHost.getHostName();

} catch( UnknownHostException e ) {
e.printStackTrace();
JOptionPane.showMessageDialog( null, "Falha ao obter Nome da máquina!", "", JOptionPane.ERROR_MESSAGE );
} catch( Exception e ) {
e.printStackTrace();
JOptionPane.showMessageDialog( null, "Falha ao obter Nome da máquina!", "", JOptionPane.ERROR_MESSAGE );
}

return ip;
}


private static String getString( byte[] bytes )
{
StringBuilder s = new StringBuilder();
for( int i = 0; i < bytes.length; i++ ) {
int parteAlta = ((bytes[i] >> 4) & 0xf) << 4;
int parteBaixa = bytes[i] & 0xf;
if( parteAlta == 0 ) s.append( '0' );
s.append( Integer.toHexString( parteAlta | parteBaixa ) );
}
return s.toString();
}



/**
* @param args
*/
public static void main( String[] args )
{
JOptionPane.showMessageDialog( null, "Nome da máquina: " + getNomeDaMaquina() + "\nIP: " + getIP() + "\nMAC: " + getMAC() );
System.out.println( "Nome da máquina: " + getNomeDaMaquina() + "\nIP: " + getIP() + "\nMAC: " + getMAC() );

}

}

sexta-feira, 1 de fevereiro de 2008

Como colocar a aplicação na bandeja do sistema (System tray).

Olá a todos!
Hoje trago um exemplo prático de como colocar sua aplicação java no 'System tray'.

Não sabe que diaxo é 'System tray'?
Bom.. 'System tray' é o local onde ficam ícones pequenos para acesso rápido à algumas funções do sistema como relógio, controle de volume, antivirus e por aí vai.
Ainda não entendeu?? a figura deve te ajudar! =]
Vamos lá!
O acesso ao 'System tray' exemplificado nesse post é parte do Java 6, portanto se sua máquina virtual for inferior a 6, o exemplo trazido nesse post não funcionará!
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.PopupMenu;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.metal.MetalIconFactory;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.SwingConstants;

/** * @author Dalí Freire Dias */
public class SystemTray extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private TrayIcon bandeja = null;
private JLabel jLabel = null;

/** * @param args */
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SystemTray thisClass = new SystemTray();
thisClass.setVisible(true);
}
});
}


/** * This is the default constructor */
public SystemTray() {
super();
initialize();

this.bandeja = new TrayIcon( getImagemTray(), "Exemplificando 'System tray'", getPopupMenuTray() );
adicionaEventos();

}

/** * This method initializes this * * @return void */
private void initialize() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(300, 200);
this.setLocation( (screenSize.width - this.getSize().width)/2, (screenSize.height - this.getSize().height)/2 );
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setContentPane(getJContentPane());
this.setTitle("System tray");
}

/** * Adiciona os eventos */
private void adicionaEventos()
{
bandeja.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
botaoAbrir();
}
});

WindowListener sair = new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
botaoFechar();
}
};
this.addWindowListener( sair );
}

/** * Encerra o sistema */
private void botaoSair()
{
Object[] opcoes = { "Sair", "Cancelar" };
int opcao = JOptionPane.showOptionDialog( this, "Deseja sair do sistema? \n",
"Exemplificando 'System tray'", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, opcoes, opcoes[1]);

if ( opcao == JOptionPane.YES_OPTION ) {

System.exit( 0 );

}
}
/** * Coloca o sistema no 'System tray' */
private void botaoFechar()
{
try {
java.awt.SystemTray.getSystemTray().add( bandeja );
bandeja.displayMessage( "Exemplificando 'System tray'",
"A aplicação 'Exemplificando System tray' ainda está em execução. " +
"\nDê um clique duplo neste ícone para visualizá-la!",
TrayIcon.MessageType.NONE );

this.setVisible( false );

} catch ( AWTException e1 ) {

JOptionPane.showMessageDialog( null, "Falha ao adicionar ícone na bandeja do sistema!", "", JOptionPane.ERROR_MESSAGE );
e1.printStackTrace();
this.setVisible( true );

} catch( Exception e ) {

JOptionPane.showMessageDialog( null, "Falha ao adicionar ícone na bandeja do sistema!", "", JOptionPane.ERROR_MESSAGE );
e.printStackTrace();
this.setVisible( true );

}
}
/** * Restaura o sistema */
private void botaoAbrir()
{
java.awt.SystemTray.getSystemTray().remove( bandeja );
this.setVisible( true );
}

/** * Retorna o icone que ficará na bandeja. * @return Image * @throws HeadlessException */
private Image getImagemTray() throws HeadlessException
{
Icon defaultIcon = MetalIconFactory.getTreeComputerIcon();
Image img = new BufferedImage(defaultIcon.getIconWidth(), defaultIcon
.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);

// Image img = Toolkit.getDefaultToolkit().getImage( getClass().getResource("/imagens/icone_bandeja.gif") );

return img;
}

/**
* Retorna o menu que é exibido quando clica com o botão direito no ícone da bandeja.

* @return PopupMenu

* @throws HeadlessException

*/
private PopupMenu getPopupMenuTray() throws HeadlessException
{
PopupMenu menu = new PopupMenu();

MenuItem abrir = new MenuItem( "Abrir" );
abrir.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
botaoAbrir();
}
});
MenuItem fechar = new MenuItem( "Fechar" );
fechar.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
botaoSair();
}
});
menu.add( abrir );
menu.add( fechar );

return menu;
}

/**
* This method initializes jLabel
* @return javax.swing.JLabel
*/
private JLabel getJLabel() {
if (jLabel == null) {
jLabel = new JLabel();
jLabel.setText("Exemplificando o uso do 'System tray'");
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setBounds(new Rectangle(1, 65, 292, 16));
}
return jLabel;
}

/**
* This method initializes jContentPane
*

* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJLabel(), null);
}
return jContentPane;
}

}

As figuras abaixo ilustram o resultado da execução do código apresentado.

Dúvidas? Sugestões? Críticas? Pode comentar!!!