JAL-1321 don't show a tooltip for empty strings
[jalview.git] / src / jalview / gui / OptsAndParamsPage.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import jalview.ws.params.ArgumentI;
21 import jalview.ws.params.OptionI;
22 import jalview.ws.params.ParameterI;
23 import jalview.ws.params.ValueConstrainI;
24 import jalview.ws.params.ValueConstrainI.ValueType;
25
26 import java.awt.BorderLayout;
27 import java.awt.Component;
28 import java.awt.Dimension;
29 import java.awt.Font;
30 import java.awt.GridLayout;
31 import java.awt.Rectangle;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.KeyEvent;
35 import java.awt.event.KeyListener;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.MouseListener;
38 import java.net.URL;
39 import java.util.ArrayList;
40 import java.util.Hashtable;
41 import java.util.List;
42 import java.util.Map;
43
44 import javax.swing.JButton;
45 import javax.swing.JCheckBox;
46 import javax.swing.JComboBox;
47 import javax.swing.JComponent;
48 import javax.swing.JLabel;
49 import javax.swing.JMenuItem;
50 import javax.swing.JPanel;
51 import javax.swing.JPopupMenu;
52 import javax.swing.JScrollPane;
53 import javax.swing.JSlider;
54 import javax.swing.JTextArea;
55 import javax.swing.JTextField;
56 import javax.swing.border.TitledBorder;
57 import javax.swing.event.ChangeEvent;
58 import javax.swing.event.ChangeListener;
59
60 import net.miginfocom.swing.MigLayout;
61
62 /**
63  * GUI generator/manager for options and parameters. Originally abstracted from
64  * the WsJobParameters dialog box.
65  * 
66  * @author jprocter
67  * 
68  */
69 public class OptsAndParamsPage
70 {
71   /**
72    * compact or verbose style parameters
73    */
74   boolean compact = false;
75
76   public class OptionBox extends JPanel implements MouseListener,
77           ActionListener
78   {
79     JCheckBox enabled = new JCheckBox();
80
81     final URL finfo;
82
83     boolean hasLink = false;
84
85     boolean initEnabled = false;
86
87     String initVal = null;
88
89     OptionI option;
90
91     JLabel optlabel = new JLabel();
92
93     JComboBox val = new JComboBox();
94
95     public OptionBox(OptionI opt)
96     {
97       option = opt;
98       setLayout(new BorderLayout());
99       enabled.setSelected(opt.isRequired()); // TODO: lock required options
100       enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
101       enabled.setText("");
102       enabled.setText(opt.getName());
103       enabled.addActionListener(this);
104       finfo = option.getFurtherDetails();
105       String desc = opt.getDescription();
106       if (finfo != null)
107       {
108         hasLink = true;
109
110         enabled.setToolTipText("<html>"
111                 + JvSwingUtils
112                         .wrapTooltip(((desc == null || desc.trim().length()==0) ? "see further details by right-clicking"
113                                 : desc)
114                                 + "<br><img src=\"" + linkImageURL + "\"/>")
115                 + "</html>");
116         enabled.addMouseListener(this);
117       }
118       else
119       {
120         if (desc != null && desc.trim().length()>0)
121         {
122           enabled.setToolTipText("<html>"
123                   + JvSwingUtils.wrapTooltip(opt.getDescription())
124                   + "</html>");
125         }
126       }
127       add(enabled, BorderLayout.NORTH);
128       for (Object str : opt.getPossibleValues())
129       {
130         val.addItem((String) str);
131       }
132       val.setSelectedItem((String) opt.getValue());
133       if (opt.getPossibleValues().size() > 1)
134       {
135         setLayout(new GridLayout(1, 2));
136         val.addActionListener(this);
137         add(val, BorderLayout.SOUTH);
138       }
139       // TODO: add actionListeners for popup (to open further info),
140       // and to update list of parameters if an option is enabled
141       // that takes a value. JBPNote: is this TODO still valid ?
142       setInitialValue();
143     }
144
145     public void actionPerformed(ActionEvent e)
146     {
147       if (e.getSource() != enabled)
148       {
149         enabled.setSelected(true);
150       }
151       checkIfModified();
152     }
153
154     private void checkIfModified()
155     {
156       boolean notmod = (initEnabled == enabled.isSelected());
157       if (enabled.isSelected())
158       {
159         if (initVal != null)
160         {
161           notmod &= initVal.equals(val.getSelectedItem());
162         }
163         else
164         {
165           // compare against default service setting
166           notmod &= option.getValue() == null
167                   || option.getValue().equals(val.getSelectedItem());
168         }
169       }
170       else
171       {
172         notmod &= (initVal != null) ? initVal.equals(val.getSelectedItem())
173                 : val.getSelectedItem() != initVal;
174       }
175       poparent.argSetModified(this, !notmod);
176     }
177
178     public OptionI getOptionIfEnabled()
179     {
180       if (!enabled.isSelected())
181       {
182         return null;
183       }
184       OptionI opt = option.copy();
185       if (opt.getPossibleValues() != null
186               && opt.getPossibleValues().size() == 1)
187       {
188         // Hack to make sure the default value for an enabled option with only
189         // one value is actually returned
190         opt.setValue(opt.getPossibleValues().get(0));
191       }
192       if (val.getSelectedItem() != null)
193       {
194         opt.setValue((String) val.getSelectedItem());
195       }
196       else
197       {
198         if (option.getValue() != null)
199         {
200           opt.setValue(option.getValue());
201         }
202       }
203       return opt;
204     }
205
206     public void mouseClicked(MouseEvent e)
207     {
208       if (javax.swing.SwingUtilities.isRightMouseButton(e))
209       {
210         showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
211       }
212     }
213
214     public void mouseEntered(MouseEvent e)
215     {
216       // TODO Auto-generated method stub
217
218     }
219
220     public void mouseExited(MouseEvent e)
221     {
222       // TODO Auto-generated method stub
223
224     }
225
226     public void mousePressed(MouseEvent e)
227     {
228       // TODO Auto-generated method stub
229
230     }
231
232     public void mouseReleased(MouseEvent e)
233     {
234       // TODO Auto-generated method stub
235
236     }
237
238     public void resetToDefault()
239     {
240       enabled.setSelected(false);
241       if (option.isRequired())
242       {
243         // Apply default value
244         selectOption(option, option.getValue());
245       }
246     }
247
248     public void setInitialValue()
249     {
250       initEnabled = enabled.isSelected();
251       if (option.getPossibleValues() != null
252               && option.getPossibleValues().size() > 1)
253       {
254         initVal = (String) val.getSelectedItem();
255       }
256       else
257       {
258         initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
259       }
260     }
261
262   }
263
264   public class ParamBox extends JPanel implements ChangeListener,
265           ActionListener, MouseListener
266   {
267     boolean adjusting = false;
268
269     boolean choice = false;
270
271     JComboBox choicebox;
272
273     JPanel controlPanel = new JPanel();
274
275     boolean descisvisible = false;
276
277     JScrollPane descPanel = new JScrollPane();
278
279     final URL finfo;
280
281     boolean integ = false;
282
283     Object lastVal;
284
285     ParameterI parameter;
286
287     final OptsParametersContainerI pmdialogbox;
288
289     JPanel settingPanel = new JPanel();
290
291     JButton showDesc = new JButton();
292
293     JSlider slider = null;
294
295     JTextArea string = new JTextArea();
296
297     ValueConstrainI validator = null;
298
299     JTextField valueField = null;
300
301     public ParamBox(final OptsParametersContainerI pmlayout, ParameterI parm)
302     {
303       pmdialogbox = pmlayout;
304       finfo = parm.getFurtherDetails();
305       validator = parm.getValidValue();
306       parameter = parm;
307       if (validator != null)
308       {
309         integ = validator.getType() == ValueType.Integer;
310       }
311       else
312       {
313         if (parameter.getPossibleValues() != null)
314         {
315           choice = true;
316         }
317       }
318
319       if (!compact)
320       {
321         makeExpanderParam(parm);
322       }
323       else
324       {
325         makeCompactParam(parm);
326
327       }
328     }
329
330     private void makeCompactParam(ParameterI parm)
331     {
332       setLayout(new MigLayout("", "[][grow]"));
333
334       String ttipText = null;
335
336       controlPanel.setLayout(new BorderLayout());
337
338       if (parm.getDescription() != null
339               && parm.getDescription().trim().length() > 0)
340       {
341         // Only create description boxes if there actually is a description.
342         ttipText = ("<html>"
343                 + JvSwingUtils
344                         .wrapTooltip(parm.getDescription()
345                                 + (finfo != null ? "<br><img src=\""
346                                         + linkImageURL
347                                         + "\"/> Right click for further information."
348                                         : "")) + "</html>");
349       }
350
351       JvSwingUtils.mgAddtoLayout(this, ttipText,
352               new JLabel(parm.getName()), controlPanel, "");
353       updateControls(parm);
354       validate();
355     }
356
357     private void makeExpanderParam(ParameterI parm)
358     {
359       setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
360       setBorder(new TitledBorder(parm.getName()));
361       setLayout(null);
362       showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
363       showDesc.setText("+");
364       string.setFont(new Font("Verdana", Font.PLAIN, 11));
365       string.setBackground(getBackground());
366
367       string.setEditable(false);
368       descPanel.getViewport().setView(string);
369
370       descPanel.setVisible(false);
371
372       JPanel firstrow = new JPanel();
373       firstrow.setLayout(null);
374       controlPanel.setLayout(new BorderLayout());
375       controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
376               PARAM_CLOSEDHEIGHT - 50));
377       firstrow.add(controlPanel);
378       firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
379               PARAM_CLOSEDHEIGHT - 30));
380
381       final ParamBox me = this;
382
383       if (parm.getDescription() != null
384               && parm.getDescription().trim().length() > 0)
385       {
386         // Only create description boxes if there actually is a description.
387         if (finfo != null)
388         {
389           showDesc.setToolTipText("<html>"
390                   + JvSwingUtils
391                           .wrapTooltip("Click to show brief description<br><img src=\""
392                                   + linkImageURL
393                                   + "\"/> Right click for further information.")
394                   + "</html>");
395           showDesc.addMouseListener(this);
396         }
397         else
398         {
399           showDesc.setToolTipText("<html>"
400                   + JvSwingUtils
401                           .wrapTooltip("Click to show brief description.")
402                   + "</html>");
403         }
404         showDesc.addActionListener(new ActionListener()
405         {
406
407           public void actionPerformed(ActionEvent e)
408           {
409             descisvisible = !descisvisible;
410             descPanel.setVisible(descisvisible);
411             descPanel.getVerticalScrollBar().setValue(0);
412             me.setPreferredSize(new Dimension(PARAM_WIDTH,
413                     (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
414             me.validate();
415             pmdialogbox.refreshParamLayout();
416           }
417         });
418         string.setWrapStyleWord(true);
419         string.setLineWrap(true);
420         string.setColumns(32);
421         string.setText(parm.getDescription());
422         showDesc.setBounds(new Rectangle(10, 10, 16, 16));
423         firstrow.add(showDesc);
424       }
425       add(firstrow);
426       validator = parm.getValidValue();
427       parameter = parm;
428       if (validator != null)
429       {
430         integ = validator.getType() == ValueType.Integer;
431       }
432       else
433       {
434         if (parameter.getPossibleValues() != null)
435         {
436           choice = true;
437         }
438       }
439       updateControls(parm);
440       descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
441               PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
442       add(descPanel);
443       validate();
444     }
445
446     public void actionPerformed(ActionEvent e)
447     {
448       if (adjusting)
449       {
450         return;
451       }
452       if (!choice)
453       {
454         updateSliderFromValueField();
455       }
456       checkIfModified();
457     }
458
459     private void checkIfModified()
460     {
461       Object cstate = updateSliderFromValueField();
462       boolean notmod = false;
463       if (cstate.getClass() == lastVal.getClass())
464       {
465         if (cstate instanceof int[])
466         {
467           notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
468         }
469         else if (cstate instanceof float[])
470         {
471           notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
472         }
473         else if (cstate instanceof String[])
474         {
475           notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
476         }
477       }
478       pmdialogbox.argSetModified(this, !notmod);
479     }
480
481     @Override
482     public int getBaseline(int width, int height)
483     {
484       return 0;
485     }
486
487     // from
488     // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
489     // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
490     @Override
491     public Component.BaselineResizeBehavior getBaselineResizeBehavior()
492     {
493       return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
494     }
495
496     public int getBoxHeight()
497     {
498       return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
499     }
500
501     public ParameterI getParameter()
502     {
503       ParameterI prm = parameter.copy();
504       if (choice)
505       {
506         prm.setValue((String) choicebox.getSelectedItem());
507       }
508       else
509       {
510         prm.setValue(valueField.getText());
511       }
512       return prm;
513     }
514
515     public void init()
516     {
517       // reset the widget's initial value.
518       lastVal = null;
519     }
520
521     public void mouseClicked(MouseEvent e)
522     {
523       if (javax.swing.SwingUtilities.isRightMouseButton(e))
524       {
525         showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
526       }
527     }
528
529     public void mouseEntered(MouseEvent e)
530     {
531       // TODO Auto-generated method stub
532
533     }
534
535     public void mouseExited(MouseEvent e)
536     {
537       // TODO Auto-generated method stub
538
539     }
540
541     public void mousePressed(MouseEvent e)
542     {
543       // TODO Auto-generated method stub
544
545     }
546
547     public void mouseReleased(MouseEvent e)
548     {
549       // TODO Auto-generated method stub
550
551     }
552
553     public void stateChanged(ChangeEvent e)
554     {
555       if (!adjusting)
556       {
557         valueField.setText(""
558                 + ((integ) ? ("" + (int) slider.getValue())
559                         : ("" + (float) (slider.getValue() / 1000f))));
560         checkIfModified();
561       }
562
563     }
564
565     public void updateControls(ParameterI parm)
566     {
567       adjusting = true;
568       boolean init = (choicebox == null && valueField == null);
569       if (init)
570       {
571         if (choice)
572         {
573           choicebox = new JComboBox();
574           choicebox.addActionListener(this);
575           controlPanel.add(choicebox, BorderLayout.CENTER);
576         }
577         else
578         {
579           slider = new JSlider();
580           slider.addChangeListener(this);
581           valueField = new JTextField();
582           valueField.addActionListener(this);
583           valueField.addKeyListener(new KeyListener()
584           {
585
586             @Override
587             public void keyTyped(KeyEvent e)
588             {
589             }
590
591             @Override
592             public void keyReleased(KeyEvent e)
593             {
594               if (valueField.getText().trim().length() > 0)
595               {
596                 actionPerformed(null);
597               }
598             }
599
600             @Override
601             public void keyPressed(KeyEvent e)
602             {
603             }
604           });
605           valueField.setPreferredSize(new Dimension(60, 25));
606           controlPanel.add(slider, BorderLayout.WEST);
607           controlPanel.add(valueField, BorderLayout.EAST);
608
609         }
610       }
611
612       if (parm != null)
613       {
614         if (choice)
615         {
616           if (init)
617           {
618             List vals = parm.getPossibleValues();
619             for (Object val : vals)
620             {
621               choicebox.addItem(val);
622             }
623           }
624
625           if (parm.getValue() != null)
626           {
627             choicebox.setSelectedItem(parm.getValue());
628           }
629         }
630         else
631         {
632           valueField.setText(parm.getValue());
633         }
634       }
635       lastVal = updateSliderFromValueField();
636       adjusting = false;
637     }
638
639     public Object updateSliderFromValueField()
640     {
641       int iVal;
642       float fVal;
643       if (validator != null)
644       {
645         if (integ)
646         {
647           iVal = 0;
648           try
649           {
650             valueField.setText(valueField.getText().trim());
651             iVal = Integer.valueOf(valueField.getText());
652             if (validator.getMin() != null
653                     && validator.getMin().intValue() > iVal)
654             {
655               iVal = validator.getMin().intValue();
656               // TODO: provide visual indication that hard limit was reached for
657               // this parameter
658             }
659             if (validator.getMax() != null
660                     && validator.getMax().intValue() < iVal)
661             {
662               iVal = validator.getMax().intValue();
663               // TODO: provide visual indication that hard limit was reached for
664               // this parameter
665             }
666           } catch (Exception e)
667           {
668           }
669           ;
670           // update value field to reflect any bound checking we performed.
671           valueField.setText("" + iVal);
672           if (validator.getMin() != null && validator.getMax() != null)
673           {
674             slider.getModel().setRangeProperties(iVal, 1,
675                     validator.getMin().intValue(),
676                     validator.getMax().intValue(), true);
677           }
678           else
679           {
680             slider.setVisible(false);
681           }
682           return new int[]
683           { iVal };
684         }
685         else
686         {
687           fVal = 0f;
688           try
689           {
690             valueField.setText(valueField.getText().trim());
691             fVal = Float.valueOf(valueField.getText());
692             if (validator.getMin() != null
693                     && validator.getMin().floatValue() > fVal)
694             {
695               fVal = validator.getMin().floatValue();
696               // TODO: provide visual indication that hard limit was reached for
697               // this parameter
698               // update value field to reflect any bound checking we performed.
699               valueField.setText("" + fVal);
700             }
701             if (validator.getMax() != null
702                     && validator.getMax().floatValue() < fVal)
703             {
704               fVal = validator.getMax().floatValue();
705               // TODO: provide visual indication that hard limit was reached for
706               // this parameter
707               // update value field to reflect any bound checking we performed.
708               valueField.setText("" + fVal);
709             }
710           } catch (Exception e)
711           {
712           }
713           ;
714           if (validator.getMin() != null && validator.getMax() != null)
715           {
716             slider.getModel().setRangeProperties((int) fVal * 1000, 1,
717                     (int) validator.getMin().floatValue() * 1000,
718                     (int) validator.getMax().floatValue() * 1000, true);
719           }
720           else
721           {
722             slider.setVisible(false);
723           }
724           return new float[]
725           { fVal };
726         }
727       }
728       else
729       {
730         if (!choice)
731         {
732           slider.setVisible(false);
733           return new String[]
734           { valueField.getText().trim() };
735         }
736         else
737         {
738           return new String[]
739           { (String) choicebox.getSelectedItem() };
740         }
741       }
742
743     }
744   }
745
746   public static final int PARAM_WIDTH = 340;
747
748   public static final int PARAM_HEIGHT = 150;
749
750   public static final int PARAM_CLOSEDHEIGHT = 80;
751
752   public OptsAndParamsPage(OptsParametersContainerI paramContainer)
753   {
754     this(paramContainer, false);
755   }
756
757   public OptsAndParamsPage(OptsParametersContainerI paramContainer,
758           boolean compact)
759   {
760     poparent = paramContainer;
761     this.compact = compact;
762   }
763
764   public static void showUrlPopUp(JComponent invoker, final String finfo,
765           int x, int y)
766   {
767
768     JPopupMenu mnu = new JPopupMenu();
769     JMenuItem mitem = new JMenuItem("View " + finfo);
770     mitem.addActionListener(new ActionListener()
771     {
772
773       @Override
774       public void actionPerformed(ActionEvent e)
775       {
776         Desktop.showUrl(finfo);
777
778       }
779     });
780     mnu.add(mitem);
781     mnu.show(invoker, x, y);
782   }
783
784   URL linkImageURL = getClass().getResource("/images/link.gif");
785
786   Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
787
788   Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
789
790   public Map<String, OptionBox> getOptSet()
791   {
792     return optSet;
793   }
794
795   public void setOptSet(Map<String, OptionBox> optSet)
796   {
797     this.optSet = optSet;
798   }
799
800   public Map<String, ParamBox> getParamSet()
801   {
802     return paramSet;
803   }
804
805   public void setParamSet(Map<String, ParamBox> paramSet)
806   {
807     this.paramSet = paramSet;
808   }
809
810   OptsParametersContainerI poparent;
811
812   OptionBox addOption(OptionI opt)
813   {
814     OptionBox cb = optSet.get(opt.getName());
815     if (cb == null)
816     {
817       cb = new OptionBox(opt);
818       optSet.put(opt.getName(), cb);
819       // jobOptions.add(cb, FlowLayout.LEFT);
820     }
821     return cb;
822   }
823
824   ParamBox addParameter(ParameterI arg)
825   {
826     ParamBox pb = paramSet.get(arg.getName());
827     if (pb == null)
828     {
829       pb = new ParamBox(poparent, arg);
830       paramSet.put(arg.getName(), pb);
831       // paramList.add(pb);
832     }
833     pb.init();
834     // take the defaults from the parameter
835     pb.updateControls(arg);
836     return pb;
837   }
838
839   void selectOption(OptionI option, String string)
840   {
841     OptionBox cb = optSet.get(option.getName());
842     if (cb == null)
843     {
844       cb = addOption(option);
845     }
846     cb.enabled.setSelected(string != null); // initial state for an option.
847     if (string != null)
848     {
849       if (option.getPossibleValues().contains(string))
850       {
851         cb.val.setSelectedItem(string);
852       }
853       else
854       {
855         throw new Error("Invalid value " + string + " for option " + option);
856       }
857
858     }
859     if (option.isRequired() && !cb.enabled.isSelected())
860     {
861       // TODO: indicate paramset is not valid.. option needs to be selected!
862     }
863     cb.setInitialValue();
864   }
865
866   void setParameter(ParameterI arg)
867   {
868     ParamBox pb = paramSet.get(arg.getName());
869     if (pb == null)
870     {
871       addParameter(arg);
872     }
873     else
874     {
875       pb.updateControls(arg);
876     }
877
878   }
879
880   /**
881    * recover options and parameters from GUI
882    * 
883    * @return
884    */
885   public List<ArgumentI> getCurrentSettings()
886   {
887     List<ArgumentI> argSet = new ArrayList<ArgumentI>();
888     for (OptionBox opts : getOptSet().values())
889     {
890       OptionI opt = opts.getOptionIfEnabled();
891       if (opt != null)
892       {
893         argSet.add(opt);
894       }
895     }
896     for (ParamBox parambox : getParamSet().values())
897     {
898       ParameterI parm = parambox.getParameter();
899       if (parm != null)
900       {
901         argSet.add(parm);
902       }
903     }
904
905     return argSet;
906   }
907
908 }