001 /* 002 Copyright (C) 2001-2003 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.event.ActionEvent; 022 import java.awt.event.ActionListener; 023 import java.io.File; 024 import javax.swing.JButton; 025 import javax.swing.JFileChooser; 026 import javax.swing.JTextField; 027 import org.objectweb.jac.aspects.gui.FieldEditor; 028 import org.objectweb.jac.aspects.gui.ResourceManager; 029 import org.objectweb.jac.core.rtti.FieldItem; 030 import org.objectweb.jac.util.Files; 031 032 /** 033 * This is a special value editor that allows the user to nicely edit 034 * a File. 035 */ 036 public class FileEditor extends AbstractFileEditor 037 implements FieldEditor, ActionListener 038 { 039 /** 040 * Constructs a new File editor. 041 */ 042 public FileEditor(Object substance, FieldItem field) { 043 super(substance,field); 044 } 045 046 /** 047 * Returns a file chooser initialized with the current value 048 */ 049 JFileChooser createFileChooser() { 050 return new JFileChooser((File)getValue()); 051 } 052 053 public Object getValue() { 054 String file = textField.getText(); 055 if (file.equals("")) { 056 return null; 057 } 058 059 return createFileInstance(Files.expandFileName(file)); 060 } 061 062 /** 063 * Create a new instance of File. Override this method to 064 * instanciate a subclass of java.util.File. 065 * @param path the path to create a file for 066 */ 067 protected File createFileInstance(String path) { 068 if (type!=null) 069 try { 070 return (File)type.newInstance(new Object[] {path}); 071 } catch(Exception e) { 072 logger.error("FileEditor.createFileInstance: failed to instanciate "+type+ 073 ", falling back on java.io.File"); 074 return new File(path); 075 } 076 else 077 return new File(path); 078 } 079 080 public void setValue(Object value) { 081 super.setValue(value); 082 if (value==null) 083 textField.setText(""); 084 else 085 textField.setText(value.toString()); 086 } 087 }