001: /*
002: * BankWindow.java Criado em 15 de Fevereiro de 2004, 15:52
003: */
004:
005: package br.com.gfp.windows;
006:
007: import java.awt.BorderLayout;
008: import java.awt.Dimension;
009: import java.awt.GridBagConstraints;
010: import java.awt.Insets;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import java.sql.SQLException;
014: import java.text.ParseException;
015: import java.util.HashMap;
016:
017: import javax.swing.JButton;
018: import javax.swing.JFrame;
019: import javax.swing.JOptionPane;
020: import javax.swing.WindowConstants;
021: import javax.swing.event.ListSelectionEvent;
022: import javax.swing.event.ListSelectionListener;
023: import javax.swing.text.DefaultFormatterFactory;
024: import javax.swing.text.MaskFormatter;
025:
026: import br.com.gfp.dao.BankDAO;
027: import br.com.gfp.dao.GFPController;
028: import br.com.gfp.data.Bank;
029: import br.com.gfp.internationalization.ErrosDeDadosMessages;
030: import br.com.gfp.internationalization.TelaBancoMessages;
031: import br.com.gfp.reports.ReportFactory;
032: import br.com.gfp.util.SimpleLog;
033: import br.com.gfp.windows.componentes.GFPHeaderRenderer;
034: import br.com.gfpshare.beans.GeneralEventHandler;
035: import br.com.gfpshare.beans.ZFormattedTextField;
036: import br.com.gfpshare.beans.ZLabel;
037: import br.com.gfpshare.beans.ZTextField;
038: import br.com.gfpshare.beans.aplicativos.IconeFactory;
039: import br.com.gfpshare.beans.aplicativos.PainelTelaBasica;
040: import br.com.gfpshare.beans.table.DBTable;
041: import br.com.gfpshare.db.DAOListener;
042:
043: /**
044: * @author Igor Regis da Silva Simoes
045: */
046: public class BankWindow extends GFPInternalFrame<Bank> {
047:
048: private Bank bank = new Bank();
049:
050: private PainelTelaBasica telaPrincipal;
051:
052: private ZFormattedTextField jftfCodigo;
053:
054: private ZLabel jlCodigo;
055:
056: private ZLabel jlNome;
057:
058: private DBTable jtLancamentos;
059:
060: private ZTextField jtfNome;
061:
062: /**
063: * Cria um novo form TelaCadastroContas
064: * @param owner
065: */
066: public BankWindow(JFrame owner) {
067: controller = new BankDAO();
068: initComponents();
069: setSize(550, 360);
070: }
071:
072: public BankWindow(JFrame owner, DAOListener<Bank> listener) {
073: this (owner);
074: setListener(listener);
075: }
076:
077: /**
078: * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always
079: * regenerated by the Form Editor.
080: */
081: private void initComponents() {
082: setClosable(true);
083: setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
084: setIconifiable(true);
085: setMaximizable(true);
086: setResizable(true);
087: setTitle(TelaBancoMessages.getMessages().getString("bancos")); //$NON-NLS-1$
088: setFrameIcon(IconeFactory.getIconeFactory().getImageIcon(
089: IconeFactory.BANCO_PEQUENA));
090:
091: GridBagConstraints gridBagConstraints;
092:
093: telaPrincipal = new PainelTelaBasica();
094:
095: jtLancamentos = new DBTable(new Bank(), new GFPHeaderRenderer());
096:
097: jtLancamentos.hideColumn("Id"); //$NON-NLS-1$
098: jtLancamentos.hideColumn("EhDeSistema"); //$NON-NLS-1$
099: this .jtLancamentos.getColumn(jtLancamentos.getColumnName(1))
100: .setWidth(150);
101: this .jtLancamentos.getColumn(jtLancamentos.getColumnName(1))
102: .setMinWidth(100);
103:
104: this .jtLancamentos.getColumn(jtLancamentos.getColumnName(2))
105: .setWidth(150);
106: this .jtLancamentos.getColumn(jtLancamentos.getColumnName(2))
107: .setMinWidth(100);
108:
109: telaPrincipal.setJtDados(jtLancamentos);
110: telaPrincipal.addPrintButtonListener(new PrintListener());
111:
112: jtfNome = new ZTextField();
113: jlCodigo = new ZLabel();
114: jlNome = new ZLabel();
115: jftfCodigo = new ZFormattedTextField();
116:
117: jtLancamentos
118: .getSelectionModel()
119: .addListSelectionListener(
120: (ListSelectionListener) GeneralEventHandler
121: .makeProxy(
122: this ,
123: "loadFromTable", ListSelectionListener.class, "valueChanged")); //$NON-NLS-1$ //$NON-NLS-2$
124:
125: jtfNome.setMinimumSize(new Dimension(100, 24));
126: jtfNome.setPreferredSize(new Dimension(180, 26));
127: jtfNome.setToolTipText(TelaBancoMessages.getMessages()
128: .getString("NomeToolTip"));
129: gridBagConstraints = new GridBagConstraints();
130: gridBagConstraints.gridx = 1;
131: gridBagConstraints.gridy = 2;
132: gridBagConstraints.insets = new Insets(5, 5, 5, 5);
133: telaPrincipal.addComponent(jtfNome, gridBagConstraints,
134: PainelTelaBasica.DATA_COMPONENTS_AREA);
135:
136: jlCodigo.setText(TelaBancoMessages.getMessages().getString(
137: "codigo")); //$NON-NLS-1$
138: jlCodigo.setDisplayedMnemonic(TelaBancoMessages.getMessages()
139: .getString("CodigoMnemonic").charAt(0));
140: jlCodigo.setRequiredField(jftfCodigo);
141: jlCodigo.setValidationNotNull(true);
142: gridBagConstraints = new GridBagConstraints();
143: gridBagConstraints.gridx = 0;
144: gridBagConstraints.gridy = 0;
145: gridBagConstraints.insets = new Insets(5, 5, 5, 5);
146: telaPrincipal.addComponent(jlCodigo, gridBagConstraints,
147: PainelTelaBasica.DATA_COMPONENTS_AREA);
148:
149: jlNome.setText(TelaBancoMessages.getMessages()
150: .getString("nome")); //$NON-NLS-1$
151: jlNome.setDisplayedMnemonic(TelaBancoMessages.getMessages()
152: .getString("NomeMnemonic").charAt(0));
153: jlNome.setRequiredField(jtfNome);
154: jlNome.setValidationNotNull(true);
155: gridBagConstraints = new GridBagConstraints();
156: gridBagConstraints.gridx = 0;
157: gridBagConstraints.gridy = 2;
158: gridBagConstraints.insets = new Insets(5, 5, 5, 5);
159: telaPrincipal.addComponent(jlNome, gridBagConstraints,
160: PainelTelaBasica.DATA_COMPONENTS_AREA);
161:
162: try {
163: MaskFormatter formato = new MaskFormatter(TelaBancoMessages
164: .getMessages().getString("mascaraBanco")); //$NON-NLS-1$
165: formato.setValidCharacters(TelaBancoMessages.getMessages()
166: .getString("caracteresValidosBanco")); //$NON-NLS-1$
167: formato.setPlaceholderCharacter('0');
168: formato.setValueContainsLiteralCharacters(false);
169: jftfCodigo.setFormatterFactory(new DefaultFormatterFactory(
170: formato));
171: } catch (ParseException e) {
172: ((SimpleLog) GFPController.getGFPController().getContexto()
173: .get(GFPController.LOG))
174: .log("Erro ao aplicar mascara para codigo de banco!");
175: ((SimpleLog) GFPController.getGFPController().getContexto()
176: .get(GFPController.LOG)).log(e
177: .getLocalizedMessage());
178: ((SimpleLog) GFPController.getGFPController().getContexto()
179: .get(GFPController.LOG)).log(e);
180: }
181: jftfCodigo.setMinimumSize(new Dimension(30, 26));
182: jftfCodigo.setPreferredSize(new Dimension(50, 26));
183: jftfCodigo.setToolTipText(TelaBancoMessages.getMessages()
184: .getString("CodigoToolTip"));
185: gridBagConstraints = new GridBagConstraints();
186: gridBagConstraints.gridx = 1;
187: gridBagConstraints.gridy = 0;
188: gridBagConstraints.anchor = GridBagConstraints.WEST;
189: gridBagConstraints.insets = new Insets(5, 5, 5, 5);
190: telaPrincipal.addComponent(jftfCodigo, gridBagConstraints,
191: PainelTelaBasica.DATA_COMPONENTS_AREA);
192:
193: telaPrincipal
194: .getPainelBotoes()
195: .addActionListener(
196: "salvar", (ActionListener) GeneralEventHandler.makeProxy(this , "salvar", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
197: telaPrincipal
198: .getPainelBotoes()
199: .addActionListener(
200: "atualizar", (ActionListener) GeneralEventHandler.makeProxy(this , "atualizar", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
201: telaPrincipal
202: .getPainelBotoes()
203: .addActionListener(
204: "novo", (ActionListener) GeneralEventHandler.makeProxy(this , "novo", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
205: telaPrincipal
206: .getPainelBotoes()
207: .addActionListener(
208: "excluir", (ActionListener) GeneralEventHandler.makeProxy(this , "excluir", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
209: telaPrincipal
210: .getPainelBotoes()
211: .addActionListener(
212: "pesquisar", (ActionListener) GeneralEventHandler.makeProxy(this , "pesquisar", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
213: telaPrincipal
214: .getPainelBotoes()
215: .addActionListener(
216: "limpar", (ActionListener) GeneralEventHandler.makeProxy(this , "limpar", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
217: telaPrincipal
218: .getPainelBotoes()
219: .addActionListener(
220: "filtrar", (ActionListener) GeneralEventHandler.makeProxy(this , "filtrar", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
221: getContentPane().add(telaPrincipal, BorderLayout.CENTER);
222:
223: pack();
224: limpar(null);
225: }
226:
227: private class PrintListener implements ActionListener {
228: public void actionPerformed(final ActionEvent e) {
229: ((JButton) e.getSource()).setEnabled(false);
230: (new Thread() {
231: {
232: setPriority(Thread.MIN_PRIORITY);
233: setDaemon(true);
234: }
235:
236: @Override
237: public void run() {
238: try {
239: ReportFactory.makeReport("Bancos",
240: new HashMap());
241: } catch (Exception e1) {
242: ((SimpleLog) GFPController.getGFPController()
243: .getContexto().get(GFPController.LOG))
244: .log("Erro na geracao do relatorio de bancos!");
245: ((SimpleLog) GFPController.getGFPController()
246: .getContexto().get(GFPController.LOG))
247: .log(e1.getLocalizedMessage());
248: ((SimpleLog) GFPController.getGFPController()
249: .getContexto().get(GFPController.LOG))
250: .log(e1);
251: } finally {
252: ((JButton) e.getSource()).setEnabled(true);
253: }
254: }
255: }).start();
256: }
257: }
258:
259: /**
260: * @param e
261: */
262: public void salvar(ActionEvent e) {
263: if (bank == null) {
264: JOptionPane.showMessageDialog(this , TelaBancoMessages
265: .getMessages().getString("semDadosParaSalvar")); //$NON-NLS-1$
266: return;
267: }
268: loadBanco();
269: if (controller.isAdicionandoNovo()) {
270: try {
271: controller.adicionarNovo(bank);
272: } catch (SQLException sqle) {
273: JOptionPane.showMessageDialog(this , sqle.getMessage(),
274: ErrosDeDadosMessages.getMessages().getString(
275: "MessageTitle"),
276: JOptionPane.WARNING_MESSAGE);
277: telaPrincipal.getPainelBotoes().setOcorreuErro(true);
278: return;
279: }
280: close();
281: } else {
282: try {
283: controller.atualizar(bank);
284: } catch (SQLException sqle) {
285: JOptionPane.showMessageDialog(this , sqle.getMessage(),
286: ErrosDeDadosMessages.getMessages().getString(
287: "MessageTitle"),
288: JOptionPane.WARNING_MESSAGE);
289: telaPrincipal.getPainelBotoes().setOcorreuErro(true);
290: return;
291: }
292: }
293: bank = null;
294: refreshTabelaBancos();
295: }
296:
297: /**
298: * @param e
299: */
300: public void pesquisar(ActionEvent e) {
301: if (bank == null)
302: bank = new Bank(jtfNome.getText());
303:
304: loadBanco();
305: try {
306: bank = controller.getBy(bank);
307: } catch (SQLException sqle) {
308: ((SimpleLog) GFPController.getGFPController().getContexto()
309: .get(GFPController.LOG))
310: .log("Erro ao pesquisar banco: " + bank);
311: ((SimpleLog) GFPController.getGFPController().getContexto()
312: .get(GFPController.LOG)).log(sqle
313: .getLocalizedMessage());
314: ((SimpleLog) GFPController.getGFPController().getContexto()
315: .get(GFPController.LOG)).log(sqle);
316: bank = null;
317: }
318: editarBanco();
319: }
320:
321: /**
322: * @param e
323: */
324: public void excluir(ActionEvent e) {
325: if (bank == null) {
326: JOptionPane.showMessageDialog(this , TelaBancoMessages
327: .getMessages().getString("semDadosParaExcluir")); //$NON-NLS-1$
328: return;
329: }
330:
331: loadBanco();
332:
333: try {
334: controller.deletar(bank);
335: } catch (SQLException sqle) {
336: if (sqle.getErrorCode() == -8) {
337: JOptionPane.showMessageDialog(this , TelaBancoMessages
338: .getMessages().getString(
339: "existemContasParaEsteBanco"));
340: } else {
341: ((SimpleLog) GFPController.getGFPController()
342: .getContexto().get(GFPController.LOG))
343: .log("Erro ao deletar banco: " + bank);
344: ((SimpleLog) GFPController.getGFPController()
345: .getContexto().get(GFPController.LOG)).log(sqle
346: .getLocalizedMessage());
347: ((SimpleLog) GFPController.getGFPController()
348: .getContexto().get(GFPController.LOG))
349: .log(sqle);
350: }
351: }
352: bank = null;
353: refreshTabelaBancos();
354: }
355:
356: /**
357: * @param e
358: */
359: public void novo(ActionEvent e) {
360: bank = new Bank(jtfNome.getText());
361: controller.setAdicionandoNovo(true);
362: }
363:
364: /**
365: * @param e
366: */
367: public void atualizar(ActionEvent e) {
368: refreshTabelaBancos();
369: }
370:
371: /**
372: * @param e
373: */
374: public void limpar(ActionEvent e) {
375: telaPrincipal.showRequiredFields();
376: bank = null;
377: jftfCodigo.setText(""); //$NON-NLS-1$
378: telaPrincipal.getTextAreaDescricao().setText(""); //$NON-NLS-1$
379: jtfNome.setText(""); //$NON-NLS-1$
380: telaPrincipal.getPainelBotoes().dadosCarregados(false);
381: controller.setAdicionandoNovo(false);
382: }
383:
384: /**
385: * @param e
386: */
387: public void filtrar(ActionEvent e) {
388: bank = new Bank();
389: loadBanco();
390:
391: jtLancamentos.setDataType(bank);
392: try {
393: jtLancamentos.filter();
394: } catch (Exception e1) {
395: ((SimpleLog) GFPController.getGFPController().getContexto()
396: .get(GFPController.LOG))
397: .log("Erro na filtragem da tabela de bancos!");
398: ((SimpleLog) GFPController.getGFPController().getContexto()
399: .get(GFPController.LOG)).log(e1
400: .getLocalizedMessage());
401: ((SimpleLog) GFPController.getGFPController().getContexto()
402: .get(GFPController.LOG)).log(e1);
403: }
404: }
405:
406: /**
407: *
408: */
409: private void loadBanco() {
410: bank.setNome(jtfNome.getText());
411: if (jftfCodigo.getValue() != null)
412: bank.setCodigo(jftfCodigo.getValue().toString());
413: bank.setDescricao(telaPrincipal.getTextAreaDescricao()
414: .getText());
415: }
416:
417: /**
418: *
419: */
420: private void editarBanco() {
421: telaPrincipal.hideRequiredFields();
422: if (bank == null)
423: return;
424: jtfNome.setText(bank.getNome());
425: jftfCodigo.setValue(bank.getCodigo());
426: telaPrincipal.getTextAreaDescricao().setText(
427: bank.getDescricao());
428: telaPrincipal.getPainelBotoes().dadosCarregados(true);
429: }
430:
431: /**
432: *
433: */
434: private void refreshTabelaBancos() {
435: try {
436: jtLancamentos.loadDados();
437: } catch (Exception e) {
438: ((SimpleLog) GFPController.getGFPController().getContexto()
439: .get(GFPController.LOG))
440: .log("Erro na atualizacao da tabela de bancos!");
441: ((SimpleLog) GFPController.getGFPController().getContexto()
442: .get(GFPController.LOG)).log(e
443: .getLocalizedMessage());
444: ((SimpleLog) GFPController.getGFPController().getContexto()
445: .get(GFPController.LOG)).log(e);
446: }
447: }
448:
449: /**
450: * @param e
451: */
452: public void loadFromTable(ListSelectionEvent e) {
453: if (BankWindow.this .isVisible()) {
454: try {
455: bank = (Bank) jtLancamentos
456: .getPersistentObject(jtLancamentos
457: .getSelectedRow());
458: } catch (SQLException sqle) {
459: ((SimpleLog) GFPController.getGFPController()
460: .getContexto().get(GFPController.LOG))
461: .log("Erro ao carregar !");
462: ((SimpleLog) GFPController.getGFPController()
463: .getContexto().get(GFPController.LOG)).log(sqle
464: .getLocalizedMessage());
465: ((SimpleLog) GFPController.getGFPController()
466: .getContexto().get(GFPController.LOG))
467: .log(sqle);
468: }
469: editarBanco();
470: }
471: }
472: }
|