KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > AttachmentEditor


1 /*
2   Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui.swing;
19
20
21
22 import org.objectweb.jac.core.rtti.FieldItem;
23 import org.objectweb.jac.lib.Attachment;
24 import org.objectweb.jac.util.Streams;
25 import org.objectweb.jac.util.WrappedThrowableException;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import javax.swing.JFileChooser JavaDoc;
30
31 /**
32  * This is a special value editor that allows the user to nicely edit
33  * an File.
34  */

35
36 public class AttachmentEditor extends FileEditor
37 {
38    public AttachmentEditor(Object JavaDoc substance, FieldItem field) {
39       super(substance,field);
40    }
41
42    public Object JavaDoc getValue() {
43       String JavaDoc file = textField.getText();
44       if (file.equals("")) {
45          return null;
46       }
47       try {
48          return new Attachment(Streams.readStream(new FileInputStream JavaDoc(file)),
49                                null,file);
50       } catch (IOException JavaDoc e) {
51          throw new WrappedThrowableException(e);
52       }
53    }
54
55    public void setValue(Object JavaDoc value) {
56       super.setValue(value);
57       if (value==null)
58          textField.setText("");
59       else
60          textField.setText(((Attachment)value).getName());
61    }
62
63
64    /**
65     * Returns a file chooser initialized with the current value
66     */

67    JFileChooser JavaDoc createFileChooser() {
68       return new JFileChooser JavaDoc(new File JavaDoc(textField.getText()));
69    }
70
71 }
72
Popular Tags