JAL-1421 added type to List and Vector
[jalview.git] / src / jalview / io / JalviewFileChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  *
5  * This file is part of Jalview.
6  *
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * Jalview is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE.  See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 //////////////////////////////////////////////////////////////////
22 package jalview.io;
23
24 import jalview.bin.Cache;
25 import jalview.gui.JvOptionPane;
26 import jalview.util.MessageManager;
27 import jalview.util.Platform;
28
29 import java.awt.Component;
30 import java.awt.Dimension;
31 import java.awt.EventQueue;
32 import java.awt.HeadlessException;
33 import java.awt.event.MouseAdapter;
34 import java.awt.event.MouseEvent;
35 import java.io.File;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.StringTokenizer;
39 import java.util.Vector;
40
41 import javax.swing.DefaultListCellRenderer;
42 import javax.swing.JFileChooser;
43 import javax.swing.JList;
44 import javax.swing.JPanel;
45 import javax.swing.JScrollPane;
46 import javax.swing.SpringLayout;
47 import javax.swing.SwingUtilities;
48 import javax.swing.border.TitledBorder;
49 import javax.swing.plaf.basic.BasicFileChooserUI;
50
51 /**
52  * Enhanced file chooser dialog box.
53  *
54  * NOTE: bug on Windows systems when filechooser opened on directory to view
55  * files with colons in title.
56  *
57  * @author AMW
58  *
59  */
60 public class JalviewFileChooser extends JFileChooser
61 {
62   /**
63    * Factory method to return a file chooser that offers readable alignment file
64    * formats
65    * 
66    * @param directory
67    * @param selected
68    * @return
69    */
70   public static JalviewFileChooser forRead(String directory,
71           String selected)
72   {
73     List<String> extensions = new ArrayList<>();
74     List<String> descs = new ArrayList<>();
75     for (FileFormatI format : FileFormats.getInstance().getFormats())
76     {
77       if (format.isReadable())
78       {
79         extensions.add(format.getExtensions());
80         descs.add(format.getName());
81       }
82     }
83     return new JalviewFileChooser(directory,
84             extensions.toArray(new String[extensions.size()]),
85             descs.toArray(new String[descs.size()]), selected, true);
86   }
87
88   /**
89    * Factory method to return a file chooser that offers writable alignment file
90    * formats
91    * 
92    * @param directory
93    * @param selected
94    * @return
95    */
96   public static JalviewFileChooser forWrite(String directory,
97           String selected)
98   {
99     // TODO in Java 8, forRead and forWrite can be a single method
100     // with a lambda expression parameter for isReadable/isWritable
101     List<String> extensions = new ArrayList<>();
102     List<String> descs = new ArrayList<>();
103     for (FileFormatI format : FileFormats.getInstance().getFormats())
104     {
105       if (format.isWritable())
106       {
107         extensions.add(format.getExtensions());
108         descs.add(format.getName());
109       }
110     }
111     return new JalviewFileChooser(directory,
112             extensions.toArray(new String[extensions.size()]),
113             descs.toArray(new String[descs.size()]), selected, false);
114   }
115
116   public JalviewFileChooser(String dir)
117   {
118     super(safePath(dir));
119     setAccessory(new RecentlyOpened());
120   }
121
122   public JalviewFileChooser(String dir, String[] suffix, String[] desc,
123           String selected)
124   {
125     this(dir, suffix, desc, selected, true);
126   }
127
128   /**
129    * Constructor for a single choice of file extension and description
130    * 
131    * @param extension
132    * @param desc
133    */
134   public JalviewFileChooser(String extension, String desc)
135   {
136     this(Cache.getProperty("LAST_DIRECTORY"), new String[] { extension },
137             new String[]
138             { desc }, desc, true);
139   }
140
141   JalviewFileChooser(String dir, String[] extensions, String[] descs,
142           String selected, boolean allFiles)
143   {
144     super(safePath(dir));
145     if (extensions.length == descs.length)
146     {
147       List<String[]> formats = new ArrayList<>();
148       for (int i = 0; i < extensions.length; i++)
149       {
150         formats.add(new String[] { extensions[i], descs[i] });
151       }
152       init(formats, selected, allFiles);
153     }
154     else
155     {
156       System.err.println("JalviewFileChooser arguments mismatch: "
157               + extensions + ", " + descs);
158     }
159   }
160
161   private static File safePath(String dir)
162   {
163     if (dir == null)
164     {
165       return null;
166     }
167
168     File f = new File(dir);
169     if (f.getName().indexOf(':') > -1)
170     {
171       return null;
172     }
173     return f;
174   }
175
176   /**
177    * 
178    * @param formats
179    *          a list of {extensions, description} for each file format
180    * @param selected
181    * @param allFiles
182    *          if true, 'any format' option is included
183    */
184   void init(List<String[]> formats, String selected, boolean allFiles)
185   {
186
187     JalviewFileFilter chosen = null;
188
189     // SelectAllFilter needs to be set first before adding further
190     // file filters to fix bug on Mac OSX
191     setAcceptAllFileFilterUsed(allFiles);
192
193     for (String[] format : formats)
194     {
195       JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]);
196       addChoosableFileFilter(jvf);
197       if ((selected != null) && selected.equalsIgnoreCase(format[1]))
198       {
199         chosen = jvf;
200       }
201     }
202
203     if (chosen != null)
204     {
205       setFileFilter(chosen);
206     }
207
208     setAccessory(new RecentlyOpened());
209   }
210
211   @Override
212   public void setFileFilter(javax.swing.filechooser.FileFilter filter)
213   {
214     super.setFileFilter(filter);
215
216     try
217     {
218       if (getUI() instanceof BasicFileChooserUI)
219       {
220         final BasicFileChooserUI fcui = (BasicFileChooserUI) getUI();
221         final String name = fcui.getFileName().trim();
222
223         if ((name == null) || (name.length() == 0))
224         {
225           return;
226         }
227
228         EventQueue.invokeLater(new Thread()
229         {
230           @Override
231           public void run()
232           {
233             String currentName = fcui.getFileName();
234             if ((currentName == null) || (currentName.length() == 0))
235             {
236               fcui.setFileName(name);
237             }
238           }
239         });
240       }
241     } catch (Exception ex)
242     {
243       ex.printStackTrace();
244       // Some platforms do not have BasicFileChooserUI
245     }
246   }
247
248   /**
249    * Returns the selected file format, or null if none selected
250    * 
251    * @return
252    */
253   public FileFormatI getSelectedFormat()
254   {
255     if (getFileFilter() == null)
256     {
257       return null;
258     }
259
260     /*
261      * logic here depends on option description being formatted as 
262      * formatName (extension, extension...)
263      * or the 'no option selected' value
264      * All Files
265      * @see JalviewFileFilter.getDescription
266      */
267     String format = getFileFilter().getDescription();
268     int parenPos = format.indexOf("(");
269     if (parenPos > 0)
270     {
271       format = format.substring(0, parenPos).trim();
272       try
273       {
274         return FileFormats.getInstance().forName(format);
275       } catch (IllegalArgumentException e)
276       {
277         System.err.println("Unexpected format: " + format);
278       }
279     }
280     return null;
281   }
282
283   File ourselectedFile = null;
284
285   @Override
286   public File getSelectedFile()
287   {
288     File selfile = super.getSelectedFile();
289     if (selfile == null && ourselectedFile != null)
290     {
291       return ourselectedFile;
292     }
293     return selfile;
294   }
295
296   @Override
297   public int showSaveDialog(Component parent) throws HeadlessException
298   {
299     this.setAccessory(null);
300     this.setSelectedFile(null);
301     return super.showSaveDialog(parent);
302   }
303
304   /**
305    * If doing a Save, and an existing file is chosen or entered, prompt for
306    * confirmation of overwrite. Proceed if Yes, else leave the file chooser
307    * open.
308    * 
309    * @see https://stackoverflow.com/questions/8581215/jfilechooser-and-checking-for-overwrite
310    */
311   @Override
312   public void approveSelection()
313   {
314     if (getDialogType() != SAVE_DIALOG)
315     {
316       super.approveSelection();
317       return;
318     }
319
320     ourselectedFile = getSelectedFile();
321
322     if (ourselectedFile == null)
323     {
324       // Workaround for Java 9,10 on OSX - no selected file, but there is a
325       // filename typed in
326       try
327       {
328         String filename = ((BasicFileChooserUI) getUI()).getFileName();
329         if (filename != null && filename.length() > 0)
330         {
331           ourselectedFile = new File(getCurrentDirectory(), filename);
332         }
333       } catch (Throwable x)
334       {
335         System.err.println(
336                 "Unexpected exception when trying to get filename.");
337         x.printStackTrace();
338       }
339       // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND
340       // THE
341       // USER PROMPTED FOR A NEW FILENAME
342     }
343     if (ourselectedFile == null)
344     {
345       return;
346     }
347
348     if (getFileFilter() instanceof JalviewFileFilter)
349     {
350       JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
351
352       if (!jvf.accept(ourselectedFile))
353       {
354         String withExtension = getSelectedFile().getName() + "."
355                 + jvf.getAcceptableExtension();
356         ourselectedFile = (new File(getCurrentDirectory(), withExtension));
357         setSelectedFile(ourselectedFile);
358       }
359     }
360
361     if (ourselectedFile.exists())
362       {
363         int confirm = JvOptionPane.showConfirmDialog(this,
364                 MessageManager.getString("label.overwrite_existing_file"),
365                 MessageManager.getString("label.file_already_exists"),
366                 JvOptionPane.YES_NO_OPTION);
367
368         if (confirm != JvOptionPane.YES_OPTION)
369         {
370           return;
371         }
372       }
373
374     super.approveSelection();
375   }
376
377   void recentListSelectionChanged(Object selection)
378   {
379     setSelectedFile(null);
380     if (selection != null)
381     {
382       File file = new File((String) selection);
383       if (getFileFilter() instanceof JalviewFileFilter)
384       {
385         JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
386
387         if (!jvf.accept(file))
388         {
389           setFileFilter(getChoosableFileFilters()[0]);
390         }
391       }
392
393       setSelectedFile(file);
394     }
395   }
396
397   /**
398    * A panel to set as the 'accessory' component to the file chooser dialog,
399    * holding a list of recently opened files (if any). These are held as a
400    * tab-separated list of file paths under key <code>RECENT_FILE</code> in
401    * <code>.jalview_properties</code>. A click in the list calls a method in
402    * JalviewFileChooser to set the chosen file as the selection.
403    */
404   class RecentlyOpened extends JPanel
405   {
406     private static final long serialVersionUID = 1L;
407
408     private JList<String> list;
409
410     RecentlyOpened()
411     {
412       String historyItems = Cache.getProperty("RECENT_FILE");
413       StringTokenizer st;
414       Vector<String> recent = new Vector<>();
415
416       if (historyItems != null)
417       {
418         st = new StringTokenizer(historyItems, "\t");
419         while (st.hasMoreTokens())
420         {
421           recent.addElement(st.nextToken());
422         }
423       }
424
425       list = new JList<>(recent);
426
427       DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
428       dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
429       list.setCellRenderer(dlcr);
430
431       list.addMouseListener(new MouseAdapter()
432       {
433         @Override
434         public void mousePressed(MouseEvent evt)
435         {
436           recentListSelectionChanged(list.getSelectedValue());
437         }
438       });
439
440       this.setBorder(new TitledBorder(
441               MessageManager.getString("label.recently_opened")));
442
443       final JScrollPane scroller = new JScrollPane(list);
444
445       SpringLayout layout = new SpringLayout();
446       layout.putConstraint(SpringLayout.WEST, scroller, 5,
447               SpringLayout.WEST, this);
448       layout.putConstraint(SpringLayout.NORTH, scroller, 5,
449               SpringLayout.NORTH, this);
450
451       if (Platform.isAMac())
452       {
453         scroller.setPreferredSize(new Dimension(500, 100));
454       }
455       else
456       {
457         scroller.setPreferredSize(new Dimension(130, 200));
458       }
459
460       this.add(scroller);
461
462       SwingUtilities.invokeLater(new Runnable()
463       {
464         @Override
465         public void run()
466         {
467           scroller.getHorizontalScrollBar()
468                   .setValue(scroller.getHorizontalScrollBar().getMaximum());
469         }
470       });
471     }
472   }
473 }