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  package org.xnap.gui.component;
21  
22  import java.awt.Insets;
23  import java.awt.event.ActionEvent;
24  import java.io.File;
25  
26  import javax.swing.AbstractAction;
27  import javax.swing.Action;
28  import javax.swing.Box;
29  import javax.swing.BoxLayout;
30  import javax.swing.JButton;
31  import javax.swing.JFileChooser;
32  import javax.swing.JTextField;
33  
34  import org.xnap.XNap;
35  import org.xnap.gui.util.IconHelper;
36  import org.xnap.util.FileHelper;
37  import org.xnap.util.SystemHelper;
38  
39  /***
40   * This class provides a {@link JTextField} that displays a directory
41   * and button that can launch a {@link JFileChooser} dialog or 
42   * a {@link DirectoryChooser} dialog to select a directory.  
43   */
44  public class DirectoryPanel extends Box
45  {
46      
47      //--- Data field(s) ---
48  
49      private XNapTextField jtfFilename;
50  
51      //--- Constructor(s) ---
52  	
53      public DirectoryPanel(String text, int size) 
54      {
55  		super(BoxLayout.X_AXIS);
56  
57  		jtfFilename = new XNapTextField(text, size);
58  		jtfFilename.setCompletionModel(new FileCompletionModel(true));
59  		jtfFilename.setPreferences("directoryPanel");
60  		add(jtfFilename);
61  
62  		add(Box.createHorizontalStrut(1));
63  
64  		// This works around a bug in Mac OS X.
65  		// When a modal dialog (this dialog) is shown on top of another modal 
66  		// dialog (like the wizard), the second dialog does not respond to
67  		// gui events and gets stuck in the background.
68  		Action action;
69  		if (SystemHelper.isMacOSX) {
70  			action = new SwingDirectoryChooserAction();
71  		}
72  		else {
73  			action = new DirectoryChooserAction();
74  		}
75  		JButton jbChooser = new XNapButton(action);
76  		jbChooser.setMargin(new Insets(1, 1, 1, 1));
77  		add(jbChooser);
78      }
79  
80      // --- Method(s) ---
81  
82      /***
83       * Sub classes can overwrite this.
84       */
85      protected void directorySelected(String dir)
86      {
87      }
88  
89      public String getDirectory()
90      {
91  		return FileHelper.directory(jtfFilename.getText());
92      }
93  
94      public void setDirectory(String dir)
95      {
96  		jtfFilename.setText(dir);
97      }
98  
99      public JTextField getTextField()
100     {
101 		return jtfFilename;
102     }
103 
104     // --- Inner Class(es) ---
105 
106     private class DirectoryChooserAction extends AbstractAction
107     {
108 		private DirectoryChooser chooser;
109 	
110         public DirectoryChooserAction() 
111 		{
112 			chooser = new DirectoryChooser();
113 
114 			putValue(IconHelper.XNAP_ICON, "fileopen.png");
115             putValue(Action.SHORT_DESCRIPTION, XNap.tr("Choose a directory"));
116 		}
117 
118         public void actionPerformed(ActionEvent event) 
119 		{
120 			chooser.setSelectedDirectory(new File(getDirectory()));
121 
122 			if (chooser.showChooseDialog(DirectoryPanel.this) 
123 				== DirectoryChooser.APPROVE_OPTION) {
124 				setDirectory(chooser.getSelectedDirectory().getAbsolutePath());
125 				directorySelected(getDirectory());
126 			}
127 		}
128     } 
129 
130     private class SwingDirectoryChooserAction extends AbstractAction {
131 
132 		private JFileChooser chooser;
133 	
134         public SwingDirectoryChooserAction() 
135 		{
136 			chooser = new JFileChooser();
137 			chooser.setApproveButtonText(XNap.tr("OK"));
138 			chooser.setDialogTitle(XNap.tr("Choose Directory"));
139 			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
140 
141 			putValue(IconHelper.XNAP_ICON, "fileopen.png");
142 			putValue(Action.SHORT_DESCRIPTION, XNap.tr("Choose a directory"));
143 		}
144 
145         public void actionPerformed(ActionEvent event) 
146 		{
147 			chooser.setSelectedFile(new File(getDirectory()));
148 	    
149 			if (chooser.showOpenDialog(DirectoryPanel.this)
150 				== JFileChooser.APPROVE_OPTION) {
151 				setDirectory(chooser.getSelectedFile().getAbsolutePath());
152 				directorySelected(getDirectory());
153 			}
154 		}
155     } 
156 
157 }