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