FecharDBAction.java :  » ERP-CRM-Financial » Personal-Finance-Manager » br » com » gfp » actions » Java Open Source

Java Open Source » ERP CRM Financial » Personal Finance Manager 
Personal Finance Manager » br » com » gfp » actions » FecharDBAction.java
/*
 * Created on 13/10/2004
 * 
 * Swing Components - visit http://sf.net/projects/gfd
 * 
 * Copyright (C) 2004  Igor Regis da Silva Simes
 * 
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package br.com.gfp.actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import br.com.gfp.dao.GFPController;
import br.com.gfp.internationalization.ActionsMessages;
import br.com.gfp.util.SimpleLog;
import br.com.gfp.util.ThreadUtil;
import br.com.gfp.windows.GFPPrincipal;
import br.com.gfpshare.beans.MessageView;
import br.com.gfpshare.controllers.ArquivoDeDadosAbertoContoller;
import br.com.gfpshare.db.DataBaseManager;

/**
 * @author Igor Regis da Silva Simoes
 */
public class FecharDBAction implements ActionListener
{
  private static FecharDBAction action = null;
  
  private Thread acao = new Thread("FecharDB")
  {
    {
      this.setPriority(Thread.MIN_PRIORITY);
      this.setDaemon(false);
    }
    @Override
        public void run()
    {
      //Se no existe arquivo aberto
      if (ArquivoDeDadosAbertoContoller.getInstance(null) == null ||
        !ArquivoDeDadosAbertoContoller.getInstance(null).isArquivoAberto())
      {
            //Recriamos a Thread para que possamos execut-la novamente mais tarde se necessrio
            acao = new Thread(this, "FecharDB");
        return;
      }
      
            MessageView messages = (MessageView)GFPController.getGFPController().getContexto().get(GFPController.RODAPE_MESSAGER);
            messages = messages.showMessage(ActionsMessages.getMessages().getString("FecharArquivoTitulo"),
                                                        ActionsMessages.getMessages().getString("FecharArquivoMessage"), true);
            messages.getMessagePanel().getProgressBar().setIndeterminate(true);
            
      //Se existe arquivo aberto e ele foi alterado
      if (ArquivoDeDadosAbertoContoller.getInstance(null) != null &&
        ArquivoDeDadosAbertoContoller.getInstance(null).isArquivoAlterado())
      {
        int escolha = JOptionPane.showOptionDialog((JFrame)GFPController.getGFPController().getContexto().get(GFPController.FRAME_PRINCIPAL),
                            ActionsMessages.getMessages().getString("fecharCancelarOuSalvarAntes"),
                            ActionsMessages.getMessages().getString("atencao"), 
                            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, 
                            new Object[]{  ActionsMessages.getMessages().getString("sim"),
                                    ActionsMessages.getMessages().getString("nao"),
                                    ActionsMessages.getMessages().getString("cancelar")
                                  },
                            ActionsMessages.getMessages().getString("sim"));
        if(escolha == JOptionPane.YES_OPTION)
        {
          SalvarDBAction.getAction().actionPerformed(null);
          ThreadUtil.waitFor("SalvarArquivo");
        }
        else if (escolha == JOptionPane.CANCEL_OPTION)
        {
              //Recriamos a Thread para que possamos execut-la novamente mais tarde se necessrio
              acao = new Thread(this, "FecharDB");
          return;
        }
      }
      
      try
      {
        //Fechamos todas janelas abertas
        FecharTodasTelasAction.getAction().actionPerformed(null);

                //Esperamos o fechamento de todas as janelas
                ThreadUtil.waitFor("FecharTodasJanelas");
        
        //Fechamos o banco de dados
          DataBaseManager.getDataBaseManager().executarAtualizacao("SHUTDOWN", null);
          DataBaseManager.getDataBaseManager().fecharBancoDeDados();
      }catch(Exception ex)
      {
                ((SimpleLog)GFPController.getGFPController().getContexto().get(GFPController.LOG)).log("Erro ao fechar banco de dados!");
                ((SimpleLog)GFPController.getGFPController().getContexto().get(GFPController.LOG)).log(ex.getLocalizedMessage());
                ((SimpleLog)GFPController.getGFPController().getContexto().get(GFPController.LOG)).log(ex);
      }
      messages.getMessagePanel().setMessage(ActionsMessages.getMessages().getString("RemovendoArquivosTemporarios"), true);
            String dbDir = System.getProperty("user.dir") + File.separator + "db" + File.separator;
            new File(dbDir + "db.script").delete();
            new File(dbDir + "db.properties").delete();
            new File(dbDir + "db.backup").delete();
            new File(dbDir + "db.data").delete();
            new File(dbDir + "gfd_db.properties").delete();
            new File(dbDir + "db.log").delete();
            
          //Reconstri os menus e menubar
          ((GFPPrincipal)GFPController.getGFPController().getContexto().get(GFPController.FRAME_PRINCIPAL)).buildMenusAndButtons(false);
          
          //Recriamos a Thread para que possamos execut-la novamente mais tarde se necessrio
          acao = new Thread(this, "FecharDB");
      
      messages.getMessagePanel().setMessage(ActionsMessages.getMessages().getString("ArquivoFechado"), true);
      messages.getMessagePanel().getProgressBar().setIndeterminate(false);
            messages.finishUse();
    }
  };
  
  /**
   * 
   */
  private FecharDBAction()
  {
    super();
  }

  /**
   * @return Retorna uma instancia desta action
   */
  public static FecharDBAction getAction()
  {
    return action;
  }
  
  /**
   * 
   */
  public static void initializeAction()
  {
    if (action == null)
      action = new FecharDBAction();
  }
  
  /**
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  public void actionPerformed(ActionEvent e)
  {
    acao.start();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.