1 /* 2 * Copyright 2002 Sun Microsystems, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or 5 * without modification, are permitted provided that the following 6 * conditions are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistribution in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following 13 * disclaimer in the documentation and/or other materials 14 * provided with the distribution. 15 * 16 * Neither the name of Sun Microsystems, Inc. or the names of 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * This software is provided "AS IS," without a warranty of any 21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY 24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY 25 * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR 26 * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR 27 * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE 28 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, 29 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER 30 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF 31 * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS 32 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 33 * 34 * You acknowledge that this software is not designed, licensed or 35 * intended for use in the design, construction, operation or 36 * maintenance of any nuclear facility. 37 */ 38 39 package javax.swing.beaninfo; 40 41 import java.awt.event.ActionEvent; 42 import java.awt.event.ActionListener; 43 import java.awt.event.KeyEvent; 44 import java.awt.event.KeyAdapter; 45 46 import java.beans.Beans; 47 48 import javax.swing.BoxLayout; 49 import javax.swing.JPanel; 50 import javax.swing.JTextField; 51 import javax.swing.JOptionPane; 52 53 /** 54 * A property editor which allows for the display and instantaition of 55 * arbitrary objects. To instantiate the object, type the package and the 56 * class in the text field and press Enter. The Class should be in the 57 * classpath. 58 * 59 * @version %I% %G% 60 * @author Mark Davidson 61 */ 62 public class SwingObjectEditor extends SwingEditorSupport { 63 64 private JTextField textfield; 65 66 public SwingObjectEditor() { 67 textfield = new JTextField(); 68 69 // NOTE: This should work but there was a regression in JDK 1.3 beta which 70 // doesn't fire for text fields. 71 // This should be fixed for RC 1. 72 textfield.addActionListener(new ActionListener() { 73 public void actionPerformed(ActionEvent evt) { 74 // XXX - debug 75 System.out.println("SwingObjectEditor.actionPerformed"); 76 handleAction(); 77 } 78 }); 79 80 // XXX - Temporary workaround for 1.3 beta 81 textfield.addKeyListener(new KeyAdapter() { 82 public void keyPressed(KeyEvent evt) { 83 if (evt.getKeyCode() == KeyEvent.VK_ENTER) { 84 handleAction(); 85 } 86 } 87 }); 88 89 panel = new JPanel(); 90 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); 91 panel.add(textfield); 92 } 93 94 public void setValue(Object value) { 95 super.setValue(value); 96 97 if (value != null) { 98 // Truncate the address from the object reference. 99 String text = value.toString(); 100 101 // XXX javax.swing.AccessibleRelationSet.toString() has a bug in which 102 // null is returned. Intecept this and other cases so that the tool 103 // doens't get hosed. 104 if (text == null) text = ""; 105 106 int index = text.indexOf('@'); 107 if (index != -1) { 108 text = text.substring(0, index); 109 } 110 textfield.setText(text); 111 } else { 112 textfield.setText(""); 113 } 114 } 115 116 /** 117 * Callback method which gets handled for actionPerformed. 118 */ 119 private void handleAction() { 120 String beanText = textfield.getText(); 121 122 try { 123 Object obj = Beans.instantiate(this.getClass().getClassLoader(), beanText); 124 setValue(obj); 125 } catch (Exception ex) { 126 JOptionPane.showMessageDialog(panel, "Can't find or load\n" + beanText); 127 } 128 } 129 130 }