View Javadoc
1 package xdoclet.gui.swing; 2 3 import org.apache.commons.logging.LogFactory; 4 5 import javax.swing.*; 6 import javax.swing.filechooser.FileFilter; 7 import javax.swing.event.DocumentListener; 8 import javax.swing.event.DocumentEvent; 9 10 import java.beans.*; 11 import java.awt.*; 12 import java.awt.event.ActionEvent; 13 import java.io.File; 14 15 /*** 16 * Support for a PropertyEditor that uses text. 17 * @author <a href="mailto:aslak.hellesoy at bekk.no">Aslak Hellesøy</a> 18 * @version $Revision: 1.1 $ 19 */ 20 class PropertyFile extends JPanel implements DocumentListener, PropertyChangeListener { 21 private final PropertyEditor _propertyEditor; 22 private final PropertyDescriptor _propertyDescriptor; 23 private final Object _bean; 24 25 private JTextField _fileField = new JTextField(); 26 private JFileChooser _fileChooser = new JFileChooser(); 27 28 private class BrowseAction extends AbstractAction { 29 public BrowseAction() { 30 super("..."); 31 } 32 33 public void actionPerformed(ActionEvent evt) { 34 int returnVal = _fileChooser.showDialog(PropertyFile.this, "Output Directory"); 35 if( returnVal == JFileChooser.APPROVE_OPTION ) { 36 File file = _fileChooser.getSelectedFile(); 37 // _propertyEditor.setValue( file ); 38 _fileField.setText( file.getAbsolutePath() ); 39 } 40 } 41 } 42 43 PropertyFile(PropertyEditor propertyEditor, PropertyDescriptor propertyDescriptor, Object bean ) { 44 super(new BorderLayout()); 45 System.out.println("new PropertyFile"); 46 _propertyEditor = propertyEditor; 47 _propertyDescriptor = propertyDescriptor; 48 _bean = bean; 49 50 _fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 51 52 add( _fileField, BorderLayout.CENTER); 53 add( new JButton(new BrowseAction()), BorderLayout.EAST); 54 55 _fileField.setText(_propertyEditor.getValue() != null ? _propertyEditor.getAsText() : ""); 56 57 _fileField.getDocument().addDocumentListener(this); 58 _propertyEditor.addPropertyChangeListener(this); 59 updateEditor(); 60 } 61 62 protected void updateEditor() { 63 try { 64 _propertyEditor.setValue(new File( _fileField.getText() )); 65 } catch (IllegalArgumentException e) { 66 LogFactory.getLog(PropertyFile.class).error("Couldn't set new value", e); 67 } 68 } 69 70 public void propertyChange(PropertyChangeEvent evt) { 71 // Set the new value 72 try { 73 // Get the source. Should be "our" Property editor. 74 // The event will always contain null as the newValue :-( 75 PropertyEditor propertyEditor = (PropertyEditor) evt.getSource(); 76 Object newValue = propertyEditor.getValue(); 77 System.out.println("New value:" + newValue); 78 _propertyDescriptor.getWriteMethod().invoke( _bean, new Object[] {newValue} ); 79 } catch (Exception e) { 80 LogFactory.getLog(PropertyFile.class).error("Couldn't set new value", e); 81 throw new IllegalStateException(e.getMessage()); 82 } 83 } 84 85 public void insertUpdate(DocumentEvent e) { 86 updateEditor(); 87 } 88 89 public void removeUpdate(DocumentEvent e) { 90 updateEditor(); 91 } 92 93 public void changedUpdate(DocumentEvent e) { 94 updateEditor(); 95 } 96 }

This page was automatically generated by Maven