001: /*
002: * Created on 01/01/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: */
023: package br.com.gfpshare.beans.table;
024:
025: import java.awt.Color;
026: import java.awt.Component;
027: import java.text.DateFormat;
028: import java.text.NumberFormat;
029: import java.text.ParseException;
030: import java.text.SimpleDateFormat;
031: import java.util.Locale;
032:
033: import javax.swing.JLabel;
034: import javax.swing.JTable;
035: import javax.swing.text.MaskFormatter;
036:
037: import br.com.gfpshare.util.CNPJ;
038: import br.com.gfpshare.util.CPF;
039:
040: /**
041: * This is a general propose table cell renderer that can render
042: * data from generic types that don't need database access to be
043: * represented
044: *
045: * @author Igor Regis da Silva Simões
046: */
047: public class ConfiguravelTableCellRenderer extends
048: TableCellRendererListradoCinza {
049: /**
050: * The money format
051: */
052: public static final int DINHEIRO = 1;
053:
054: /**
055: * The percentage format
056: */
057: public static final int PORCENTAGEM = 2;
058:
059: /**
060: * The Date format
061: */
062: public static final int DATA = 3;
063:
064: /**
065: * The Brazilian CPF format
066: */
067: public static final int CPF = 4;
068:
069: /**
070: * The BRazilian CNPJ format
071: */
072: public static final int CNPJ = 5;
073:
074: /**
075: * The boolean format
076: */
077: public static final int BOOLEANO = 6;
078:
079: private int formato;
080:
081: private int subformato = -1;
082:
083: @SuppressWarnings("unused")
084: private Locale local = Locale.getDefault();
085:
086: /**
087: * This is a general propose table cell renderer that can render
088: * data from generic types that don't need database access to be
089: * represented
090: *
091: * @param formato The format that will be used to represent the data
092: * @param subformato The sub-format to be used (if exists)
093: */
094: public ConfiguravelTableCellRenderer(int formato, int subformato) {
095: setFormato(formato, subformato);
096: }
097:
098: /**
099: * Determina o formato de dados queeste renderer irá exibir
100: *
101: * @param formato Formato aplicado ao dado
102: * @param subformato Subformata a ser aplicado
103: */
104: public void setFormato(int formato, int subformato) {
105: this .formato = formato;
106: this .subformato = subformato;
107: }
108:
109: /**
110: * Retorna o subformato atual
111: *
112: * @return int representando o subformato atual
113: */
114: public int getSubformato() {
115: return this .subformato;
116: }
117:
118: /**
119: * Retorna o formato atual
120: *
121: * @return int representando o formato atual
122: */
123: public int getFormato() {
124: return this .formato;
125: }
126:
127: /**
128: * Determina a localização dos formatos
129: *
130: * @param local
131: */
132: @Override
133: public void setLocale(Locale local) {
134: this .local = local;
135: }
136:
137: /**
138: * Sobrescreve o método da superclasse para retornar um renderer formatado, conforme descrito acima
139: *
140: * @param table JTable Tabela que será configurada por este renderer
141: * @param value Object Valor que será exibido pelo renderer
142: * @param isSelected boolean Indicando se a célula está selecionada
143: * @param hasFocus boolean indicando se a célual possui o foco
144: * @param row int Linha da célula
145: * @param column int Coluna da célula
146: * @return Retorna o Compoment responsável por renderizar o valor da célula
147: */
148: @Override
149: public Component getTableCellRendererComponent(JTable table,
150: Object value, boolean isSelected, boolean hasFocus,
151: int row, int column) {
152: super .getTableCellRendererComponent(table, value, isSelected,
153: hasFocus, row, column);
154:
155: setForeground(Color.BLACK);
156:
157: String valor = null;
158:
159: if (value != null) {
160: switch (formato) {//TODO Adicionar internacionalização
161: case DINHEIRO: {
162: try {
163:
164: valor = NumberFormat.getCurrencyInstance().format(
165: value);
166: if (Double.parseDouble(value.toString()) < 0)
167: setForeground(Color.RED);
168: else
169: setForeground(Color.BLACK);
170: } catch (Exception e) {
171: //Não Fazemos anda
172: }
173: break;
174: }
175: case PORCENTAGEM: {
176: NumberFormat formatter = NumberFormat
177: .getPercentInstance();
178: formatter.setMaximumFractionDigits(5);
179: formatter.setMinimumFractionDigits(5);
180: valor = formatter.format((Double.parseDouble(value
181: .toString()) / 100d));
182: setHorizontalAlignment(JLabel.RIGHT);
183: break;
184: }
185: case DATA: {
186: valor = DateFormat.getDateInstance(DateFormat.MEDIUM)
187: .format(value);
188: setHorizontalAlignment(JLabel.CENTER);
189: setToolTipText(new SimpleDateFormat("EEEEEE").format(value)); //$NON-NLS-1$
190: break;
191: }
192: case BOOLEANO: {
193: if (value instanceof Boolean)
194: if (((Boolean) value).booleanValue() == true) {
195: valor = ConfiguravelTableCellRendererMessages
196: .getMessages().getString("sim"); //$NON-NLS-1$
197: } else {
198: valor = ConfiguravelTableCellRendererMessages
199: .getMessages().getString("nao"); //$NON-NLS-1$
200: }
201: setHorizontalAlignment(JLabel.CENTER);
202: break;
203: }
204: case CPF:
205: case CNPJ: {
206: if (new CNPJ(value.toString()).isValido()) {
207: try {
208: MaskFormatter cnpj = new MaskFormatter(
209: "##.###.###/####-##"); //$NON-NLS-1$
210: cnpj.setValueContainsLiteralCharacters(false);
211: valor = cnpj.valueToString(value.toString());
212: setHorizontalAlignment(JLabel.RIGHT);
213: break;
214: } catch (ParseException pe) {
215: //TODO Excecao
216: pe.printStackTrace();
217: }
218: } else if (new CPF(value.toString()).isValido()) {
219: try {
220: MaskFormatter cpf = new MaskFormatter(
221: "###.###.###-##"); //$NON-NLS-1$
222: cpf.setValueContainsLiteralCharacters(false);
223: valor = cpf.valueToString(value.toString());
224: setHorizontalAlignment(JLabel.RIGHT);
225: break;
226: } catch (ParseException pe) {
227: //TODO Excecao
228: pe.printStackTrace();
229: }
230: }
231: }
232: }
233: }
234:
235: if (valor != null)
236: setValue(valor);
237:
238: return this;
239: }
240: }
|