001    /**
002     * FileEditor.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * GAIA - Group for Artificial Intelligence Applications
006     * http://gaia.fdi.ucm.es
007     * 02/11/2007
008     */
009    package jcolibri.method.gui.editors;
010    
011    import java.awt.FileDialog;
012    import java.awt.Frame;
013    import java.io.File;
014    import java.util.Collection;
015    
016    import javax.swing.JButton;
017    import javax.swing.JComponent;
018    import javax.swing.JPanel;
019    import javax.swing.JTextField;
020    
021    
022    /**
023     * Parameter Editor for File values.
024     * 
025     * @author Juan A. Recio-Garcia
026     * @version 1.0
027     * @see jcolibri.method.gui.editors.ParameterEditor
028     */
029    public class FileEditor extends JPanel implements ParameterEditor {
030            private static final long serialVersionUID = 1L;
031    
032            
033            JTextField text;
034            JButton button;
035            
036            
037            /**
038             *  Creates a new instance
039             */
040            public FileEditor() {
041                    text = new JTextField("");
042                    button = new JButton("...");
043                    this.add(text);
044                    this.add(button);
045                    button.addActionListener(new java.awt.event.ActionListener() {
046                            public void actionPerformed(java.awt.event.ActionEvent evt) {
047                                    buttonPressed();
048                            }
049                    });
050                    text.setMinimumSize(new java.awt.Dimension(150, 25));
051                    text.setPreferredSize(new java.awt.Dimension(150, 25));
052    
053            }
054    
055            private void buttonPressed() {
056                    FileDialog fd = new FileDialog((Frame)null,
057                                    "Load file...", FileDialog.LOAD);
058                    fd.setVisible(true);
059                    if (fd.getFile() != null) {
060                            text.setText(fd.getDirectory() + fd.getFile());
061                    }
062            }
063    
064            /**
065             * Returns a File object
066             */
067            public Object getEditorValue() {
068                try{
069                    return new File(text.getText());
070                }catch(Exception e){}
071                return null;
072                    
073            }
074    
075            /**
076             * Returns the JComponent
077             */
078            public JComponent getJComponent() {
079                    return (JComponent) this;
080            }
081    
082            /**
083             * Receives a File value
084             */
085            public void setEditorValue(Object defaultValue) {
086                    if(defaultValue==null)
087                    {
088                        text.setText("");
089                        return;
090                    } 
091                    if (!(defaultValue instanceof String))
092                            return;
093                    String value = (String) defaultValue;
094                    text.setText(value);
095            }
096    
097            /**
098             * Does nothing
099             */
100            public void setAllowedValues(Collection<Object> allowedValues)
101            {
102            }
103    
104    }