001: /*
002: * Created on 22/02/2006
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2005 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.windows.views;
024:
025: import java.awt.BorderLayout;
026: import java.awt.GridBagConstraints;
027: import java.awt.GridBagLayout;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.sql.SQLException;
031: import java.text.DateFormat;
032: import java.util.ArrayList;
033: import java.util.Calendar;
034: import java.util.Date;
035:
036: import javax.swing.DefaultListModel;
037: import javax.swing.JButton;
038: import javax.swing.JList;
039: import javax.swing.JPanel;
040: import javax.swing.JScrollPane;
041:
042: import org.flexdock.docking.DockingConstants;
043: import org.flexdock.view.View;
044:
045: import br.com.gfp.dao.CreditCardDAO;
046: import br.com.gfp.data.CreditCard;
047: import br.com.gfp.internationalization.ViewsMessages;
048: import br.com.gfpshare.beans.DBComboBox;
049: import br.com.gfpshare.beans.ZCalendarTextField;
050: import br.com.gfpshare.beans.aplicativos.IconeFactory;
051: import br.com.gfpshare.db.SQLCondition;
052: import br.com.gfpshare.db.SQLCondition.Condicao;
053:
054: /**
055: * Esta é a view de help, usada para visualizar a ajuda do GFP
056: * @author Igor Regis da Silva Simoes
057: * @since 22/02/2006
058: */
059: public class CreditCardBestDayView extends View {
060: /**
061: * Cartões de crédito a serem consultados
062: */
063: private ArrayList<CreditCard> cartoesCredito = null;
064:
065: /**
066: * Combo que permite selecionar um cartão de crédito a ser consultado
067: */
068: private DBComboBox cartoes = null;
069:
070: /**
071: * Inicio da consulta, o default e o mes atual
072: */
073: private ZCalendarTextField inicio = null;
074:
075: /**
076: * Fim da consulta o default e o mes atual + 12
077: */
078: private ZCalendarTextField fim = null;
079:
080: /**
081: * Paine que exibirá a lista com o resultado da consulta
082: */
083: private Report resultado = null;
084:
085: /**
086: * Cria uma instancia dea view de help
087: */
088: public CreditCardBestDayView() {
089: super (ViewsMessages.getMessages()
090: .getString("BestCreditCardDay"), ViewsMessages
091: .getMessages().getString("BestCreditCardDay"),
092: ViewsMessages.getMessages().getString(
093: "BestCreditCardDay"));
094: setIcon(IconeFactory.getIconeFactory().getScaledImage(
095: "/icones/16x16/info.png", 10));
096: addAction(DockingConstants.CLOSE_ACTION);
097: addAction(DockingConstants.PIN_ACTION);
098: setContentPane(createMyContentPane());
099: }
100:
101: /**
102: * Inicializamos o painel de conteúdo do help
103: * @return JPanel
104: */
105: private JPanel createMyContentPane() {
106: JPanel content = new JPanel();
107: content.setLayout(new BorderLayout());
108:
109: JPanel north = new JPanel();
110: north.setLayout(new GridBagLayout());
111:
112: cartoes = new DBComboBox(new CreditCard());
113: GridBagConstraints pos = new GridBagConstraints();
114: pos.gridx = 0;
115: pos.gridy = 0;
116: pos.weightx = 1;
117: pos.gridwidth = 2;
118: pos.fill = GridBagConstraints.HORIZONTAL;
119: north.add(cartoes, pos);
120:
121: inicio = new ZCalendarTextField();
122: inicio.setDate(new Date());
123: pos = new GridBagConstraints();
124: pos.gridx = 0;
125: pos.gridy = 1;
126: pos.weightx = 0.5;
127: pos.fill = GridBagConstraints.HORIZONTAL;
128: north.add(inicio, pos);
129:
130: fim = new ZCalendarTextField();
131: Calendar dataFim = Calendar.getInstance();
132: dataFim.setTime(inicio.getDate());
133: dataFim.add(Calendar.MONTH, 12);
134: fim.setDate(dataFim.getTime());
135: pos = new GridBagConstraints();
136: pos.gridx = 1;
137: pos.gridy = 1;
138: pos.weightx = 0.5;
139: pos.fill = GridBagConstraints.HORIZONTAL;
140: north.add(fim, pos);
141:
142: JButton limpar = new JButton(ViewsMessages.getMessages()
143: .getString("limpar"));
144: limpar.addActionListener(new ActionListener() {
145: public void actionPerformed(ActionEvent e) {
146: cartoes.setSelectedItem(null);
147: inicio.setDate(new Date());
148:
149: Calendar dataFim = Calendar.getInstance();
150: dataFim.setTime(inicio.getDate());
151: dataFim.add(Calendar.MONTH, 12);
152: fim.setDate(dataFim.getTime());
153: }
154: });
155: pos = new GridBagConstraints();
156: pos.gridx = 0;
157: pos.gridy = 2;
158: pos.weightx = 0.5;
159: pos.fill = GridBagConstraints.HORIZONTAL;
160: north.add(limpar, pos);
161:
162: JButton gerar = new JButton(ViewsMessages.getMessages()
163: .getString("gerar"));
164: gerar.addActionListener(new ActionListener() {
165: public void actionPerformed(ActionEvent e) {
166: gerarRelatorio();
167: resultado.reload();
168: }
169: });
170: pos = new GridBagConstraints();
171: pos.gridx = 1;
172: pos.gridy = 2;
173: pos.weightx = 0.5;
174: pos.fill = GridBagConstraints.HORIZONTAL;
175: north.add(gerar, pos);
176:
177: content.add(north, BorderLayout.NORTH);
178:
179: resultado = new Report();
180: content.add(resultado, BorderLayout.CENTER);
181: return content;
182: }
183:
184: /**
185: * Gera o relatorio buscando os dados que deverão ser exibidos na tela
186: */
187: private void gerarRelatorio() {
188: CreditCardDAO controller = new CreditCardDAO();
189: cartoesCredito = null;
190: try {
191: if (cartoes.getSelectedItem() != null)
192: cartoesCredito = controller.getAllBy(new CreditCard(
193: ((CreditCard) cartoes
194: .getPersistentObject(cartoes
195: .getSelectedIndex())).getId()));
196: else {
197: CreditCard creditCard = new CreditCard();
198: creditCard.addCondicaoExtra(new SQLCondition<Object>(
199: "DataFechamento", Condicao.IS_NULL));
200: cartoesCredito = controller.getAllBy(creditCard);
201: }
202: } catch (SQLException e) {
203: // TODO Auto-generated catch block
204: e.printStackTrace();
205: }
206: }
207:
208: /**
209: * Esta classe é responsável por imprimir o relatório de melhor dia para compras
210: * com cartão na tela.
211: * @author Igor Regis da Silva Simoes
212: * @since 22/02/2006
213: */
214: private class Report extends JPanel {
215: /**
216: * Exibe os resultados em forma de lista
217: */
218: private JList resultado = new JList();
219:
220: /**
221: *
222: */
223: public Report() {
224: setLayout(new BorderLayout());
225: add(new JScrollPane(resultado), BorderLayout.CENTER);
226: }
227:
228: /**
229: *
230: */
231: public void reload() {
232: if (cartoesCredito == null || cartoesCredito.size() == 0)
233: return;
234: //Calculamos os meses de intervalo
235: Calendar inicio = Calendar.getInstance();
236: inicio.setTime(CreditCardBestDayView.this .inicio.getDate());
237:
238: Calendar fim = Calendar.getInstance();
239: fim.setTime(CreditCardBestDayView.this .fim.getDate());
240:
241: //Imprimimos o titulo do relatorio
242: DefaultListModel model = new DefaultListModel();
243: model.addElement(ViewsMessages.getMessages().getString(
244: "ListaCartoesTitulo"));
245: model.addElement("");
246:
247: //Formatador da data a ser exebida
248: DateFormat formater = DateFormat
249: .getDateInstance(DateFormat.SHORT);
250: //formater.format(new Date());
251:
252: //Imprimimos os cartões
253: for (CreditCard cartao : cartoesCredito) {
254: model.addElement("");
255: model.addElement(cartao.getDescricao());
256: model.addElement("");
257:
258: inicio.setTime(CreditCardBestDayView.this .inicio
259: .getDate());
260: Calendar coluna2 = Calendar.getInstance();
261: coluna2.setTime(inicio.getTime());
262: coluna2.add(Calendar.MONTH, 1);
263:
264: Calendar coluna3 = Calendar.getInstance();
265: coluna3.setTime(inicio.getTime());
266: coluna3.add(Calendar.MONTH, 2);
267:
268: while (inicio.get(Calendar.YEAR) <= fim
269: .get(Calendar.YEAR)) {
270: String um = " ";
271: String dois = " ";
272: String tres = " ";
273: if (inicio.get(Calendar.MONTH) <= fim
274: .get(Calendar.MONTH)
275: || inicio.get(Calendar.YEAR) <= fim
276: .get(Calendar.YEAR))
277: um = formater.format(cartao
278: .getMelhorDiaParaComprar(inicio
279: .getTime()));
280: if (coluna2.get(Calendar.MONTH) <= fim
281: .get(Calendar.MONTH)
282: || inicio.get(Calendar.YEAR) <= fim
283: .get(Calendar.YEAR))
284: dois = formater.format(cartao
285: .getMelhorDiaParaComprar(coluna2
286: .getTime()));
287: if (coluna3.get(Calendar.MONTH) <= fim
288: .get(Calendar.MONTH)
289: || inicio.get(Calendar.YEAR) <= fim
290: .get(Calendar.YEAR))
291: tres = formater.format(cartao
292: .getMelhorDiaParaComprar(coluna3
293: .getTime()));
294:
295: model.addElement(um + " " + dois + " " + tres);
296: if (um.equals(" ") || dois.equals(" ")
297: || tres.equals(" "))
298: break;
299: inicio.add(Calendar.MONTH, 3);
300: coluna2.add(Calendar.MONTH, 3);
301: coluna3.add(Calendar.MONTH, 3);
302: }
303: }
304: resultado.setModel(model);
305: }
306: }
307: }
|