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