View Javadoc
1 /* ------------------------------------------------------------------- 2 * Java source file for the class IntegerEditor 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: IntegerEditor.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.*; 27 import java.awt.event.*; 28 import java.beans.*; 29 import javax.swing.*; 30 31 import net.jbeans.util.text.*; 32 33 /*==================================================================== 34 Implementation of class IntegerEditor 35 ====================================================================*/ 36 /*** 37 * A property editor for editing integers. This editor also supports enumerated 38 * type properties which are identified if the "enumerationValues" key returns 39 * a non-null value. 40 * Note: the init() method must be called before the set/get methods can be 41 * called. 42 * 43 * @version $Revision: 1.1.1.1 $ 44 * @author Masahiro Takatsuka (masa@jbeans.net) 45 * @see EditorSupport 46 */ 47 48 public final class IntegerEditor extends EditorSupport { 49 /* Property editor to use if the Integer represents an Enumerated type. */ 50 private EnumEditor enumEditor = new EnumEditor(); 51 52 private JTextField textfield; 53 54 private boolean isEnumeration = false; 55 56 public final void setValue(Object value) { 57 if (this.isEnumeration) { 58 this.enumEditor.setValue(value); 59 } else { 60 super.setValue(value); 61 62 if (value != null) { 63 this.textfield.setText(value.toString()); 64 } 65 } 66 } 67 68 public final Object getValue() { 69 if (this.isEnumeration) { 70 return this.enumEditor.getValue(); 71 } else { 72 return super.getValue(); 73 } 74 } 75 76 /*** 77 * Must overloade the PropertyChangeListener registration because 78 * this class is the only interface to the EnumEditor. 79 */ 80 public final void addPropertyChangeListener(PropertyChangeListener l) { 81 this.enumEditor.addPropertyChangeListener(l); 82 super.addPropertyChangeListener(l); 83 } 84 85 public final void removePropertyChangeListener(PropertyChangeListener l) { 86 this.enumEditor.removePropertyChangeListener(l); 87 super.removePropertyChangeListener(l); 88 } 89 90 /*** 91 * Initializes this property editor with the enumerated items. 92 */ 93 public final void init(PropertyDescriptor descriptor) { 94 Object[] enum = (Object[])descriptor.getValue("enumerationValues"); 95 if ( enum != null ) { 96 // The property descriptor describes an enumerated item. 97 this.isEnumeration = true; 98 99 this.enumEditor.init(descriptor); 100 101 } else { 102 // This is an integer item 103 this.isEnumeration = false; 104 105 if (this.textfield == null) { 106 this.textfield = new JTextField(); 107 this.textfield.setDocument(new NumberDocument()); 108 // XXX - Textfield should sent an actionPerformed event. 109 // this was broken for 1.3 beta 110 this.textfield.addKeyListener(new KeyAdapter() { 111 public final void keyPressed(KeyEvent evt) { 112 if (evt.getKeyCode() == KeyEvent.VK_ENTER) { 113 setValue(new Integer(IntegerEditor.this.textfield.getText())); 114 } 115 } 116 }); 117 JPanel panel = new JPanel(); 118 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); 119 panel.add(this.textfield); 120 setPanel(panel); 121 } 122 } 123 } 124 125 /*** 126 * Return the custom editor for the enumeration or the integer. 127 */ 128 public final Component getCustomEditor() { 129 if (this.isEnumeration) { 130 return this.enumEditor.getCustomEditor(); 131 } else { 132 return super.getCustomEditor(); 133 } 134 } 135 136 public final String getJavaInitializationString() { 137 return "" + getValue(); 138 } 139 }

This page was automatically generated by Maven