001: /*
002: * Created on 13/10/2004
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2004 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022: package br.com.gfp.actions;
023:
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.io.File;
027:
028: import javax.swing.JFrame;
029: import javax.swing.JOptionPane;
030:
031: import br.com.gfp.dao.GFPController;
032: import br.com.gfp.internationalization.ActionsMessages;
033: import br.com.gfp.util.SimpleLog;
034: import br.com.gfp.util.ThreadUtil;
035: import br.com.gfp.windows.GFPPrincipal;
036: import br.com.gfpshare.beans.MessageView;
037: import br.com.gfpshare.controllers.ArquivoDeDadosAbertoContoller;
038: import br.com.gfpshare.db.DataBaseManager;
039:
040: /**
041: * @author Igor Regis da Silva Simoes
042: */
043: public class FecharDBAction implements ActionListener {
044: private static FecharDBAction action = null;
045:
046: private Thread acao = new Thread("FecharDB") {
047: {
048: this .setPriority(Thread.MIN_PRIORITY);
049: this .setDaemon(false);
050: }
051:
052: @Override
053: public void run() {
054: //Se não existe arquivo aberto
055: if (ArquivoDeDadosAbertoContoller.getInstance(null) == null
056: || !ArquivoDeDadosAbertoContoller.getInstance(null)
057: .isArquivoAberto()) {
058: //Recriamos a Thread para que possamos executá-la novamente mais tarde se necessário
059: acao = new Thread(this , "FecharDB");
060: return;
061: }
062:
063: MessageView messages = (MessageView) GFPController
064: .getGFPController().getContexto().get(
065: GFPController.RODAPE_MESSAGER);
066: messages = messages.showMessage(ActionsMessages
067: .getMessages().getString("FecharArquivoTitulo"),
068: ActionsMessages.getMessages().getString(
069: "FecharArquivoMessage"), true);
070: messages.getMessagePanel().getProgressBar()
071: .setIndeterminate(true);
072:
073: //Se existe arquivo aberto e ele foi alterado
074: if (ArquivoDeDadosAbertoContoller.getInstance(null) != null
075: && ArquivoDeDadosAbertoContoller.getInstance(null)
076: .isArquivoAlterado()) {
077: int escolha = JOptionPane.showOptionDialog(
078: (JFrame) GFPController.getGFPController()
079: .getContexto().get(
080: GFPController.FRAME_PRINCIPAL),
081: ActionsMessages.getMessages().getString(
082: "fecharCancelarOuSalvarAntes"),
083: ActionsMessages.getMessages().getString(
084: "atencao"),
085: JOptionPane.YES_NO_CANCEL_OPTION,
086: JOptionPane.INFORMATION_MESSAGE, null,
087: new Object[] {
088: ActionsMessages.getMessages()
089: .getString("sim"),
090: ActionsMessages.getMessages()
091: .getString("nao"),
092: ActionsMessages.getMessages()
093: .getString("cancelar") },
094: ActionsMessages.getMessages().getString("sim"));
095: if (escolha == JOptionPane.YES_OPTION) {
096: SalvarDBAction.getAction().actionPerformed(null);
097: ThreadUtil.waitFor("SalvarArquivo");
098: } else if (escolha == JOptionPane.CANCEL_OPTION) {
099: //Recriamos a Thread para que possamos executá-la novamente mais tarde se necessário
100: acao = new Thread(this , "FecharDB");
101: return;
102: }
103: }
104:
105: try {
106: //Fechamos todas janelas abertas
107: FecharTodasTelasAction.getAction()
108: .actionPerformed(null);
109:
110: //Esperamos o fechamento de todas as janelas
111: ThreadUtil.waitFor("FecharTodasJanelas");
112:
113: //Fechamos o banco de dados
114: DataBaseManager.getDataBaseManager()
115: .executarAtualizacao("SHUTDOWN", null);
116: DataBaseManager.getDataBaseManager()
117: .fecharBancoDeDados();
118: } catch (Exception ex) {
119: ((SimpleLog) GFPController.getGFPController()
120: .getContexto().get(GFPController.LOG))
121: .log("Erro ao fechar banco de dados!");
122: ((SimpleLog) GFPController.getGFPController()
123: .getContexto().get(GFPController.LOG)).log(ex
124: .getLocalizedMessage());
125: ((SimpleLog) GFPController.getGFPController()
126: .getContexto().get(GFPController.LOG)).log(ex);
127: }
128: messages.getMessagePanel().setMessage(
129: ActionsMessages.getMessages().getString(
130: "RemovendoArquivosTemporarios"), true);
131: String dbDir = System.getProperty("user.dir")
132: + File.separator + "db" + File.separator;
133: new File(dbDir + "db.script").delete();
134: new File(dbDir + "db.properties").delete();
135: new File(dbDir + "db.backup").delete();
136: new File(dbDir + "db.data").delete();
137: new File(dbDir + "gfd_db.properties").delete();
138: new File(dbDir + "db.log").delete();
139:
140: //Reconstrói os menus e menubar
141: ((GFPPrincipal) GFPController.getGFPController()
142: .getContexto().get(GFPController.FRAME_PRINCIPAL))
143: .buildMenusAndButtons(false);
144:
145: //Recriamos a Thread para que possamos executá-la novamente mais tarde se necessário
146: acao = new Thread(this , "FecharDB");
147:
148: messages.getMessagePanel().setMessage(
149: ActionsMessages.getMessages().getString(
150: "ArquivoFechado"), true);
151: messages.getMessagePanel().getProgressBar()
152: .setIndeterminate(false);
153: messages.finishUse();
154: }
155: };
156:
157: /**
158: *
159: */
160: private FecharDBAction() {
161: super ();
162: }
163:
164: /**
165: * @return Retorna uma instancia desta action
166: */
167: public static FecharDBAction getAction() {
168: return action;
169: }
170:
171: /**
172: *
173: */
174: public static void initializeAction() {
175: if (action == null)
176: action = new FecharDBAction();
177: }
178:
179: /**
180: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
181: */
182: public void actionPerformed(ActionEvent e) {
183: acao.start();
184: }
185: }
|