// Lettre la ligne passée en parametre
  private void actionLettrage(int[] rowIndex) {
    String codeLettre = this.codeLettrage.getText().trim();

    List<SQLRow> rowsSelected = new ArrayList<SQLRow>(rowIndex.length);

    long solde = 0;
    for (int i = 0; i < rowIndex.length; i++) {
      int id = this.ecriturePanel.getListe().idFromIndex(rowIndex[i]);
      SQLRow row = this.tableEcr.getRow(id);
      rowsSelected.add(row);

      solde += ((Long) row.getObject("DEBIT")).longValue();
      solde -= ((Long) row.getObject("CREDIT")).longValue();
    }

    if (solde == 0) {

      for (SQLRow row2 : rowsSelected) {

        SQLRowValues rowVals = new SQLRowValues(this.tableEcr);

        // Lettrage
        // On lettre ou relettre la ligne avec le code saisi
        if (codeLettre.length() > 0) {

          // Si la ligne est en brouillard on valide le mouvement associé
          if (this.boxValidEcriture.isSelected() && (!row2.getBoolean("VALIDE"))) {
            EcritureSQLElement.validationEcritures(row2.getInt("ID_MOUVEMENT"));
          }

          rowVals.put("LETTRAGE", codeLettre);
          rowVals.put("DATE_LETTRAGE", this.dateLettrage.getDate());
          try {
            rowVals.update(row2.getID());
          } catch (SQLException e1) {

            e1.printStackTrace();
          }
        }
      }
      // Mise à jour du code de lettrage
      SQLElement elt = Configuration.getInstance().getDirectory().getElement("NUMEROTATION_AUTO");
      SQLRowValues rowVals = elt.getTable().getRow(2).createEmptyUpdateRow();
      rowVals.put("CODE_LETTRAGE", codeLettre);
      try {
        rowVals.update();
      } catch (SQLException e) {
        e.printStackTrace();
      }
      this.codeLettrage.setText(NumerotationAutoSQLElement.getNextCodeLettrage());

      this.model.updateTotauxCompte();
    }
  }
  public LettragePanel() {
    this.setLayout(new GridBagLayout());
    GridBagConstraints c = new DefaultGridBagConstraints();

    this.modeSelect = allEcriture;

    // Selection du compte à lettrer
    JLabel labelPointageCompte = new JLabel("Lettrage du compte");
    labelPointageCompte.setHorizontalAlignment(SwingConstants.RIGHT);

    this.add(labelPointageCompte, c);
    this.selCompte = new ISQLCompteSelector();
    this.selCompte.init();
    this.selCompte.setValue(ComptePCESQLElement.getId("5"));

    c.gridx++;
    c.weightx = 1;
    c.gridwidth = GridBagConstraints.REMAINDER;
    this.add(this.selCompte, c);

    c.gridwidth = 1;

    // Gestion du lettrage
    c.insets = new Insets(2, 2, 1, 2);
    TitledSeparator sepGestionLettrage = new TitledSeparator("Gestion du lettrage");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridy++;
    c.gridx = 0;
    c.gridwidth = GridBagConstraints.REMAINDER;
    this.add(sepGestionLettrage, c);

    // Code de lettrage
    JLabel labelCode = new JLabel("Code lettrage");
    labelCode.setHorizontalAlignment(SwingConstants.RIGHT);
    c.gridx = 0;
    c.gridy++;
    c.gridwidth = 1;
    c.weightx = 0;
    this.add(labelCode, c);

    this.codeLettrage = new JTextField(10);
    DocumentFilterList.add(
        (AbstractDocument) this.codeLettrage.getDocument(),
        new UpperCaseFormatFilter(),
        FilterType.SIMPLE_FILTER);
    c.gridx++;
    c.weightx = 1;
    this.add(this.codeLettrage, c);
    this.codeLettrage.setText(NumerotationAutoSQLElement.getNextCodeLettrage());

    // Warning si aucun code rentré
    createPanelWarning();
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 0;
    c.gridx++;
    this.add(this.warningPanel, c);

    // Date de lettrage
    JLabel labelDate = new JLabel("Date");
    labelDate.setHorizontalAlignment(SwingConstants.RIGHT);
    c.gridx = 0;
    c.gridy++;
    c.weightx = 0;
    c.gridwidth = 1;
    this.add(labelDate, c);
    this.dateLettrage = new JDate(true);
    c.gridx++;
    this.add(this.dateLettrage, c);

    // Warning si solde non nul
    c.gridx++;
    createPanelWarningSolde();
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 0;
    this.add(this.warningSolde, c);

    c.gridwidth = 1;

    TitledSeparator sepPeriode = new TitledSeparator("Filtre ");
    c.gridy++;
    c.gridx = 0;
    c.anchor = GridBagConstraints.WEST;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.fill = GridBagConstraints.HORIZONTAL;
    this.add(sepPeriode, c);

    JPanel panelSelectEcritures = createPanelSelectionEcritures();
    c.gridy++;
    c.gridx = 0;
    c.gridwidth = 1;
    c.weightx = 0;

    JLabel labelEcr = new JLabel("Ecritures");
    labelEcr.setHorizontalAlignment(SwingConstants.RIGHT);
    this.add(labelEcr, c);
    c.gridx++;
    c.weightx = 1;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.fill = GridBagConstraints.NONE;
    this.add(panelSelectEcritures, c);

    JPanel panelPeriode = new JPanel();
    // Date de début
    this.dateDeb = new JDate();
    c.gridy++;
    c.gridx = 0;
    c.gridwidth = 1;
    c.weightx = 0;
    JLabel labelPerio = new JLabel("Période du ");
    labelPerio.setHorizontalAlignment(SwingConstants.RIGHT);
    c.fill = GridBagConstraints.HORIZONTAL;
    this.add(labelPerio, c);

    panelPeriode.add(this.dateDeb);
    this.dateDeb.addValueListener(
        new PropertyChangeListener() {
          public void propertyChange(PropertyChangeEvent evt) {
            changeListRequest();
          }
        });

    // Date de fin
    this.dateFin = new JDate(true);
    panelPeriode.add(new JLabel("au"));
    this.dateFin.addValueListener(
        new PropertyChangeListener() {
          public void propertyChange(PropertyChangeEvent evt) {
            changeListRequest();
          }
        });

    panelPeriode.add(this.dateFin);
    c.gridx++;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 1;
    c.fill = GridBagConstraints.NONE;
    this.add(panelPeriode, c);

    c.gridx = 0;
    c.gridy++;
    this.boxAddSousCompte = new JCheckBox("Ajouter les sous comptes");
    this.boxAddSousCompte.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            changeListRequest();
          }
        });

    this.add(this.boxAddSousCompte, c);

    TitledSeparator sepEcriture = new TitledSeparator("Ecritures ");
    c.gridy++;
    c.gridx = 0;
    c.weightx = 1;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.fill = GridBagConstraints.HORIZONTAL;
    this.add(sepEcriture, c);

    // Liste des ecritures
    final EcritureSQLElement ecritureElem =
        Configuration.getInstance().getDirectory().getElement(EcritureSQLElement.class);
    this.ecriturePanel =
        new ListPanelEcritures(ecritureElem, new IListe(ecritureElem.createLettrageTableSource()));
    c.gridx = 0;
    c.gridy++;
    c.weighty = 1;
    c.weightx = 1;
    c.fill = GridBagConstraints.BOTH;
    c.gridwidth = GridBagConstraints.REMAINDER;
    this.ecriturePanel
        .getListe()
        .setPreferredSize(
            new Dimension(this.ecriturePanel.getListe().getPreferredSize().width, 200));
    this.add(this.ecriturePanel, c);

    // JTable Totaux
    c.gridy++;
    c.gridx = 0;
    c.weighty = 0;
    c.weightx = 1;
    c.fill = GridBagConstraints.BOTH;
    c.gridwidth = 3;
    c.gridheight = 3;
    this.model = new LettrageModel(this.selCompte.getSelectedId());
    JTable table = new JTable(this.model);

    // AlternateTableCellRenderer.setAllColumns(table);
    for (int i = 0; i < table.getColumnCount(); i++) {
      // if (table.getColumnClass(i) == Long.class || table.getColumnClass(i) ==
      // BigInteger.class) {
      table.getColumnModel().getColumn(i).setCellRenderer(new DeviseNiceTableCellRenderer());
      // }
    }
    JScrollPane sPane = new JScrollPane(table);

    // TODO Gerer la taille des colonnes
    Dimension d =
        new Dimension(
            table.getPreferredSize().width,
            table.getPreferredSize().height + table.getTableHeader().getPreferredSize().height + 4);
    sPane.setPreferredSize(d);
    this.add(sPane, c);

    // Legende
    c.gridx = 4;
    c.gridwidth = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0;
    this.add(createPanelLegende(), c);

    c.gridheight = 1;
    final JButton buttonDelettrer = new JButton("Délettrer");
    this.buttonLettrer = new JButton("Lettrer");
    // Validation des ecritures pointées
    this.boxValidEcriture = new JCheckBox("Valider les écritures lettrées");
    c.gridx++;
    c.gridwidth = GridBagConstraints.REMAINDER;
    this.add(this.boxValidEcriture, c);

    JPanel panelButton = new JPanel();

    // Boutton lettrer

    panelButton.add(this.buttonLettrer, c);

    // Boutton Delettrer

    panelButton.add(buttonDelettrer, c);

    c.gridy++;
    c.gridx = 5;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.gridheight = 1;
    c.weightx = 1;
    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.EAST;
    this.add(panelButton, c);

    c.gridy++;
    c.anchor = GridBagConstraints.SOUTHEAST;
    JButton buttonClose = new JButton("Fermer");
    buttonClose.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            ((Window) SwingUtilities.getRoot((Component) e.getSource())).dispose();
          }
        });
    this.add(buttonClose, c);
    this.buttonLettrer.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {

            int[] rowIndex =
                LettragePanel.this.ecriturePanel.getListe().getJTable().getSelectedRows();

            // System.err.println("Action lettrage sur " + i);
            actionLettrage(rowIndex);
          }
        });

    buttonDelettrer.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {

            int[] rowIndex =
                LettragePanel.this.ecriturePanel.getListe().getJTable().getSelectedRows();
            actionDelettrage(rowIndex);
          }
        });

    // Changement de compte
    this.selCompte.addValueListener(
        new PropertyChangeListener() {
          public void propertyChange(PropertyChangeEvent evt) {

            changeListRequest();
          };
        });

    // Action Souris sur la IListe

    addActionMenuDroit();
    this.ecriturePanel
        .getListe()
        .getJTable()
        .addMouseListener(
            new MouseAdapter() {

              public void mouseReleased(MouseEvent e) {
                System.err.println("Mouse released");
                int[] selectedRows =
                    LettragePanel.this.ecriturePanel.getListe().getJTable().getSelectedRows();
                int[] idRows = new int[selectedRows.length];
                for (int i = 0; i < idRows.length; i++) {
                  idRows[i] =
                      LettragePanel.this.ecriturePanel.getListe().idFromIndex(selectedRows[i]);
                }

                LettragePanel.this.model.updateSelection(idRows);
                LettragePanel.this.warningSolde.setVisible(
                    LettragePanel.this.model.getSoldeSelection() != 0);
                buttonDelettrer.setEnabled(LettragePanel.this.model.getSoldeSelection() == 0);
                LettragePanel.this.buttonLettrer.setEnabled(
                    LettragePanel.this.model.getSoldeSelection() == 0);
              }
            });

    // action sur la IListe
    this.ecriturePanel
        .getListe()
        .getJTable()
        .addKeyListener(
            new KeyAdapter() {
              public void keyReleased(KeyEvent e) {

                System.err.println("Key released");
                int[] selectedRows =
                    LettragePanel.this.ecriturePanel.getListe().getJTable().getSelectedRows();
                int[] idRows = new int[selectedRows.length];
                for (int i = 0; i < idRows.length; i++) {
                  idRows[i] =
                      LettragePanel.this.ecriturePanel.getListe().idFromIndex(selectedRows[i]);
                }

                LettragePanel.this.model.updateSelection(idRows);
                LettragePanel.this.warningPanel.setVisible(
                    (LettragePanel.this.codeLettrage.getText().trim().length() == 0));
                LettragePanel.this.warningSolde.setVisible(
                    LettragePanel.this.model.getSoldeSelection() != 0);
              }
            });

    // Gestion du code
    this.codeLettrage
        .getDocument()
        .addDocumentListener(
            new SimpleDocumentListener() {

              @Override
              public void update(DocumentEvent e) {
                // TODO Auto-generated method stub
                LettragePanel.this.warningPanel.setVisible(
                    (LettragePanel.this.codeLettrage.getText().trim().length() == 0));
                LettragePanel.this.buttonLettrer.setEnabled(
                    (LettragePanel.this.codeLettrage.getText().trim().length() != 0));
              }
            });

    changeListRequest();
    this.warningPanel.setVisible((this.codeLettrage.getText().trim().length() == 0));
    this.buttonLettrer.setEnabled((this.codeLettrage.getText().trim().length() != 0));
  }