/* 
* Copyright 2016 Agencia Estatal de Administración Tibutaria
*
* Licencia con arreglo a la EUPL, Versión 1.1 o –en cuanto
* sean aprobadas por la Comisión Europea– versiones
* posteriores de la EUPL (la «Licencia»);
* Solo podrá usarse esta obra si se respeta la Licencia.
* Puede obtenerse una copia de la Licencia en:
*  
* https://joinup.ec.europa.eu/software/page/eupl
*  
* Salvo cuando lo exija la legislación aplicable o se acuerde
* por escrito, el programa distribuido con arreglo a la
* Licencia se distribuye «TAL CUAL», SIN GARANTÍAS NI CONDICIONES
* DE NINGÚN TIPO, ni expresasni implícitas.
*
* Véase la Licencia en el idioma concreto que rige los permisos y 
* limitaciones que establece la Licencia.
*/ 

package administrador;

import java.util.ArrayList;

/**
 *
 * @author f11600za
 */
public final class FormMensajeAdministradores extends javax.swing.JDialog {

    String[] admin;
    public ArrayList<String> marcados;
    public boolean enviar;
    String mensaje;

    public FormMensajeAdministradores(FormPrincipal principal, String[] administradores) {
        super(principal, true);
        admin = administradores;
        initComponents();
        cargarDatos();
        setSize(540, 470);
        setLocationRelativeTo(null);
        marcados = new ArrayList<>();
        enviar = false;
    }

    public void cargarDatos() {
        lista.setModel(new javax.swing.AbstractListModel() {

            String[] strings = admin;

            @Override
            public int getSize() {
                return strings.length;
            }

            @Override
            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        lista.setListData(admin);

    }

    void pulsadoCancelar() {
        enviar = false;
        setVisible(false);
    }

    void buscarMarcados() {
        for (int i = 0; i < admin.length; i++) {
            if (lista.getChecked(i)) {
                marcados.add(admin[i]);
            }
        }
    }

    void pulsadoEnviar() {
        buscarMarcados();
        if (marcados.size() < 1) {
            administrador.utiles.Global.MsgBox("No se han marcado destinatarios.\nEl mensaje no se enviará.");
            enviar = false;
            return;
        }
        String texto = memo.getText();
        if (texto.trim().length() == 0) {
            administrador.utiles.Global.MsgBox("Mensaje sin texto.\nNo se enviará.");
            enviar = false;
            return;
        } else {
            mensaje = texto.replaceAll("\n", "#");
            enviar = true;
        }
        setVisible(false);
    }

    public String getMensaje() {
        return mensaje;
    }

    /**
     * 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() {

      jLabel2 = new javax.swing.JLabel();
      jScrollPane1 = new javax.swing.JScrollPane();
      lista = new javax.swing.CheckBoxList();
      jLabel1 = new javax.swing.JLabel();
      btEnviar = new javax.swing.JButton();
      btCancelar = new javax.swing.JButton();
      jScrollPane2 = new javax.swing.JScrollPane();
      memo = new javax.swing.JTextArea();

      setTitle("Envio de mensajes a Administradores");

      jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      jLabel2.setText("Texto para enviar");

      lista.setBackground(new java.awt.Color(255, 255, 204));
      jScrollPane1.setViewportView(lista);

      jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      jLabel1.setText("Seleccionar destinatarios/as");

      btEnviar.setText("Enviar");
      btEnviar.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btEnviarActionPerformed(evt);
         }
      });

      btCancelar.setText("Cancelar");
      btCancelar.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btCancelarActionPerformed(evt);
         }
      });

      memo.setColumns(45);
      memo.setRows(4);
      memo.setAutoscrolls(false);
      jScrollPane2.setViewportView(memo);

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
               .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
               .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
                  .addGap(19, 19, 19))
               .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                  .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
                  .addGap(18, 18, 18)))
            .addGap(0, 0, 0))
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(137, 137, 137)
            .addComponent(btEnviar, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
            .addGap(64, 64, 64)
            .addComponent(btCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
            .addGap(133, 133, 133))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(btCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
               .addComponent(btEnviar, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(16, 16, 16))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

private void btEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEnviarActionPerformed
    pulsadoEnviar();
}//GEN-LAST:event_btEnviarActionPerformed

private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
    pulsadoCancelar();
}//GEN-LAST:event_btCancelarActionPerformed

   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton btCancelar;
   private javax.swing.JButton btEnviar;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JScrollPane jScrollPane2;
   private javax.swing.CheckBoxList lista;
   private javax.swing.JTextArea memo;
   // End of variables declaration//GEN-END:variables
}
