1 package com.ca.directory.jxplorer.tree; 2 3 import com.ca.commons.cbutil.*; 4 import com.ca.commons.naming.*; 5 import com.ca.directory.jxplorer.*; 6 7 import javax.naming.InvalidNameException ; 8 import javax.naming.NamingException ; 9 import javax.naming.directory.*; 10 import javax.swing.*; 11 import java.awt.*; 12 import java.awt.event.*; 13 import java.util.*; 14 import java.util.logging.Logger ; 15 import java.util.logging.Level ; 16 17 23 24 31 32 public class NewEntryWin extends CBDialog implements ActionListener, DataListener 33 { 34 JTextField rdnField,parentDNField; 35 36 JScrollPane possiblePanel; JList possibleList; 39 JScrollPane selectedPanel; JList selectedList; 42 DefaultListModel selDataModel; DefaultListModel posDataModel; 45 DataSink entryEditor; 47 JCheckBox autoSuggest; 48 49 CBButton select, remove; 50 51 static boolean suggest = true; 52 53 Vector suggestedClasses = new Vector(); 54 55 DataSource dataSource; 56 57 60 61 DN parentDN; 62 DN childDN; 63 64 67 68 DN entryDN = null; 69 DXAttributes defaultAttributes = null; 70 71 protected CBHelpSystem helpBroker; 72 73 String objectClassName = null; 75 private static Logger log = Logger.getLogger(NewEntryWin.class.getName()); 76 77 81 82 protected boolean simpleMode; 83 84 88 89 protected boolean disableName = false; 90 91 92 boolean virtualEntry = false; 94 public BasicAttribute newObjectClasses = null; 95 96 113 114 115 public NewEntryWin(DN pDN, DN cDN, DataSource dSource, 116 DataSink attDisplay, Frame parent) 117 { 118 this(pDN, cDN, dSource, null, null, attDisplay, parent); 119 disableName = false; 120 rdnField.setEnabled(true); 121 parentDNField.setEnabled(true); 122 } 123 124 125 148 149 public NewEntryWin(DN pDN, DN cDN, 150 DataSource dSource, Attributes defaultValues, 151 String rdn, DataSink attDisplay, Frame parent) 152 { 153 super(parent, CBIntText.get("Set Entry Object Classes"), HelpIDs.ENTRY_NEW); 154 155 simpleMode = false; 156 157 dataSource = dSource; 158 159 entryEditor = attDisplay; 160 161 if (defaultValues!=null) 162 { 163 if (defaultValues instanceof DXAttributes) 164 defaultAttributes = (DXAttributes)defaultValues; 165 else 166 defaultAttributes = new DXAttributes(defaultAttributes); 167 } 168 169 try 171 { 172 objectClassName = dataSource.getSchemaOps().getNameOfObjectClassAttribute(); 173 } 174 catch (Exception e) 175 { 176 } 178 parentDN = pDN; 179 childDN = cDN; 180 181 getContentPane().add(display); 182 183 autoSuggest = new JCheckBox(CBIntText.get("Suggest Classes?"), suggest); 184 display.add(autoSuggest,3,0); 185 display.newLine(); 186 187 display.add(new JLabel(CBIntText.get("Parent DN") + ": ")); 188 display.addLine(parentDNField = new JTextField(parentDN.toString(), 20)); 189 190 parentDNField.setEnabled(false); 192 display.add(new JLabel(CBIntText.get("Enter RDN") + ": ")); 193 display.addLine(rdnField = new JTextField("=", 20)); 194 195 if (rdn != null) 197 { 198 rdnField.setText(rdn); 199 rdnField.setEnabled(false); } 201 else if ((suggest == true)&&(childDN != null)) 202 { 203 rdnField.setText(childDN.getLowestRDN().getAtt() + "="); 204 } 205 206 setupObjectClassPanels(null); 207 208 209 210 autoSuggest.addActionListener(this); 211 212 registerMouseListeners(); 213 } 214 215 231 232 public NewEntryWin(DataSource dSource, DN entryDN, Attributes defaultValues, 233 DataSink attDisplay, Frame parent) 234 { 235 super(parent, CBIntText.get("Set Entry Object Classes"), HelpIDs.CLASS_CHANGE); 236 237 simpleMode = true; 238 239 this.entryDN = entryDN; 240 241 dataSource = dSource; 242 243 entryEditor = attDisplay; 244 245 if (defaultValues instanceof DXAttributes) 246 defaultAttributes = (DXAttributes) defaultValues; 247 else 248 defaultAttributes = new DXAttributes(defaultAttributes); 249 250 try 252 { 253 objectClassName = dataSource.getSchemaOps().getNameOfObjectClassAttribute(); 254 } 255 catch (Exception e) {} 257 getContentPane().add(display); 258 259 setupObjectClassPanels(defaultAttributes); 260 261 registerMouseListeners(); 262 } 263 264 265 266 280 281 public NewEntryWin(DataSource dSource, DN entryDN, DataSink attDisplay, Frame parent, boolean virtualEntry) 282 { 283 super(parent, CBIntText.get("Set Entry Object Classes"), HelpIDs.CLASS_CHANGE); 284 285 this.virtualEntry = virtualEntry; 286 287 simpleMode = true; 288 289 this.entryDN = entryDN; 290 291 dataSource = dSource; 292 293 entryEditor = attDisplay; 294 getContentPane().add(display); 295 296 setupObjectClassPanels(null); 297 registerMouseListeners(); 298 } 299 300 301 302 protected void setupObjectClassPanels(Attributes currentAtts) 303 { 304 305 display.addWide(new JLabel(CBIntText.get("Available Classes") + ": "),2); 306 display.addWide(new JLabel(CBIntText.get("Selected Classes") + ": "),2); 307 display.newLine(); 308 309 selDataModel = new DefaultListModel(); 310 selectedList = new JList(selDataModel); 311 selectedList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 312 selectedList.setSelectionModel(new CBSingleSelectionModel(selectedList)); 314 posDataModel = new DefaultListModel(); 315 possibleList = new JList(posDataModel); 316 possibleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 317 possibleList.setSelectionModel(new CBSingleSelectionModel(possibleList)); 319 possiblePanel = new JScrollPane(possibleList); 320 selectedPanel = new JScrollPane(selectedList); 321 322 326 327 if (currentAtts != null) 328 { 329 Attribute objectClasses = currentAtts.get(objectClassName); 330 try 331 { 332 Enumeration recommendedObjectClasses = objectClasses.getAll(); 333 while (recommendedObjectClasses.hasMoreElements()) 334 { 335 String oc = (String )recommendedObjectClasses.nextElement(); 336 selDataModel.addElement(oc); 337 suggestedClasses.add(oc); 338 } 339 } 340 catch (Exception e) {} 341 } 342 343 352 353 else if (childDN != null) 354 { 355 DataQuery myQuery = dataSource.getRecommendedObjectClasses(childDN); 356 myQuery.addDataListener(this); 357 } 358 359 366 try 367 { 368 ArrayList allObjectClasses = dataSource.getSchemaOps().objectClasses(); 369 370 for (int i=0; i<allObjectClasses.size(); i++) 371 { 372 posDataModel.addElement(allObjectClasses.get(i)); 373 } 374 } 375 catch (NamingException e) 376 { 377 } 379 Enumeration existing = selDataModel.elements(); 380 while (existing.hasMoreElements()) 381 { 382 posDataModel.removeElement(existing.nextElement()); 383 } 384 385 display.makeHeavy(); 386 display.add(possiblePanel,0,4,2,2); 387 display.add(selectedPanel,2,4,2,2); 388 display.makeLight(); 389 display.newLine(); 390 391 select = new CBButton(CBIntText.get("Add"), CBIntText.get("Click to add the selection.")); 392 remove = new CBButton(CBIntText.get("Remove"), CBIntText.get("Click to remove the selection.")); 393 display.addWide(select, 2); 394 display.addWide(remove, 2); 395 display.newLine(); 396 397 validate(); 398 399 select.addActionListener(this); 400 remove.addActionListener(this); 401 } 402 403 404 407 408 public void dataReady(DataQuery result) 409 { 410 try 411 { 412 432 if (result.getType() == DataQuery.GETRECOC) 433 { 434 if (suggestedClasses.size() < 1) { 436 ArrayList recommendedObjectClasses = result.getObjectClasses(); 437 for (int i=0; i<recommendedObjectClasses.size(); i++) 438 suggestedClasses.addElement(recommendedObjectClasses.get(i)); 439 } 440 441 checkSuggestedList(); 442 } 443 } 444 catch (NamingException e) {CBUtility.error(CBIntText.get("threaded broker error: "), e); } } 446 447 450 451 protected void checkSuggestedList() 452 { 453 if (suggest == true && suggestedClasses.size() > 0) 454 { 455 for (int i=0; i<suggestedClasses.size(); i++) 456 { 457 selectClass((String )suggestedClasses.get(i)); 458 } 459 } 460 } 461 462 463 464 public void registerMouseListeners() 465 { 466 possibleList.addMouseListener(new MouseAdapter() 468 { 469 public void mouseClicked(MouseEvent e) 470 { 471 if (e.getClickCount() == 2) 472 { 473 selectClass((String )possibleList.getSelectedValue()); 474 } 475 else 476 super.mouseClicked(e); 477 } 478 }); 479 480 selectedList.addMouseListener(new MouseAdapter() 481 { 482 public void mouseClicked(MouseEvent e) 483 { 484 if (e.getClickCount() == 2) 485 { 486 removeClass((String )selectedList.getSelectedValue()); 487 } 488 else 489 super.mouseClicked(e); 490 } 491 }); 492 } 493 494 495 public void selectClass(String value) 496 { 497 if (selDataModel.contains(value)==false) 498 { 499 selDataModel.addElement(value); 500 posDataModel.removeElement(value); 501 } 502 } 503 504 public void removeClass(String value) 505 { 506 if (selDataModel.contains(value)) 507 { 508 selDataModel.removeElement(value); 509 if (posDataModel.contains(value) == false) 510 { 511 Object [] possibleValues = posDataModel.toArray(); 513 int len = possibleValues.length; 514 for (int i=0; i<len; i++) 515 { 516 if (value.compareTo(possibleValues[i].toString()) <= 0) 517 { 518 posDataModel.add(i, value); 519 break; 520 } 521 } 522 } 523 } 524 } 525 526 527 533 534 public void doOK() 535 { 536 538 String rdnText = null; 539 540 if (simpleMode == false) 541 { 542 543 rdnText = rdnField.getText().trim(); 544 545 int equalpos = rdnText.indexOf('='); 546 rdnText = rdnText.substring(0, equalpos+1) + NameUtility.escape(rdnText.substring(equalpos+1)); 548 try { 550 if (rdnText==null || "".equals(rdnText)) throw new InvalidNameException (CBIntText.get("Empty RDN, please enter a valid RDN. ") + ((rdnText==null)?"<null>":CBIntText.get("The RDN value entered was: '")+rdnText) + "'"); 553 554 if (NameUtility.next(rdnText,0,'=')<0) throw new InvalidNameException (CBIntText.get("Invalid RDN, please enter a naming attribute followed by '=' followed by a name in the RDN field (for example, 'cn=Trudi). '") + ((rdnText==null)?"<null>":CBIntText.get("The RDN value entered was: '")+rdnText) + "'"); 556 else if (NameUtility.next(rdnText,0,'=')<1) throw new InvalidNameException (CBIntText.get("Invalid RDN, please enter a naming attribute in the RDN field (for example, cn: 'cn=Trudi). '") + ((rdnText==null)?"<null>":CBIntText.get("The RDN value entered was: '")+rdnText) + "'"); 558 } 559 catch (InvalidNameException ine) 560 { 561 CBUtility.warning(this, CBIntText.get("Please Fill in the RDN field with a valid RDN."), CBIntText.get("Invalid RDN")); 562 log.log(Level.WARNING, "Invalid RDN value in the New Entry dialog: ", ine); 563 return; 564 } 565 566 String dn; 567 if (parentDNField.getText().trim().length() > 0) 568 dn = rdnText + "," + parentDNField.getText(); 569 else 570 dn = rdnText; 571 572 entryDN = new DN(dn); 574 } 575 576 Enumeration atts = selDataModel.elements(); 577 578 if (atts == null) {CBUtility.warning(this, CBIntText.get("At least one object class must be selected!"),CBIntText.get("Need ObjectClass(s)"));return;} 580 581 582 BasicAttribute obclasses = new BasicAttribute((objectClassName==null)?"objectClass":objectClassName); 583 while (atts.hasMoreElements()) 584 { 585 String oc = atts.nextElement().toString(); 586 587 obclasses.add(oc); 588 } 589 if (obclasses.size()==0) 590 {CBUtility.warning(this, CBIntText.get("At least one object class must be selected."), CBIntText.get(" Need ObjectClass(s)")); return;} 592 if (virtualEntry) 593 { 594 newObjectClasses = obclasses; 597 doCancel(); 598 } 599 600 if (createNewEntry(obclasses, rdnText, entryDN) == true) 601 doCancel(); 602 } 604 605 610 611 public void doCancel() 612 { 613 setVisible(false); 614 dispose(); 615 this.getParent().repaint(); 616 } 617 618 619 public void actionPerformed(ActionEvent e) 620 { 621 Object src = e.getSource(); 622 623 if (src == select) 624 { 625 if (possibleList.getSelectedValue() != null) 626 selectClass(possibleList.getSelectedValue().toString()); 627 } 628 else if (src == remove) 629 { 630 if (selectedList.getSelectedValue() != null) 631 removeClass(selectedList.getSelectedValue().toString()); 632 } 633 else if (src == autoSuggest) 634 { 635 suggest = autoSuggest.isSelected(); 636 if (suggest == true) 637 checkSuggestedList(); 638 else 639 { 640 if (suggestedClasses.size() > 0) 641 { 642 for (int i=0; i<suggestedClasses.size(); i++) 643 removeClass((String )suggestedClasses.get(i)); 644 } 645 } 646 } 647 } 648 649 650 655 656 public boolean createNewEntry(Attribute obclasses, String rdnText, DN newObjectDN) 657 { 658 661 DXAttributes attlist = new DXAttributes(obclasses); 662 Attribute allObjectClasses = attlist.getAllObjectClasses(); 663 664 if (allObjectClasses == null) return false; 666 attlist.put(allObjectClasses); 668 attlist.expandAllAttributes(); 669 670 if (defaultAttributes !=null) 672 { 673 DXNamingEnumeration defAtts = (DXNamingEnumeration)defaultAttributes.getAllNonNull(); 674 while (defAtts.hasMore()) 675 { 676 Attribute defAtt = (Attribute)defAtts.next(); 677 String defAttName = defAtt.getID(); 678 if (defAttName.equalsIgnoreCase("objectclass")==false) 679 { 680 if (attlist.get(defAttName)!=null) attlist.put(defAtt); 682 } 683 } 684 } 685 686 687 if (rdnText != null) 688 { 689 RDN rdn = new RDN(rdnText); 690 691 if (rdn.size()==0 || "".equals(rdn.getRawVal(0))) 692 { 693 CBUtility.warning(this, CBIntText.get("Please fill in a valid name for the entry"), CBIntText.get("Can't read RDN")); 694 return false; 695 } 696 697 String rdnAttribute = rdn.getAtt(0); 698 String rdnValue = rdn.getRawVal(0); 699 700 if (rdn.isMultiValued()==false) 701 { 702 BasicAttribute rdnAtt = new BasicAttribute(rdnAttribute, rdnValue); 703 attlist.put(rdnAtt); 704 } 705 else { 707 for (int i=0; i<rdn.size(); i++) 708 { 709 BasicAttribute rdnAtt = new BasicAttribute(rdn.getAtt(i), rdn.getRawVal(i)); 710 attlist.put(rdnAtt); 711 } 712 } 713 if (rdn.validate() == false) 714 { 715 CBUtility.warning(this, CBIntText.get("Couldn't parse entry's name - please try again"), CBIntText.get("Can't read RDN")); 716 return false; 717 } 718 } 719 720 setVisible(false); 721 this.getParent().repaint(); 722 723 if (dataSource == null) { 725 CBUtility.error(CBIntText.get("unusual error:") + "\nno Data Source registered in NewEntryWin.", null); 726 return false; 727 } 728 729 log.fine("creating entry " + newObjectDN); 731 DXEntry newEntry = new DXEntry(attlist, newObjectDN); 732 733 newEntry.setStatus(DXEntry.NEW); 734 entryEditor.displayEntry(newEntry, dataSource); 735 736 return true; 739 } 740 } | Popular Tags |