JAL-4186 Turn JvOptionPane Callable<Void> handlers back into Runnable for jalviewjs...
[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 java.awt.Component;
25 import java.awt.Dimension;
26 import java.awt.EventQueue;
27 import java.awt.HeadlessException;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.MouseAdapter;
31 import java.awt.event.MouseEvent;
32 import java.beans.PropertyChangeEvent;
33 import java.beans.PropertyChangeListener;
34 import java.io.File;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.StringTokenizer;
40 import java.util.Vector;
41
42 import javax.swing.BoxLayout;
43 import javax.swing.DefaultListCellRenderer;
44 import javax.swing.JCheckBox;
45 import javax.swing.JDialog;
46 import javax.swing.JFileChooser;
47 import javax.swing.JList;
48 import javax.swing.JOptionPane;
49 import javax.swing.JPanel;
50 import javax.swing.JScrollPane;
51 import javax.swing.SpringLayout;
52 import javax.swing.filechooser.FileFilter;
53 import javax.swing.plaf.basic.BasicFileChooserUI;
54
55 import jalview.bin.Cache;
56 import jalview.gui.JvOptionPane;
57 import jalview.util.ChannelProperties;
58 import jalview.util.MessageManager;
59 import jalview.util.Platform;
60 import jalview.util.dialogrunner.DialogRunnerI;
61
62 /**
63  * Enhanced file chooser dialog box.
64  *
65  * NOTE: bug on Windows systems when filechooser opened on directory to view
66  * files with colons in title.
67  *
68  * @author AMW
69  *
70  */
71 public class JalviewFileChooser extends JFileChooser
72         implements DialogRunnerI, PropertyChangeListener
73 {
74   private static final long serialVersionUID = 1L;
75
76   private Map<Object, Runnable> callbacks = new HashMap<>();
77
78   File selectedFile = null;
79
80   /**
81    * backupfilesCheckBox = "Include backup files" checkbox includeBackupfiles =
82    * flag set by checkbox
83    */
84   private JCheckBox backupfilesCheckBox = null;
85
86   protected boolean includeBackupFiles = false;
87
88   /**
89    * Factory method to return a file chooser that offers readable alignment file
90    * formats
91    * 
92    * @param directory
93    * @param selected
94    * @return
95    */
96   public static JalviewFileChooser forRead(String directory,
97           String selected)
98   {
99     return JalviewFileChooser.forRead(directory, selected, false);
100   }
101
102   public static JalviewFileChooser forRead(String directory,
103           String selected, boolean allowBackupFiles)
104   {
105     List<String> extensions = new ArrayList<>();
106     List<String> descs = new ArrayList<>();
107     for (FileFormatI format : FileFormats.getInstance().getFormats())
108     {
109       if (format.isReadable())
110       {
111         extensions.add(format.getExtensions());
112         descs.add(format.getName());
113       }
114     }
115
116     return new JalviewFileChooser(directory,
117             extensions.toArray(new String[extensions.size()]),
118             descs.toArray(new String[descs.size()]), selected, true,
119             allowBackupFiles);
120   }
121
122   /**
123    * Factory method to return a file chooser that offers writable alignment file
124    * formats
125    * 
126    * @param directory
127    * @param selected
128    * @return
129    */
130   public static JalviewFileChooser forWrite(String directory,
131           String selected)
132   {
133     // TODO in Java 8, forRead and forWrite can be a single method
134     // with a lambda expression parameter for isReadable/isWritable
135     List<String> extensions = new ArrayList<>();
136     List<String> descs = new ArrayList<>();
137     for (FileFormatI format : FileFormats.getInstance().getFormats())
138     {
139       if (format.isWritable())
140       {
141         extensions.add(format.getExtensions());
142         descs.add(format.getName());
143       }
144     }
145     return new JalviewFileChooser(directory,
146             extensions.toArray(new String[extensions.size()]),
147             descs.toArray(new String[descs.size()]), selected, false);
148   }
149
150   public JalviewFileChooser(String dir)
151   {
152     super(safePath(dir));
153     setAccessory(new RecentlyOpened());
154   }
155
156   public JalviewFileChooser(String dir, String[] suffix, String[] desc,
157           String selected)
158   {
159     this(dir, suffix, desc, selected, true);
160   }
161
162   /**
163    * Constructor for a single choice of file extension and description
164    * 
165    * @param extension
166    * @param desc
167    */
168   public JalviewFileChooser(String extension, String desc)
169   {
170     this(Cache.getProperty("LAST_DIRECTORY"), new String[] { extension },
171             new String[]
172             { desc }, desc, true);
173   }
174
175   JalviewFileChooser(String dir, String[] extensions, String[] descs,
176           String selected, boolean acceptAny)
177   {
178     this(dir, extensions, descs, selected, acceptAny, false);
179   }
180
181   public JalviewFileChooser(String dir, String[] extensions, String[] descs,
182           String selected, boolean acceptAny, boolean allowBackupFiles)
183   {
184     super(safePath(dir));
185     if (extensions.length == descs.length)
186     {
187       List<String[]> formats = new ArrayList<>();
188       for (int i = 0; i < extensions.length; i++)
189       {
190         formats.add(new String[] { extensions[i], descs[i] });
191       }
192       init(formats, selected, acceptAny, allowBackupFiles);
193     }
194     else
195     {
196       System.err.println("JalviewFileChooser arguments mismatch: "
197               + extensions + ", " + descs);
198     }
199   }
200
201   private static File safePath(String dir)
202   {
203     if (dir == null)
204     {
205       return null;
206     }
207
208     File f = new File(dir);
209     if (f.getName().indexOf(':') > -1)
210     {
211       return null;
212     }
213     return f;
214   }
215
216   /**
217    * Overridden for JalviewJS compatibility: only one thread in Javascript, so
218    * we can't wait for user choice in another thread and then perform the
219    * desired action
220    */
221   @Override
222   public int showOpenDialog(Component parent)
223   {
224     int value = super.showOpenDialog(this);
225
226     if (!Platform.isJS())
227     /**
228      * Java only
229      * 
230      * @j2sIgnore
231      */
232     {
233       /*
234        * code here is not run in JalviewJS, instead
235        * propertyChange() is called for dialog action
236        */
237       handleResponse(value);
238     }
239     return value;
240   }
241
242   /**
243    * 
244    * @param formats
245    *          a list of {extensions, description} for each file format
246    * @param selected
247    * @param acceptAny
248    *          if true, 'any format' option is included
249    */
250   void init(List<String[]> formats, String selected, boolean acceptAny)
251   {
252     init(formats, selected, acceptAny, false);
253   }
254
255   void init(List<String[]> formats, String selected, boolean acceptAny,
256           boolean allowBackupFiles)
257   {
258
259     JalviewFileFilter chosen = null;
260
261     // SelectAllFilter needs to be set first before adding further
262     // file filters to fix bug on Mac OSX
263     setAcceptAllFileFilterUsed(acceptAny);
264
265     for (String[] format : formats)
266     {
267       JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]);
268       if (allowBackupFiles)
269       {
270         jvf.setParentJFC(this);
271       }
272       addChoosableFileFilter(jvf);
273       if ((selected != null) && selected.equalsIgnoreCase(format[1]))
274       {
275         chosen = jvf;
276       }
277     }
278
279     if (chosen != null)
280     {
281       setFileFilter(chosen);
282     }
283
284     if (allowBackupFiles)
285     {
286       JPanel multi = new JPanel();
287       multi.setLayout(new BoxLayout(multi, BoxLayout.PAGE_AXIS));
288       if (backupfilesCheckBox == null)
289       {
290         try
291         {
292           includeBackupFiles = Boolean.parseBoolean(
293                   Cache.getProperty(BackupFiles.NS + "_FC_INCLUDE"));
294         } catch (Exception e)
295         {
296           includeBackupFiles = false;
297         }
298         backupfilesCheckBox = new JCheckBox(
299                 MessageManager.getString("label.include_backup_files"),
300                 includeBackupFiles);
301         backupfilesCheckBox.setAlignmentX(Component.CENTER_ALIGNMENT);
302         JalviewFileChooser jfc = this;
303         backupfilesCheckBox.addActionListener(new ActionListener()
304         {
305           @Override
306           public void actionPerformed(ActionEvent e)
307           {
308             includeBackupFiles = backupfilesCheckBox.isSelected();
309             Cache.setProperty(BackupFiles.NS + "_FC_INCLUDE",
310                     String.valueOf(includeBackupFiles));
311
312             FileFilter f = jfc.getFileFilter();
313             // deselect the selected file if it's no longer choosable
314             File selectedFile = jfc.getSelectedFile();
315             if (selectedFile != null && !f.accept(selectedFile))
316             {
317               jfc.setSelectedFile(null);
318             }
319             // fake the OK button changing (to force it to upate)
320             String s = jfc.getApproveButtonText();
321             jfc.firePropertyChange(APPROVE_BUTTON_TEXT_CHANGED_PROPERTY,
322                     null, s);
323             // fake the file filter changing (its behaviour actually has)
324             jfc.firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, null, f);
325
326             jfc.rescanCurrentDirectory();
327             jfc.revalidate();
328             jfc.repaint();
329           }
330         });
331       }
332       multi.add(new RecentlyOpened());
333       multi.add(backupfilesCheckBox);
334       setAccessory(multi);
335     }
336     else
337     {
338       // set includeBackupFiles=false to avoid other file choosers from picking
339       // up backup files (Just In Case)
340       includeBackupFiles = false;
341       setAccessory(new RecentlyOpened());
342     }
343   }
344
345   @Override
346   public void setFileFilter(javax.swing.filechooser.FileFilter filter)
347   {
348     super.setFileFilter(filter);
349
350     try
351     {
352       if (getUI() instanceof BasicFileChooserUI)
353       {
354         final BasicFileChooserUI fcui = (BasicFileChooserUI) getUI();
355         final String name = fcui.getFileName().trim();
356
357         if ((name == null) || (name.length() == 0))
358         {
359           return;
360         }
361
362         EventQueue.invokeLater(new Thread()
363         {
364           @Override
365           public void run()
366           {
367             String currentName = fcui.getFileName();
368             if ((currentName == null) || (currentName.length() == 0))
369             {
370               fcui.setFileName(name);
371             }
372           }
373         });
374       }
375     } catch (Exception ex)
376     {
377       ex.printStackTrace();
378       // Some platforms do not have BasicFileChooserUI
379     }
380   }
381
382   /**
383    * Returns the selected file format, or null if none selected
384    * 
385    * @return
386    */
387   public FileFormatI getSelectedFormat()
388   {
389     if (getFileFilter() == null)
390     {
391       return null;
392     }
393
394     /*
395      * logic here depends on option description being formatted as 
396      * formatName (extension, extension...)
397      * or the 'no option selected' value
398      * All Files
399      * @see JalviewFileFilter.getDescription
400      */
401     String format = getFileFilter().getDescription();
402     int parenPos = format.indexOf("(");
403     if (parenPos > 0)
404     {
405       format = format.substring(0, parenPos).trim();
406       try
407       {
408         return FileFormats.getInstance().forName(format);
409       } catch (IllegalArgumentException e)
410       {
411         System.err.println("Unexpected format: " + format);
412       }
413     }
414     return null;
415   }
416
417   @Override
418   public File getSelectedFile()
419   {
420     File f = super.getSelectedFile();
421     return f == null ? selectedFile : f;
422   }
423
424   @Override
425   public int showSaveDialog(Component parent) throws HeadlessException
426   {
427     this.setAccessory(null);
428     // Java 9,10,11 on OSX - clear selected file so name isn't auto populated
429     this.setSelectedFile(null);
430
431     return super.showSaveDialog(parent);
432   }
433
434   /**
435    * If doing a Save, and an existing file is chosen or entered, prompt for
436    * confirmation of overwrite. Proceed if Yes, else leave the file chooser
437    * open.
438    * 
439    * @see https://stackoverflow.com/questions/8581215/jfilechooser-and-checking-for-overwrite
440    */
441   @Override
442   public void approveSelection()
443   {
444     if (getDialogType() != SAVE_DIALOG)
445     {
446       super.approveSelection();
447       return;
448     }
449
450     selectedFile = getSelectedFile();
451
452     if (selectedFile == null)
453     {
454       // Workaround for Java 9,10 on OSX - no selected file, but there is a
455       // filename typed in
456       try
457       {
458         String filename = ((BasicFileChooserUI) getUI()).getFileName();
459         if (filename != null && filename.length() > 0)
460         {
461           selectedFile = new File(getCurrentDirectory(), filename);
462         }
463       } catch (Throwable x)
464       {
465         System.err.println(
466                 "Unexpected exception when trying to get filename.");
467         x.printStackTrace();
468       }
469       // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND
470       // THE
471       // USER PROMPTED FOR A NEW FILENAME
472     }
473
474     if (selectedFile == null)
475     {
476       return;
477     }
478
479     if (getFileFilter() instanceof JalviewFileFilter)
480     {
481       JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
482
483       if (!jvf.accept(selectedFile))
484       {
485         String withExtension = getSelectedFile().getName() + "."
486                 + jvf.getAcceptableExtension();
487         selectedFile = (new File(getCurrentDirectory(), withExtension));
488         setSelectedFile(selectedFile);
489       }
490     }
491
492     if (selectedFile.exists())
493     {
494       int confirm = Cache.getDefault("CONFIRM_OVERWRITE_FILE", true)
495               ? JvOptionPane.showConfirmDialog(this,
496                       MessageManager
497                               .getString("label.overwrite_existing_file"),
498                       MessageManager.getString("label.file_already_exists"),
499                       JvOptionPane.YES_NO_OPTION)
500               : JOptionPane.YES_OPTION;
501
502       if (confirm != JvOptionPane.YES_OPTION)
503       {
504         return;
505       }
506     }
507
508     super.approveSelection();
509   }
510
511   void recentListSelectionChanged(Object selection)
512   {
513     setSelectedFile(null);
514     if (selection != null)
515     {
516       File file = new File((String) selection);
517       if (getFileFilter() instanceof JalviewFileFilter)
518       {
519         JalviewFileFilter jvf = (JalviewFileFilter) this.getFileFilter();
520
521         if (!jvf.accept(file))
522         {
523           setFileFilter(getChoosableFileFilters()[0]);
524         }
525       }
526
527       setSelectedFile(file);
528     }
529   }
530
531   class RecentlyOpened extends JPanel
532   {
533     private static final long serialVersionUID = 1L;
534
535     JList<String> list;
536
537     RecentlyOpened()
538     {
539       setPreferredSize(new Dimension(300, 100));
540       String historyItems = Cache.getProperty("RECENT_FILE");
541       StringTokenizer st;
542       Vector<String> recent = new Vector<>();
543
544       if (historyItems != null)
545       {
546         st = new StringTokenizer(historyItems, "\t");
547
548         while (st.hasMoreTokens())
549         {
550           recent.addElement(st.nextToken());
551         }
552       }
553
554       list = new JList<>(recent);
555
556       DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
557       dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
558       list.setCellRenderer(dlcr);
559
560       list.addMouseListener(new MouseAdapter()
561       {
562         @Override
563         public void mousePressed(MouseEvent evt)
564         {
565           recentListSelectionChanged(list.getSelectedValue());
566         }
567       });
568
569       this.setBorder(new javax.swing.border.TitledBorder(
570               MessageManager.getString("label.recently_opened")));
571
572       final JScrollPane scroller = new JScrollPane(list);
573
574       SpringLayout layout = new SpringLayout();
575       layout.putConstraint(SpringLayout.WEST, scroller, 5,
576               SpringLayout.WEST, this);
577       layout.putConstraint(SpringLayout.NORTH, scroller, 5,
578               SpringLayout.NORTH, this);
579
580       if (Platform.isAMacAndNotJS())
581       {
582         scroller.setPreferredSize(new Dimension(500, 100));
583       }
584       else
585       {
586         scroller.setPreferredSize(new Dimension(530, 200));
587       }
588
589       this.add(scroller);
590
591       javax.swing.SwingUtilities.invokeLater(new Runnable()
592       {
593         @Override
594         public void run()
595         {
596           scroller.getHorizontalScrollBar()
597                   .setValue(scroller.getHorizontalScrollBar().getMaximum());
598         }
599       });
600
601     }
602
603   }
604
605   /*
606   @Override
607   public JalviewFileChooser setResponseHandler(Object response,
608           Runnable action)
609   {
610     callbacks.put(response, new Callable<Void>()
611     {
612       @Override
613       public Void call()
614       {
615         action.run();
616         return null;
617       }
618     });
619     return this;
620   }
621   */
622
623   @Override
624   public DialogRunnerI setResponseHandler(Object response, Runnable action)
625   {
626     callbacks.put(response, action);
627     return this;
628   }
629
630   @Override
631   public void handleResponse(Object response)
632   {
633     /*
634     * this test is for NaN in Chrome
635     */
636     if (response != null && !response.equals(response))
637     {
638       return;
639     }
640     Runnable action = callbacks.get(response);
641     if (action != null)
642     {
643       try
644       {
645         action.run();
646       } catch (Exception e)
647       {
648         e.printStackTrace();
649       }
650     }
651   }
652
653   /**
654    * JalviewJS signals file selection by a property change event for property
655    * "SelectedFile". This methods responds to that by running the response
656    * action for 'OK' in the dialog.
657    * 
658    * @param evt
659    */
660   @Override
661   public void propertyChange(PropertyChangeEvent evt)
662   {
663     // TODO other properties need runners...
664     switch (evt.getPropertyName())
665     {
666     /*
667      * property name here matches that used in JFileChooser.js
668      */
669     case "SelectedFile":
670       handleResponse(APPROVE_OPTION);
671       break;
672     }
673   }
674
675   @Override
676   protected JDialog createDialog(Component parent) throws HeadlessException
677   {
678     JDialog dialog = super.createDialog(parent);
679     dialog.setIconImages(ChannelProperties.getIconList());
680     return dialog;
681   }
682
683 }