/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 L.Meckert - laurent.meckert.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.cp.xemelios.plugin.signerfichier;

/*
 * DlgSignerfichierFull.java
 *
 * Created on 23 septembre 2008, 09:16
 */
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.io.File;
import org.apache.log4j.Logger;

import javax.swing.JPanel;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;

/**
 *
 * @author  laurent.meckert
 */
public abstract class AbstractDlgSignerfichier implements KeyListener, MouseListener {

  protected javax.swing.JDialog dialog = null;
  protected JFrame parentWindow;
//  private File fileToSign = null;
//  File fileToGenerate = null;
//  File certFile = null;
  static Logger logger = Logger.getLogger(AbstractDlgSignerfichier.class);
//  protected JFileChooser fc;
//  IFileSigner signer = null;
//
  /**
   * The busy state of the frame.  
   */
  private boolean busy = false;
  /**
   * The glass pane used when busy.  
   */
  private Component glassPane = null;
  /**
   * The original glass pane, which is reset when not busy.  
   */
  private Component defaultGlassPane = null;

  /** Creates new form AbstractDlgSignerfichier
   * @param parent
   * @param modal 
   */
  public AbstractDlgSignerfichier(java.awt.Frame parent, boolean modal) {
    dialog = new javax.swing.JDialog(parent, modal);
    Actions actions = Actions.getInstance();
    actions.setParentWindow(parent);

  }

  

  /**
   * Montre le résultat de la vérification
   * @param signer 
   */
  public void showStatus(IFileSigner signer) {
    String bla;
    int sever;
    if (signer != null) {
      bla = signer.getStatusAsString();
      sever = signer.getStatusAsSeverity();
    } else {
      sever = FileSigner.SIGN_VERYBAD;
      bla = "Une erreur inattendue est survenue; veuillez consulter le log de l'application";
    }

    JOptionPane.showMessageDialog(parentWindow,
            bla,
            "Résultat de la vérification",
            sever == FileSigner.SIGN_VERYBAD ? JOptionPane.ERROR_MESSAGE : (sever == FileSigner.SIGN_BAD ? JOptionPane.WARNING_MESSAGE : JOptionPane.PLAIN_MESSAGE));
  }

  /**
   * 
   * @return
   */
  public boolean isBusy() {
    return this.busy;
  }

  void setBusy(boolean busy) {
    // only set if changing
    if (this.busy != busy) {
      this.busy = busy;
      // If busy, keep current glass pane to put back when not 
      // busy.  This is done in case the application is using 
      // it's own glass pane for something special.  
      if (busy) {
        defaultGlassPane = dialog.getGlassPane();
        dialog.setGlassPane(glassPane);
      } else {
        dialog.setGlassPane(defaultGlassPane);
        defaultGlassPane = null;
      }
      glassPane.setVisible(busy);
      glassPane.setCursor(busy ? Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR) : Cursor.getDefaultCursor());
      dialog.setCursor(glassPane.getCursor());
    }
  }

  /**
   * Handle key typed events.  Consumes the event for the glasspane 
   * when the frame is busy.  
   * 
   * @param  ke  the key event
   */
  public void keyTyped(KeyEvent ke) {
    ke.consume();
  }

  /**
   * Handle key released events.  Consumes the event for the glasspane 
   * when the frame is busy.  
   * 
   * @param  ke  the key event
   */
  public void keyReleased(KeyEvent ke) {
    ke.consume();
  }

  /**
   * Handle key pressed events.  Consumes the event for the glasspane 
   * when the frame is busy.  
   * 
   * @param  ke  the key event
   */
  public void keyPressed(KeyEvent ke) {
    ke.consume();
  }

  /**
   * Handle mouse clicked events.  Consumes the event for the glasspane 
   * when the frame is busy.  
   * 
   * @param  me  the mouse event
   */
  public void mouseClicked(MouseEvent me) {
    me.consume();
  }

  /**
   * Handle mouse entered events.  Consumes the event for the glasspane 
   * when the frame is busy.  
   * 
   * @param  me  the mouse event
   */
  public void mouseEntered(MouseEvent me) {
    me.consume();
  }

  /**
   * Handle mouse exited events.  Consumes the event for the glasspane 
   * when the frame is busy.  
   * 
   * @param  me  the mouse event
   */
  public void mouseExited(MouseEvent me) {
    me.consume();
  }

  /**
   * Handle mouse pressed events.  Consumes the event for the glasspane 
   * when the frame is busy.  
   * 
   * @param  me  the mouse event
   */
  public void mousePressed(MouseEvent me) {
    me.consume();
  }

  /**
   * Handle mouse released events.  Consumes the event for the glasspane 
   * when the frame is busy.  
   * 
   * @param  me  the mouse event
   */
  public void mouseReleased(MouseEvent me) {
    me.consume();
  }

  protected void initGlassPane() {
    glassPane = new JPanel();
    ((JPanel) glassPane).setOpaque(false);
    glassPane.addKeyListener(this);
    glassPane.addMouseListener(this);
  }

  protected void initSigner() {

    Actions.getInstance().initSigner();
  }

  abstract void initOthers();

  abstract void showSelectedInputFile(File f);

  

  /**
   * 
   */
  public void run() {
    initGlassPane();
    initSigner();
    initOthers();
    dialog.setVisible(true);
  }

  /**
   * 
   */
  public void close() {
    System.out.println("closing dialog (DlgSignerFichierBase)");
    

    SwingUtilities.getWindowAncestor(dialog).dispose();
  }

 
}
