1 package com.ca.directory.jxplorer.viewer.tableviewer; 2 3 import com.ca.commons.cbutil.CBIntText; 4 import com.ca.commons.cbutil.CBJComboBox; 5 import com.ca.commons.cbutil.CBUtility; 6 import com.ca.commons.jndi.SchemaOps; 7 import com.ca.commons.naming.DN; 8 import com.ca.commons.security.cert.CertViewer; 9 import com.ca.directory.jxplorer.DataSource; 10 import com.ca.directory.jxplorer.editor.*; 11 12 import javax.naming.NamingException ; 13 import javax.naming.directory.Attribute ; 14 import javax.naming.directory.Attributes ; 15 import javax.swing.*; 16 import java.awt.*; 17 import java.awt.event.MouseAdapter ; 18 import java.awt.event.MouseEvent ; 19 import java.lang.reflect.Constructor ; 20 import java.security.cert.X509Certificate ; 21 import java.util.Arrays ; 22 import java.util.EventObject ; 23 import java.util.logging.Logger ; 24 import java.util.logging.Level ; 25 26 33 public class AttributeValueCellEditor extends AbstractCellEditor 34 { 35 Frame owner; 36 37 JTextField textField = new JTextField(); 38 JLabel label = new JLabel(""); 39 40 CBJComboBox combobox = new CBJComboBox(); 41 42 JComponent editorComponent = textField; 43 44 abstractbinaryeditor abstractEditor = null; 46 Object value; 47 boolean binaryEditFlag = false; boolean specialStringEditor = false; protected ClassLoader myLoader = null; public DataSource datasource = null; public DN currentDN = null; 53 int lastSelectedRow = 0; 55 public static final String BINARY_SYNTAX = "1.3.6.1.4.1.1466.115.121.1.5"; 56 public static final String BOOLEAN_SYNTAX = "1.3.6.1.4.1.1466.115.121.1.7"; 57 public static final String CERTIFICATE_SYNTAX = "1.3.6.1.4.1.1466.115.121.1.8"; 58 public static final String GENERALIZED_TIME_SYNTAX = "1.3.6.1.4.1.1466.115.121.1.24"; 59 public static final String POSTAL_ADDRESS_SYNTAX = "1.3.6.1.4.1.1466.115.121.1.41"; 60 61 private static Logger log = Logger.getLogger(AttributeValueCellEditor.class.getName()); 62 63 68 public AttributeValueCellEditor(Frame parent) 69 { 70 owner = parent; 71 72 74 editorComponent.addMouseListener(new MouseAdapter () 75 { 76 public void mousePressed(MouseEvent e) 77 { 78 } 82 }); 83 } 84 85 89 91 105 public Component getTableCellEditorComponent(JTable table, 106 Object value, boolean isSelected, 107 int row, int column) 108 { 109 binaryEditFlag = false; 110 specialStringEditor = false; 111 112 table.setRowHeight(lastSelectedRow, 16); 113 table.setRowHeight(row, 24); 114 115 lastSelectedRow = row; 116 117 if (value instanceof AttributeValue) 118 { 119 AttributeValue att = (AttributeValue) value; 120 121 if (hasSyntax(att, CERTIFICATE_SYNTAX)) setCertificateEditor(att); 123 else if (att.isBinary()) setBinaryEditor(att); 125 else if (hasSyntax(att, POSTAL_ADDRESS_SYNTAX)) setPostalAddressEditor(att); 127 else if (hasSyntax(att, GENERALIZED_TIME_SYNTAX)) setGeneralizedTimeEditor(att); 129 else if (hasSyntax(att, BOOLEAN_SYNTAX)) setBooleanEditor(att); 131 else if (att.hasOptions()) setOptions(att); 133 else 134 setString(att); 135 136 setCellEditorValue(att); 137 } 138 return editorComponent; 139 } 140 141 147 public boolean hasSyntax(AttributeValue att, String syntax) 148 { 149 String attSyntax = getAttributeSyntax(att); 150 return (attSyntax != null && attSyntax.indexOf(syntax) > -1); 151 } 152 153 158 private void setCertificateEditor(AttributeValue att) 159 { 160 CertViewer.CertAndFileName returnVal = CertViewer.editCertificate(owner, att.getValue()); 161 X509Certificate cert = returnVal.cert; 162 if (cert != null) 163 { 164 try 165 { 166 byte[] newData = cert.getEncoded(); 167 if (Arrays.equals(newData, att.getValue()) == false) 168 att.setValue(newData); 169 } 170 catch(Exception e) 171 { 172 CBUtility.error(CBIntText.get("Error: unable to modify certificate."), e); 173 } 174 } 175 176 binaryEditFlag = true; 177 178 if (att.isEmpty()) 179 { 180 label.setText(" "); 181 } 182 else 183 { 184 label.setText(CBIntText.get("(non string data)")); 185 } 186 187 editorComponent = label; 188 } 189 190 196 private void setString(AttributeValue att) 197 { 198 String textValue = att.toString(); 199 if (textValue.length() > 100) { 201 setLargeStringEditor(att); 202 } 203 else 204 { 205 textValue = textValue.trim(); 207 textField.setText(textValue); 208 209 editorComponent = textField; 210 } 211 } 212 213 218 private void setLargeStringEditor(AttributeValue att) 219 { 220 largestringeditor lse = new largestringeditor(owner, att); 221 specialStringEditor = true; 222 CBUtility.center(lse, owner); 223 lse.setVisible(true); 224 label.setText(att.getStringValue().substring(0,100)); 225 editorComponent = label; 226 } 227 228 233 private void setOptions(AttributeValue att) 234 { 235 combobox.removeAllItems(); 236 String [] ops = att.getOptions(); 237 for (int i=0; i<ops.length; i++) 238 combobox.addItem(ops[i]); 239 editorComponent = combobox; 240 } 241 242 247 private void setBinaryEditor(AttributeValue att) 248 { 249 startBinaryEditor(att); binaryEditFlag = true; 251 252 if (att.isEmpty()) 253 { 254 label.setText(" "); 255 } 256 else 257 { 258 label.setText(CBIntText.get("(non string data)")); 259 } 260 261 editorComponent = label; 262 } 263 264 269 private void setGeneralizedTimeEditor(AttributeValue att) 270 { 271 generalizedtimeeditor timeEditor = null; 272 273 if (att==null) 274 { 275 timeEditor = new generalizedtimeeditor(owner,"", true); 276 } 277 else 278 { 279 timeEditor = new generalizedtimeeditor(owner, att.toString(), true); 280 } 281 282 specialStringEditor = true; 283 CBUtility.center(timeEditor, owner); timeEditor.setStringValue(att); 285 timeEditor.setVisible(true); 286 287 if (att.isEmpty()) 288 { 289 label.setText(" "); 290 } 291 else 292 { 293 label.setText(att.getStringValue()); } 295 296 editorComponent = label; 297 } 298 299 304 private void setPostalAddressEditor(AttributeValue att) 305 { 306 postaladdresseditor postalEditor = new postaladdresseditor(owner, att); 307 specialStringEditor = true; 308 CBUtility.center(postalEditor, owner); postalEditor.setStringValue(att); 310 postalEditor.setVisible(true); 311 312 if (att.isEmpty()) 313 { 314 label.setText(" "); 315 } 316 else 317 { 318 label.setText(att.getStringValue()); } 320 321 editorComponent = label; 322 } 323 324 329 private void setBooleanEditor(AttributeValue att) 330 { 331 booleaneditor booleanEditor = new booleaneditor(owner, att); 332 specialStringEditor = true; 333 CBUtility.center(booleanEditor, owner); booleanEditor.setStringValue(att); 335 booleanEditor.setVisible(true); 336 337 if (att.isEmpty()) 338 { 339 label.setText(" "); 340 } 341 else 342 { 343 label.setText(att.getStringValue()); } 345 346 editorComponent = label; 347 } 348 349 356 public String getAttributeSyntax(AttributeValue att) 357 { 358 String attID = att.getID(); 359 return getAttributeSyntaxFromName(attID); 360 } 361 362 369 private String getAttributeSyntaxFromName(String attID) 370 { 371 if (attID.indexOf(';') > 0) 372 attID = attID.substring(0, attID.indexOf(';')); 374 try 375 { 376 if (datasource == null) 377 throw new NamingException ("No datasource!"); 378 379 SchemaOps schema = datasource.getSchemaOps(); 380 if (schema == null) 381 throw new NamingException ("No schema!"); 382 Attributes attSchema = schema.getAttributes("AttributeDefinition/" + attID); 385 if (attSchema == null) 386 throw new NamingException ("No schema for AttributeDefinition/"+attID); 387 388 Attribute attSyntax = attSchema.get("SYNTAX"); 390 if (attSyntax == null) { 392 Attribute supSchema = attSchema.get("SUP"); 393 if (supSchema == null) 394 throw new NamingException ("Error processing attribute definition for " + attID + " no schema and no sup entry found"); 395 396 if (supSchema.get().toString().equals(attID)) throw new NamingException ("recursive schema definition: " + attID + " sup of itself"); 398 399 return getAttributeSyntaxFromName(supSchema.get().toString()); } 401 else 402 { 403 String syntax = attSyntax.toString(); 404 if (syntax.startsWith("SYNTAX: ")) 405 syntax = syntax.substring(8); 406 return syntax; 407 } 408 } 409 catch (NamingException e) 410 { 411 log.log(Level.WARNING, "Problem processing attribute definition: ", e); 412 return "1.3.6.1.4.1.1466.115.121.1.15"; } 414 } 415 416 420 public boolean stopCellEditing() 422 { 423 if (binaryEditFlag) 424 { 425 return super.stopCellEditing(); 426 } 427 else if (specialStringEditor) 428 { 429 return super.stopCellEditing(); 430 } 431 432 Object o = getCellEditorValue(); 433 if (o == null) return true; 435 if (o instanceof AttributeValue) 436 { 437 AttributeValue v = (AttributeValue)o; 438 439 if (editorComponent instanceof JTextField) 440 { 441 String userData = ((JTextField)editorComponent).getText(); 442 int len = userData.length(); 444 if (len > 0) 445 { 446 userData.trim(); 447 if (userData.length() == 0) 448 userData = " "; 450 if (userData.length() != len) 451 ((JTextField)editorComponent).setText(userData); 452 } 453 v.update(userData); 454 } 455 else if (editorComponent instanceof CBJComboBox) 456 v.update(((CBJComboBox)editorComponent).getSelectedItem()); 457 else 458 log.warning("unknown editorComponent = " + editorComponent.getClass()); 459 } 460 else 461 log.warning("(AttValCellEdit) Not an Att Val: is " + o.getClass()); 462 463 setCellEditorValue(o); 464 465 return super.stopCellEditing(); 466 } 467 468 472 public boolean isCellEditable(EventObject e) 473 { 474 boolean editable = true; 475 if (e instanceof MouseEvent ) 476 { 477 editable = ((MouseEvent )e).getClickCount() >= clickCountToStart; 478 } 479 return editable; 480 } 481 482 487 public void startBinaryEditor(AttributeValue att) 488 { 489 if (abstractEditor != null && 490 (abstractEditor instanceof Component) && 491 ((Component)abstractEditor).isVisible() ) { 493 return; } 495 496 if (att.isBinary()==false) {log.warning("Error: non binary value passed to binary editor!"); return; } 498 499 String attName = att.getID(); 500 int trim = attName.indexOf(";binary"); 501 if (trim>0) 502 attName = attName.substring(0,trim); 503 504 String className = "com.ca.directory.jxplorer.editor." + attName + "editor"; 505 506 try 507 { 508 Class c = null; 509 if (myLoader == null) 510 { 511 System.out.println("using default loader for "+className ); 512 c = Class.forName(className); 513 } 514 else 515 { 516 System.out.println("looking for: " + className.toLowerCase()); 517 c = myLoader.loadClass(className.toLowerCase()); 518 } 519 if (abstractbinaryeditor.class.isAssignableFrom(c)) 520 { 521 Constructor constructor; 522 if (owner instanceof Frame) 523 { 524 constructor = c.getConstructor(new Class [] {java.awt.Frame .class}); 525 abstractEditor = (abstractbinaryeditor) constructor.newInstance(new Object [] {owner}); 526 } 527 else 528 { 529 constructor = c.getConstructor(new Class [0]); 530 abstractEditor = (abstractbinaryeditor) constructor.newInstance(new Object [0]); 531 } 532 533 abstractEditor.setValue(att); 534 if(abstractEditor instanceof basicbinaryeditor) ((basicbinaryeditor)abstractEditor).setDN(currentDN); 536 else if(abstractEditor instanceof defaultbinaryeditor) ((defaultbinaryeditor)abstractEditor).setDN(currentDN); 538 539 if (abstractEditor instanceof defaultbinaryeditor) 540 { 541 ((defaultbinaryeditor)abstractEditor).showDialog(); abstractEditor = null; } 544 else if (abstractEditor instanceof Component) 545 { 546 ((Component)abstractEditor).setVisible(true); 547 } 548 549 fireEditingStopped(); return; } 552 else 553 { 554 log.warning("error: can't load editor class " + className + " since it is not inherited from AbstractBinaryEditor\n"); 555 } 556 557 } 558 catch (NoSuchMethodException e) 559 { 560 log.log(Level.WARNING, "coding error in editor " + className+ " - using default binary editor instead.", e); 561 } 562 catch (ClassNotFoundException e) 563 { 564 log.info("(expected) can not find editor " + className + "\n" + e.toString()); try 566 { 567 defaultbinaryeditor dbe = new defaultbinaryeditor(owner); dbe.setDN(currentDN); 569 CBUtility.center(dbe, owner); dbe.setValue(att); 571 dbe.showDialog(); 572 } 573 catch (Exception e2) 574 { 575 log.log(Level.WARNING, "unable to start backup editor", e2); 576 e2.printStackTrace(); 577 } 578 } 579 catch (Exception e3) 580 { 581 log.log(Level.WARNING, "error loading editor " + className, e3); 582 e3.printStackTrace(); 583 } 584 } 585 586 589 public void registerClassLoader(ClassLoader loader) 590 { 591 myLoader = loader; 592 } 593 594 598 public void setDataSource(DataSource ds) 599 { 600 datasource = ds; 601 } 602 603 607 public void setDN(DN dn) 608 { 609 currentDN = dn; 610 } 611 612 615 public void cleanupEditor() 616 { 617 abstractEditor = null; 618 } 619 } 620 | Popular Tags |