package es.unizar.cps.tecnodiscap.gui;
import es.unizar.tecnodiscap.osgi4ami.device.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import es.unizar.tecnodiscap.util.sockets.udpClient;
import es.unizar.tecnodiscap.util.sockets.udpListener;
import es.unizar.cps.tecnodiscap.data.DeviceManager;
import es.unizar.cps.tecnodiscap.data.Acelerometro;
import es.unizar.cps.tecnodiscap.data.Brazo;
import es.unizar.cps.tecnodiscap.data.DeviceManagerListener;
import es.unizar.cps.tecnodiscap.data.Imagen;
import es.unizar.cps.tecnodiscap.data.Nodo;
import es.unizar.cps.tecnodiscap.data.Usuario;
import es.unizar.cps.tecnodiscap.i18n.Messages;
import es.unizar.cps.tecnodiscap.io.SerialConector;
import es.unizar.cps.tecnodiscap.net.SocketRaton;
import es.unizar.cps.tecnodiscap.util.Audio;
import es.unizar.cps.tecnodiscap.i18n.Messages;
import es.unizar.cps.tecnodiscap.io.SerialConector;
import es.unizar.cps.tecnodiscap.net.SocketRaton;
import es.unizar.cps.tecnodiscap.util.Audio;
import es.unizar.cps.tecnodiscap.util.ProcesadorDatos;
import es.unizar.cps.tecnodiscap.util.ProcesadorXML;
import es.unizar.cps.tecnodiscap.util.ProcesadorDatos;
import es.unizar.cps.tecnodiscap.util.ProcesadorXML;
import javax.swing.UIManager;
import org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel;
/**
* @author ciru
*
*/
public class GUI extends JFrame implements DeviceManagerListener, udpListener {
private Process appRatonVirtual;
private Process appControlEntorno;
public SerialConector conector; // @jve:decl-index=0:
private List nodos = new LinkedList(); // @jve:decl-index=0:
// TODO lista de nodos
private DeviceManager deviceManager; // @jve:decl-index=0:
private udpClient cliente; // @jve:decl-index=0:
private InetAddress ipserver;
private int portserver;
public Acelerometro acelerometro = new Acelerometro(this); // @jve:decl-index=0:
public Brazo brazo = new Brazo(this); // @jve:decl-index=0:
public Usuario user; // @jve:decl-index=0:
public int salida = 0;
private int count = 0;
private static final long serialVersionUID = 1L;
private JPanel contenedor = null;
public SocketRaton ratonSocket;
private Socket RatonVirtual;
public ProcesadorXML procesadorXML;
private ProcesadorDatos procesadorDatos;
private AdministrationUser dialogUsuarios;
private AdministracionResorces dialogRecursos;
private final String localizacionXML = "data/xml/data_users.xml"; // @jve:decl-index=0:
// //$NON-NLS-1$
private JButton desconecta = null;
private JPanel menuPanel = null;
private JPanel entrenamientoPanel = null;
public JLabel entrenamientoIzquierdaLabel = null;
public JLabel entrenamientoDerechaLabel = null;
public JButton entrenamientoButton = null;
public JButton controlEntornojButton = null;
public JButton controlBrazoButton = null;
public JButton ratonButton = null;
private JButton conectorButton = null;
public JLabel estadoLabel = null;
private JLabel jLabel3 = null;
private JPanel modoPanel = null;
private JPanel conexionPanel = null;
public JButton salirButton = null;
private JMenuBar menuBarPrincipal = null;
private JMenu menuArchivo = null;
private JMenuItem menuItemCargar = null;
private JMenuItem menuItemGuardar = null;
private JMenuItem menuItemSalir = null;
private JMenu menuAdministracion = null;
private JMenuItem menuItemAdminUsuarios = null;
private JMenuItem menuItemAdminResorces = null;
private JComboBox comboBoxImagenesIzquierda = null;
private JComboBox comboBoxImagenesDerecha = null;
private Audio audio = null; // @jve:decl-index=0:
private boolean antEstaDerecha = false;
private boolean antEstaIzquierda = false;
/**
*
*/
public void actualiza() {
procesadorDatos.analizaEje();
salida();
if (dialogUsuarios != null) {
dialogUsuarios.actualiza();
}
}
private void salida() {
switch (salida) {
case 0:
System.out.println(this.deviceManager.getRemoteControl().getX());
break;
case 1:
entrenamientoDerechaLabel.setEnabled(this.deviceManager
.getRemoteControl().isRight()
&& user.getDerecha() == 1);
entrenamientoIzquierdaLabel.setEnabled(this.deviceManager
.getRemoteControl().isLeft()
&& user.getIzquierda() == 1);
if (this.deviceManager.getRemoteControl().isRight()
&& !antEstaDerecha && user.getDerecha() == 1) {
System.out.println(Messages.getString("gui.GUI.1")); //$NON-NLS-1$
audio.reproduceAudio((Imagen) comboBoxImagenesDerecha
.getSelectedItem());
} else if (this.deviceManager.getRemoteControl().isLeft()
&& !antEstaIzquierda && user.getIzquierda() == 1) {
System.out.println(Messages.getString("gui.GUI.2")); //$NON-NLS-1$
audio.reproduceAudio((Imagen) comboBoxImagenesIzquierda
.getSelectedItem());
} else if (this.deviceManager.getRemoteControl().isCenter()) {
audio.paraAudio();
}
antEstaDerecha = this.deviceManager.getRemoteControl().isRight();
antEstaIzquierda = this.deviceManager.getRemoteControl().isLeft();
break;
case 2:
if (this.deviceManager.getRemoteControl().isRight()
&& !antEstaDerecha && user.getDerecha() == 1) {
ratonSocket.sendToSocket(0);
ratonSocket.sendToSocket(1);
} else if (this.deviceManager.getRemoteControl().isLeft()
&& !antEstaIzquierda && user.getIzquierda() == 1) {
ratonSocket.sendToSocket(0);
ratonSocket.sendToSocket(1);
}
antEstaDerecha = this.deviceManager.getRemoteControl().isRight();
antEstaIzquierda = this.deviceManager.getRemoteControl().isLeft();
break;
case 3:
if (this.deviceManager.getRemoteControl().isRight()
&& !antEstaDerecha && user.getDerecha() == 1) {
conector.writeToPort(this.brazo.getExtremoSuperior(),
this.brazo.getAddress());
System.out.println(" :D"
+ this.deviceManager.getRemoteControl().getX());
count = 0;
} else if (this.deviceManager.getRemoteControl().isLeft()
&& !antEstaIzquierda && user.getIzquierda() == 1) {
conector.writeToPort(this.brazo.getExtremoInferior(),
this.brazo.getAddress());
System.out.println(" :D"
+ this.deviceManager.getRemoteControl().getX());
count = 0;
}
entrenamientoDerechaLabel.setEnabled(this.deviceManager
.getRemoteControl().isRight()
&& user.getDerecha() == 1);
entrenamientoIzquierdaLabel.setEnabled(this.deviceManager
.getRemoteControl().isLeft()
&& user.getIzquierda() == 1);
antEstaDerecha = this.deviceManager.getRemoteControl().isRight();
antEstaIzquierda = this.deviceManager.getRemoteControl().isLeft();
count++;
break;
case 4:
entrenamientoDerechaLabel.setEnabled(this.deviceManager
.getRemoteControl().isRight()
&& user.getDerecha() == 1);
entrenamientoIzquierdaLabel.setEnabled(this.deviceManager
.getRemoteControl().isLeft()
&& user.getIzquierda() == 1);
break;
}
}
private void cerrarConexiones() {
if (ratonSocket != null) {
try {
ratonSocket.closeSocket();
} catch (RuntimeException e1) {
}
}
// if (conector != null && conector.isPortFound()) {
// pararAcelerometro();
// conector.closeSerialUSBPort();
// }
pararAcelerometro();
if (this.deviceManager != null) {
this.deviceManager.switchOffDevices();
this.deviceManager.removeListener(this);
deviceManager = null;
}
setEstadoLabel(false);
desactivaBotones(-1);
modoPanel.setVisible(false);
conectorButton.setEnabled(true);
desconecta.setEnabled(false);
entrenamientoPanel.setVisible(false);
}
public void pararAcelerometro() {
// TODO cuando sea un lista desconectar todos los
// acelerometros.
// if (acelerometro.getAddress() != null) {
// conector.writeToPort(acelerometro.getDesconectaAcelerometro(),
// acelerometro.getAddress());
// }
if (deviceManager != null)
if (deviceManager.getRemoteControl() != null)
if (deviceManager.getRemoteControl().getAccelerometer() != null)
deviceManager.getRemoteControl().getAccelerometer()
.stopStreaming();
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
if (cliente != null) {
cliente.close();
cliente.unregisterListener(this);
cliente = null;
}
salida = 0;
}
public void startAcelerometer() {
deviceManager.getRemoteControl().getAccelerometer().setStreamingPort(
"5000");
deviceManager.getRemoteControl().getAccelerometer().startStreaming();
cliente = new udpClient();
cliente.registerListener(this);
String s1 = "tecnodiscap";
try {
InetAddress ip2 = InetAddress.getByName("localhost");
cliente.sendData(ip2, 5000, s1.getBytes(), 0, s1.getBytes().length);
} catch (Exception e) {
System.out.println("Error en socket");
}
}
private void abrirConexiones() {
// TODO Pasar lista de nodos.
if (deviceManager == null) {
deviceManager = new DeviceManager();
deviceManager.addListener(this);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// conector = new SerialConector(acelerometro, brazo);
// Modificación para la GUI
// conector.liberalizaPuertosNoUsados();
// if (conector.isPortFound()) {
// if (deviceManager.getRemoteControl() != null) {
if (!deviceManager.getZigbeeDriver().getGateways().isEmpty()) {
setEstadoLabel(true);
modoPanel.setVisible(true);
desconecta.setEnabled(true);
conectorButton.setEnabled(false);
JOptionPane.showMessageDialog(entrenamientoPanel, Messages
.getString("gui.GUI.3"), Messages.getString("gui.GUI.4"), //$NON-NLS-1$ //$NON-NLS-2$
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(entrenamientoPanel, Messages
.getString("gui.GUI.5"), //$NON-NLS-1$
Messages.getString("gui.GUI.6"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$
}
}
/**
* @param nodo
*
*/
public void direccionActualizada(Nodo nodo) {
if (nodo instanceof Acelerometro) {
JOptionPane.showMessageDialog(entrenamientoPanel, Messages
.getString("gui.GUI.7"), //$NON-NLS-1$
Messages.getString("gui.GUI.8"), //$NON-NLS-1$
JOptionPane.INFORMATION_MESSAGE);
} else if (nodo instanceof Brazo) {
JOptionPane.showMessageDialog(entrenamientoPanel, Messages
.getString("gui.GUI.9"), //$NON-NLS-1$
Messages.getString("gui.GUI.10"), //$NON-NLS-1$
JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* @param b
*/
private void setEstadoLabel(boolean b) {
if (b == true) {
estadoLabel.setText(Messages.getString("gui.GUI.11")); //$NON-NLS-1$
estadoLabel.setForeground(new Color(51, 188, 51));
estadoLabel.setIcon(new ImageIcon("data/icons/ledgreen.png")); //$NON-NLS-1$
} else {
estadoLabel.setText(Messages.getString("gui.GUI.13")); //$NON-NLS-1$
estadoLabel.setForeground(new Color(188, 51, 51));
estadoLabel.setIcon(new ImageIcon("data/icons/ledred.png")); //$NON-NLS-1$
}
}
/**
* @param i
*/
private void desactivaBotones(int i) {
switch (i) {
case 1:// Modo entrenamiento
menuArchivo.setEnabled(false);
menuAdministracion.setEnabled(false);
controlEntornojButton.setEnabled(false);
controlBrazoButton.setEnabled(false);
ratonButton.setEnabled(false);
salirButton.setEnabled(true);
break;
case 2:// Modo control de entorno
menuArchivo.setEnabled(false);
menuAdministracion.setEnabled(false);
entrenamientoButton.setEnabled(false);
controlBrazoButton.setEnabled(false);
ratonButton.setEnabled(false);
salirButton.setEnabled(true);
break;
case 3:// Modo control de brazo
menuArchivo.setEnabled(false);
menuAdministracion.setEnabled(false);
controlEntornojButton.setEnabled(false);
entrenamientoButton.setEnabled(false);
ratonButton.setEnabled(false);
salirButton.setEnabled(true);
break;
case 4:// Modo control de ratón virtual
menuArchivo.setEnabled(false);
menuAdministracion.setEnabled(false);
controlEntornojButton.setEnabled(false);
controlBrazoButton.setEnabled(false);
entrenamientoButton.setEnabled(false);
salirButton.setEnabled(true);
break;
default:
menuArchivo.setEnabled(true);
menuAdministracion.setEnabled(true);
controlEntornojButton.setEnabled(true);
controlBrazoButton.setEnabled(true);
entrenamientoButton.setEnabled(true);
ratonButton.setEnabled(true);
salirButton.setEnabled(false);
}
}
/**
* This method initializes desconecta
*
* @return javax.swing.JButton
*/
private JButton getDesconecta() {
if (desconecta == null) {
desconecta = new JButton();
desconecta.setText("Desconecta"); //$NON-NLS-1$
desconecta.setEnabled(false);
desconecta.setIcon(new ImageIcon("data/icons/usbDisable.png")); //$NON-NLS-1$
desconecta.setVisible(true);
desconecta.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
cerrarConexiones();
}
});
}
return desconecta;
}
/**
* This method initializes menuPanel
*
* @return javax.swing.JPanel
*/
private JPanel getMenuPanel() {
if (menuPanel == null) {
GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
gridBagConstraints12.gridx = 1;
gridBagConstraints12.gridy = 1;
jLabel3 = new JLabel();
jLabel3.setText(Messages.getString("gui.GUI.17")); //$NON-NLS-1$
estadoLabel = new JLabel();
estadoLabel.setText(Messages.getString("gui.GUI.18")); //$NON-NLS-1$
estadoLabel.setIcon(new ImageIcon("data/icons/ledred.png")); //$NON-NLS-1$
estadoLabel.setForeground(new Color(188, 51, 51));
menuPanel = new JPanel();
menuPanel.setLayout(new BorderLayout());
menuPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
menuPanel.add(getModoPanel(), BorderLayout.CENTER);
menuPanel.add(getConexionPanel(), BorderLayout.SOUTH);
}
return menuPanel;
}
/**
* This method initializes entrenamientoPanel
*
* @return javax.swing.JPanel
*/
private JPanel getEntrenamientoPanel() {
if (entrenamientoPanel == null) {
GridLayout gridLayout = new GridLayout(2, 2);
gridLayout.setColumns(2);
GridBagConstraints gridBagConstraints61 = new GridBagConstraints();
gridBagConstraints61.fill = GridBagConstraints.NONE;
gridBagConstraints61.gridheight = 1;
gridBagConstraints61.gridx = 1;
gridBagConstraints61.gridy = 1;
gridBagConstraints61.weightx = 0.0;
gridBagConstraints61.weighty = 0.0;
gridBagConstraints61.insets = new Insets(100, 0, 0, 0);
GridBagConstraints gridBagConstraints51 = new GridBagConstraints();
gridBagConstraints51.insets = new Insets(0, 20, 0, 0);
gridBagConstraints51.gridwidth = 1;
gridBagConstraints51.gridx = 1;
gridBagConstraints51.gridy = 0;
gridBagConstraints51.gridheight = 1;
GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
gridBagConstraints41.fill = GridBagConstraints.NONE;
gridBagConstraints41.gridx = 0;
gridBagConstraints41.gridy = 1;
gridBagConstraints41.weightx = 0.0;
gridBagConstraints41.weighty = 0.0;
gridBagConstraints41.insets = new Insets(100, 0, 0, 0);
GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
gridBagConstraints31.insets = new Insets(0, 0, 0, 20);
gridBagConstraints31.gridy = 0;
gridBagConstraints31.fill = GridBagConstraints.NONE;
gridBagConstraints31.gridx = 0;
// GridBagConstraints gridBagConstraints18 = new
// GridBagConstraints();
// gridBagConstraints18.fill = GridBagConstraints.BOTH;
// gridBagConstraints18.weighty = 1.0;
// gridBagConstraints18.weightx = 1.0;
// GridBagConstraints gridBagConstraints = new GridBagConstraints();
// gridBagConstraints.fill = GridBagConstraints.BOTH;
// gridBagConstraints.weighty = 1.0;
// gridBagConstraints.weightx = 1.0;
entrenamientoDerechaLabel = new JLabel();
entrenamientoDerechaLabel.setText(""); //$NON-NLS-1$
entrenamientoDerechaLabel.setEnabled(false);
// procesadorDatos.escalaImageIcon("data/imagenes/coche.jpg");
// entrenamientoDerechaLabel.setIcon(new
// ImageIcon("data/imagenes/coche.jpg"));
// entrenamientoDerechaLabel.setIcon(procesadorDatos
// .escalaImageIcon("data/imagenes/coche.jpg"));
entrenamientoDerechaLabel.setVisible(true);
entrenamientoIzquierdaLabel = new JLabel();
entrenamientoIzquierdaLabel.setText(""); //$NON-NLS-1$
entrenamientoIzquierdaLabel.setEnabled(false);
// entrenamientoIzquierdaLabel.setIcon(procesadorDatos
// .escalaImageIcon("data/imagenes/cole.jpg"));
entrenamientoIzquierdaLabel.setVisible(true);
entrenamientoPanel = new JPanel();
entrenamientoPanel.setLayout(new GridBagLayout());
entrenamientoPanel.setBorder(BorderFactory
.createEtchedBorder(EtchedBorder.LOWERED));
entrenamientoPanel.setVisible(false);
entrenamientoPanel.add(entrenamientoIzquierdaLabel,
gridBagConstraints31);
entrenamientoPanel.add(getListImagenesLeft(), gridBagConstraints41);
entrenamientoPanel.add(entrenamientoDerechaLabel,
gridBagConstraints51);
entrenamientoPanel
.add(getListImagenesRight(), gridBagConstraints61);
}
return entrenamientoPanel;
}
/**
* This method initializes entrenamientoButton
*
* @return javax.swing.JButton
*/
private JButton getEntrenamientoButton() {
if (entrenamientoButton == null) {
entrenamientoButton = new JButton();
entrenamientoButton.setText(Messages.getString("gui.GUI.22")); //$NON-NLS-1$
entrenamientoButton
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// if (acelerometro.getAddress() != null) {
// conector.writeToPort(acelerometro
// .getConectaAcelerometro(), acelerometro
// .getAddress());
if (deviceManager != null
&& deviceManager.getRemoteControl() != null
&& deviceManager.getRemoteControl()
.isEnable()) {
startAcelerometer();
// try {
// Thread.sleep(5000);//Para que de
// tiempo a que
// empiece a enviar
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
// if(acelerometro.isEnviando()){
entrenamientoPanel.setVisible(true);
entrenamientoIzquierdaLabel.setVisible(user
.getIzquierda() == 1);
entrenamientoDerechaLabel.setVisible(user
.getDerecha() == 1);
comboBoxImagenesIzquierda.setVisible(user
.getIzquierda() == 1);
comboBoxImagenesDerecha.setVisible(user
.getDerecha() == 1);
salida = 1;
desactivaBotones(1);
// }else{
// conector.writeToPort(acelerometro
// .getDesconectaAcelerometro(),
// acelerometro
// .getAddress());
// }
} else {
JOptionPane.showMessageDialog(
entrenamientoPanel, Messages
.getString("gui.GUI.23"), //$NON-NLS-1$
Messages.getString("gui.GUI.24"), //$NON-NLS-1$
JOptionPane.ERROR_MESSAGE);
}
}
});
}
return entrenamientoButton;
}
/**
* This method initializes controlEntornojButton
*
* @return javax.swing.JButton
*/
private JButton getControlEntornojButton() {
if (controlEntornojButton == null) {
controlEntornojButton = new JButton();
controlEntornojButton.setText(Messages.getString("gui.GUI.25")); //$NON-NLS-1$
controlEntornojButton
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// if (acelerometro.getAddress() != null) {
if (deviceManager != null
&& deviceManager.getRemoteControl() != null
&& deviceManager.getRemoteControl()
.isEnable()) {
// String Aplicacion =
// "C:\\Raton_Virtual.exe";
// //$NON-NLS-1$
String Aplicacion = "C:\\WINDOWS\\system32\\notepad.exe"; //$NON-NLS-1$
try {
appRatonVirtual = Runtime.getRuntime()
.exec(Aplicacion);
appControlEntorno = Runtime
// .getRuntime()
// .exec(
// "C:\\Archivos de
// programa\\B&J
// Adaptaciones\\B&J
// Perseo\\Perseo.exe");
.getRuntime()
.exec(
"C:\\WINDOWS\\system32\\notepad.exe");
try {
Thread.sleep(2000);// Para dar
// tiempo a
// que se abra
// el serv. rat�n virtual
} catch (InterruptedException e1) {
e1.printStackTrace();
}
ratonSocket = new SocketRaton(RatonVirtual);
if (ratonSocket.getRatonVirtual() != null) {
// conector.writeToPort(acelerometro
// .getConectaAcelerometro(),
// acelerometro.getAddress());
// System.out.println(acelerometro
// .getConectaAcelerometro()
// + " " + acelerometro
// //$NON-NLS-1$
// .getAddress());
startAcelerometer();
desactivaBotones(2);
salida = 2;
} else {
if (appRatonVirtual != null) {
appRatonVirtual.destroy();
}
if (appControlEntorno != null) {
appControlEntorno.destroy();
}
}
} catch (IOException e1) {
if (appRatonVirtual != null) {
appRatonVirtual.destroy();
}
if (appControlEntorno != null) {
appControlEntorno.destroy();
}
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(
entrenamientoPanel, Messages
.getString("gui.GUI.23"), //$NON-NLS-1$
Messages.getString("gui.GUI.24"), //$NON-NLS-1$
JOptionPane.ERROR_MESSAGE);
}
}
});
}
return controlEntornojButton;
}
/**
* This method initializes controlBrazoButton
*
* @return javax.swing.JButton
*/
private JButton getControlBrazoButton() {
if (controlBrazoButton == null) {
controlBrazoButton = new JButton();
controlBrazoButton.setText(Messages.getString("gui.GUI.28")); //$NON-NLS-1$
controlBrazoButton
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// if (acelerometro.getAddress() != null) {
if (deviceManager != null
&& deviceManager.getRemoteControl() != null
&& deviceManager.getRemoteControl()
.isEnable()) {
// conector.writeToPort(acelerometro
// .getConectaAcelerometro(),
// acelerometro
// .getAddress());
startAcelerometer();
if (brazo.getAddress() != null) {
salida = 3;
desactivaBotones(3);
entrenamientoPanel.setVisible(true);
comboBoxImagenesIzquierda
.setSelectedIndex(procesadorXML
.getImagenPosition("Brazo_cerrado")); //$NON-NLS-1$
comboBoxImagenesDerecha
.setSelectedIndex(procesadorXML
.getImagenPosition("Brazo_abierto")); //$NON-NLS-1$
entrenamientoIzquierdaLabel.setVisible(user
.getIzquierda() == 1);
entrenamientoDerechaLabel.setVisible(user
.getDerecha() == 1);
comboBoxImagenesIzquierda.setVisible(user
.getIzquierda() == 1);
comboBoxImagenesDerecha.setVisible(user
.getDerecha() == 1);
} else {
// conector.writeToPort(acelerometro
// .getDesconectaAcelerometro(),
// acelerometro.getAddress());
pararAcelerometro();
JOptionPane.showMessageDialog(
entrenamientoPanel, Messages
.getString("gui.GUI.31"), //$NON-NLS-1$
Messages.getString("gui.GUI.32"), //$NON-NLS-1$
JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(
entrenamientoPanel, Messages
.getString("gui.GUI.23"), //$NON-NLS-1$
Messages.getString("gui.GUI.24"), //$NON-NLS-1$
JOptionPane.ERROR_MESSAGE);
}
}
});
}
return controlBrazoButton;
}
/**
* This method initializes ratonButton
*
* @return javax.swing.JButton
*/
private JButton getRatonButton() {
if (ratonButton == null) {
ratonButton = new JButton();
ratonButton.setText(Messages.getString("gui.GUI.35")); //$NON-NLS-1$
ratonButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// if (acelerometro.getAddress() != null) {
if (deviceManager != null
&& deviceManager.getRemoteControl() != null
&& deviceManager.getRemoteControl().isEnable()) {
// String Aplicacion = "C:\\Raton_Virtual.exe";
// //$NON-NLS-1$
String Aplicacion = "C:\\WINDOWS\\system32\\notepad.exe"; //$NON-NLS-1$
try {
appRatonVirtual = Runtime.getRuntime().exec(
Aplicacion);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(3000);// Para dar tiempo a que
// se abra
// el serv. rat�n virtual
} catch (InterruptedException e1) {
e1.printStackTrace();
}
ratonSocket = new SocketRaton(RatonVirtual);
if (ratonSocket.getRatonVirtual() != null) {
// conector.writeToPort(acelerometro
// .getConectaAcelerometro(), acelerometro
// .getAddress());
// System.out.println(acelerometro
// .getConectaAcelerometro()
// + " " + acelerometro //$NON-NLS-1$
// .getAddress());
startAcelerometer();
desactivaBotones(4);
salida = 2;
} else {
if (appRatonVirtual != null) {
appRatonVirtual.destroy();
}
}
} else {
JOptionPane.showMessageDialog(entrenamientoPanel,
Messages.getString("gui.GUI.23"), //$NON-NLS-1$
Messages.getString("gui.GUI.24"), //$NON-NLS-1$
JOptionPane.ERROR_MESSAGE);
}
}
});
}
return ratonButton;
}
/**
* This method initializes conectorButton
*
* @return javax.swing.JButton
*/
private JButton getConectorButton() {
if (conectorButton == null) {
conectorButton = new JButton();
conectorButton.setText(Messages.getString("gui.GUI.40")); //$NON-NLS-1$
conectorButton.setIcon(new ImageIcon("data/icons/usb.png")); //$NON-NLS-1$
conectorButton
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
abrirConexiones();
}
});
}
return conectorButton;
}
/**
* This method initializes modoPanel
*
* @return javax.swing.JPanel
*/
private JPanel getModoPanel() {
if (modoPanel == null) {
GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
gridBagConstraints16.gridx = 1;
gridBagConstraints16.insets = new Insets(0, 0, 252, 0);
gridBagConstraints16.gridy = 1;
GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
gridBagConstraints17.gridx = 1;
gridBagConstraints17.gridy = 2;
GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
gridBagConstraints9.fill = GridBagConstraints.VERTICAL;
gridBagConstraints9.gridy = -1;
gridBagConstraints9.gridx = -1;
gridBagConstraints9.weightx = 1.0;
GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
gridBagConstraints15.gridx = 1;
gridBagConstraints15.gridy = 7;
GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints11.gridy = 5;
gridBagConstraints11.gridx = 0;
GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints6.gridy = 4;
gridBagConstraints6.gridx = 1;
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints5.gridy = 5;
gridBagConstraints5.gridx = 1;
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints4.gridy = 6;
gridBagConstraints4.gridx = 1;
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints3.gridy = 3;
gridBagConstraints3.gridx = 1;
modoPanel = new JPanel();
modoPanel.setLayout(new GridBagLayout());
modoPanel.setName(Messages.getString("gui.GUI.64")); //$NON-NLS-1$
modoPanel.setVisible(false);
modoPanel.setBorder(BorderFactory.createTitledBorder(null, Messages
.getString("gui.GUI.43"), //$NON-NLS-1$
TitledBorder.CENTER, TitledBorder.BELOW_TOP, new Font(
"Dialog", Font.BOLD, 18), new Color(51, 51, 51))); //$NON-NLS-1$
modoPanel.setComponentOrientation(ComponentOrientation.UNKNOWN);
modoPanel.add(getEntrenamientoButton(), gridBagConstraints3);
modoPanel.add(getControlEntornojButton(), gridBagConstraints4);
modoPanel.add(getControlBrazoButton(), gridBagConstraints5);
modoPanel.add(getRatonButton(), gridBagConstraints6);
modoPanel.add(getSalirButton(), gridBagConstraints15);
}
return modoPanel;
}
/**
* This method initializes conexionPanel
*
* @return javax.swing.JPanel
*/
private JPanel getConexionPanel() {
if (conexionPanel == null) {
GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
gridBagConstraints8.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints8.gridy = 2;
gridBagConstraints8.gridx = 2;
GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
gridBagConstraints10.gridx = 0;
gridBagConstraints10.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints10.gridy = 2;
GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints7.gridy = 0;
gridBagConstraints7.gridx = 0;
GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
gridBagConstraints14.anchor = GridBagConstraints.CENTER;
gridBagConstraints14.gridx = 2;
gridBagConstraints14.fill = GridBagConstraints.HORIZONTAL;
GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
gridBagConstraints13.anchor = GridBagConstraints.SOUTH;
gridBagConstraints13.gridy = 11;
gridBagConstraints13.gridx = 1;
conexionPanel = new JPanel();
conexionPanel.setLayout(new GridBagLayout());
conexionPanel.setName(Messages.getString("gui.GUI.0")); //$NON-NLS-1$
conexionPanel.add(getDesconecta(), gridBagConstraints14);
conexionPanel.add(getConectorButton(), gridBagConstraints7);
conexionPanel.add(jLabel3, gridBagConstraints10);
conexionPanel.add(estadoLabel, gridBagConstraints8);
}
return conexionPanel;
}
/**
* This method initializes salirButton
*
* @return javax.swing.JButton
*/
private JButton getSalirButton() {
if (salirButton == null) {
salirButton = new JButton();
salirButton.setText(Messages.getString("gui.GUI.46")); //$NON-NLS-1$
salirButton.setEnabled(false);
salirButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (ratonSocket != null) {
try {
ratonSocket.closeSocket();
} catch (RuntimeException e1) {
}
}
pararAcelerometro();
desactivaBotones(0);
entrenamientoPanel.setVisible(false);
if (appRatonVirtual != null) {
appRatonVirtual.destroy();
}
if (appControlEntorno != null) {
appControlEntorno.destroy();
}
audio.paraAudio();
}
});
}
return salirButton;
}
/**
* This method initializes menuBarPrincipal
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getMenuBarPrincipal() {
if (menuBarPrincipal == null) {
menuBarPrincipal = new JMenuBar();
menuBarPrincipal.add(getMenuArchivo());
menuBarPrincipal.add(getMenuAdministracion());
}
return menuBarPrincipal;
}
/**
* This method initializes menuArchivo
*
* @return javax.swing.JMenu
*/
private JMenu getMenuArchivo() {
if (menuArchivo == null) {
menuArchivo = new JMenu();
menuArchivo.setText(Messages.getString("gui.GUI.47")); //$NON-NLS-1$
menuArchivo.setIcon(new ImageIcon("data/icons/fileimport.png")); //$NON-NLS-1$
menuArchivo.add(getMenuItemCargar());
menuArchivo.add(getMenuItemGuardar());
menuArchivo.add(getMenuItemSalir());
}
return menuArchivo;
}
/**
* This method initializes menuItemCargar
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMenuItemCargar() {
if (menuItemCargar == null) {
menuItemCargar = new JMenuItem();
menuItemCargar.setText(Messages.getString("gui.GUI.49")); //$NON-NLS-1$
menuItemCargar
.setIcon(new ImageIcon("data/icons/download-mdk.png")); //$NON-NLS-1$
menuItemCargar
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JFileChooser filechooser = new JFileChooser();
int returnVal = filechooser.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = filechooser.getSelectedFile();
procesadorXML = new ProcesadorXML(file,
deviceManager);
initializeUser();
}
}
});
}
return menuItemCargar;
}
/**
* This method initializes menuItemGuardar
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMenuItemGuardar() {
if (menuItemGuardar == null) {
menuItemGuardar = new JMenuItem();
menuItemGuardar.setText(Messages.getString("gui.GUI.51")); //$NON-NLS-1$
menuItemGuardar.setIcon(new ImageIcon("data/icons/filesaveas.png")); //$NON-NLS-1$
menuItemGuardar
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JFileChooser filechooser = new JFileChooser();
int returnVal = filechooser.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = filechooser.getSelectedFile();
procesadorXML.GuardaXML(file, deviceManager);
}
}
});
}
return menuItemGuardar;
}
/**
* This method initializes menuItemSalir
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMenuItemSalir() {
if (menuItemSalir == null) {
menuItemSalir = new JMenuItem();
menuItemSalir.setText(Messages.getString("gui.GUI.53")); //$NON-NLS-1$
menuItemSalir.setIcon(new ImageIcon("data/icons/exit.png")); //$NON-NLS-1$
menuItemSalir
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
int n = JOptionPane
.showConfirmDialog(
entrenamientoPanel,
Messages.getString("gui.GUI.55"), //$NON-NLS-1$
Messages.getString("gui.GUI.56"), JOptionPane.YES_NO_OPTION); //$NON-NLS-1$
if (n == JOptionPane.YES_OPTION) {
cerrarConexiones();
File file = new File(localizacionXML);
procesadorXML.GuardaXML(file, deviceManager);
System.exit(0);
} else if (n == JOptionPane.NO_OPTION) {
} else {
}
}
});
}
return menuItemSalir;
}
/**
* This method initializes menuAdministracion
*
* @return javax.swing.JMenu
*/
private JMenu getMenuAdministracion() {
if (menuAdministracion == null) {
menuAdministracion = new JMenu();
menuAdministracion.setText(Messages.getString("gui.GUI.57")); //$NON-NLS-1$
menuAdministracion.setIcon(new ImageIcon(
"data/icons/package_system.png")); //$NON-NLS-1$
menuAdministracion.add(getMenuItemAdminUsuarios());
menuAdministracion.add(getMenuItemAdminResorces());
}
return menuAdministracion;
}
/**
* This method initializes menuItemAdminUsuarios
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMenuItemAdminUsuarios() {
if (menuItemAdminUsuarios == null) {
menuItemAdminUsuarios = new JMenuItem();
menuItemAdminUsuarios.setText(Messages.getString("gui.GUI.59")); //$NON-NLS-1$
menuItemAdminUsuarios.setIcon(new ImageIcon(
"data/icons/edit_user.png")); //$NON-NLS-1$
menuItemAdminUsuarios
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// if (acelerometro.getAddress() != null
// && conector.isPortFound()) {
// conector.writeToPort(acelerometro
// .getConectaAcelerometro(), acelerometro
// .getAddress());
if (deviceManager != null && deviceManager.getRemoteControl() != null && deviceManager.getRemoteControl().isEnable()) {
startAcelerometer();
salida = 4;
dialogoAdminUsuarios();
} else {
JOptionPane.showMessageDialog(
entrenamientoPanel, Messages
.getString("gui.GUI.23"), //$NON-NLS-1$
Messages.getString("gui.GUI.24"), //$NON-NLS-1$
JOptionPane.ERROR_MESSAGE);
}
}
});
}
return menuItemAdminUsuarios;
}
private void dialogoAdminUsuarios() {
dialogUsuarios = new AdministrationUser(this, false);
}
/**
* This method initializes menuItemAdminResorces
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMenuItemAdminResorces() {
if (menuItemAdminResorces == null) {
menuItemAdminResorces = new JMenuItem();
menuItemAdminResorces.setText(Messages.getString("gui.GUI.61")); //$NON-NLS-1$
menuItemAdminResorces.setIcon(new ImageIcon(
"data/icons/music-mdk.png")); //$NON-NLS-1$
menuItemAdminResorces
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
dialogoRecursos();
}
});
}
return menuItemAdminResorces;
}
private void dialogoRecursos() {
dialogRecursos = new AdministracionResorces(this);
}
/**
* This method initializes listImagenesLeft
*
* @return javax.swing.JList
*/
private JComboBox getListImagenesLeft() {
if (comboBoxImagenesIzquierda == null) {
comboBoxImagenesIzquierda = new JComboBox(procesadorXML
.getImagenesArray2());
comboBoxImagenesIzquierda.setSelectedIndex(0);
entrenamientoIzquierdaLabel.setIcon(procesadorDatos
.escalaImageIcon(((Imagen) comboBoxImagenesIzquierda
.getSelectedItem()).getImagenURL()));
comboBoxImagenesIzquierda
.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
if (e.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
if (!(((Imagen) comboBoxImagenesIzquierda
.getSelectedItem()).getImagenURL())
.equals("")) { //$NON-NLS-1$
entrenamientoIzquierdaLabel
.setIcon(procesadorDatos
.escalaImageIcon(((Imagen) comboBoxImagenesIzquierda
.getSelectedItem())
.getImagenURL()));
} else {
entrenamientoIzquierdaLabel
.setIcon(new ImageIcon(
getClass()
.getResource(
"data/imagenes/foto_no_disponible.jpg"))); //$NON-NLS-1$
}
}
}
});
}
return comboBoxImagenesIzquierda;
}
/**
* This method initializes listImagenesRight
*
* @return javax.swing.JList
*/
private JComboBox getListImagenesRight() {
if (comboBoxImagenesDerecha == null) {
comboBoxImagenesDerecha = new JComboBox(procesadorXML
.getImagenesArray2());
comboBoxImagenesDerecha.setSelectedIndex(1);
entrenamientoDerechaLabel.setIcon(procesadorDatos
.escalaImageIcon(((Imagen) comboBoxImagenesDerecha
.getSelectedItem()).getImagenURL()));
comboBoxImagenesDerecha
.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
if (e.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
if (!(((Imagen) comboBoxImagenesDerecha
.getSelectedItem()).getImagenURL())
.equals("")) { //$NON-NLS-1$
entrenamientoDerechaLabel
.setIcon(procesadorDatos
.escalaImageIcon(((Imagen) comboBoxImagenesDerecha
.getSelectedItem())
.getImagenURL()));
} else {
entrenamientoDerechaLabel
.setIcon(new ImageIcon(
"data/imagenes/foto_no_disponible.jpg")); //$NON-NLS-1$
}
}
}
});
}
return comboBoxImagenesDerecha;
}
private void initializeUser() {
user = procesadorXML.getUsuarioByPosition(0);
if (user == null)
user = new Usuario();
}
/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI thisClass = new GUI();
thisClass.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public GUI() {
super();
try {// loading substance look and feel
UIManager
.setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
} catch (Exception e) {
e.printStackTrace();
}
initialize();
if (deviceManager == null) {
deviceManager = new DeviceManager();
deviceManager.addListener(this);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!deviceManager.getZigbeeDriver().getGateways().isEmpty()) {
setEstadoLabel(true);
modoPanel.setVisible(true);
desconecta.setEnabled(true);
conectorButton.setEnabled(false);
JOptionPane.showMessageDialog(entrenamientoPanel, Messages
.getString("gui.GUI.3"), Messages.getString("gui.GUI.4"), //$NON-NLS-1$ //$NON-NLS-2$
JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
File file = new File(this.localizacionXML);
procesadorXML = new ProcesadorXML(file, deviceManager);
procesadorDatos = new ProcesadorDatos(this);
audio = new Audio();
initializeUser();
this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
this.setJMenuBar(getMenuBarPrincipal());
this.setContentPane(getContenedor());
this.setTitle(Messages.getString("gui.GUI.63")); //$NON-NLS-1$
}
/**
* This method initializes contenedor
*
* @return javax.swing.JPanel
*/
private JPanel getContenedor() {
if (contenedor == null) {
BorderLayout borderLayout = new BorderLayout();
borderLayout.setHgap(0);
borderLayout.setVgap(0);
contenedor = new JPanel();
contenedor.setLayout(borderLayout);
contenedor.add(getMenuPanel(), BorderLayout.WEST);
contenedor.add(getEntrenamientoPanel(), BorderLayout.CENTER);
}
return contenedor;
}
public Audio getAudio() {
return audio;
}
public ProcesadorDatos getProcesadorDatos() {
return procesadorDatos;
}
public JComboBox getComboBoxImagenesDerecha() {
return comboBoxImagenesDerecha;
}
public JComboBox getComboBoxImagenesIzquierda() {
return comboBoxImagenesIzquierda;
}
public JLabel getEntrenamientoDerechaLabel() {
return entrenamientoDerechaLabel;
}
public JLabel getEntrenamientoIzquierdaLabel() {
return entrenamientoIzquierdaLabel;
}
public JPanel getEntrenamientoPanelPosition() {
return entrenamientoPanel;
}
public void DeviceManagerEvent(int event) {
// TODO Auto-generated method stub
switch (event) {
case DeviceManagerListener.RemoteControlEnable:
JOptionPane.showMessageDialog(entrenamientoPanel, Messages
.getString("gui.GUI.7"), Messages.getString("gui.GUI.8"), //$NON-NLS-1$ //$NON-NLS-2$
JOptionPane.INFORMATION_MESSAGE);
break;
}
}
public void receivedData(InetAddress ip, int port, byte[] data, int size) {
// TODO Auto-generated method stub
// System.out.println("-------------------------");
// for(int i=0;i<size;i++){
// System.out.print("->"+bytesToInts(data)[i]+"<-");
// }
// System.out.println();
System.out.println("Aceleracion: " + "x:" + bytesToInts(data)[0]
+ " y:" + bytesToInts(data)[1] + " z:" + bytesToInts(data)[2]);
this.deviceManager.getRemoteControl().setX(bytesToInts(data)[0]);
this.deviceManager.getRemoteControl().setY(bytesToInts(data)[1]);
this.deviceManager.getRemoteControl().setZ(bytesToInts(data)[2]);
this.actualiza();
}
public DeviceManager getDeviceManager() {
return this.deviceManager;
}
public static int[] bytesToInts(byte[] message) {
int numBytes = message.length;
int[] intMessage = new int[numBytes];
for (int i = 0; i < numBytes; i++) {
intMessage[i] = (int) (message[i] & 0xFF);
}
return intMessage;
}
} // @jve:decl-index=0:visual-constraint="10,10"