001: /*
002: * Created on 26/01/2007
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2007 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: */
023: package br.com.gfp.ofc.actions;
024:
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027:
028: import javax.swing.JFileChooser;
029: import javax.swing.JFrame;
030: import javax.swing.JOptionPane;
031:
032: import br.com.gfp.dao.GFPController;
033: import br.com.gfp.internationalization.ActionsMessages;
034: import br.com.gfp.ofc.parser.MapeamentoToContaException;
035: import br.com.gfp.ofc.parser.OFCImportException;
036: import br.com.gfp.ofc.tools.ImportUtils;
037: import br.com.gfp.ofx.parser.OFX2TransactionConversor;
038: import br.com.gfp.util.SimpleLog;
039: import br.com.gfp.windows.ConfirmImportedTransactionsWindow;
040: import br.com.gfpshare.util.MyFileFilter;
041:
042: public class ImportOFXTransactionAction implements ActionListener {
043: private static ImportOFXTransactionAction action;
044: private JFileChooser chooser = null;
045:
046: public ImportOFXTransactionAction() {
047: chooser = new JFileChooser();
048: chooser.setDialogTitle(ActionsMessages.getMessages().getString(
049: "tituloAbrirOFXDialog"));
050: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
051: chooser.setFileFilter(new MyFileFilter("ofx", ActionsMessages
052: .getMessages().getString("ofxFile")));
053: }
054:
055: public void actionPerformed(ActionEvent e) {
056: new Thread() {
057: @Override
058: public void run() {
059: chooser.updateUI();
060: if (chooser.showOpenDialog((JFrame) GFPController
061: .getGFPController().getContexto().get(
062: GFPController.FRAME_PRINCIPAL)) == JFileChooser.APPROVE_OPTION) {
063: try {
064: OFX2TransactionConversor conversor = new OFX2TransactionConversor(
065: chooser.getSelectedFile()
066: .getAbsolutePath());
067: ImportUtils.conciliate(conversor
068: .getLancamentos());
069: } catch (MapeamentoToContaException e) {
070: ((SimpleLog) GFPController.getGFPController()
071: .getContexto().get(GFPController.LOG))
072: .log("Error when mapping the imported transactions to an account!");
073: ((SimpleLog) GFPController.getGFPController()
074: .getContexto().get(GFPController.LOG))
075: .log(e.getLocalizedMessage());
076: ((SimpleLog) GFPController.getGFPController()
077: .getContexto().get(GFPController.LOG))
078: .log(e);
079: JOptionPane
080: .showMessageDialog(
081: (JFrame) GFPController
082: .getGFPController()
083: .getContexto()
084: .get(
085: GFPController.FRAME_PRINCIPAL),
086: ActionsMessages
087: .getMessages()
088: .getString(
089: "ErroMapeamentoConta"));
090: } catch (OFCImportException e) {
091: ((SimpleLog) GFPController.getGFPController()
092: .getContexto().get(GFPController.LOG))
093: .log("Error when importing transactions from OFC file!");
094: ((SimpleLog) GFPController.getGFPController()
095: .getContexto().get(GFPController.LOG))
096: .log(e.getLocalizedMessage());
097: ((SimpleLog) GFPController.getGFPController()
098: .getContexto().get(GFPController.LOG))
099: .log(e);
100: JOptionPane
101: .showMessageDialog(
102: (JFrame) GFPController
103: .getGFPController()
104: .getContexto()
105: .get(
106: GFPController.FRAME_PRINCIPAL),
107: ActionsMessages
108: .getMessages()
109: .getString(
110: "ErroImportacaoOFC"));
111: }
112: GFPController.getGFPController().create(
113: ConfirmImportedTransactionsWindow.class);
114: }
115: }
116:
117: }.start();
118: }
119:
120: /**
121: * @return Retorna uma instancia desta action
122: */
123: public static ImportOFXTransactionAction getAction() {
124: return action;
125: }
126:
127: /**
128: *
129: */
130: public static void initializeAction() {
131: if (action == null)
132: action = new ImportOFXTransactionAction();
133: }
134: }
|