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