001 /* 002 Copyright (C) 2004 Renaud Pawlak, Laurent Martelli 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 import java.awt.Dimension; 021 import java.awt.Insets; 022 import java.awt.event.ActionEvent; 023 import java.awt.event.ActionListener; 024 import java.io.File; 025 import javax.swing.JButton; 026 import javax.swing.JFileChooser; 027 import javax.swing.JTextField; 028 import org.objectweb.jac.aspects.gui.FieldEditor; 029 import org.objectweb.jac.aspects.gui.ResourceManager; 030 import org.objectweb.jac.core.rtti.FieldItem; 031 032 /** 033 * Base class for file related types. It shows a TextField and a 034 * button to show a file chooser. 035 */ 036 public abstract class AbstractFileEditor extends AbstractFieldEditor 037 implements FieldEditor, ActionListener 038 { 039 /** Stores the file path value. */ 040 protected JTextField textField; 041 042 /** 043 * Constructs a new File editor. 044 */ 045 public AbstractFileEditor(Object substance, FieldItem field) { 046 super(substance,field); 047 textField = new JTextField(20); 048 textField.setMaximumSize(new Dimension(1000,22)); 049 textField.addFocusListener(this); 050 add(textField); 051 052 JButton button = new JButton(ResourceManager.getIconResource("open_icon")); 053 button.setToolTipText("Edit"); 054 button.setActionCommand("choose"); 055 button.addActionListener(this); 056 button.setMargin(new Insets(1,1,1,1)); 057 add(button); 058 } 059 060 /** 061 * Handles the actions performed by the users on this view. 062 * 063 * <p>On an URL editor, a "choose" action can be performed to allow 064 * the user to open a file chooser box and to navigate within the 065 * file system to choose his file. 066 * 067 * @param evt the performed action 068 */ 069 public void actionPerformed(ActionEvent evt) { 070 if (evt.getActionCommand().equals("choose")) { 071 JFileChooser chooser = createFileChooser(); 072 int returnVal = chooser.showOpenDialog(this); 073 074 if (returnVal==JFileChooser.APPROVE_OPTION) { 075 textField.setText(chooser.getSelectedFile().toString()); 076 } 077 } 078 } 079 080 /** 081 * Returns a file chooser initialized with the current value 082 */ 083 JFileChooser createFileChooser() { 084 return new JFileChooser(); 085 } 086 087 public void setValue(Object value) { 088 super.setValue(value); 089 if (value==null) 090 textField.setText(""); 091 else 092 textField.setText(value.toString()); 093 } 094 095 public void setWidth(int width) { 096 textField.setColumns(width); 097 } 098 099 /** 100 * Set the focus on the JTextField 101 */ 102 public void requestFocus() { 103 textField.requestFocus(); 104 loggerFocus.debug("focusing "+textField.getClass().getName()); 105 } 106 107 }