001    /*
002      Copyright (C) 2001 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 org.objectweb.jac.aspects.gui.ResourceManager;
021    import org.objectweb.jac.aspects.gui.FieldEditor;
022    import org.objectweb.jac.core.rtti.FieldItem;
023    
024    import java.net.MalformedURLException;
025    import java.net.URL;
026    
027    import java.awt.Dimension;
028    import java.awt.event.ActionEvent;
029    import java.awt.event.ActionListener;
030    import javax.swing.JButton;
031    import javax.swing.JFileChooser;
032    import javax.swing.JTextField;
033    
034    /**
035     * This is a special value editor that allows the user to nicely edit
036     * an URL. */
037    
038    public class URLEditor extends AbstractFieldEditor
039        implements FieldEditor, ActionListener
040    {
041        /** Stores the URL value. */
042        protected JTextField textField;
043    
044       /**
045        * Constructs a new URL editor.
046        */
047    
048        public URLEditor(Object substance, FieldItem field) {
049            super(substance,field);
050            textField = new JTextField(10);
051            textField.setMaximumSize(new Dimension(1000,22));
052            textField.addFocusListener(this);
053            add(textField);
054    
055            JButton button = new JButton (ResourceManager.getIconResource("edit_icon"));
056            button.setToolTipText("Edit");
057            button.setActionCommand("choose");
058            button.addActionListener(this);
059            add(button);
060        }
061    
062        /**
063        * Handles the actions performed by the users on this view.
064        *
065        * <p>On an URL editor, a "choose" action can be performed to allow
066        * the user to open a file chooser box and to navigate within the
067        * file system to choose his file.
068        *
069        * @param evt the performed action */
070    
071        public void actionPerformed(ActionEvent evt) {
072            if (evt.getActionCommand().equals("choose")) {
073                JFileChooser chooser = new JFileChooser();      
074                //chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
075                /*String fileSelectionMode = (String)field.getAttribute(GuiAC.FILE_SELECTION_MODE");
076                  if(fileSelectionMode!=null) {
077                  if(fileSelectionMode.equals("DIRECTORIES_ONLY")) {
078                  chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
079                  } else if(fileSelectionMode.equals("FILES_AND_DIRECTORIES")) {
080                  chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
081                  }
082                  } 
083                  String[] fileExtensions = (String[])field.getAttribute(GuiAC.FILE_EXTENSIONS);
084                  String fileExtensionsDescription = 
085                  (String)field.getAttribute(GuiAC.FILE_EXTENSIONS_DESCRIPTION");
086    
087                  if(fileExtensions!=null) {
088                  CustomizedFileFilter filter = 
089                  new CustomizedFileFilter(fileExtensions,fileExtensionsDescription);
090                  chooser.setFileFilter(filter);
091                  }*/
092                int returnVal = chooser.showOpenDialog( this );
093             
094                if ( returnVal == JFileChooser.APPROVE_OPTION ) {
095                    textField.setText( "file:" + chooser.getSelectedFile().toString() );
096                }
097            }
098        }
099    
100        public Object getValue() {
101            String urlString = textField.getText();
102            if (urlString.equals("")) {
103                return null;
104            }
105            if (urlString.indexOf(":")==-1) {
106                urlString = "file:/"+urlString;
107            }
108          
109            try {
110                return new URL(urlString);
111            } catch (MalformedURLException e) {
112                logger.warn("Malfromed URL: "+urlString);
113                return null;
114            }
115        }
116    
117        public void setValue(Object value) {
118            super.setValue(value);
119            if( value == null ) 
120                textField.setText("");
121            else
122                textField.setText(((URL)value).toString());
123        }
124    
125        public void setWidth(int width) {
126            textField.setColumns(width);
127        }
128    
129        /**
130         * Set the focus on the JTextField
131         */
132        public void requestFocus() {
133            textField.requestFocus();
134            loggerFocus.debug("focusing "+textField.getClass().getName());
135        }
136    
137    }