updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / io / JalviewFileChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19
20 //////////////////////////////////////////////////////////////////
21 package jalview.io;
22
23 import java.io.*;
24 import java.awt.*;
25 import javax.swing.*;
26 import java.awt.event.*;
27 import java.util.*;
28
29 public class JalviewFileChooser
30     extends JFileChooser
31 {
32
33   public JalviewFileChooser(String dir)
34   {
35     super(dir);
36     setAccessory(new RecentlyOpened());
37   }
38
39   public JalviewFileChooser(String dir,
40                             String[] suffix,
41                             String[] desc,
42                             String selected,
43                             boolean selectAll)
44   {
45     super(dir);
46     init( suffix, desc, selected, selectAll);
47   }
48
49   public JalviewFileChooser(String dir,
50                             String[] suffix,
51                             String[] desc,
52                             String selected)
53   {
54     super(dir);
55     init( suffix, desc, selected, true);
56   }
57
58   void init(String[] suffix,
59             String[] desc,
60             String selected,
61             boolean selectAll)
62   {
63
64     JalviewFileFilter chosen = null;
65
66     //SelectAllFilter needs to be set first before adding further
67     //file filters to fix bug on Mac OSX
68     setAcceptAllFileFilterUsed(selectAll);
69
70     for (int i = 0; i < suffix.length; i++)
71     {
72       JalviewFileFilter jvf = new JalviewFileFilter(suffix[i], desc[i]);
73       addChoosableFileFilter(jvf);
74
75       if ( (selected != null) && selected.equalsIgnoreCase(desc[i]))
76       {
77         chosen = jvf;
78       }
79     }
80
81     if (chosen != null)
82     {
83       setFileFilter(chosen);
84     }
85
86     setAccessory(new RecentlyOpened());
87   }
88
89
90   public void setFileFilter(javax.swing.filechooser.FileFilter filter)
91   {
92     super.setFileFilter(filter);
93
94
95     try{
96       if(getUI() instanceof javax.swing.plaf.basic.BasicFileChooserUI)
97       {
98         final javax.swing.plaf.basic.BasicFileChooserUI ui = (javax.swing.plaf.
99             basic.BasicFileChooserUI) getUI();
100         final String name = ui.getFileName().trim();
101
102         if ( (name == null) || (name.length() == 0))
103         {
104           return;
105         }
106
107         EventQueue.invokeLater(new Thread()
108         {
109           public void run()
110           {
111             String currentName = ui.getFileName();
112             if ( (currentName == null) || (currentName.length() == 0))
113             {
114               ui.setFileName(name);
115             }
116           }
117         });
118       }
119     }catch(Exception ex)
120     {
121       ex.printStackTrace();
122       // Some platforms do not have BasicFileChooserUI
123     }
124   }
125
126   public String getSelectedFormat()
127   {
128     if(getFileFilter()==null)
129     {
130       return null;
131     }
132
133     String format = getFileFilter().getDescription();
134
135     if (format.toUpperCase().startsWith("JALVIEW"))
136     {
137       format = "Jalview";
138     }
139     else if (format.toUpperCase().startsWith("FASTA"))
140     {
141       format = "FASTA";
142     }
143     else if (format.toUpperCase().startsWith("MSF"))
144     {
145       format = "MSF";
146     }
147     else if (format.toUpperCase().startsWith("CLUSTAL"))
148     {
149       format = "CLUSTAL";
150     }
151     else if (format.toUpperCase().startsWith("BLC"))
152     {
153       format = "BLC";
154     }
155     else if (format.toUpperCase().startsWith("PIR"))
156     {
157       format = "PIR";
158     }
159     else if (format.toUpperCase().startsWith("PFAM"))
160     {
161       format = "PFAM";
162     }
163
164     return format;
165   }
166
167   public int showSaveDialog(Component parent)
168       throws HeadlessException
169   {
170     this.setAccessory(null);
171
172
173     setDialogType(SAVE_DIALOG);
174
175     int ret = showDialog(parent, "Save");
176
177     if (getFileFilter() instanceof JalviewFileFilter)
178     {
179       JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
180
181       if (!jvf.accept(getSelectedFile()))
182       {
183         String withExtension = getSelectedFile() + "." +
184             jvf.getAcceptableExtension();
185         setSelectedFile(new File(withExtension));
186       }
187     }
188
189     if ( (ret == JalviewFileChooser.APPROVE_OPTION) &&
190         getSelectedFile().exists())
191     {
192       int confirm = JOptionPane.showConfirmDialog(parent,
193                                                   "Overwrite existing file?",
194                                                   "File exists",
195                                                   JOptionPane.YES_NO_OPTION);
196
197       if (confirm != JOptionPane.YES_OPTION)
198       {
199         ret = JalviewFileChooser.CANCEL_OPTION;
200       }
201     }
202
203     return ret;
204   }
205
206   void recentListSelectionChanged(String selection)
207   {
208     setSelectedFile(null);
209
210     File file = new File(selection);
211     if (getFileFilter() instanceof JalviewFileFilter)
212     {
213       JalviewFileFilter jvf = (JalviewFileFilter)this.getFileFilter();
214
215       if (!jvf.accept(file))
216       {
217         setFileFilter(getChoosableFileFilters()[0]);
218       }
219     }
220
221      setSelectedFile( file );
222   }
223
224   class RecentlyOpened extends JPanel
225   {
226     JList list;
227     public RecentlyOpened()
228     {
229       String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
230       StringTokenizer st;
231       Vector recent = new Vector();
232
233       if (historyItems != null)
234       {
235         st = new StringTokenizer(historyItems, "\t");
236
237         while (st.hasMoreTokens())
238         {
239           recent.addElement(st.nextElement());
240         }
241       }
242
243       list = new JList(recent);
244
245       DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
246       dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
247       list.setCellRenderer(dlcr);
248
249
250       list.addMouseListener(new MouseAdapter()
251           {
252             public void mousePressed(MouseEvent evt)
253             {
254               recentListSelectionChanged(list.getSelectedValue().toString());
255             }
256           });
257
258       this.setBorder(new javax.swing.border.TitledBorder("Recently Opened"));
259
260       final JScrollPane scroller = new JScrollPane(list);
261       scroller.setPreferredSize(new Dimension(130, 200));
262       this.add(scroller);
263
264       javax.swing.SwingUtilities.invokeLater(new Runnable()
265       {
266         public void run()
267         {
268           scroller.getHorizontalScrollBar().setValue(
269               scroller.getHorizontalScrollBar().getMaximum());
270         }
271       });
272
273
274     }
275
276   }
277 }
278
279
280
281
282