001: /*
002: * Created on 23/03/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.Toolkit;
026: import java.awt.datatransfer.Clipboard;
027: import java.awt.datatransfer.DataFlavor;
028: import java.awt.datatransfer.Transferable;
029: import java.awt.datatransfer.UnsupportedFlavorException;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.io.IOException;
033: import java.util.ArrayList;
034: import java.util.Calendar;
035: import java.util.Date;
036: import java.util.StringTokenizer;
037:
038: import javax.swing.JFrame;
039: import javax.swing.JOptionPane;
040:
041: import br.com.gfp.dao.GFPController;
042: import br.com.gfp.data.PendingTransaction;
043: import br.com.gfp.internationalization.ActionsMessages;
044: import br.com.gfp.ofc.tools.ImportUtils;
045: import br.com.gfp.ofc.windows.SelectAccountDialog;
046: import br.com.gfp.util.SimpleLog;
047: import br.com.gfp.windows.ConfirmImportedTransactionsWindow;
048:
049: public class ImportInMemoryTextBBAction implements ActionListener {
050: private static ImportInMemoryTextBBAction action;
051:
052: public void actionPerformed(ActionEvent e) {
053: new Thread() {
054: @Override
055: public void run() {
056: StringTokenizer text = new StringTokenizer(
057: getClipboardContents().replace('\n', '\t'),
058: "\t");
059: ArrayList<PendingTransaction> transactions = parseTransactions(text);
060: defineAccount(transactions);
061: ImportUtils.defineTypeForTransactions(transactions);
062: ImportUtils.conciliate(transactions);
063: GFPController.getGFPController().create(
064: ConfirmImportedTransactionsWindow.class);
065: }
066: }.start();
067: }
068:
069: private void defineAccount(
070: ArrayList<PendingTransaction> transactions) {
071: SelectAccountDialog dialog = new SelectAccountDialog("");
072: dialog.setVisible(true);
073:
074: for (PendingTransaction transaction : transactions) {
075: transaction.setConta(dialog.getConta());
076: transaction.setTipoConta(dialog.getTipoConta());
077: }
078: }
079:
080: private ArrayList<PendingTransaction> parseTransactions(
081: StringTokenizer text) {
082: ArrayList<PendingTransaction> result = new ArrayList<PendingTransaction>();
083: while (text.hasMoreTokens()) {
084: PendingTransaction pendingTransaction = new PendingTransaction();
085: pendingTransaction.setDia(parseDay(text.nextElement()
086: .toString()));
087: text.nextElement();
088: pendingTransaction.setDescricao(text.nextElement()
089: .toString().trim());
090: text.nextElement();
091: pendingTransaction.setValor(parseValue(text.nextElement()
092: .toString()));
093: result.add(pendingTransaction);
094: }
095: return result;
096: }
097:
098: private Double parseValue(String string) {
099: String temp = string.substring(0, string.lastIndexOf(",") + 3);
100: temp = temp.replace('.', 'p').replace(',', 'v');
101: temp = temp.replace("p", "").replace('v', '.');
102: double value = Double.parseDouble(temp);
103: if (string.indexOf("D") != -1)
104: value *= -1;
105: return value;
106: }
107:
108: private Date parseDay(String str) {
109: Calendar dia = Calendar.getInstance();
110: dia.set(Calendar.DAY_OF_MONTH, Integer.parseInt(str.substring(
111: 0, 2)));
112: dia.set(Calendar.MONTH,
113: Integer.parseInt(str.substring(3, 5)) - 1);
114: return dia.getTime();
115: }
116:
117: /**
118: * Here we get the clipboard contents if it is of text type
119: * @return
120: */
121: public String getClipboardContents() {
122: String result = "";
123: Clipboard clipboard = Toolkit.getDefaultToolkit()
124: .getSystemClipboard();
125: // the Object param of getContents is not currently used
126: Transferable contents = clipboard.getContents(null);
127: boolean hasTransferableText = (contents != null)
128: && contents
129: .isDataFlavorSupported(DataFlavor.stringFlavor);
130: if (hasTransferableText) {
131: try {
132: result = (String) contents
133: .getTransferData(DataFlavor.stringFlavor);
134: } catch (UnsupportedFlavorException e) {
135: // highly unlikely since we are using a standard DataFlavor
136: ((SimpleLog) GFPController.getGFPController()
137: .getContexto().get(GFPController.LOG))
138: .log("Error when importing transactions from ClipBoard!");
139: ((SimpleLog) GFPController.getGFPController()
140: .getContexto().get(GFPController.LOG)).log(e
141: .getLocalizedMessage());
142: ((SimpleLog) GFPController.getGFPController()
143: .getContexto().get(GFPController.LOG)).log(e);
144: } catch (IOException e) {
145: ((SimpleLog) GFPController.getGFPController()
146: .getContexto().get(GFPController.LOG))
147: .log("Error when importing transactions from ClipBoard!");
148: ((SimpleLog) GFPController.getGFPController()
149: .getContexto().get(GFPController.LOG)).log(e
150: .getLocalizedMessage());
151: ((SimpleLog) GFPController.getGFPController()
152: .getContexto().get(GFPController.LOG)).log(e);
153: JOptionPane.showMessageDialog((JFrame) GFPController
154: .getGFPController().getContexto().get(
155: GFPController.FRAME_PRINCIPAL),
156: ActionsMessages.getMessages().getString(
157: "ErroImportacaoClipboard"));
158: }
159: }
160: return result;
161: }
162:
163: public static ImportInMemoryTextBBAction getAction() {
164: return action;
165: }
166:
167: public static void initializeAction() {
168: if (action == null)
169: action = new ImportInMemoryTextBBAction();
170: }
171: }
|