1 28 package com.genimen.djeneric.tools.administrator.editors; 29 30 import java.awt.BorderLayout ; 31 import java.awt.Dimension ; 32 import java.awt.event.ActionEvent ; 33 import java.awt.event.MouseEvent ; 34 import java.util.ArrayList ; 35 36 import javax.swing.JButton ; 37 import javax.swing.JCheckBox ; 38 import javax.swing.JLabel ; 39 import javax.swing.JOptionPane ; 40 import javax.swing.JPanel ; 41 import javax.swing.JPasswordField ; 42 import javax.swing.JScrollPane ; 43 import javax.swing.JTabbedPane ; 44 import javax.swing.JTable ; 45 import javax.swing.JTextField ; 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 borderLayout1 = new BorderLayout (); 65 JPanel jPanel3 = new JPanel (); 66 BorderLayout borderLayout2 = new BorderLayout (); 67 JPanel jPanel4 = new JPanel (); 68 JButton _butOk = new JButton (); 69 JButton _butCancel = new JButton (); 70 AdministratorTreeNode _theNode; 71 DjUser _user; 72 JPanel jPanel5 = new JPanel (); 73 BorderLayout borderLayout3 = new BorderLayout (); 74 JPanel jPanel1 = new JPanel (); 75 JCheckBox _chkAdmin = new JCheckBox (); 76 DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout(); 77 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 78 JTextField _edtName = new JTextField (); 79 JTextField _edtCode = new JTextField (); 80 JLabel jLabel2 = new JLabel (); 81 JLabel jLabel1 = new JLabel (); 82 JPanel jPanel2 = new JPanel (); 83 AccessTableModel _model; 84 ModelViewTableModel _viewModel; 85 ArrayList _allAssocs = new ArrayList (); 86 ArrayList _allViews = new ArrayList (); 87 JTabbedPane _tabs = new JTabbedPane (); 88 JScrollPane _scrAccess = new JScrollPane (); 89 DjTable _grdAccess = new DjTable(); 90 JScrollPane _scrViews = new JScrollPane (); 91 DjTable _grdViews = new DjTable(); 92 JCheckBox _chkModeler = new JCheckBox (); 93 JLabel jLabel3 = new JLabel (); 94 JPanel _pnlPw = new JPanel (); 95 BorderLayout borderLayout4 = new BorderLayout (); 96 JPasswordField _edtPassword = new JPasswordField (); 97 JButton _butPw = new JButton (); 98 JButton _butDown = new JButton (); 99 JButton _butDelete = new JButton (); 100 JButton _butBottom = new JButton (); 101 JButton _butFirst = new JButton (); 102 JButton _butCreate = new JButton (); 103 JButton _butUp = new JButton (); 104 JPanel jPanel6 = new JPanel (); 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 ex) 140 { 141 DjLogger.log(ex); 142 } 143 } 144 145 void jbInit() throws Exception 146 { 147 this.setLayout(borderLayout1); 148 jPanel3.setLayout(borderLayout2); 149 _butOk.setText(Messages.getString("global.Ok")); 150 _butOk.addActionListener(new java.awt.event.ActionListener () 151 { 152 public void actionPerformed(ActionEvent e) 153 { 154 jButton1_actionPerformed(e); 155 } 156 }); 157 _butCancel.setText(Messages.getString("global.Cancel")); 158 _butCancel.addActionListener(new java.awt.event.ActionListener () 159 { 160 public void actionPerformed(ActionEvent e) 161 { 162 jButton2_actionPerformed(e); 163 } 164 }); 165 jPanel5.setLayout(borderLayout3); 166 jPanel1.setLayout(verticalFlowLayout1); 167 _chkAdmin.setPreferredSize(new Dimension (98, 21)); 168 _chkAdmin.setText(Messages.getString("UserEditor.Administrator")); 169 jLabel2.setPreferredSize(new Dimension (34, 21)); 170 jLabel2.setText(Messages.getString("global.Name")); 171 jLabel1.setPreferredSize(new Dimension (41, 21)); 172 jLabel1.setText(Messages.getString("global.Code")); 173 jPanel2.setLayout(verticalFlowLayout2); 174 _scrAccess.addMouseListener(new java.awt.event.MouseAdapter () 175 { 176 public void mouseClicked(MouseEvent e) 177 { 178 _scrAccess_mouseClicked(e); 179 } 180 }); 181 _scrViews.addMouseListener(new java.awt.event.MouseAdapter () 182 { 183 public void mouseClicked(MouseEvent e) 184 { 185 _scrViews_mouseClicked(e); 186 } 187 }); 188 _chkModeler.setText(Messages.getString("UserEditor.Modeler")); 189 _chkModeler.setPreferredSize(new Dimension (98, 21)); 190 jLabel3.setPreferredSize(new Dimension (60, 21)); 191 jLabel3.setText(Messages.getString("global.Password")); 192 _pnlPw.setLayout(borderLayout4); 193 _butPw.setPreferredSize(new Dimension (65, 21)); 194 _butPw.setText(Messages.getString("UserEditor.Set")); 195 _butPw.addActionListener(new java.awt.event.ActionListener () 196 { 197 public void actionPerformed(ActionEvent e) 198 { 199 _butPw_actionPerformed(e); 200 } 201 }); 202 _edtPassword.setEditable(false); 203 _butDown.setPreferredSize(new Dimension (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 () 208 { 209 public void actionPerformed(ActionEvent e) 210 { 211 _butDown_actionPerformed(e); 212 } 213 }); 214 _butDelete.setPreferredSize(new Dimension (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 () 219 { 220 public void actionPerformed(ActionEvent e) 221 { 222 _butDelete_actionPerformed(e); 223 } 224 }); 225 _butBottom.addActionListener(new java.awt.event.ActionListener () 226 { 227 public void actionPerformed(ActionEvent 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 (24, 24)); 236 _butFirst.addActionListener(new java.awt.event.ActionListener () 237 { 238 public void actionPerformed(ActionEvent 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 (24, 24)); 247 _butCreate.setPreferredSize(new Dimension (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 () 252 { 253 public void actionPerformed(ActionEvent e) 254 { 255 _butCreate_actionPerformed(e); 256 } 257 }); 258 _butUp.addActionListener(new java.awt.event.ActionListener () 259 { 260 public void actionPerformed(ActionEvent 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 (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 []{_butOk, _butCancel}); 299 } 300 301 void jButton2_actionPerformed(ActionEvent e) 302 { 303 _adminPanel.closeEditor(this); 304 setStatusMessage(Messages.getString("global.Ok")); 305 } 306 307 public void setStatusMessage(String 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 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 (_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 (Messages.getString("global.ContextRequired")); 350 } 351 if (dca.getUser() == null) 352 { 353 _grdAccess.setRowSelectionInterval(i, i); 354 throw new Exception (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 x) 385 { 386 setStatusMessage(x); 387 } 388 } 389 390 public String getID() 391 { 392 if (_user.getId() == -1) return null; 393 return "U" + _user.getId(); 394 } 395 396 public void doubleClick(MouseEvent e, DjTable table) 397 { 398 table.requestFocus(); 399 if (e.getClickCount() < 2) return; 400 401 try 402 { 403 table.insertRow(); 404 } 405 catch (Exception x) 406 { 407 setStatusMessage(x); 408 } 409 } 410 411 void _scrAccess_mouseClicked(MouseEvent e) 412 { 413 doubleClick(e, _grdAccess); 414 } 415 416 void _scrAccess_mousePressed(MouseEvent e) 417 { 418 } 419 420 void _scrAccess_mouseReleased(MouseEvent e) 421 { 422 } 423 424 void _scrAccess_mouseEntered(MouseEvent e) 425 { 426 } 427 428 void _scrAccess_mouseExited(MouseEvent e) 429 { 430 } 431 432 void _scrViews_mouseClicked(MouseEvent e) 433 { 434 doubleClick(e, _grdViews); 435 } 436 437 void _butPw_actionPerformed(ActionEvent e) 438 { 439 String 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 x) 446 { 447 setStatusMessage(x); 448 } 449 } 450 451 private void tableFirst(JTable table) 452 { 453 if (table.getRowCount() <= 0) return; 454 455 table.setRowSelectionInterval(0, 0); 456 } 457 458 private void tableBottom(JTable table) 459 { 460 int row = table.getRowCount() - 1; 461 if (row != -1) table.setRowSelectionInterval(row, row); 462 } 463 464 private void tableUp(JTable table) 465 { 466 int row = table.getSelectedRow(); 467 if (row > 0) table.setRowSelectionInterval(row - 1, row - 1); 468 } 469 470 private void tableDown(JTable 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 e) 477 { 478 if (_tabs.getSelectedComponent() == _scrAccess) tableDown(_grdAccess); 479 else if (_tabs.getSelectedComponent() == _scrViews) tableDown(_grdViews); 480 } 481 482 void _butDelete_actionPerformed(ActionEvent e) 483 { 484 try 485 { 486 if (_tabs.getSelectedComponent() == _scrAccess) _grdAccess.deleteRow(); 487 else if (_tabs.getSelectedComponent() == _scrViews) _grdViews.deleteRow(); 488 } 489 catch (Exception x) 490 { 491 setStatusMessage(x.getMessage(), false); 492 } 493 } 494 495 void _butBottom_actionPerformed(ActionEvent e) 496 { 497 if (_tabs.getSelectedComponent() == _scrAccess) tableBottom(_grdAccess); 498 else if (_tabs.getSelectedComponent() == _scrViews) tableBottom(_grdViews); 499 } 500 501 void _butFirst_actionPerformed(ActionEvent e) 502 { 503 if (_tabs.getSelectedComponent() == _scrAccess) tableFirst(_grdAccess); 504 else if (_tabs.getSelectedComponent() == _scrViews) tableFirst(_grdViews); 505 } 506 507 void _butCreate_actionPerformed(ActionEvent e) 508 { 509 try 510 { 511 if (_tabs.getSelectedComponent() == _scrAccess) _grdAccess.insertRow(); 512 else if (_tabs.getSelectedComponent() == _scrViews) _grdViews.insertRow(); 513 } 514 catch (Exception x) 515 { 516 setStatusMessage(x.getMessage(), false); 517 } 518 } 519 520 void _butUp_actionPerformed(ActionEvent e) 521 { 522 if (_tabs.getSelectedComponent() == _scrAccess) tableUp(_grdAccess); 523 else if (_tabs.getSelectedComponent() == _scrViews) tableUp(_grdViews); 524 } 525 526 } | Popular Tags |