001: /*
002: * TelaCadastroContas.java Criado em 28 de Janeiro de 2004, 22:57
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:
015: import javax.swing.JFrame;
016: import javax.swing.JLabel;
017: import javax.swing.JOptionPane;
018: import javax.swing.JPasswordField;
019: import javax.swing.JTextField;
020: import javax.swing.WindowConstants;
021: import javax.swing.event.ListSelectionEvent;
022: import javax.swing.event.ListSelectionListener;
023:
024: import br.com.gfp.dao.GFPController;
025: import br.com.gfp.internationalization.ErrosDeDadosMessages;
026: import br.com.gfp.internationalization.TelaUsuarioMessages;
027: import br.com.gfp.util.SimpleLog;
028: import br.com.gfp.windows.componentes.GFPHeaderRenderer;
029: import br.com.gfpshare.beans.GeneralEventHandler;
030: import br.com.gfpshare.beans.aplicativos.PainelTelaBasica;
031: import br.com.gfpshare.beans.table.DBTable;
032: import br.com.gfpshare.controllers.UsuarioController;
033: import br.com.gfpshare.security.Usuario;
034:
035: /**
036: * @author Igor Regis da Silva Simoes
037: */
038: public class UsersWindow extends GFPInternalFrame<Usuario> {
039: private Usuario usuario = new Usuario();
040:
041: private PainelTelaBasica telaPrincipal;
042:
043: private JLabel jlSenha;
044:
045: private JPasswordField jpfSenha;
046:
047: private JLabel jlConfirmaSenha;
048:
049: private JPasswordField jpfConfirmaSenha;
050:
051: private JLabel jlNome;
052:
053: private DBTable jtUsuarios;
054:
055: private JTextField jtfNome;
056:
057: /**
058: * Cria um novo form TelaCadastroContas
059: *
060: * @param owner
061: */
062: public UsersWindow(JFrame owner) {
063: controller = new UsuarioController();
064: initComponents();
065: setSize(700, 450);
066: }
067:
068: /**
069: * 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
070: * regenerated by the Form Editor.
071: */
072: private void initComponents() {
073: setClosable(true);
074: setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
075: setIconifiable(true);
076: setMaximizable(true);
077: setResizable(true);
078: setTitle(TelaUsuarioMessages.getMessages()
079: .getString("usuarios")); //$NON-NLS-1$
080: // setFrameIcon(IconeFactory.getIconeFactory().getImageIcon(IconeFactory.TIPO_LANCAMENTO_PEQUENA));
081:
082: GridBagConstraints gridBagConstraints;
083:
084: jtUsuarios = new DBTable(new Usuario(), new GFPHeaderRenderer());
085: jtUsuarios.hideColumn("Id"); //$NON-NLS-1$
086: jtUsuarios.hideColumn("Senha"); //$NON-NLS-1$
087: jtUsuarios
088: .getSelectionModel()
089: .addListSelectionListener(
090: (ListSelectionListener) GeneralEventHandler
091: .makeProxy(
092: this ,
093: "loadFromTable", ListSelectionListener.class, "valueChanged")); //$NON-NLS-1$ //$NON-NLS-2$
094:
095: telaPrincipal = new PainelTelaBasica();
096: telaPrincipal.setJtDados(jtUsuarios);
097:
098: jtfNome = new JTextField();
099: jlNome = new JLabel();
100: jlSenha = new JLabel();
101: jpfSenha = new JPasswordField();
102: jlConfirmaSenha = new JLabel();
103: jpfConfirmaSenha = new JPasswordField();
104:
105: jtfNome.setMinimumSize(new Dimension(100, 24));
106: jtfNome.setPreferredSize(new Dimension(100, 26));
107: jtfNome.setToolTipText(TelaUsuarioMessages.getMessages()
108: .getString("NomeToolTip"));
109: gridBagConstraints = new GridBagConstraints();
110: gridBagConstraints.gridx = 1;
111: gridBagConstraints.gridy = 1;
112: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
113: gridBagConstraints.anchor = GridBagConstraints.WEST;
114: gridBagConstraints.insets = new Insets(5, 5, 5, 5);
115: telaPrincipal.addComponent(jtfNome, gridBagConstraints,
116: PainelTelaBasica.DATA_COMPONENTS_AREA);
117:
118: jlNome.setText(TelaUsuarioMessages.getMessages().getString(
119: "nome")); //$NON-NLS-1$
120: jlNome.setDisplayedMnemonic(TelaUsuarioMessages.getMessages()
121: .getString("NomeMnemonic").charAt(0));
122: gridBagConstraints = new GridBagConstraints();
123: gridBagConstraints.gridx = 0;
124: gridBagConstraints.gridy = 1;
125: gridBagConstraints.anchor = GridBagConstraints.WEST;
126: gridBagConstraints.insets = new Insets(5, 5, 5, 5);
127: telaPrincipal.addComponent(jlNome, gridBagConstraints,
128: PainelTelaBasica.DATA_COMPONENTS_AREA);
129:
130: jlSenha.setText(TelaUsuarioMessages.getMessages().getString(
131: "senha")); //$NON-NLS-1$
132: jlSenha.setDisplayedMnemonic(TelaUsuarioMessages.getMessages()
133: .getString("SenhaMnemonic").charAt(0));
134: gridBagConstraints = new GridBagConstraints();
135: gridBagConstraints.gridx = 0;
136: gridBagConstraints.gridy = 2;
137: gridBagConstraints.anchor = GridBagConstraints.WEST;
138: gridBagConstraints.insets = new Insets(5, 5, 5, 5);
139: telaPrincipal.addComponent(jlSenha, gridBagConstraints,
140: PainelTelaBasica.DATA_COMPONENTS_AREA);
141:
142: jpfSenha.setMinimumSize(new Dimension(100, 25));
143: jpfSenha.setPreferredSize(new Dimension(150, 25));
144: jpfSenha.setToolTipText(TelaUsuarioMessages.getMessages()
145: .getString("SenhaToolTop"));
146: gridBagConstraints = new GridBagConstraints();
147: gridBagConstraints.gridx = 1;
148: gridBagConstraints.gridy = 2;
149: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
150: gridBagConstraints.insets = new Insets(5, 5, 5, 5);
151: telaPrincipal.addComponent(jpfSenha, gridBagConstraints,
152: PainelTelaBasica.DATA_COMPONENTS_AREA);
153:
154: jlConfirmaSenha.setText(TelaUsuarioMessages.getMessages()
155: .getString("confirmarSenha")); //$NON-NLS-1$
156: jlConfirmaSenha.setDisplayedMnemonic(TelaUsuarioMessages
157: .getMessages().getString("ConfirmarSenhaMenmonic")
158: .charAt(0));
159: gridBagConstraints = new GridBagConstraints();
160: gridBagConstraints.gridx = 0;
161: gridBagConstraints.gridy = 3;
162: gridBagConstraints.anchor = GridBagConstraints.WEST;
163: gridBagConstraints.insets = new Insets(5, 5, 5, 5);
164: telaPrincipal.addComponent(jlConfirmaSenha, gridBagConstraints,
165: PainelTelaBasica.DATA_COMPONENTS_AREA);
166:
167: jpfConfirmaSenha.setMinimumSize(new Dimension(100, 25));
168: jpfConfirmaSenha.setPreferredSize(new Dimension(150, 25));
169: jpfConfirmaSenha.setToolTipText(TelaUsuarioMessages
170: .getMessages().getString("ConfirmarSenhaToolTip"));
171: gridBagConstraints = new GridBagConstraints();
172: gridBagConstraints.gridx = 1;
173: gridBagConstraints.gridy = 3;
174: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
175: gridBagConstraints.insets = new Insets(5, 5, 5, 5);
176: telaPrincipal.addComponent(jpfConfirmaSenha,
177: gridBagConstraints,
178: PainelTelaBasica.DATA_COMPONENTS_AREA);
179:
180: telaPrincipal
181: .getPainelBotoes()
182: .addActionListener(
183: "salvar", (ActionListener) GeneralEventHandler.makeProxy(this , "salvar", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
184: telaPrincipal
185: .getPainelBotoes()
186: .addActionListener(
187: "atualizar", (ActionListener) GeneralEventHandler.makeProxy(this , "atualizar", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
188: telaPrincipal
189: .getPainelBotoes()
190: .addActionListener(
191: "novo", (ActionListener) GeneralEventHandler.makeProxy(this , "novo", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
192: telaPrincipal
193: .getPainelBotoes()
194: .addActionListener(
195: "excluir", (ActionListener) GeneralEventHandler.makeProxy(this , "excluir", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
196: telaPrincipal
197: .getPainelBotoes()
198: .addActionListener(
199: "pesquisar", (ActionListener) GeneralEventHandler.makeProxy(this , "pesquisar", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
200: telaPrincipal
201: .getPainelBotoes()
202: .addActionListener(
203: "limpar", (ActionListener) GeneralEventHandler.makeProxy(this , "limpar", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
204: telaPrincipal
205: .getPainelBotoes()
206: .addActionListener(
207: "filtrar", (ActionListener) GeneralEventHandler.makeProxy(this , "filtrar", ActionListener.class, "actionPerformed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
208: getContentPane().add(telaPrincipal, BorderLayout.CENTER);
209:
210: pack();
211: }
212:
213: /**
214: * @param e
215: */
216: public void atualizar(ActionEvent e) {
217: refreshTabelaUsuarios();
218: }
219:
220: /**
221: * @param e
222: */
223: public void pesquisar(ActionEvent e) {
224: if (usuario == null)
225: usuario = new Usuario();
226: loadUsuario();
227: try {
228: usuario = controller.getBy(usuario);
229: } catch (SQLException sqle) {
230: ((SimpleLog) GFPController.getGFPController().getContexto()
231: .get(GFPController.LOG))
232: .log("Erro ao pesquisar usuario: " + usuario);
233: ((SimpleLog) GFPController.getGFPController().getContexto()
234: .get(GFPController.LOG)).log(sqle
235: .getLocalizedMessage());
236: ((SimpleLog) GFPController.getGFPController().getContexto()
237: .get(GFPController.LOG)).log(sqle);
238: usuario = null;
239: }
240: editarUsuario();
241: }
242:
243: /**
244: * @param e
245: */
246: public void excluir(ActionEvent e) {
247: if (usuario == null) {
248: JOptionPane.showMessageDialog(this , TelaUsuarioMessages
249: .getMessages().getString("semDadosParaExcluir")); //$NON-NLS-1$
250: return;
251: }
252:
253: loadUsuario();
254:
255: try {
256: controller.deletar(usuario);
257: } catch (SQLException sqle) {
258: if (sqle.getErrorCode() == -9645)
259: JOptionPane.showMessageDialog(this , sqle.getMessage());
260: else {
261: ((SimpleLog) GFPController.getGFPController()
262: .getContexto().get(GFPController.LOG))
263: .log("Erro ao deletar ususario: " + usuario);
264: ((SimpleLog) GFPController.getGFPController()
265: .getContexto().get(GFPController.LOG)).log(sqle
266: .getLocalizedMessage());
267: ((SimpleLog) GFPController.getGFPController()
268: .getContexto().get(GFPController.LOG))
269: .log(sqle);
270: }
271: }
272: usuario = null;
273: refreshTabelaUsuarios();
274: }
275:
276: /**
277: * @param e
278: */
279: public void salvar(ActionEvent e) {
280: if (usuario == null) {//Todo Rever Mensagem
281: JOptionPane.showMessageDialog(this , TelaUsuarioMessages
282: .getMessages().getString("semDadosParaSalvar")); //$NON-NLS-1$
283: return;
284: }
285: if (!isSenhaOk()) {
286: JOptionPane.showMessageDialog(this , TelaUsuarioMessages
287: .getMessages().getString(
288: "senhaDigitadaIncorretamente")); //$NON-NLS-1$
289: return;
290: }
291: loadUsuario();
292: if (controller.isAdicionandoNovo()) {
293: try {
294: controller.adicionarNovo(usuario);
295: } catch (SQLException sqle) {
296: JOptionPane.showMessageDialog(this , sqle.getMessage(),
297: ErrosDeDadosMessages.getMessages().getString(
298: "MessageTitle"),
299: JOptionPane.WARNING_MESSAGE);
300: telaPrincipal.getPainelBotoes().setOcorreuErro(true);
301: return;
302: }
303: } else {
304: try {
305: controller.atualizar(usuario);
306: } catch (SQLException sqle) {
307: if (sqle.getErrorCode() == -9645)
308: JOptionPane.showMessageDialog(this , sqle
309: .getMessage());
310: else
311: JOptionPane.showMessageDialog(this , sqle
312: .getMessage(), ErrosDeDadosMessages
313: .getMessages().getString("MessageTitle"),
314: JOptionPane.WARNING_MESSAGE);
315: telaPrincipal.getPainelBotoes().setOcorreuErro(true);
316: return;
317: }
318: }
319: usuario = null;
320: refreshTabelaUsuarios();
321: }
322:
323: /**
324: * @param e
325: */
326: public void novo(ActionEvent e) {
327: usuario = new Usuario(jtfNome.getText());
328: controller.setAdicionandoNovo(true);
329: }
330:
331: /**
332: * @param e
333: */
334: public void limpar(ActionEvent e) {
335: telaPrincipal.showRequiredFields();
336: usuario = null;
337: jpfSenha.setText(""); //$NON-NLS-1$
338: jpfConfirmaSenha.setText(""); //$NON-NLS-1$
339: jtfNome.setText(""); //$NON-NLS-1$
340: telaPrincipal.getTextAreaDescricao().setText(""); //$NON-NLS-1$
341: controller.setAdicionandoNovo(false);
342: telaPrincipal.getPainelBotoes().dadosCarregados(false);
343: }
344:
345: /**
346: * @param e
347: */
348: public void filtrar(ActionEvent e) {
349: usuario = new Usuario();
350: loadUsuario();
351:
352: jtUsuarios.setDataType(usuario);
353: try {
354: jtUsuarios.filter();
355: } catch (Exception e1) {
356: ((SimpleLog) GFPController.getGFPController().getContexto()
357: .get(GFPController.LOG))
358: .log("Erro ao filtar tabela de usuarios: "
359: + usuario);
360: ((SimpleLog) GFPController.getGFPController().getContexto()
361: .get(GFPController.LOG)).log(e1
362: .getLocalizedMessage());
363: ((SimpleLog) GFPController.getGFPController().getContexto()
364: .get(GFPController.LOG)).log(e1);
365: }
366: }
367:
368: /**
369: *
370: */
371: private void loadUsuario() {
372: usuario.setNome(jtfNome.getText());
373: usuario.setSenha(new String(jpfSenha.getPassword()));
374: usuario.setDescricao(telaPrincipal.getTextAreaDescricao()
375: .getText());
376: }
377:
378: /**
379: *
380: */
381: private void editarUsuario() {
382: if (usuario != null) {
383: jtfNome.setText(usuario.getNome());
384: telaPrincipal.getTextAreaDescricao().setText(
385: usuario.getDescricao());
386: telaPrincipal.getPainelBotoes().dadosCarregados(true);
387: }
388: }
389:
390: private boolean isSenhaOk() {
391: String senha = new String(jpfSenha.getPassword());
392: String confirmacao = new String(jpfConfirmaSenha.getPassword());
393: return senha.equals(confirmacao);
394: }
395:
396: /**
397: *
398: */
399: private void refreshTabelaUsuarios() {
400: try {
401: jtUsuarios.loadDados();
402: } catch (Exception e) {
403: ((SimpleLog) GFPController.getGFPController().getContexto()
404: .get(GFPController.LOG))
405: .log("Erro ao atualizar tabela de usuarios");
406: ((SimpleLog) GFPController.getGFPController().getContexto()
407: .get(GFPController.LOG)).log(e
408: .getLocalizedMessage());
409: ((SimpleLog) GFPController.getGFPController().getContexto()
410: .get(GFPController.LOG)).log(e);
411: }
412: }
413:
414: /**
415: * @param e
416: */
417: public void loadFromTable(ListSelectionEvent e) {
418: if (UsersWindow.this .isVisible()) {
419: try {
420: usuario = (Usuario) jtUsuarios
421: .getPersistentObject(jtUsuarios
422: .getSelectedRow());
423: } catch (SQLException sqle) {
424: ((SimpleLog) GFPController.getGFPController()
425: .getContexto().get(GFPController.LOG))
426: .log("Erro ao carregar usuario da tabela");
427: ((SimpleLog) GFPController.getGFPController()
428: .getContexto().get(GFPController.LOG)).log(sqle
429: .getLocalizedMessage());
430: ((SimpleLog) GFPController.getGFPController()
431: .getContexto().get(GFPController.LOG))
432: .log(sqle);
433: }
434: editarUsuario();
435: }
436: }
437: }
|