001: /*
002: * GFDTableCellRenderer.java Criado em 4 de Maio de 2004, 15:08
003: */
004:
005: package br.com.gfp.windows.componentes;
006:
007: import java.awt.Color;
008: import java.awt.Component;
009: import java.sql.SQLException;
010: import java.util.HashMap;
011:
012: import javax.swing.JTable;
013: import javax.swing.event.TableModelEvent;
014: import javax.swing.event.TableModelListener;
015: import javax.swing.table.AbstractTableModel;
016:
017: import br.com.gfp.dao.AccountTypeDAO;
018: import br.com.gfp.dao.GFPController;
019: import br.com.gfp.data.AccountType;
020: import br.com.gfp.data.Transaction;
021: import br.com.gfp.util.SimpleLog;
022: import br.com.gfpshare.beans.table.ConfiguravelTableCellRenderer;
023: import br.com.gfpshare.beans.table.DBTable;
024: import br.com.gfpshare.beans.table.DBTreeTable;
025: import br.com.gfpshare.db.PersistentObject;
026: import br.com.gfpshare.db.formatter.GeneralFormatter;
027:
028: /**
029: * This is a renderer that can render GFP domain data.
030: * This renderer knows how to access the database through the
031: * persistence layer to retrieve the data to be rendered.
032: *
033: * Usually a table is loaded with the data from a database table and
034: * some fields are only foreign keys id's, so the GFPTableCellRender
035: * perform a database query to get the textual representation for that
036: * field according to its configuration
037: *
038: * @author Igor Regis da Silva Simões
039: */
040: public class GFPTableCellRenderer extends ConfiguravelTableCellRenderer
041: implements TableModelListener {
042: /**
043: * Account Saving
044: */
045: public static final int APLICACAO = 101;
046: /**
047: * Bank
048: */
049: public static final int BANCO = 102;
050: /**
051: * User assets
052: */
053: public static final int BEM = 103;
054: /**
055: * Checking Account
056: */
057: public static final int CONTA_CORRENTE = 104;
058: /**
059: * A contact from contact list
060: */
061: public static final int ENTIDADE = 105;
062: /**
063: * A Transaction
064: */
065: public static final int LANCAMENTO = 106;
066: /**
067: * Type of Transaction
068: */
069: public static final int TIPO_LANCAMENTO = 107;
070: /**
071: * Branch number
072: */
073: public static final int NUMERO_AGENCIA = 108;
074: /**
075: * Checking account number
076: */
077: public static final int NUMERO_CONTA_CORRENTE = 109;
078: /**
079: * Type of earning period for some account saving
080: */
081: public static final int TIPO_DE_RENDIMENTO = 110;
082: /**
083: * Type of repetition a transaction will do
084: */
085: public static final int TIPO_REPETICAO = 111;
086: /**
087: * Type of account (saving, checking, credit card)
088: */
089: public static final int TIPO_CONTA = 112;
090: /**
091: * Credit card account
092: */
093: public static final int CARTAO_CREDITO = 113;
094:
095: /**
096: *
097: */
098: private static GeneralFormatter formatter = new GeneralFormatter();
099:
100: /**
101: *
102: */
103: private HashMap<Integer, String> tiposDeDados = new HashMap<Integer, String>();
104:
105: /**
106: *
107: */
108: private HashMap<String, String> cache = new HashMap<String, String>();
109:
110: /**
111: *
112: */
113: private boolean registrouListener = false;
114:
115: /**
116: *
117: */
118: private static Integer aplicacao = null;
119:
120: /**
121: *
122: */
123: private static Integer contaCorrente = null;
124:
125: protected PersistentObject currentPersistentObject = null;
126:
127: /**
128: * Cria uma nova instância de GFDTableCellRenderer
129: *
130: * @param formato
131: * @param subformato
132: */
133: public GFPTableCellRenderer(int formato, int subformato) {
134: super (formato, subformato);
135:
136: tiposDeDados.put(GFPTableCellRenderer.BANCO, "Bank");
137: tiposDeDados.put(GFPTableCellRenderer.BEM, "Assets");
138: tiposDeDados.put(GFPTableCellRenderer.ENTIDADE, "Contact");
139: tiposDeDados
140: .put(GFPTableCellRenderer.LANCAMENTO, "Transaction");
141: tiposDeDados.put(GFPTableCellRenderer.TIPO_LANCAMENTO,
142: "TransactionType");
143: tiposDeDados.put(GFPTableCellRenderer.NUMERO_CONTA_CORRENTE,
144: "NumeroContaCorrente");
145: tiposDeDados.put(GFPTableCellRenderer.NUMERO_AGENCIA,
146: "NumeroAgencia");
147: tiposDeDados.put(GFPTableCellRenderer.APLICACAO,
148: "AccountSaving");
149: tiposDeDados.put(GFPTableCellRenderer.CARTAO_CREDITO,
150: "CreditCard");
151: tiposDeDados.put(GFPTableCellRenderer.CONTA_CORRENTE,
152: "CheckingAccount");
153: tiposDeDados.put(GFPTableCellRenderer.TIPO_DE_RENDIMENTO,
154: "IncomeType");
155: tiposDeDados.put(GFPTableCellRenderer.TIPO_REPETICAO,
156: "RepetitionType");
157: tiposDeDados
158: .put(GFPTableCellRenderer.TIPO_CONTA, "AccountType");
159:
160: if (aplicacao == null)
161: try {
162: aplicacao = new AccountTypeDAO().getBy(
163: new AccountType("aplicacao")).getId();
164: } catch (SQLException e) {
165: ((SimpleLog) GFPController.getGFPController()
166: .getContexto().get(GFPController.LOG))
167: .log("Erro ao carregar ID de aplicacao!");
168: ((SimpleLog) GFPController.getGFPController()
169: .getContexto().get(GFPController.LOG)).log(e
170: .getLocalizedMessage());
171: ((SimpleLog) GFPController.getGFPController()
172: .getContexto().get(GFPController.LOG)).log(e);
173: }
174: if (contaCorrente == null)
175: try {
176: contaCorrente = new AccountTypeDAO().getBy(
177: new AccountType("contaCorrente")).getId();
178: } catch (SQLException e) {
179: ((SimpleLog) GFPController.getGFPController()
180: .getContexto().get(GFPController.LOG))
181: .log("Erro ao carregar ID de conta corrente!");
182: ((SimpleLog) GFPController.getGFPController()
183: .getContexto().get(GFPController.LOG)).log(e
184: .getLocalizedMessage());
185: ((SimpleLog) GFPController.getGFPController()
186: .getContexto().get(GFPController.LOG)).log(e);
187: }
188: }
189:
190: /**
191: * Sobrescreve o método da superclasse para retornar um renderer formatado,
192: * conforme descrito acima
193: *
194: * @param table
195: * JTable Tabela que será configurada por este renderer
196: * @param value
197: * Object Valor que será exibido pelo renderer
198: * @param isSelected
199: * boolean Indicando se a célula está selecionada
200: * @param hasFocus
201: * boolean indicando se a célual possui o foco
202: * @param row
203: * int Linha da célula
204: * @param column
205: * int Coluna da célula
206: * @return Retorna o Compoment responsável por renderizar o valor da célula
207: */
208: @Override
209: public Component getTableCellRendererComponent(JTable table,
210: Object value, boolean isSelected, boolean hasFocus,
211: int row, int column) {
212: super .getTableCellRendererComponent(table, value, isSelected,
213: hasFocus, row, column);
214: currentPersistentObject = null;//None persistentObject is selected yet
215: if (!registrouListener) {
216: table.getModel().addTableModelListener(this );
217: registrouListener = true;
218: }
219:
220: String valor = null;
221:
222: /*
223: * No caso da coluna que possui o tipo de dado AccoutSaving ela também pode
224: * possuir dados do tipo ContaCorrente Portanto, nós precisamos buscar
225: * uma coluna chamada TipoConta que indicará qual o tipo de dado daquela
226: * linha se é AccoutSaving ou ContaCorrente
227: */
228: if (getFormato() == GFPTableCellRenderer.APLICACAO
229: || getFormato() == GFPTableCellRenderer.CONTA_CORRENTE
230: || getFormato() == GFPTableCellRenderer.CARTAO_CREDITO) {
231: Object o = null;
232: int index = ((AbstractTableModel) table.getModel())
233: .findColumn("TipoConta") - 1;
234:
235: if (index > 0)
236: o = table.getModel().getValueAt(row, index);
237: if (o == null)
238: o = table.getModel().getValueAt(
239: row,
240: ((AbstractTableModel) table.getModel())
241: .findColumn("TipoContaValido") - 1);
242:
243: if (o != null && o.toString().equals(aplicacao.toString())) {
244: setFormato(GFPTableCellRenderer.APLICACAO,
245: getSubformato());
246: } else if (o != null
247: && o.toString().equals(contaCorrente.toString())) {
248: setFormato(GFPTableCellRenderer.CONTA_CORRENTE,
249: getSubformato());
250: } else if (o != null) {
251: setFormato(GFPTableCellRenderer.CARTAO_CREDITO,
252: getSubformato());
253: }
254: }
255:
256: // Se não for null e não estiver no Cache... ( o cache é usado para
257: // evitar consultas redundantes ao banco de dados)
258: if (value != null
259: && (valor = cache.get(value.toString() + getFormato())) == null) {
260: if (tiposDeDados.get(new Integer(getFormato())) != null) {
261: valor = formatter.format(value,
262: GeneralFormatter.FORMATO_CURTO, tiposDeDados
263: .get(new Integer(getFormato()))
264: .toString());
265: cache.put(value.toString() + getFormato(), valor);
266: }
267: }
268:
269: try {
270: if (table instanceof DBTable) {
271: currentPersistentObject = ((DBTable) table)
272: .getPersistentObject(row);
273: } else if (table instanceof DBTreeTable) {
274: currentPersistentObject = ((DBTreeTable) table)
275: .getPersistentObject(row);
276: }
277:
278: if (currentPersistentObject != null
279: && currentPersistentObject instanceof Transaction) {
280: Boolean ehPrevisao = ((Transaction) currentPersistentObject)
281: .getEhPrevisao();
282: if (ehPrevisao != null && ehPrevisao.booleanValue()) {
283: if (getForeground().equals(Color.RED))
284: setForeground(new Color(255, 150, 150));
285: else if (getForeground().equals(Color.BLACK))
286: setForeground(new Color(180, 180, 180));
287: }
288: }
289: } catch (SQLException e) {
290: ((SimpleLog) GFPController.getGFPController().getContexto()
291: .get(GFPController.LOG))
292: .log("Erro na formatacao de lancamentos de previsao!");
293: ((SimpleLog) GFPController.getGFPController().getContexto()
294: .get(GFPController.LOG)).log(e
295: .getLocalizedMessage());
296: ((SimpleLog) GFPController.getGFPController().getContexto()
297: .get(GFPController.LOG)).log(e);
298: }
299:
300: if (valor != null)
301: setValue(valor);
302:
303: return this ;
304: }
305:
306: /**
307: * @param e
308: */
309: public void tableChanged(TableModelEvent e) {
310: cache.clear();
311: }
312: }
|