View Javadoc
1 /* ------------------------------------------------------------------- 2 * Java source file for the class ObjectEditor 3 * 4 * Copyright (c), 2002, Masahiro Takatsuka. 5 * All Rights Researved. 6 * 7 * Original Author: Masahiro Takatsuka (masa@jbeans.net) 8 * $Author: takatsukam $ 9 * 10 * $Date: 2003/07/25 04:51:45 $ 11 * 12 * $Id: ObjectEditor.java,v 1.1.1.1 2003/07/25 04:51:45 takatsukam Exp $ 13 * 14 * Reference: Document no: 15 * ___ ___ 16 * 17 * To Do: 18 * ___ 19 * 20 ------------------------------------------------------------------- */ 21 22 /* --------------------------- Package ---------------------------- */ 23 package net.jbeans.bean.property.editor; 24 25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */ 26 import java.awt.event.*; 27 import java.beans.*; 28 import javax.swing.*; 29 30 /*==================================================================== 31 Implementation of class ObjectEditor 32 ====================================================================*/ 33 /*** 34 * A property editor which allows for the display and instantaition of 35 * arbitrary objects. To instantiate the object, type the package and the 36 * class in the text field and press Enter. The Class should be in the 37 * classpath. 38 * 39 * @version $Revision: 1.1.1.1 $ 40 * @author Masahiro Takatsuka (masa@jbeans.net) 41 * @see EditorSupport 42 */ 43 44 public final class ObjectEditor extends EditorSupport { 45 private JTextField textfield; 46 47 public ObjectEditor() { 48 this.textfield = new JTextField(); 49 this.textfield.addActionListener(new ActionListener() { 50 public final void actionPerformed(ActionEvent evt) { 51 // XXX - debug 52 System.out.println("ObjectEditor.actionPerformed"); 53 handleAction(); 54 } 55 }); 56 57 // XXX - Temporary workaround for 1.3 beta 58 this.textfield.addKeyListener(new KeyAdapter() { 59 public final void keyPressed(KeyEvent evt) { 60 if (evt.getKeyCode() == KeyEvent.VK_ENTER) { 61 handleAction(); 62 } 63 } 64 }); 65 66 JPanel panel = new JPanel(); 67 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); 68 panel.add(this.textfield); 69 setPanel(panel); 70 } 71 72 public final void setValue(Object value) { 73 super.setValue(value); 74 75 if (value != null) { 76 // Truncate the address from the object reference. 77 String text = value.toString(); 78 79 // XXX javax.swing.AccessibleRelationSet.toString() has a bug in which 80 // null is returned. Intecept this and other cases so that the tool 81 // doens't get hosed. 82 if (text == null) text = ""; 83 84 int index = text.indexOf('@'); 85 if (index != -1) { 86 text = text.substring(0, index); 87 } 88 this.textfield.setText(text); 89 } else { 90 this.textfield.setText(""); 91 } 92 } 93 94 /*** 95 * Callback method which gets handled for actionPerformed. 96 */ 97 private void handleAction() { 98 String beanText = this.textfield.getText(); 99 100 try { 101 Object obj = Beans.instantiate(this.getClass().getClassLoader(), beanText); 102 setValue(obj); 103 } catch (Exception ex) { 104 JOptionPane.showMessageDialog(getPanel(), "Can't find or load\n" + beanText); 105 } 106 } 107 108 public final String getJavaInitializationString() { 109 return null; 110 } 111 }

This page was automatically generated by Maven