1 7 package org.ejtools.adwt.editor; 8 9 import java.awt.Component ; 10 import java.awt.GridLayout ; 11 import java.beans.PropertyChangeEvent ; 12 import java.beans.PropertyChangeListener ; 13 import java.beans.PropertyEditor ; 14 15 import javax.swing.JLabel ; 16 import javax.swing.JPanel ; 17 18 import org.ejtools.beans.CustomPropertyEditorManager; 19 20 import com.dreambean.awt.GenericPropertyCustomizer; 21 22 26 public class ArrayEditor extends GenericEditor 27 { 28 29 protected Object [] array = new Object [0]; 30 31 protected Class clazz = null; 32 33 protected JPanel panel = null; 34 35 36 41 public ArrayEditor(Class clazz) 42 { 43 this.clazz = clazz; 44 this.panel = new JPanel (); 45 } 46 47 48 51 public Component getCustomEditor() 52 { 53 return this.panel; 54 } 55 56 57 60 public void setValue(Object value) 61 { 62 super.setValue(value); 63 if (this.value == null) 64 { 65 return; 66 } 67 this.array = (Object []) this.value; 68 this.buildPanel(); 69 } 70 71 72 75 public boolean supportsCustomEditor() 76 { 77 return true; 78 } 79 80 81 82 private void buildPanel() 83 { 84 this.panel.removeAll(); 85 86 PropertyEditor editor = CustomPropertyEditorManager.findEditor(this.clazz); 87 if (editor == null) 88 { 89 editor = CustomPropertyEditorManager.findEditor(String .class); 90 } 91 92 this.panel = new JPanel (new GridLayout (this.array.length, 1)); 93 for (int i = 0; i < this.array.length; i++) 94 { 95 try 96 { 97 PropertyEditor peInstance = (PropertyEditor ) editor.getClass().newInstance(); 98 peInstance.setValue(array[i]); 99 100 Object component; 101 102 if (peInstance.supportsCustomEditor()) 103 { 104 component = peInstance.getCustomEditor(); 105 } 106 else 107 { 108 String as[] = peInstance.getTags(); 109 if (as != null) 110 { 111 component = new GenericPropertyCustomizer(peInstance, as); 112 } 113 else 114 { 115 final JLabel label = new JLabel (peInstance.getAsText()); 116 final PropertyEditor pcEditor = peInstance; 117 component = label; 118 pcEditor.addPropertyChangeListener( 119 new PropertyChangeListener () 120 { 121 public void propertyChange(PropertyChangeEvent evt) 122 { 123 label.setText(pcEditor.getAsText()); 124 } 125 }); 126 } 127 } 128 this.panel.add((Component ) component); 129 } 130 catch (Exception exception) 131 { 132 exception.printStackTrace(); 133 } 134 } 135 136 this.panel.validate(); 137 this.panel.repaint(); 138 } 139 } 140 | Popular Tags |