View Javadoc

1   /*
2    *                Doelan development code
3    *
4    * This code may be freely distributed and modified under the
5    * terms of the GNU General Public Licence.  This should
6    * be distributed with the code. If you do not have a copy,
7    * see:
8    *
9    *      http://www.gnu.org/copyleft/gpl.txt
10   *
11   * Copyright (c) 2004-2005 ENS Microarray Platform
12   * Copyright for this code is held jointly by the individual
13   * authors.  These should be listed in @author doc comments.
14   *
15   * For more information on the Doelan project and its aims,
16   * or to join the Doelan mailing list, visit the home page
17   * at:
18   *
19   *      http://www.transcriptome.ens.fr/doelan
20   */
21  
22  package fr.ens.transcriptome.doelan.gui;
23  
24  import java.awt.GridBagConstraints;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.io.File;
28  
29  import javax.swing.JButton;
30  import javax.swing.JFileChooser;
31  import javax.swing.JLabel;
32  import javax.swing.JPanel;
33  import javax.swing.filechooser.FileFilter;
34  
35  import fr.ens.transcriptome.doelan.DoelanRegistery;
36  import fr.ens.transcriptome.nividic.util.SystemUtils;
37  
38  /***
39   * This class implements a file chooser.
40   * @author Laurent Jourdren
41   */
42  public class FileChooserWidget {
43  
44    private String label;
45    private String extension;
46    private String extensionDescription;
47    private JPanel jPanel;
48    private int position;
49    private int mnemonicKey;
50    private String buttonText;
51  
52    private File file;
53    private boolean fileChooser;
54    private JLabel filenameLabel = new JLabel();
55  
56    //
57    // Getters
58    //
59  
60    /***
61     * Get if the file choose must be displayed.
62     * @return true if the file choose must be displayed
63     */
64    private boolean isFileChooser() {
65  
66      return fileChooser;
67    }
68  
69    /***
70     * Get the data file.
71     * @return The data file
72     */
73    public File getFile() {
74      return file;
75    }
76  
77    /***
78     * Get the text of the label.
79     * @return Returns the labelText
80     */
81    private String getLabel() {
82      return label;
83    }
84  
85    /***
86     * Get extension.
87     * @return Returns the extension
88     */
89    private String getExtension() {
90      return extension;
91    }
92  
93    /***
94     * Get the description of the extension.
95     * @return Returns the extensionDescription
96     */
97    private String getExtensionDescription() {
98      return extensionDescription;
99    }
100 
101   /***
102    * Get the panel.
103    * @return Returns the Panel
104    */
105   private JPanel getPanel() {
106     return jPanel;
107   }
108 
109   /***
110    * Get the position in the panel.
111    * @return Returns the position
112    */
113   private int getPosition() {
114     return position;
115   }
116 
117   /***
118    * Get the mnemonic key.
119    * @return Returns the mnemonicKey
120    */
121   public int getMnemonicKey() {
122     return mnemonicKey;
123   }
124 
125   /***
126    * Get the text of the button
127    * @return Returns the buttonText
128    */
129   public String getButtonText() {
130     return buttonText;
131   }
132 
133   //
134   // Setters
135   //
136 
137   /***
138    * Set the data file.
139    * @param file The data file
140    */
141   private void setFile(final File file) {
142     this.file = file;
143   }
144 
145   /***
146    * Set if the file chooser must be visible.
147    * @param visible
148    */
149   private void setFileChooser(final boolean visible) {
150     fileChooser = visible;
151   }
152 
153   /***
154    * Set the label of the text.
155    * @param labelText The labelText to set
156    */
157   private void setLabel(final String labelText) {
158     this.label = labelText;
159   }
160 
161   /***
162    * Set the extension.
163    * @param extension The extension to set
164    */
165   private void setExtension(final String extension) {
166     this.extension = extension;
167   }
168 
169   /***
170    * Set the description of the extension.
171    * @param extensionDescription The extensionDescription to set
172    */
173   private void setExtensionDescription(final String extensionDescription) {
174     this.extensionDescription = extensionDescription;
175   }
176 
177   /***
178    * Set the panel.
179    * @param panel The Panel to set
180    */
181   private void setPanel(final JPanel panel) {
182     jPanel = panel;
183   }
184 
185   /***
186    * Set the postion in the panel.
187    * @param position The position to set
188    */
189   private void setPosition(final int position) {
190     this.position = position;
191   }
192 
193   /***
194    * set the mnemonic key.
195    * @param mnemonicKey The mnemonicKey to set
196    */
197   public void setMnemonicKey(final int mnemonicKey) {
198     this.mnemonicKey = mnemonicKey;
199   }
200 
201   /***
202    * Set the text of the button
203    * @param buttonText The buttonText to set
204    */
205   public void setButtonText(final String buttonText) {
206     this.buttonText = buttonText;
207   }
208 
209   //
210   // Other methods
211   //
212 
213   private void init() {
214 
215     GridBagConstraints gridBagConstraints;
216     //JPanel selectorPanel = new JPanel();
217     //selectorPanel.setLayout(new java.awt.GridBagLayout());
218     final JPanel selectorPanel = getPanel();
219 
220     JButton chooseFileButton = new JButton();
221     chooseFileButton.setToolTipText("Press this button to select a file");
222 
223     //  File label
224     JLabel jLabel1 = new JLabel(getLabel());
225     gridBagConstraints = new java.awt.GridBagConstraints();
226     gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
227     gridBagConstraints.gridx = 0;
228     gridBagConstraints.gridy = getPosition();
229     gridBagConstraints.ipadx = 30;
230     selectorPanel.add(jLabel1, gridBagConstraints);
231 
232     //  Name of file selected
233     gridBagConstraints = new java.awt.GridBagConstraints();
234     gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
235     gridBagConstraints.gridx = 1;
236     gridBagConstraints.gridwidth = 3;
237     gridBagConstraints.gridy = getPosition();
238     selectorPanel.add(filenameLabel, gridBagConstraints);
239 
240     if (isFileChooser()) {
241 
242       filenameLabel.setText("no file seleted");
243       filenameLabel.setEnabled(false);
244       chooseFileButton.setText(getButtonText());
245 
246       if (!SystemUtils.isMacOsX())
247         chooseFileButton.setMnemonic(getMnemonicKey());
248 
249       // Choose file button
250       gridBagConstraints = new java.awt.GridBagConstraints();
251       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
252       gridBagConstraints.gridx = 4;
253       gridBagConstraints.gridy = getPosition();
254       gridBagConstraints.insets = new java.awt.Insets(3, 10, 3, 0);
255       selectorPanel.add(chooseFileButton, gridBagConstraints);
256 
257     } else {
258       filenameLabel.setText("Genepix Pro data");
259     }
260 
261     //  File chooser
262     chooseFileButton.addActionListener(new ActionListener() {
263       public void actionPerformed(final ActionEvent e) {
264         JFileChooser jfc = new JFileChooser();
265         jfc.setCurrentDirectory(new File(DoelanRegistery
266             .getDoelanDataDirectory()));
267         jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
268         jfc.addChoosableFileFilter(new FileFilter() {
269           public boolean accept(final File f) {
270 
271             if (f.isDirectory())
272               return true;
273             if (f.getName().length() < 4)
274               return false;
275             String end = f.getName().substring(f.getName().length() - 4);
276 
277             return end.toLowerCase().endsWith(getExtension());
278           }
279 
280           public String getDescription() {
281             return getExtensionDescription();
282           }
283         });
284 
285         int result = jfc.showOpenDialog(jPanel);
286         if (result == JFileChooser.APPROVE_OPTION) {
287           setFile(jfc.getSelectedFile());
288           filenameLabel.setText(getFile().getName());
289           filenameLabel.setEnabled(true);
290         }
291 
292       }
293     });
294 
295   }
296 
297   //
298   // Constructor
299   //
300 
301   /***
302    * Public constructor.
303    * @param fileChooserVisible Set if the fileChooser must be visible
304    * @param label the Label of the widget
305    * @param mnemonicKey mnemonic key
306    * @param extension The extension of the file
307    * @param extensionDescription The description of the extension
308    * @param buttonText button text
309    * @param panel Calling panel
310    * @param position Position of the chooser
311    */
312   public FileChooserWidget(final boolean fileChooserVisible,
313       final String label, final int mnemonicKey, final String extension,
314       final String extensionDescription, final String buttonText,
315       final JPanel panel, final int position) {
316 
317     setLabel(label);
318     setMnemonicKey(mnemonicKey);
319     setExtension(extension);
320     setExtensionDescription(extensionDescription);
321     setButtonText(buttonText);
322     setFileChooser(fileChooserVisible);
323     setPanel(panel);
324     setPosition(position);
325     init();
326   }
327 
328 }