KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak, Laurent Martelli
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 import java.awt.Dimension JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.io.File JavaDoc;
24 import javax.swing.JButton JavaDoc;
25 import javax.swing.JFileChooser JavaDoc;
26 import org.objectweb.jac.aspects.gui.FieldEditor;
27 import org.objectweb.jac.aspects.gui.ResourceManager;
28 import org.objectweb.jac.core.rtti.FieldItem;
29 import org.objectweb.jac.util.Files;
30
31 /**
32  * This is a special value editor that allows the user to nicely edit
33  * a File.
34  */

35 public class FileEditor extends AbstractFileEditor
36     implements FieldEditor, ActionListener JavaDoc
37 {
38    /**
39     * Constructs a new File editor.
40     */

41     public FileEditor(Object JavaDoc substance, FieldItem field) {
42         super(substance,field);
43     }
44
45     /**
46      * Returns a file chooser initialized with the current value
47      */

48     JFileChooser JavaDoc createFileChooser() {
49         return new JFileChooser JavaDoc((File JavaDoc)getValue());
50     }
51
52     public Object JavaDoc getValue() {
53         String JavaDoc file = textField.getText();
54         if (file.equals("")) {
55             return null;
56         }
57
58         return createFileInstance(Files.expandFileName(file));
59     }
60
61     /**
62      * Create a new instance of File. Override this method to
63      * instanciate a subclass of java.util.File.
64      * @param path the path to create a file for
65      */

66     protected File JavaDoc createFileInstance(String JavaDoc path) {
67         if (type!=null)
68             try {
69                 return (File JavaDoc)type.newInstance(new Object JavaDoc[] {path});
70             } catch(Exception JavaDoc e) {
71                 logger.error("FileEditor.createFileInstance: failed to instanciate "+type+
72                              ", falling back on java.io.File");
73                 return new File JavaDoc(path);
74             }
75         else
76             return new File JavaDoc(path);
77     }
78
79     public void setValue(Object JavaDoc value) {
80         super.setValue(value);
81         if (value==null)
82             textField.setText("");
83         else
84             textField.setText(value.toString());
85     }
86 }
87
Popular Tags