KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2004 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.Insets JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.io.File JavaDoc;
25 import javax.swing.JButton JavaDoc;
26 import javax.swing.JFileChooser JavaDoc;
27 import org.objectweb.jac.aspects.gui.FieldEditor;
28 import org.objectweb.jac.aspects.gui.Length;
29 import org.objectweb.jac.aspects.gui.ResourceManager;
30 import org.objectweb.jac.core.rtti.FieldItem;
31
32 /**
33  * Base class for file related types. It shows a TextField and a
34  * button to show a file chooser.
35  */

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

42     public AbstractFileEditor(Object JavaDoc substance, FieldItem field) {
43         super(substance,field);
44         textField = new JTextField(20);
45         textField.addFocusListener(this);
46         add(textField);
47
48         JButton JavaDoc button = new JButton JavaDoc(ResourceManager.getIconResource("open_icon"));
49         button.setToolTipText("Edit");
50         button.setActionCommand("choose");
51         button.addActionListener(this);
52         button.setMargin(new Insets JavaDoc(1,1,1,1));
53         add(button);
54     }
55
56     /**
57      * Handles the actions performed by the users on this view.
58      *
59      * <p>On an URL editor, a "choose" action can be performed to allow
60      * the user to open a file chooser box and to navigate within the
61      * file system to choose his file.
62      *
63      * @param evt the performed action
64      */

65     public void actionPerformed(ActionEvent JavaDoc evt) {
66         if (evt.getActionCommand().equals("choose")) {
67             JFileChooser JavaDoc chooser = createFileChooser();
68             int returnVal = chooser.showOpenDialog(this);
69          
70             if (returnVal==JFileChooser.APPROVE_OPTION) {
71                 textField.setText(chooser.getSelectedFile().toString());
72             }
73         }
74     }
75
76     /**
77      * Returns a file chooser initialized with the current value
78      */

79     JFileChooser JavaDoc createFileChooser() {
80         return new JFileChooser JavaDoc();
81     }
82
83     public void setValue(Object JavaDoc value) {
84         super.setValue(value);
85         if (value==null)
86             textField.setText("");
87         else
88             textField.setText(value.toString());
89     }
90 }
91
Popular Tags