Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.magikbyte.smalldoode.meeting.views; import com.magikbyte.smalldoode.meeting.Manager; import com.magikbyte.smalldoode.meeting.model.Groupe; import com.magikbyte.smalldoode.meeting.model.Agent; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import java.util.Observable; import java.util.Observer; import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; /** * * @author yirou */ public class ManagerView extends javax.swing.JFrame implements Observer { Connection connection; Channel channel; private static ManagerView INSTANCE = new ManagerView(); /** * Creates new form ManagerView */ private ManagerView() { initComponents(); this.setVisible(true); this.setResizable(false); displayUsers(); displayGroupes(); } public static ManagerView getInstance() { return INSTANCE; } private void displayGroupes() { String[] groupes = objectListToStringArray(Manager.getInstance().getGroupes()); listGroupeView.setListData(groupes); } private String[] objectListToStringArray(List<?> liste) { String[] result = new String[liste.size()]; Groupe groupe; Agent user; usersCombo.removeAllItems(); for (int i = 0; i < liste.size(); i++) { Object object = liste.get(i); if (object instanceof Groupe) { groupe = (Groupe) liste.get(i); result[i] = groupe.getName(); } if (object instanceof Agent) { user = (Agent) liste.get(i); result[i] = user.getName(); usersCombo.addItem(result[i]); } } return result; } private void displayUsers() { String[] users = objectListToStringArray(Manager.getInstance().getUsers()); // System.out.println("size user " + Manager.getInstance().getUsers().size()); listUserView.setListData(users); } public void init() { try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(Manager.QUEUE_NAME, false, false, false, null); } catch (IOException ex) { Logger.getLogger(ManagerView.class.getName()).log(Level.SEVERE, null, ex); } catch (TimeoutException ex) { Logger.getLogger(ManagerView.class.getName()).log(Level.SEVERE, null, ex); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); nameTxt = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); listGroupeView = new javax.swing.JList<>(); jScrollPane2 = new javax.swing.JScrollPane(); listUserView = new javax.swing.JList<>(); jButton1 = new javax.swing.JButton(); jPanel2 = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); groupeTxt = new javax.swing.JTextField(); usersCombo = new javax.swing.JComboBox<>(); jLabel3 = new javax.swing.JLabel(); jButton3 = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); dureTxt = new javax.swing.JTextField(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenu2 = new javax.swing.JMenu(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("nom nouvel agent :"); nameTxt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { nameTxtActionPerformed(evt); } }); listGroupeView.setBorder(javax.swing.BorderFactory.createTitledBorder("Choisir groupe")); listGroupeView.setModel(new javax.swing.AbstractListModel<String>() { String[] strings = { "groupe1" }; public int getSize() { return strings.length; } public String getElementAt(int i) { return strings[i]; } }); jScrollPane1.setViewportView(listGroupeView); listUserView.setBorder(javax.swing.BorderFactory.createTitledBorder("Choisir agent")); listUserView.setModel(new javax.swing.AbstractListModel<String>() { String[] strings = { "user1" }; public int getSize() { return strings.length; } public String getElementAt(int i) { return strings[i]; } }); jScrollPane2.setViewportView(listUserView); jButton1.setText("Next >>"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Nouveau groupe")); jLabel2.setText("nom groupe :"); jLabel3.setText("admin :"); jButton3.setText("Ajouter"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jLabel4.setText("Dure(Nombre de minutes):"); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2).addComponent(jLabel4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(dureTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE) .addComponent(groupeTxt)) .addGap(4, 4, 4).addComponent(jLabel3).addGap(18, 18, 18) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(usersCombo, 0, 138, Short.MAX_VALUE).addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(65, 65, 65))); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2) .addComponent(groupeTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(usersCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(dureTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton3)) .addContainerGap())); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(nameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 578, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(38, 38, 38))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(3, 3, 3) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(nameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE) .addGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jScrollPane1).addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1))); jMenu1.setText("File"); jMenuBar1.add(jMenu1); jMenu2.setText("Edit"); jMenuBar1.add(jMenu2); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); pack(); }// </editor-fold>//GEN-END:initComponents @Override public void update(Observable o, Object arg) { if (o instanceof Manager) { displayGroupes(); displayUsers(); System.out.println("update"); } } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed if (groupeTxt.getText().equals("") || dureTxt.getText().equals("")) { JOptionPane.showMessageDialog(null, "Veuillez complter tous les champs svp"); } else { try { int duree = Integer.parseInt(dureTxt.getText()); String message = Manager.MSG_NEW_GROUPE + "," + groupeTxt.getText() + "," + usersCombo.getSelectedItem().toString() + "," + duree; channel.basicPublish("", Manager.QUEUE_NAME, null, message.getBytes("UTF-8")); groupeTxt.setText(""); dureTxt.setText(""); } catch (NumberFormatException | IOException ex) { JOptionPane.showMessageDialog(null, "Dure du groupe incorrecte"); } } }//GEN-LAST:event_jButton3ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed if (listGroupeView.getSelectedIndex() == -1 || listUserView.getSelectedIndex() == -1) { JOptionPane.showMessageDialog(null, "Veuillez choisir un groupe et un utilisateur"); } else { String grp = listGroupeView.getSelectedValue(); String user = listUserView.getSelectedValue(); new ConnetedView(grp, user, this); } }//GEN-LAST:event_jButton1ActionPerformed private void nameTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameTxtActionPerformed if (!nameTxt.getText().equals("")) { try { String message = Manager.MSG_NEW_USER + "," + nameTxt.getText(); channel.basicPublish("", Manager.QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(channel); nameTxt.setText(""); } catch (UnsupportedEncodingException ex) { Logger.getLogger(ManagerView.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ManagerView.class.getName()).log(Level.SEVERE, null, ex); } } else { JOptionPane.showMessageDialog(null, "Ce champs ne peut tre vide"); } }//GEN-LAST:event_nameTxtActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField dureTxt; private javax.swing.JTextField groupeTxt; private javax.swing.JButton jButton1; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JList<String> listGroupeView; private javax.swing.JList<String> listUserView; private javax.swing.JTextField nameTxt; private javax.swing.JComboBox<String> usersCombo; // End of variables declaration//GEN-END:variables }