KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > administrator > editors > UserEditor


1 /*
2  Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  Redistribution and use in source and binary forms, with or without modification, is permitted
4  provided that the following conditions are met:
5  - Redistributions of source code must retain the above copyright notice, this list of conditions
6  and the following disclaimer.
7  - Redistributions in binary form must reproduce the above copyright notice, this list of
8  conditions and the following disclaimer in the documentation and/or other materials
9  provided with the distribution.
10  - All advertising materials mentioning features or use of this software must display the
11  following acknowledgment: "This product includes Djeneric."
12  - Products derived from this software may not be called "Djeneric" nor may
13  "Djeneric" appear in their names without prior written permission of Genimen BV.
14  - Redistributions of any form whatsoever must retain the following acknowledgment: "This
15  product includes Djeneric."
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
17  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
20  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */

28 package com.genimen.djeneric.tools.administrator.editors;
29
30 import java.awt.BorderLayout JavaDoc;
31 import java.awt.Dimension JavaDoc;
32 import java.awt.event.ActionEvent JavaDoc;
33 import java.awt.event.MouseEvent JavaDoc;
34 import java.util.ArrayList JavaDoc;
35
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JCheckBox JavaDoc;
38 import javax.swing.JLabel JavaDoc;
39 import javax.swing.JOptionPane JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import javax.swing.JPasswordField JavaDoc;
42 import javax.swing.JScrollPane JavaDoc;
43 import javax.swing.JTabbedPane JavaDoc;
44 import javax.swing.JTable JavaDoc;
45 import javax.swing.JTextField JavaDoc;
46
47 import com.genimen.djeneric.language.Messages;
48 import com.genimen.djeneric.repository.DjModelView;
49 import com.genimen.djeneric.repository.DjPersistenceManager;
50 import com.genimen.djeneric.repository.DjSession;
51 import com.genimen.djeneric.repository.DjUser;
52 import com.genimen.djeneric.repository.DjUserContextAssociation;
53 import com.genimen.djeneric.tools.administrator.Administrator;
54 import com.genimen.djeneric.tools.administrator.helpers.AdminPanel;
55 import com.genimen.djeneric.tools.administrator.nodes.AdministratorTreeNode;
56 import com.genimen.djeneric.ui.DjTable;
57 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
58 import com.genimen.djeneric.ui.Util;
59 import com.genimen.djeneric.util.DjLogger;
60
61 public class UserEditor extends AdminEditor
62 {
63   private static final long serialVersionUID = 1L;
64   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
65   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
66   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
67   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
68   JButton JavaDoc _butOk = new JButton JavaDoc();
69   JButton JavaDoc _butCancel = new JButton JavaDoc();
70   AdministratorTreeNode _theNode;
71   DjUser _user;
72   JPanel JavaDoc jPanel5 = new JPanel JavaDoc();
73   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
74   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
75   JCheckBox JavaDoc _chkAdmin = new JCheckBox JavaDoc();
76   DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout();
77   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
78   JTextField JavaDoc _edtName = new JTextField JavaDoc();
79   JTextField JavaDoc _edtCode = new JTextField JavaDoc();
80   JLabel JavaDoc jLabel2 = new JLabel JavaDoc();
81   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
82   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
83   AccessTableModel _model;
84   ModelViewTableModel _viewModel;
85   ArrayList JavaDoc _allAssocs = new ArrayList JavaDoc();
86   ArrayList JavaDoc _allViews = new ArrayList JavaDoc();
87   JTabbedPane JavaDoc _tabs = new JTabbedPane JavaDoc();
88   JScrollPane JavaDoc _scrAccess = new JScrollPane JavaDoc();
89   DjTable _grdAccess = new DjTable();
90   JScrollPane JavaDoc _scrViews = new JScrollPane JavaDoc();
91   DjTable _grdViews = new DjTable();
92   JCheckBox JavaDoc _chkModeler = new JCheckBox JavaDoc();
93   JLabel JavaDoc jLabel3 = new JLabel JavaDoc();
94   JPanel JavaDoc _pnlPw = new JPanel JavaDoc();
95   BorderLayout JavaDoc borderLayout4 = new BorderLayout JavaDoc();
96   JPasswordField JavaDoc _edtPassword = new JPasswordField JavaDoc();
97   JButton JavaDoc _butPw = new JButton JavaDoc();
98   JButton JavaDoc _butDown = new JButton JavaDoc();
99   JButton JavaDoc _butDelete = new JButton JavaDoc();
100   JButton JavaDoc _butBottom = new JButton JavaDoc();
101   JButton JavaDoc _butFirst = new JButton JavaDoc();
102   JButton JavaDoc _butCreate = new JButton JavaDoc();
103   JButton JavaDoc _butUp = new JButton JavaDoc();
104   JPanel JavaDoc jPanel6 = new JPanel JavaDoc();
105
106   public UserEditor(AdminPanel adminPanel, AdministratorTreeNode theNode, DjUser user)
107   {
108     super(adminPanel);
109     _user = user;
110     _theNode = theNode;
111
112     try
113     {
114       jbInit();
115       _edtCode.setText(user.getCode());
116       _edtName.setText(user.getName());
117       _edtPassword.setText(user.getPassword());
118       _chkAdmin.setSelected(user.isAdministrator());
119       _chkModeler.setSelected(user.isModeler());
120
121       DjUserContextAssociation[] dca = user.getContextAssociations();
122       for (int i = 0; i < dca.length; i++)
123       {
124         _allAssocs.add(dca[i]);
125       }
126       _model = new AccessTableModel(adminPanel.getPersistenceManager().getContextManager(), _allAssocs, user);
127       _grdAccess.setModel(_model);
128
129       DjModelView[] views = user.getViews();
130       for (int i = 0; i < views.length; i++)
131       {
132         _allViews.add(views[i]);
133       }
134       _viewModel = new ModelViewTableModel(adminPanel.getPersistenceManager(), _allViews, user);
135       _grdViews.setModel(_viewModel);
136       tableFirst(_grdAccess);
137       tableFirst(_grdViews);
138     }
139     catch (Exception JavaDoc ex)
140     {
141       DjLogger.log(ex);
142     }
143   }
144
145   void jbInit() throws Exception JavaDoc
146   {
147     this.setLayout(borderLayout1);
148     jPanel3.setLayout(borderLayout2);
149     _butOk.setText(Messages.getString("global.Ok"));
150     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
151     {
152       public void actionPerformed(ActionEvent JavaDoc e)
153       {
154         jButton1_actionPerformed(e);
155       }
156     });
157     _butCancel.setText(Messages.getString("global.Cancel"));
158     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
159     {
160       public void actionPerformed(ActionEvent JavaDoc e)
161       {
162         jButton2_actionPerformed(e);
163       }
164     });
165     jPanel5.setLayout(borderLayout3);
166     jPanel1.setLayout(verticalFlowLayout1);
167     _chkAdmin.setPreferredSize(new Dimension JavaDoc(98, 21));
168     _chkAdmin.setText(Messages.getString("UserEditor.Administrator"));
169     jLabel2.setPreferredSize(new Dimension JavaDoc(34, 21));
170     jLabel2.setText(Messages.getString("global.Name"));
171     jLabel1.setPreferredSize(new Dimension JavaDoc(41, 21));
172     jLabel1.setText(Messages.getString("global.Code"));
173     jPanel2.setLayout(verticalFlowLayout2);
174     _scrAccess.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
175     {
176       public void mouseClicked(MouseEvent JavaDoc e)
177       {
178         _scrAccess_mouseClicked(e);
179       }
180     });
181     _scrViews.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
182     {
183       public void mouseClicked(MouseEvent JavaDoc e)
184       {
185         _scrViews_mouseClicked(e);
186       }
187     });
188     _chkModeler.setText(Messages.getString("UserEditor.Modeler"));
189     _chkModeler.setPreferredSize(new Dimension JavaDoc(98, 21));
190     jLabel3.setPreferredSize(new Dimension JavaDoc(60, 21));
191     jLabel3.setText(Messages.getString("global.Password"));
192     _pnlPw.setLayout(borderLayout4);
193     _butPw.setPreferredSize(new Dimension JavaDoc(65, 21));
194     _butPw.setText(Messages.getString("UserEditor.Set"));
195     _butPw.addActionListener(new java.awt.event.ActionListener JavaDoc()
196     {
197       public void actionPerformed(ActionEvent JavaDoc e)
198       {
199         _butPw_actionPerformed(e);
200       }
201     });
202     _edtPassword.setEditable(false);
203     _butDown.setPreferredSize(new Dimension JavaDoc(24, 24));
204     _butDown.setIcon(Administrator.getImageIcon("down.gif"));
205     _butDown.setToolTipText(Messages.getString("global.NextInList"));
206     _butDown.setRequestFocusEnabled(true);
207     _butDown.addActionListener(new java.awt.event.ActionListener JavaDoc()
208     {
209       public void actionPerformed(ActionEvent JavaDoc e)
210       {
211         _butDown_actionPerformed(e);
212       }
213     });
214     _butDelete.setPreferredSize(new Dimension JavaDoc(24, 24));
215     _butDelete.setIcon(Administrator.getImageIcon("delete.gif"));
216     _butDelete.setToolTipText(Messages.getString("global.DeleteCtrl"));
217     _butDelete.setRequestFocusEnabled(true);
218     _butDelete.addActionListener(new java.awt.event.ActionListener JavaDoc()
219     {
220       public void actionPerformed(ActionEvent JavaDoc e)
221       {
222         _butDelete_actionPerformed(e);
223       }
224     });
225     _butBottom.addActionListener(new java.awt.event.ActionListener JavaDoc()
226     {
227       public void actionPerformed(ActionEvent JavaDoc e)
228       {
229         _butBottom_actionPerformed(e);
230       }
231     });
232     _butBottom.setRequestFocusEnabled(true);
233     _butBottom.setToolTipText(Messages.getString("global.LastInList"));
234     _butBottom.setIcon(Administrator.getImageIcon("bottom.gif"));
235     _butBottom.setPreferredSize(new Dimension JavaDoc(24, 24));
236     _butFirst.addActionListener(new java.awt.event.ActionListener JavaDoc()
237     {
238       public void actionPerformed(ActionEvent JavaDoc e)
239       {
240         _butFirst_actionPerformed(e);
241       }
242     });
243     _butFirst.setRequestFocusEnabled(true);
244     _butFirst.setToolTipText(Messages.getString("global.FirstInList"));
245     _butFirst.setIcon(Administrator.getImageIcon("top.gif"));
246     _butFirst.setPreferredSize(new Dimension JavaDoc(24, 24));
247     _butCreate.setPreferredSize(new Dimension JavaDoc(24, 24));
248     _butCreate.setIcon(Administrator.getImageIcon("new.gif"));
249     _butCreate.setToolTipText(Messages.getString("global.CreateCtrl"));
250     _butCreate.setRequestFocusEnabled(true);
251     _butCreate.addActionListener(new java.awt.event.ActionListener JavaDoc()
252     {
253       public void actionPerformed(ActionEvent JavaDoc e)
254       {
255         _butCreate_actionPerformed(e);
256       }
257     });
258     _butUp.addActionListener(new java.awt.event.ActionListener JavaDoc()
259     {
260       public void actionPerformed(ActionEvent JavaDoc e)
261       {
262         _butUp_actionPerformed(e);
263       }
264     });
265     _butUp.setRequestFocusEnabled(true);
266     _butUp.setToolTipText(Messages.getString("global.PreviousInList"));
267     _butUp.setIcon(Administrator.getImageIcon("up.gif"));
268     _butUp.setPreferredSize(new Dimension JavaDoc(24, 24));
269     this.add(jPanel3, BorderLayout.SOUTH);
270     jPanel3.add(jPanel4, BorderLayout.EAST);
271     jPanel4.add(_butCancel, null);
272     jPanel4.add(_butOk, null);
273     jPanel3.add(jPanel6, BorderLayout.WEST);
274     jPanel6.add(_butUp, null);
275     jPanel6.add(_butDown, null);
276     jPanel6.add(_butFirst, null);
277     jPanel6.add(_butBottom, null);
278     jPanel6.add(_butCreate, null);
279     jPanel6.add(_butDelete, null);
280     this.add(jPanel5, BorderLayout.NORTH);
281     jPanel1.add(jLabel1, null);
282     jPanel1.add(jLabel2, null);
283     jPanel1.add(jLabel3, null);
284     this.add(_tabs, BorderLayout.CENTER);
285     _tabs.add(_scrAccess, Messages.getString("global.Contexts"));
286     _scrAccess.getViewport().add(_grdAccess, null);
287     jPanel5.add(jPanel2, BorderLayout.CENTER);
288     jPanel2.add(_edtCode, null);
289     jPanel2.add(_edtName, null);
290     jPanel2.add(_pnlPw, null);
291     _pnlPw.add(_edtPassword, BorderLayout.CENTER);
292     _pnlPw.add(_butPw, BorderLayout.EAST);
293     jPanel2.add(_chkModeler, null);
294     jPanel2.add(_chkAdmin, null);
295     jPanel5.add(jPanel1, BorderLayout.WEST);
296     _tabs.add(_scrViews, Messages.getString("global.Views"));
297     _scrViews.getViewport().add(_grdViews, null);
298     Util.sizeButtons(new JButton JavaDoc[]{_butOk, _butCancel});
299   }
300
301   void jButton2_actionPerformed(ActionEvent JavaDoc e)
302   {
303     _adminPanel.closeEditor(this);
304     setStatusMessage(Messages.getString("global.Ok"));
305   }
306
307   public void setStatusMessage(String JavaDoc msg, boolean isInformative)
308   {
309     _adminPanel.setStatusMessage(msg, isInformative);
310   }
311
312   public void requestFocus()
313   {
314     _edtCode.requestFocus();
315   }
316
317   void jButton1_actionPerformed(ActionEvent JavaDoc e)
318   {
319     if (_edtCode.getText().trim().length() == 0)
320     {
321       setStatusMessage(Messages.getString("global.CodeRequired"), false);
322       return;
323     }
324     if (_edtName.getText().trim().length() == 0)
325     {
326       setStatusMessage(Messages.getString("global.NameRequired"), false);
327       return;
328     }
329
330     _user.setCode(_edtCode.getText().toLowerCase());
331     _user.setName(_edtName.getText());
332     _user.setPassword(new String JavaDoc(_edtPassword.getPassword()));
333     _user.setAdministrator(_chkAdmin.isSelected());
334     _user.setModeler(_chkModeler.isSelected());
335
336     try
337     {
338       DjSession session = _adminPanel.getPersistenceManager().createSession();
339       try
340       {
341         _user.persist(session);
342
343         for (int i = 0; i < _allAssocs.size(); i++)
344         {
345           DjUserContextAssociation dca = (DjUserContextAssociation) _allAssocs.get(i);
346           if (dca.getContext() == null)
347           {
348             _grdAccess.setRowSelectionInterval(i, i);
349             throw new Exception JavaDoc(Messages.getString("global.ContextRequired"));
350           }
351           if (dca.getUser() == null)
352           {
353             _grdAccess.setRowSelectionInterval(i, i);
354             throw new Exception JavaDoc(Messages.getString("global.UserIsRequired"));
355           }
356         }
357         _user.removeAllContextAssociations(session);
358         for (int i = 0; i < _allAssocs.size(); i++)
359         {
360           DjUserContextAssociation dca = (DjUserContextAssociation) _allAssocs.get(i);
361           dca.persist(session);
362         }
363         _user.removeAllViews(session);
364         for (int i = 0; i < _allViews.size(); i++)
365         {
366           DjModelView view = (DjModelView) _allViews.get(i);
367           if (view != null) _user.addView(session, view);
368         }
369
370         session.commit();
371         setStatusMessage(Messages.getString("global.Ok"));
372       }
373       finally
374       {
375         session.close();
376       }
377
378       _adminPanel.closeEditor(this);
379       if (_theNode != null)
380       {
381         _theNode.reload();
382       }
383     }
384     catch (Exception JavaDoc x)
385     {
386       setStatusMessage(x);
387     }
388   }
389
390   public String JavaDoc getID()
391   {
392     if (_user.getId() == -1) return null;
393     return "U" + _user.getId();
394   }
395
396   public void doubleClick(MouseEvent JavaDoc e, DjTable table)
397   {
398     table.requestFocus();
399     if (e.getClickCount() < 2) return;
400
401     try
402     {
403       table.insertRow();
404     }
405     catch (Exception JavaDoc x)
406     {
407       setStatusMessage(x);
408     }
409   }
410
411   void _scrAccess_mouseClicked(MouseEvent JavaDoc e)
412   {
413     doubleClick(e, _grdAccess);
414   }
415
416   void _scrAccess_mousePressed(MouseEvent JavaDoc e)
417   {
418   }
419
420   void _scrAccess_mouseReleased(MouseEvent JavaDoc e)
421   {
422   }
423
424   void _scrAccess_mouseEntered(MouseEvent JavaDoc e)
425   {
426   }
427
428   void _scrAccess_mouseExited(MouseEvent JavaDoc e)
429   {
430   }
431
432   void _scrViews_mouseClicked(MouseEvent JavaDoc e)
433   {
434     doubleClick(e, _grdViews);
435   }
436
437   void _butPw_actionPerformed(ActionEvent JavaDoc e)
438   {
439     String JavaDoc pw = JOptionPane.showInputDialog(this, Messages.getString("UserEditor.PleaseEnterPW"), Messages
440         .getString("UserEditor.SetPassword"), JOptionPane.INFORMATION_MESSAGE);
441     try
442     {
443       _edtPassword.setText(DjPersistenceManager.encodePassword(pw));
444     }
445     catch (Exception JavaDoc x)
446     {
447       setStatusMessage(x);
448     }
449   }
450
451   private void tableFirst(JTable JavaDoc table)
452   {
453     if (table.getRowCount() <= 0) return;
454
455     table.setRowSelectionInterval(0, 0);
456   }
457
458   private void tableBottom(JTable JavaDoc table)
459   {
460     int row = table.getRowCount() - 1;
461     if (row != -1) table.setRowSelectionInterval(row, row);
462   }
463
464   private void tableUp(JTable JavaDoc table)
465   {
466     int row = table.getSelectedRow();
467     if (row > 0) table.setRowSelectionInterval(row - 1, row - 1);
468   }
469
470   private void tableDown(JTable JavaDoc table)
471   {
472     int row = table.getSelectedRow();
473     if (row < table.getRowCount() - 1) table.setRowSelectionInterval(row + 1, row + 1);
474   }
475
476   void _butDown_actionPerformed(ActionEvent JavaDoc e)
477   {
478     if (_tabs.getSelectedComponent() == _scrAccess) tableDown(_grdAccess);
479     else if (_tabs.getSelectedComponent() == _scrViews) tableDown(_grdViews);
480   }
481
482   void _butDelete_actionPerformed(ActionEvent JavaDoc e)
483   {
484     try
485     {
486       if (_tabs.getSelectedComponent() == _scrAccess) _grdAccess.deleteRow();
487       else if (_tabs.getSelectedComponent() == _scrViews) _grdViews.deleteRow();
488     }
489     catch (Exception JavaDoc x)
490     {
491       setStatusMessage(x.getMessage(), false);
492     }
493   }
494
495   void _butBottom_actionPerformed(ActionEvent JavaDoc e)
496   {
497     if (_tabs.getSelectedComponent() == _scrAccess) tableBottom(_grdAccess);
498     else if (_tabs.getSelectedComponent() == _scrViews) tableBottom(_grdViews);
499   }
500
501   void _butFirst_actionPerformed(ActionEvent JavaDoc e)
502   {
503     if (_tabs.getSelectedComponent() == _scrAccess) tableFirst(_grdAccess);
504     else if (_tabs.getSelectedComponent() == _scrViews) tableFirst(_grdViews);
505   }
506
507   void _butCreate_actionPerformed(ActionEvent JavaDoc e)
508   {
509     try
510     {
511       if (_tabs.getSelectedComponent() == _scrAccess) _grdAccess.insertRow();
512       else if (_tabs.getSelectedComponent() == _scrViews) _grdViews.insertRow();
513     }
514     catch (Exception JavaDoc x)
515     {
516       setStatusMessage(x.getMessage(), false);
517     }
518   }
519
520   void _butUp_actionPerformed(ActionEvent JavaDoc e)
521   {
522     if (_tabs.getSelectedComponent() == _scrAccess) tableUp(_grdAccess);
523     else if (_tabs.getSelectedComponent() == _scrViews) tableUp(_grdViews);
524   }
525
526 }
Popular Tags