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