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