001    /*
002      Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com>
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.gui.swing;
019    
020    
021    
022    import org.objectweb.jac.core.rtti.FieldItem;
023    import org.objectweb.jac.lib.Attachment;
024    import org.objectweb.jac.util.Streams;
025    import org.objectweb.jac.util.WrappedThrowableException;
026    import java.io.File;
027    import java.io.FileInputStream;
028    import java.io.IOException;
029    import javax.swing.JFileChooser;
030    
031    /**
032     * This is a special value editor that allows the user to nicely edit
033     * an File. 
034     */
035    
036    public class AttachmentEditor extends FileEditor
037    {
038       public AttachmentEditor(Object substance, FieldItem field) {
039          super(substance,field);
040       }
041    
042       public Object getValue() {
043          String file = textField.getText();
044          if (file.equals("")) {
045             return null;
046          }
047          try {
048             return new Attachment(Streams.readStream(new FileInputStream(file)),
049                                   null,file);
050          } catch (IOException e) {
051             throw new WrappedThrowableException(e);
052          }
053       }
054    
055       public void setValue(Object value) {
056          super.setValue(value);
057          if (value==null) 
058             textField.setText("");
059          else
060             textField.setText(((Attachment)value).getName());
061       }
062    
063    
064       /**
065        * Returns a file chooser initialized with the current value
066        */
067       JFileChooser createFileChooser() {
068          return new JFileChooser(new File(textField.getText()));
069       }
070    
071    }