package Applications.ClientsFactory; import org.omg.CosEventCommImpl.*; import org.omg.CosEventChannelAdminImpl.*; import java.awt.event.*; import java.awt.*; import javax.swing.*; import java.util.*; import org.omg.CORBA.*; public class PushConsumerPanel extends JPanel implements EventChannelMonitor, ActionListener { private int recibidos = 0; private Misc varios = new Misc(); private PushConsumerImpl pushconsumerimpl = new PushConsumerImpl (this); private JTextArea textArea = new JTextArea (); private JButton Conectar = new JButton ("Conectar"); private JButton Desconectar = new JButton ("Desconectar"); private JTextField Canal = new JTextField(); private JTextField Tipo = new JTextField(); private JTextField Direccion = new JTextField(); private JTextField Puerto = new JTextField(); private JTextField Recibidos = new JTextField(); private JSplitPane panel_2 = new JSplitPane (JSplitPane.VERTICAL_SPLIT); private JPanel panel_2_1 = (new JPanel () {public Insets getInsets () { return new Insets ( 4, 4, 4, 4 );}} ); private JScrollPane panel_2_2 = new JScrollPane(); public PushConsumerPanel() { try { this.setPreferredSize(new Dimension(650, 175)); this.setLayout(new BorderLayout()); this.add ( panel_2, BorderLayout.CENTER ); panel_2.add ( panel_2_2, JSplitPane.RIGHT ); panel_2.add ( panel_2_1, JSplitPane.LEFT ); panel_2.setEnabled(false); panel_2_2.getViewport().add ( textArea, null ); panel_2_1.setLayout (new GridLayout(4, 2, 2, 2)); panel_2_1.add (varios.CreaLabelTexto ("Canal:" , "Nombre del canal al que se va a conectar." , Canal , "CANAL" , true), null); panel_2_1.add (varios.CreaLabelTexto ("Tipo de canal:" , "Tipo del canal al que se va a conectar." , Tipo , "" , true), null); panel_2_1.add (varios.CreaLabelTexto ("Dirección:" , "Dirección del servidor de nombres." , Direccion , "127.0.0.1", true), null); panel_2_1.add (varios.CreaLabelTexto ("Puerto:" , "Puerto de escucha del servidor de nombres." , Puerto , "900" , true), null); panel_2_1.add (varios.CreaLabelTexto ("Eventos recibidos:", "Número global de eventos recibidos.", Recibidos, "0", false), null); panel_2_1.add (new JPanel ()); Conectar.setToolTipText ("Conectar el Push Consumer al canal de eventos."); Conectar.addActionListener(this); panel_2_1.add ( Conectar , null ); Desconectar.setToolTipText ( "Desconectar el Push Consumer del canal de eventos."); Desconectar.setNextFocusableComponent(Canal); Desconectar.addActionListener(this); panel_2_1.add ( Desconectar , null ); } catch(Exception exx) { varios.PopUpError (this, "ERROR en constructor de PushConsumerPanel: " + exx); System.out.println ("Error en clase PushConsumerPanel." + exx); } } public void actionPerformed (ActionEvent e) { try { if ( e.getActionCommand().equals("Conectar")) { pushconsumerimpl.connect (Canal.getText(), Tipo.getText(), Direccion.getText(), Puerto.getText()); } else if ( e.getActionCommand().equals("Desconectar")) { pushconsumerimpl.disconnect(); recibidos = 0; Recibidos.setText("0"); } } catch ( org.omg.CORBA.SystemException ex ) { Mensaje (ERROR, "PullConsumerPanel", varios.TraduceSystemException(ex)); } catch(Exception exx) { Mensaje (ERROR, "PullConsumerPanel", "ERROR al conectar: " + exx); } } public void Destruir () { try { pushconsumerimpl.disconnect(); } catch(Exception exx){} } /* Ahora viene la parte en la que se implementa el interfaz EventChannelMonitor */ public void Mensaje ( int tipo, String origen, String mensaje ) { if ( tipo == this._ERROR ) mensaje = "Error en " + origen + ". "+ mensaje; textArea.append ( mensaje + "\n" ); } public void Accion ( int accion ) { switch ( accion ) { case RECIBIDO_PUSH: recibidos ++; Recibidos.setText(""+ recibidos); break; case CANAL_DESCONECTA: Mensaje ( INFO, "PushConsumerPanel", "El canal ha desconectado al Push Consumer." ); break; } } /** El evento ya está en el consumidor y por panto se puede hacer con él lo que sea necesario. En éste caso analizamos el contenido y en función de él componemos el mensaje que va a la pantalla. */ public void insert ( Any data ) { String mens =""; try { Vector v = (Vector) data.extract_Value (); String tipo = (String) v.elementAt (0); Integer num = (Integer) v.elementAt (1); Date date = (Date) v.elementAt (2); if ( num.intValue() == 0 ) mens="Evento individual "; else mens="Evento número " + num.toString() + " de ráfaga "; mens = mens + "tipo " + tipo + ". "; mens = mens + date.toString().substring( 0, 19 ); Mensaje ( INFO, "", mens ); Accion (RECIBIDO_PUSH); } catch( Exception ex ) { Mensaje ( INFO, "", "Recibido un evento que no nos corresponde." ); } } }