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