b16cf501c2157e62f8822feebd6c4e29917c20a3
[jalview.git] / src / jalview / gui / Preferences.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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 package jalview.gui;
20
21 import java.util.*;
22
23 import java.awt.*;
24 import java.awt.event.*;
25 import javax.swing.*;
26
27 import jalview.bin.*;
28 import jalview.io.*;
29 import jalview.jbgui.*;
30 import jalview.schemes.*;
31
32 /**
33  * DOCUMENT ME!
34  *
35  * @author $author$
36  * @version $Revision$
37  */
38 public class Preferences
39     extends GPreferences
40 {
41   /** Holds name and link separated with | character. Sequence ID must be $SEQUENCE_ID$ */
42   public static Vector sequenceURLLinks;
43   static
44   {
45     String string = Cache.getDefault("SEQUENCE_LINKS",
46                                      "SRS|http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?-newId+(([uniprot-all:$SEQUENCE_ID$]))+-view+SwissEntry");
47     sequenceURLLinks = new Vector();
48
49     try
50     {
51       StringTokenizer st = new StringTokenizer(string, "|");
52       while (st.hasMoreElements())
53       {
54         sequenceURLLinks.addElement(st.nextToken() + "|" + st.nextToken());
55       }
56     }
57     catch (Exception ex)
58     {
59       System.out.println(ex + "\nError parsing sequence links");
60     }
61   }
62
63   Vector nameLinks, urlLinks;
64
65   JInternalFrame frame;
66
67   DasSourceBrowser dasSource;
68
69   /**
70    * Creates a new Preferences object.
71    */
72   public Preferences()
73   {
74
75     frame = new JInternalFrame();
76     frame.setContentPane(this);
77     dasSource = new DasSourceBrowser();
78     dasPanel.add(dasSource, BorderLayout.CENTER);
79
80     int width = 500, height = 420;
81     if (new jalview.util.Platform().isAMac())
82     {
83       width = 570;
84       height = 460;
85     }
86
87     Desktop.addInternalFrame(frame, "Preferences", width, height);
88     frame.setMinimumSize(new Dimension(width, height));
89
90     seqLimit.setSelected(Cache.getDefault("SHOW_JVSUFFIX", true));
91     rightAlign.setSelected(Cache.getDefault("RIGHT_ALIGN_IDS", false));
92     fullScreen.setSelected(Cache.getDefault("SHOW_FULLSCREEN", false));
93     annotations.setSelected(Cache.getDefault("SHOW_ANNOTATIONS", true));
94
95     conservation.setEnabled(Cache.getDefault("SHOW_ANNOTATIONS", true));
96     quality.setEnabled(Cache.getDefault("SHOW_ANNOTATIONS", true));
97     identity.setEnabled(Cache.getDefault("SHOW_ANNOTATIONS", true));
98
99     conservation.setSelected(Cache.getDefault("SHOW_CONSERVATION", true));
100     quality.setSelected(Cache.getDefault("SHOW_QUALITY", true));
101     identity.setSelected(Cache.getDefault("SHOW_IDENTITY", true));
102     openoverv.setSelected(Cache.getDefault("SHOW_OVERVIEW", false));
103
104     for (int i = 0; i < 13; i++)
105     {
106       colour.addItem(ColourSchemeProperty.getColourName(i));
107     }
108
109     String string = Cache.getDefault("DEFAULT_COLOUR", "None");
110
111     colour.setSelectedItem(string);
112
113     String[] fonts = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment()
114         .getAvailableFontFamilyNames();
115
116     for (int i = 0; i < fonts.length; i++)
117     {
118       fontNameCB.addItem(fonts[i]);
119     }
120
121     for (int i = 1; i < 31; i++)
122     {
123       fontSizeCB.addItem(i + "");
124     }
125
126     fontStyleCB.addItem("plain");
127     fontStyleCB.addItem("bold");
128     fontStyleCB.addItem("italic");
129
130     fontNameCB.setSelectedItem(Cache.getDefault("FONT_NAME", "SansSerif"));
131     fontSizeCB.setSelectedItem(Cache.getDefault("FONT_SIZE", "10"));
132     fontStyleCB.setSelectedItem(Cache.getDefault("FONT_STYLE", Font.PLAIN + ""));
133
134     smoothFont.setSelected(Cache.getDefault("ANTI_ALIAS", false));
135
136     idItalics.setSelected(Cache.getDefault("ID_ITALICS", true));
137
138     wrap.setSelected(Cache.getDefault("WRAP_ALIGNMENT", false));
139
140     gapSymbolCB.addItem("-");
141     gapSymbolCB.addItem(".");
142
143     gapSymbolCB.setSelectedItem(Cache.getDefault("GAP_SYMBOL", "-"));
144
145     startupCheckbox.setSelected(Cache.getDefault("SHOW_STARTUP_FILE", true));
146     startupFileTextfield.setText(Cache.getDefault("STARTUP_FILE",
147                                                   "http://www.jalview.org/examples/exampleFile_2_3.jar"));
148
149     sortby.addItem("No sort");
150     sortby.addItem("Id");
151     sortby.addItem("Pairwise Identity");
152     sortby.setSelectedItem(Cache.getDefault("SORT_ALIGNMENT", "No sort"));
153
154     epsRendering.addItem("Prompt each time");
155     epsRendering.addItem("Lineart");
156     epsRendering.addItem("Text");
157     epsRendering.setSelectedItem(Cache.getDefault("EPS_RENDERING",
158                                                   "Prompt each time"));
159
160     blcjv.setSelected(Cache.getDefault("BLC_JVSUFFIX", true));
161     clustaljv.setSelected(Cache.getDefault("CLUSTAL_JVSUFFIX", true));
162     fastajv.setSelected(Cache.getDefault("FASTA_JVSUFFIX", true));
163     msfjv.setSelected(Cache.getDefault("MSF_JVSUFFIX", true));
164     pfamjv.setSelected(Cache.getDefault("PFAM_JVSUFFIX", true));
165     pileupjv.setSelected(Cache.getDefault("PILEUP_JVSUFFIX", true));
166     pirjv.setSelected(Cache.getDefault("PIR_JVSUFFIX", true));
167
168     modellerOutput.setSelected(Cache.getDefault("PIR_MODELLER", false));
169
170     autoCalculateConsCheck.setSelected(Cache.getDefault("AUTO_CALC_CONSENSUS", true));
171     padGaps.setSelected(Cache.getDefault("PAD_GAPS", false));
172
173     /****************************************************
174      * Set up Connections
175      */
176     nameLinks = new Vector();
177     urlLinks = new Vector();
178     for (int i = 0; i < sequenceURLLinks.size(); i++)
179     {
180       String link = sequenceURLLinks.elementAt(i).toString();
181       nameLinks.addElement(link.substring(0, link.indexOf("|")));
182       urlLinks.addElement(link.substring(link.indexOf("|") + 1));
183     }
184
185     updateLinkData();
186
187     useProxy.setSelected(Cache.getDefault("USE_PROXY", false));
188     proxyServerTB.setEnabled(useProxy.isSelected());
189     proxyPortTB.setEnabled(useProxy.isSelected());
190     proxyServerTB.setText(Cache.getDefault("PROXY_SERVER", ""));
191     proxyPortTB.setText(Cache.getDefault("PROXY_PORT", ""));
192
193     defaultBrowser.setText(Cache.getDefault("DEFAULT_BROWSER", ""));
194     try
195     {
196       jbInit();
197     }
198     catch (Exception ex)
199     {
200       ex.printStackTrace();
201     }
202
203   }
204
205   /**
206    * DOCUMENT ME!
207    *
208    * @param e DOCUMENT ME!
209    */
210   public void ok_actionPerformed(ActionEvent e)
211   {
212
213     Cache.applicationProperties.setProperty("SHOW_JVSUFFIX",
214                                             Boolean.toString(seqLimit.
215         isSelected()));
216     Cache.applicationProperties.setProperty("RIGHT_ALIGN_IDS",
217                                             Boolean.toString(rightAlign.
218         isSelected()));
219     Cache.applicationProperties.setProperty("SHOW_FULLSCREEN",
220                                             Boolean.toString(fullScreen.
221         isSelected()));
222     Cache.applicationProperties.setProperty("SHOW_OVERVIEW",
223                                             Boolean.toString(openoverv.
224         isSelected()));
225     Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS",
226                                             Boolean.
227                                             toString(annotations.isSelected()));
228     Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
229                                             Boolean.
230                                             toString(conservation.isSelected()));
231     Cache.applicationProperties.setProperty("SHOW_QUALITY",
232                                             Boolean.toString(quality.isSelected()));
233     Cache.applicationProperties.setProperty("SHOW_IDENTITY",
234                                             Boolean.toString(identity.
235         isSelected()));
236
237     Cache.applicationProperties.setProperty("DEFAULT_COLOUR",
238                                             colour.getSelectedItem().toString());
239     Cache.applicationProperties.setProperty("GAP_SYMBOL",
240                                             gapSymbolCB.getSelectedItem().
241                                             toString());
242
243     Cache.applicationProperties.setProperty("FONT_NAME",
244                                             fontNameCB.getSelectedItem().
245                                             toString());
246     Cache.applicationProperties.setProperty("FONT_STYLE",
247                                             fontStyleCB.getSelectedItem().
248                                             toString());
249     Cache.applicationProperties.setProperty("FONT_SIZE",
250                                             fontSizeCB.getSelectedItem().
251                                             toString());
252
253     Cache.applicationProperties.setProperty("ID_ITALICS",
254                                             Boolean.toString(idItalics.
255         isSelected()));
256
257     Cache.applicationProperties.setProperty("ANTI_ALIAS",
258                                             Boolean.toString(smoothFont.
259         isSelected()));
260
261     Cache.applicationProperties.setProperty("WRAP_ALIGNMENT",
262                                             Boolean.toString(wrap.isSelected()));
263
264     Cache.applicationProperties.setProperty("STARTUP_FILE",
265                                             startupFileTextfield.getText());
266     Cache.applicationProperties.setProperty("SHOW_STARTUP_FILE",
267                                             Boolean.
268                                             toString(startupCheckbox.isSelected()));
269
270     Cache.applicationProperties.setProperty("SORT_ALIGNMENT",
271                                             sortby.getSelectedItem().toString());
272
273     if (epsRendering.getSelectedItem().equals("Prompt each time"))
274     {
275       Cache.applicationProperties.remove("EPS_RENDERING");
276     }
277     else
278     {
279       Cache.applicationProperties.setProperty("EPS_RENDERING",
280                                               epsRendering.getSelectedItem().
281                                               toString());
282     }
283
284     if (defaultBrowser.getText().trim().length() < 1)
285     {
286       Cache.applicationProperties.remove("DEFAULT_BROWSER");
287     }
288     else
289     {
290       Cache.applicationProperties.setProperty("DEFAULT_BROWSER",
291                                               defaultBrowser.getText());
292     }
293
294     jalview.util.BrowserLauncher.resetBrowser();
295
296     if (nameLinks.size() > 0)
297     {
298       StringBuffer links = new StringBuffer();
299       sequenceURLLinks = new Vector();
300       for (int i = 0; i < nameLinks.size(); i++)
301       {
302         sequenceURLLinks.addElement(nameLinks.elementAt(i) + "|" +
303                                     urlLinks.elementAt(i));
304         links.append(sequenceURLLinks.elementAt(i).toString());
305         links.append("|");
306       }
307       // remove last "|"
308       links.setLength(links.length() - 1);
309       Cache.applicationProperties.setProperty("SEQUENCE_LINKS", links.toString());
310     }
311     else
312     {
313       Cache.applicationProperties.remove("SEQUENCE_LINKS");
314     }
315
316     Cache.applicationProperties.setProperty("USE_PROXY",
317                                             Boolean.toString(useProxy.
318         isSelected()));
319
320     if (proxyServerTB.getText().trim().length() < 1)
321     {
322       Cache.applicationProperties.remove("PROXY_SERVER");
323     }
324     else
325     {
326       Cache.applicationProperties.setProperty("PROXY_SERVER",
327                                               proxyServerTB.getText());
328     }
329
330     if (proxyPortTB.getText().trim().length() < 1)
331     {
332       Cache.applicationProperties.remove("PROXY_PORT");
333     }
334     else
335     {
336       Cache.applicationProperties.setProperty("PROXY_PORT", proxyPortTB.getText());
337     }
338
339     if (useProxy.isSelected())
340     {
341       System.setProperty("http.proxyHost", proxyServerTB.getText());
342       System.setProperty("http.proxyPort", proxyPortTB.getText());
343     }
344     else
345     {
346       System.setProperty("http.proxyHost", "");
347       System.setProperty("http.proxyPort", "");
348     }
349
350     Cache.applicationProperties.setProperty("BLC_JVSUFFIX",
351                                             Boolean.toString(blcjv.isSelected()));
352     Cache.applicationProperties.setProperty("CLUSTAL_JVSUFFIX",
353                                             Boolean.
354                                             toString(clustaljv.isSelected()));
355     Cache.applicationProperties.setProperty("FASTA_JVSUFFIX",
356                                             Boolean.toString(fastajv.isSelected()));
357     Cache.applicationProperties.setProperty("MSF_JVSUFFIX",
358                                             Boolean.toString(msfjv.isSelected()));
359     Cache.applicationProperties.setProperty("PFAM_JVSUFFIX",
360                                             Boolean.toString(pfamjv.isSelected()));
361     Cache.applicationProperties.setProperty("PILEUP_JVSUFFIX",
362                                             Boolean.toString(pileupjv.
363         isSelected()));
364     Cache.applicationProperties.setProperty("PIR_JVSUFFIX",
365                                             Boolean.toString(pirjv.isSelected()));
366     Cache.applicationProperties.setProperty("PIR_MODELLER",
367                                             Boolean.toString(modellerOutput.
368         isSelected()));
369     jalview.io.PIRFile.useModellerOutput = modellerOutput.isSelected();
370
371     Cache.applicationProperties.setProperty("AUTO_CALC_CONSENSUS",
372                                             Boolean.toString(
373         autoCalculateConsCheck.isSelected()));
374     Cache.applicationProperties.setProperty("PAD_GAPS",
375                                             Boolean.toString(padGaps.isSelected()));
376
377     dasSource.saveProperties(Cache.applicationProperties);
378
379     Cache.saveProperties();
380     try
381     {
382       frame.setClosed(true);
383     }
384     catch (Exception ex)
385     {
386     }
387   }
388
389   /**
390    * DOCUMENT ME!
391    */
392   public void startupFileTextfield_mouseClicked()
393   {
394     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
395         getProperty(
396             "LAST_DIRECTORY"),
397         new String[]
398         {
399         "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
400         "jar"
401     },
402         new String[]
403         {
404         "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"
405     }, jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
406     chooser.setFileView(new JalviewFileView());
407     chooser.setDialogTitle("Select startup file");
408
409     int value = chooser.showOpenDialog(this);
410
411     if (value == JalviewFileChooser.APPROVE_OPTION)
412     {
413       jalview.bin.Cache.applicationProperties.setProperty("DEFAULT_FILE_FORMAT",
414           chooser.getSelectedFormat());
415       startupFileTextfield.setText(chooser.getSelectedFile()
416                                    .getAbsolutePath());
417     }
418   }
419
420   /**
421    * DOCUMENT ME!
422    *
423    * @param e DOCUMENT ME!
424    */
425   public void cancel_actionPerformed(ActionEvent e)
426   {
427     try
428     {
429       frame.setClosed(true);
430     }
431     catch (Exception ex)
432     {
433     }
434   }
435
436   /**
437    * DOCUMENT ME!
438    *
439    * @param e DOCUMENT ME!
440    */
441   public void annotations_actionPerformed(ActionEvent e)
442   {
443     conservation.setEnabled(annotations.isSelected());
444     quality.setEnabled(annotations.isSelected());
445     identity.setEnabled(annotations.isSelected());
446   }
447
448   public void newLink_actionPerformed(ActionEvent e)
449   {
450
451     GSequenceLink link = new GSequenceLink();
452     boolean valid = false;
453     while (!valid)
454     {
455       if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
456                                                 "New sequence URL link",
457                                                 JOptionPane.OK_CANCEL_OPTION
458                                                 , -1, null)
459           == JOptionPane.OK_OPTION)
460       {
461         if (link.checkValid())
462         {
463           nameLinks.addElement(link.getName());
464           urlLinks.addElement(link.getURL());
465           updateLinkData();
466           valid = true;
467         }
468       }
469       else
470       {
471         break;
472       }
473     }
474   }
475
476   public void editLink_actionPerformed(ActionEvent e)
477   {
478     GSequenceLink link = new GSequenceLink();
479
480     int index = linkNameList.getSelectedIndex();
481     if (index == -1)
482     {
483       JOptionPane.showInternalMessageDialog(Desktop.desktop,
484                                             "No link selected!"
485                                             , "No link selected",
486                                             JOptionPane.WARNING_MESSAGE);
487       return;
488     }
489
490     link.setName(nameLinks.elementAt(index).toString());
491     link.setURL(urlLinks.elementAt(index).toString());
492
493     boolean valid = false;
494     while (!valid)
495     {
496
497       if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
498                                                 "New sequence URL link",
499                                                 JOptionPane.OK_CANCEL_OPTION
500                                                 , -1, null)
501           == JOptionPane.OK_OPTION)
502       {
503         if (link.checkValid())
504         {
505           nameLinks.setElementAt(link.getName(), index);
506           urlLinks.setElementAt(link.getURL(), index);
507           updateLinkData();
508           valid = true;
509         }
510       }
511
512       else
513       {
514         break;
515       }
516     }
517   }
518
519   public void deleteLink_actionPerformed(ActionEvent e)
520   {
521     int index = linkNameList.getSelectedIndex();
522     if (index == -1)
523     {
524       JOptionPane.showInternalMessageDialog(Desktop.desktop,
525                                             "No link selected!"
526                                             , "No link selected",
527                                             JOptionPane.WARNING_MESSAGE);
528       return;
529     }
530     nameLinks.removeElementAt(index);
531     urlLinks.removeElementAt(index);
532     updateLinkData();
533   }
534
535   void updateLinkData()
536   {
537     linkNameList.setListData(nameLinks);
538     linkURLList.setListData(urlLinks);
539   }
540
541   public void defaultBrowser_mouseClicked(MouseEvent e)
542   {
543     JFileChooser chooser = new JFileChooser(".");
544     chooser.setDialogTitle("Select default web browser");
545
546     int value = chooser.showOpenDialog(this);
547
548     if (value == JFileChooser.APPROVE_OPTION)
549     {
550       defaultBrowser.setText(chooser.getSelectedFile().getAbsolutePath());
551     }
552
553   }
554
555   private void jbInit()
556       throws Exception
557   {
558   }
559 }