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