KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001 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.net.MalformedURLException JavaDoc;
24 import java.net.URL 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  * This is a special value editor that allows the user to nicely edit
34  * an URL. */

35
36 public class URLEditor extends TextFieldEditor
37     implements FieldEditor, ActionListener JavaDoc
38 {
39     /**
40      * Constructs a new URL editor.
41      */

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

64     public void actionPerformed(ActionEvent JavaDoc evt) {
65         if (evt.getActionCommand().equals("choose")) {
66             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
67             //chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
68
/*String fileSelectionMode = (String)field.getAttribute(GuiAC.FILE_SELECTION_MODE");
69               if(fileSelectionMode!=null) {
70               if(fileSelectionMode.equals("DIRECTORIES_ONLY")) {
71               chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
72               } else if(fileSelectionMode.equals("FILES_AND_DIRECTORIES")) {
73               chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
74               }
75               }
76               String[] fileExtensions = (String[])field.getAttribute(GuiAC.FILE_EXTENSIONS);
77               String fileExtensionsDescription =
78               (String)field.getAttribute(GuiAC.FILE_EXTENSIONS_DESCRIPTION");
79
80               if(fileExtensions!=null) {
81               CustomizedFileFilter filter =
82               new CustomizedFileFilter(fileExtensions,fileExtensionsDescription);
83               chooser.setFileFilter(filter);
84               }*/

85             int returnVal = chooser.showOpenDialog( this );
86          
87             if ( returnVal == JFileChooser.APPROVE_OPTION ) {
88                 textField.setText( "file:" + chooser.getSelectedFile().toString() );
89             }
90         }
91     }
92
93     public Object JavaDoc getValue() {
94         String JavaDoc urlString = textField.getText();
95         if (urlString.equals("")) {
96             return null;
97         }
98         if (urlString.indexOf(":")==-1) {
99             urlString = "file:/"+urlString;
100         }
101       
102         try {
103             return new URL JavaDoc(urlString);
104         } catch (MalformedURLException JavaDoc e) {
105             logger.warn("Malfromed URL: "+urlString);
106             return null;
107         }
108     }
109
110     public void setValue(Object JavaDoc value) {
111         super.setValue(value);
112         if( value == null )
113             textField.setText("");
114         else
115             textField.setText(((URL JavaDoc)value).toString());
116     }
117
118 }
119
Popular Tags