1 package sellwin.gui; 2 3 import sellwin.domain.*; 4 import sellwin.server.*; 5 import sellwin.utils.*; 6 7 import java.util.*; 8 import java.rmi.*; 9 10 import javax.swing.*; 11 import javax.swing.event.*; 12 import javax.swing.border.*; 13 import java.awt.*; 14 import java.awt.event.*; 15 16 20 25 public class SalesPersonDialog extends JDialog implements GUIChars { 26 private Whiteboard wb; 27 private JDialog thisDialog = null; 28 29 private ArrayList currentRoles=null; 30 private ArrayList roleNamesList=new ArrayList(); 31 private JPanel userPanel = new JPanel(); 32 private JList userList = new JList(); 33 private JLabel userIDLabel = new JLabel("User ID"); 34 private JTextField id = new JTextField(20); 35 private JLabel passwordLabel = new JLabel("Password"); 36 private JTextField password = new JTextField(20); 37 private ArrayList roles = new ArrayList(); 38 private JPanel rolePanel = new JPanel(); 39 private JLabel roleListLabel = new JLabel("Roles"); 40 private JList roleList = new JList(); 41 42 private JPopupMenu rolePopup = new JPopupMenu(); 43 private JMenuItem addRoleMenuItem = new JMenuItem("Add"); 44 private JMenuItem deleteRoleMenuItem = new JMenuItem("Delete"); 45 46 private JLabel addressLabel = new JLabel("Address"); 47 private AddressPanel addressPanel = new AddressPanel(); 48 49 private JButton applyButton = new JButton("Apply"); 50 private JButton clearButton = new JButton("Clear"); 51 private JButton closeButton = new JButton("Cancel"); 52 53 private JPopupMenu userPopup = new JPopupMenu(); 54 private JScrollPane scrollPane3, scrollPane9; 55 56 59 public SalesPersonDialog() { 60 61 super(); 62 63 thisDialog = this; 64 wb = MainWindow.getWhiteboard(); 65 66 67 setSize(630,575); 68 69 setFonts(); 70 setColors(); 71 72 getContentPane().setLayout(new BorderLayout()); 73 74 GridBagLayout gbl = new GridBagLayout(); 75 GridBagConstraints gbc = new GridBagConstraints(); 76 JPanel mainPanel = new JPanel(); 77 mainPanel.setLayout(gbl); 78 79 gbc = new GridBagConstraints(); 80 gbc.gridwidth = GridBagConstraints.REMAINDER; 81 gbc.anchor = GridBagConstraints.WEST; 82 gbc.gridx = 0; 83 gbc.gridy = 0; 84 gbc.ipadx = 10; 85 gbc.ipady = 10; 86 userList.setVisibleRowCount(3); 87 userList.setPrototypeCellValue("XXXXXXXXXXXXXXXXXX"); 88 userList.setMinimumSize(new Dimension(500,150)); 89 userList.setPreferredSize(new Dimension(500,150)); 90 scrollPane3 = new JScrollPane(userList); 91 scrollPane3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 92 scrollPane3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 93 userPanel.add(scrollPane3); 94 mainPanel.add(userPanel, gbc); 95 96 JPanel idpswPanel = new JPanel(new GridBagLayout()); 97 gbc = new GridBagConstraints(); 98 gbc.anchor = GridBagConstraints.WEST; 99 gbc.gridx = 0; 100 gbc.gridy = 1; 101 mainPanel.add(idpswPanel, gbc); 102 103 gbc = new GridBagConstraints(); 104 gbc.anchor = GridBagConstraints.WEST; 105 gbc.gridx = 0; 106 gbc.gridy = 0; 107 gbc.gridwidth = 1; 108 gbc.fill = GridBagConstraints.HORIZONTAL; 109 gbc.insets = new Insets(Prefs.I_TOP,Prefs.I_LF,Prefs.I_BOTT,Prefs.I_RT); 110 idpswPanel.add(userIDLabel, gbc); 111 112 gbc = new GridBagConstraints(); 113 gbc.insets = new Insets(Prefs.I_TOP,Prefs.I_LF,Prefs.I_BOTT,Prefs.I_RT); 114 gbc.anchor = GridBagConstraints.WEST; 115 gbc.gridx = 1; 116 gbc.gridy = 0; 117 idpswPanel.setMinimumSize(new Dimension(200,100)); 118 idpswPanel.setPreferredSize(new Dimension(200,100)); 119 idpswPanel.setMaximumSize(new Dimension(200,100)); 120 id.setMinimumSize(new Dimension(70,Prefs.FIELD_HEIGHT)); 121 id.setMaximumSize(new Dimension(70,Prefs.FIELD_HEIGHT)); 122 id.setPreferredSize(new Dimension(70,Prefs.FIELD_HEIGHT)); 123 idpswPanel.add(id, gbc); 124 125 gbc = new GridBagConstraints(); 126 gbc.anchor = GridBagConstraints.WEST; 127 gbc.gridx = 0; 128 gbc.gridy = 1; 129 gbc.gridwidth = 1; 130 gbc.fill = GridBagConstraints.HORIZONTAL; 131 gbc.insets = new Insets(Prefs.I_TOP,Prefs.I_LF,Prefs.I_BOTT,Prefs.I_RT); 132 idpswPanel.add(passwordLabel, gbc); 133 134 gbc = new GridBagConstraints(); 135 gbc.insets = new Insets(Prefs.I_TOP,Prefs.I_LF,Prefs.I_BOTT,Prefs.I_RT); 136 gbc.anchor = GridBagConstraints.WEST; 137 gbc.gridx = 1; 138 gbc.gridy = 1; 139 password.setMinimumSize(new Dimension(70,Prefs.FIELD_HEIGHT)); 140 password.setMaximumSize(new Dimension(70,Prefs.FIELD_HEIGHT)); 141 password.setPreferredSize(new Dimension(70,Prefs.FIELD_HEIGHT)); 142 idpswPanel.add(password, gbc); 143 144 gbc = new GridBagConstraints(); 145 gbc.gridwidth = GridBagConstraints.REMAINDER; 146 gbc.anchor = GridBagConstraints.WEST; 147 gbc.gridx = 1; 148 gbc.gridy = 1; 149 gbc.ipadx = 10; 150 gbc.ipady = 10; 151 roleList.setVisibleRowCount(3); 152 roleList.setPrototypeCellValue("XXXXXXXXXXXXXXXXXX"); 153 roleList.setMinimumSize(new Dimension(500,100)); 154 roleList.setPreferredSize(new Dimension(500,100)); 155 scrollPane9 = new JScrollPane(roleList); 156 scrollPane9.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 157 scrollPane9.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 158 rolePanel.add(scrollPane9); 159 mainPanel.add(rolePanel, gbc); 160 161 gbc = new GridBagConstraints(); 162 gbc.anchor = GridBagConstraints.WEST; 163 gbc.gridx = 0; 164 gbc.gridy = 2; 165 gbc.gridwidth = GridBagConstraints.REMAINDER; 166 gbc.fill = GridBagConstraints.HORIZONTAL; 167 gbc.insets = new Insets(Prefs.I_TOP,Prefs.I_LF,Prefs.I_BOTT,Prefs.I_RT); 168 addressPanel.setBorder(new TitledBorder("Address")); 169 mainPanel.add(addressPanel, gbc); 170 mainPanel.setBorder(new javax.swing.border.EtchedBorder ()); 171 172 getContentPane().add(mainPanel, BorderLayout.CENTER); 173 174 JPanel buttonPanel = new JPanel(); 175 buttonPanel.add(applyButton); 176 buttonPanel.add(clearButton); 177 buttonPanel.add(closeButton); 178 getContentPane().add(buttonPanel, BorderLayout.SOUTH); 179 180 181 182 JMenuItem deleteUserMenuItem = new JMenuItem("Delete"); 183 184 deleteUserMenuItem.addActionListener( 185 new ActionListener() { 186 public void actionPerformed(ActionEvent e) { 187 String id = (String )userList.getSelectedValue(); 188 try { 189 TreeMap all = wb.getSalesPersons(); 190 SalesPerson sp = (SalesPerson)(all.get(id)); 191 wb.deleteSalesPerson(sp); 192 String v = (String )userList.getSelectedValue(); 193 DefaultListModel m = (DefaultListModel)userList.getModel(); 194 m.removeElement(v); 195 if (m.size() > 0) { 196 userList.setSelectedIndex(m.size() - 1); 197 id = (String )userList.getSelectedValue(); 198 if (id != null) 199 loadSalesPerson(id); 200 } 201 clear(); 202 } catch (AngError x) { 203 ErrorHandler.show(thisDialog, x); 204 } 205 } 206 }); 207 userPopup.add(deleteUserMenuItem); 208 209 210 clearButton.addActionListener( 211 new ActionListener() { 212 public void actionPerformed(ActionEvent e) { 213 clear(); 214 } 215 } 216 ); 217 218 closeButton.addActionListener( 219 new ActionListener() { 220 public void actionPerformed(ActionEvent e) { 221 thisDialog.hide(); 222 } 223 } 224 ); 225 226 applyButton.addActionListener( 227 new ActionListener() { 228 public void actionPerformed(ActionEvent e) { 229 230 int option; 231 SalesPerson s=null; 232 233 if (nameAlreadyUsed() == false) { 234 option = JOptionPane.showConfirmDialog(thisDialog, wb.getLang().getString("addUserQuestion")); 235 if (option == JOptionPane.YES_OPTION) { 236 try { 237 s = getSalesPerson(); 238 if (s == null) return; 239 240 wb.addSalesPerson(s); 241 setData(); 242 } catch (AngError f) { 243 ErrorHandler.show(thisDialog, f); 244 } 245 } 246 } else { 247 option = JOptionPane.showConfirmDialog(thisDialog, wb.getLang().getString("continueUpdateQuestion")); 248 if (option == JOptionPane.YES_OPTION) { 249 try { 250 s = getSalesPerson(); 251 if (s == null) return; 252 253 wb.updateSalesPerson(s); 254 JOptionPane.showMessageDialog( 255 thisDialog, 256 "Update Successful", 257 "Update Successful", 258 JOptionPane.INFORMATION_MESSAGE); 259 } catch (Exception f) { 260 f.printStackTrace(); 261 } 262 } 263 } 264 } 265 } 266 ); 267 268 269 WindowListener l = new WindowAdapter() { 270 public void windowClosed(WindowEvent e) { 271 } 272 273 public void windowClosing(WindowEvent e) { 274 hide(); 275 } 276 }; 277 278 addWindowListener(l); 279 280 userList.addMouseListener( 281 new MouseAdapter() { 282 public void mousePressed(MouseEvent e) { 283 if (e.isPopupTrigger()) 284 userPopup.show(userList, e.getX(), e.getY()); 285 } 286 } 287 ); 288 289 userList.addMouseListener( 290 new MouseAdapter() { 291 public void mouseClicked(MouseEvent e) { 292 switch (e.getClickCount()) { 293 case 1: 294 String id = (String )userList.getSelectedValue(); 295 if (id != null) 296 loadSalesPerson(id); 297 break; 298 default: 299 break; 300 } 301 } 302 } 303 ); 304 305 rolePopup.add(addRoleMenuItem); 306 rolePopup.add(deleteRoleMenuItem); 307 308 roleList.addMouseListener( 309 new MouseAdapter() { 310 public void mousePressed(MouseEvent e) { 311 if (e.isPopupTrigger()) 312 rolePopup.show(roleList, e.getX(), e.getY()); 313 } 314 public void mouseReleased(MouseEvent e) { 315 if (e.isPopupTrigger()) 316 rolePopup.show(roleList, e.getX(), e.getY()); 317 } 318 public void mouseClicked(MouseEvent e) { 319 if (e.isPopupTrigger()) 320 rolePopup.show(roleList, e.getX(), e.getY()); 321 } 322 } 323 ); 324 325 addRoleMenuItem.addActionListener( 326 new ActionListener() { 327 public void actionPerformed(ActionEvent e) { 328 try { 329 initRoles(); 330 Object [] roleNames = roleNamesList.toArray(); 331 String roleName = (String )JOptionPane.showInputDialog( 332 thisDialog, 333 "Choose a role for this user.", 334 "Select a role", 335 JOptionPane.PLAIN_MESSAGE, 336 null, 337 roleNames, 338 roleNames[0]); 339 340 String id = (String )userList.getSelectedValue(); 341 TreeMap all = wb.getSalesPersons(); 342 SalesPerson sp = (SalesPerson)(all.get(id)); 343 344 UserRole role; 345 for (int j=0;j<currentRoles.size();j++) { 346 role = (UserRole)currentRoles.get(j); 347 if (role.getName().equals(roleName)) { 348 sp.addUserRole(role); 349 wb.updateSalesPerson(sp); 350 setRoleData(sp.getUserRoles().toArray()); 351 break; 352 } 353 } 354 } catch (AngError f) { 355 ErrorHandler.show(thisDialog, f); 356 } 357 } 358 } 359 ); 360 361 deleteRoleMenuItem.addActionListener( 362 new ActionListener() { 363 public void actionPerformed(ActionEvent e) { 364 try { 365 String roleName = (String )roleList.getSelectedValue(); 366 String id = (String )userList.getSelectedValue(); 367 TreeMap all = wb.getSalesPersons(); 368 SalesPerson sp = (SalesPerson)(all.get(id)); 369 sp.removeUserRole(roleName); 370 wb.updateSalesPerson(sp); 371 setRoleData(sp.getUserRoles().toArray()); 372 } catch (AngError f) { 373 ErrorHandler.show(thisDialog, f); 374 } 375 } 376 } 377 ); 378 379 380 setData(); 381 setLang(); 382 } 383 384 388 private final boolean nameAlreadyUsed() { 389 TreeMap allUsers = null; 390 try { 391 allUsers = wb.getSalesPersons(); 392 } catch (AngError e) { 393 ErrorHandler.show(thisDialog, e); 394 } 395 396 String screenID = id.getText(); 397 SalesPerson sp = (SalesPerson)allUsers.get(screenID); 398 if (sp == null) 399 return false; 400 else 401 return true; 402 } 403 404 408 public final void loadSalesPerson(String idValue) { 409 try { 410 SalesPerson sp = (SalesPerson)(wb.getSalesPersons().get(idValue)); 411 id.setText(sp.getID()); 412 password.setText(sp.getPassword()); 413 setRoleData(sp.getUserRoles().toArray()); 414 addressPanel.setAddress(sp.getAddress()); 415 } catch (Exception e) { 416 e.printStackTrace(); 417 } 418 } 419 420 421 424 public final void setData() { 425 try { 426 TreeMap u = wb.getSalesPersons(); 427 Object [] keys = u.keySet().toArray(); 428 DefaultListModel model = new DefaultListModel(); 429 String id = null; 430 for (int i=0;i<keys.length;i++) { 431 id = (String )(keys[i]); 432 model.addElement(id); 433 } 434 435 userList.setModel(model); 436 if (keys.length > 0) { 437 userList.setSelectedIndex(0); 438 loadSalesPerson((String )keys[0]); 439 } 440 } catch (AngError e) { 441 ErrorHandler.show(thisDialog, e); 442 } 443 } 444 445 449 public final void setRoleData(Object [] roles) { 450 DefaultListModel model = new DefaultListModel(); 451 UserRole role = null; 452 for (int i=0;i<roles.length;i++) { 453 role = (UserRole)roles[i]; 454 model.addElement(role.getName()); 455 } 456 457 roleList.setModel(model); 458 } 459 460 463 public final void clear() { 464 id.setText(""); 465 password.setText(""); 466 Object [] items = { }; 467 setRoleData(items); 468 addressPanel.clear(); 469 } 470 471 475 public final SalesPerson getSalesPerson() { 476 if (password.getText().length() == 0) { 477 JOptionPane.showMessageDialog( 478 thisDialog, 479 "Password Field is blank or empty.", 480 "Error", 481 JOptionPane.INFORMATION_MESSAGE); 482 return null; 483 } 484 485 if (id.getText().length() == 0) { 486 JOptionPane.showMessageDialog( 487 thisDialog, 488 "User ID field is blank or emtpy.", 489 "Error", 490 JOptionPane.INFORMATION_MESSAGE); 491 return null; 492 } 493 494 SalesPerson s = null; 495 TreeMap all = null; 496 try { 497 all = wb.getSalesPersons(); 498 } catch (AngError e) { 499 ErrorHandler.show(thisDialog, e); 500 return s; 501 } 502 503 String idVal = (String )userList.getSelectedValue(); 504 s = (SalesPerson)(all.get(idVal)); 505 s.setID(id.getText()); 506 s.setPassword(password.getText()); 507 Address a = addressPanel.getAddress(); 508 Address.copyFields(s.getAddress(), a); 509 return s; 510 } 511 512 513 516 public final void setFonts() { 517 userList.setFont(MainWindow.FIELD_FONT); 518 userIDLabel.setFont(MainWindow.LABEL_FONT); 519 id.setFont(MainWindow.FIELD_FONT); 520 passwordLabel.setFont(MainWindow.LABEL_FONT); 521 password.setFont(MainWindow.FIELD_FONT); 522 addressLabel.setFont(MainWindow.LABEL_FONT); 523 } 524 525 528 public final void setColors() { 529 userIDLabel.setForeground(MainWindow.LETTERS); 530 passwordLabel.setForeground(MainWindow.LETTERS); 531 addressLabel.setForeground(MainWindow.LETTERS); 532 } 533 534 537 private final void initRoles() { 538 try { 539 currentRoles = wb.getAllUserRoles(); 540 UserRole role; 541 for (int i=0;i<currentRoles.size();i++) { 542 role = (UserRole)currentRoles.get(i); 543 roleNamesList.add(role.getName()); 544 } 545 } catch (AngError e) { 546 ErrorHandler.show(thisDialog, e); 547 } 548 } 549 550 554 private final void setSecurity() { 555 try { 556 if (wb.getLogin().userAdminWritable()) { 557 addRoleMenuItem.setEnabled(true); 558 deleteRoleMenuItem.setEnabled(true); 559 applyButton.setEnabled(true); 560 } else { 561 addRoleMenuItem.setEnabled(false); 562 deleteRoleMenuItem.setEnabled(false); 563 applyButton.setEnabled(false); 564 } 565 } catch (AngError e) { 566 ErrorHandler.show(thisDialog, e); 567 } 568 } 569 570 573 public final void setLang() { 574 setTitle(wb.getLang().getString("userMaint")); 575 userIDLabel.setText(wb.getLang().getString("userid")); 576 passwordLabel.setText(wb.getLang().getString("password")); 577 roleListLabel.setText(wb.getLang().getString("roles")); 578 addRoleMenuItem.setText(wb.getLang().getString("add")); 579 deleteRoleMenuItem.setText(wb.getLang().getString("delete")); 580 addressLabel.setText(wb.getLang().getString("address")); 581 applyButton.setText(wb.getLang().getString("apply")); 582 clearButton.setText(wb.getLang().getString("clear")); 583 closeButton.setText(wb.getLang().getString("close")); 584 userPanel.setBorder( 585 new TitledBorder( 586 null, 587 wb.getLang().getString("currUsers"), 588 TitledBorder.DEFAULT_JUSTIFICATION, 589 TitledBorder.DEFAULT_POSITION, 590 MainWindow.LABEL_FONT, 591 MainWindow.LETTERS 592 ) 593 ); 594 rolePanel.setBorder( 595 new TitledBorder( 596 null, 597 wb.getLang().getString("roles"), 598 TitledBorder.DEFAULT_JUSTIFICATION, 599 TitledBorder.DEFAULT_POSITION, 600 MainWindow.LABEL_FONT, 601 MainWindow.LETTERS 602 ) 603 ); 604 addressPanel.setBorder( 605 new TitledBorder( 606 null, 607 wb.getLang().getString("address"), 608 TitledBorder.DEFAULT_JUSTIFICATION, 609 TitledBorder.DEFAULT_POSITION, 610 MainWindow.LABEL_FONT, 611 MainWindow.LETTERS 612 ) 613 ); 614 } 615 } 616 | Popular Tags |