View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  
21  package org.xnap.gui.component;
22  
23  import java.awt.Insets;
24  import java.awt.event.ActionEvent;
25  import java.io.File;
26  
27  import javax.swing.AbstractAction;
28  import javax.swing.Action;
29  import javax.swing.Box;
30  import javax.swing.BoxLayout;
31  import javax.swing.JButton;
32  import javax.swing.JFileChooser;
33  import javax.swing.JPanel;
34  import javax.swing.JTextField;
35  
36  import org.xnap.XNap;
37  import org.xnap.gui.util.IconHelper;
38  
39  /***
40   * Provides a panel with a <code>JTextField</code> and a button for file
41   * selection.
42   */
43  public class FilePanel extends JPanel {
44      
45      //--- Data field(s) ---
46  
47      private XNapTextField jtfFilename;
48  
49      //--- Constructor(s) ---
50  	
51      public FilePanel(String text, int size) 
52      {
53  		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
54  
55  		jtfFilename = new XNapTextField(text, size);
56  		jtfFilename.setCompletionModel(new FileCompletionModel());
57  		jtfFilename.setPreferences("filePanel");
58  		add(jtfFilename);
59  
60  		add(Box.createHorizontalStrut(1));
61  
62  		JButton jbChooser = new XNapButton(new FileChooserAction());
63  		jbChooser.setMargin(new Insets(1, 1, 1, 1));
64  		add(jbChooser);
65      }
66  
67      /***
68       * Sub classes can overwrite this.
69       */
70      protected void filenameSelected(String dir)
71      {
72      }
73  
74      public String getFilename()
75      {
76  		return jtfFilename.getText();
77      }
78  
79      public void setFilename(String dir)
80      {
81  		jtfFilename.setText(dir);
82      }
83  
84      public XNapTextField getTextField()
85      {
86  		return jtfFilename;
87      }
88  
89      // --- Inner Class(es) ---
90  
91      private class FileChooserAction extends AbstractAction {
92  	
93  		JFileChooser chooser;
94  
95          public FileChooserAction() 
96  		{
97  			chooser = new JFileChooser();
98  			chooser.setApproveButtonText(XNap.tr("OK"));
99  			chooser.setDialogTitle(XNap.tr("Choose File"));
100 
101 			putValue(IconHelper.XNAP_ICON, "fileopen.png");
102             putValue(Action.SHORT_DESCRIPTION, XNap.tr("Choose a file") );
103 		}
104 
105         public void actionPerformed(ActionEvent event) 
106 		{
107 			chooser.setSelectedFile(new File(getFilename()));
108 	    
109 			if (chooser.showSaveDialog(FilePanel.this) 
110 				== JFileChooser.APPROVE_OPTION) {
111 				String filename = chooser.getSelectedFile().getAbsolutePath();
112 				setFilename(filename);
113 				filenameSelected(filename);
114 			}
115 		}
116     } 
117 
118 }