patch for JAL-1172 ensure option's default value is set in widget so it gets saved...
[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           if (validator.getMin() != null && validator.getMax() != null)
664           {
665             slider.getModel().setRangeProperties(iVal, 1,
666                     validator.getMin().intValue(),
667                     validator.getMax().intValue(), true);
668           }
669           else
670           {
671             slider.setVisible(false);
672           }
673           return new int[]
674           { iVal };
675         }
676         else
677         {
678           fVal = 0f;
679           try
680           {
681             fVal = Float.valueOf(valueField.getText());
682             if (validator.getMin() != null
683                     && validator.getMin().floatValue() > fVal)
684             {
685               fVal = validator.getMin().floatValue();
686               // TODO: provide visual indication that hard limit was reached for
687               // this parameter
688             }
689             if (validator.getMax() != null
690                     && validator.getMax().floatValue() < fVal)
691             {
692               fVal = validator.getMax().floatValue();
693               // TODO: provide visual indication that hard limit was reached for
694               // this parameter
695             }
696           } catch (Exception e)
697           {
698           }
699           ;
700           if (validator.getMin() != null && validator.getMax() != null)
701           {
702             slider.getModel().setRangeProperties((int) fVal * 1000, 1,
703                     (int) validator.getMin().floatValue() * 1000,
704                     (int) validator.getMax().floatValue() * 1000, true);
705           }
706           else
707           {
708             slider.setVisible(false);
709           }
710           return new float[]
711           { fVal };
712         }
713       }
714       else
715       {
716         if (!choice)
717         {
718           slider.setVisible(false);
719           return new String[]
720           { valueField.getText().trim() };
721         }
722         else
723         {
724           return new String[]
725           { (String) choicebox.getSelectedItem() };
726         }
727       }
728
729     }
730   }
731
732   public static final int PARAM_WIDTH = 340;
733
734   public static final int PARAM_HEIGHT = 150;
735
736   public static final int PARAM_CLOSEDHEIGHT = 80;
737
738   public OptsAndParamsPage(OptsParametersContainerI paramContainer)
739   {
740     this(paramContainer,false);
741   }
742   public OptsAndParamsPage(OptsParametersContainerI paramContainer, boolean compact)
743   {
744     poparent = paramContainer;
745     this.compact=compact;
746   }
747
748   public static void showUrlPopUp(JComponent invoker, final String finfo,
749           int x, int y)
750   {
751
752     JPopupMenu mnu = new JPopupMenu();
753     JMenuItem mitem = new JMenuItem("View " + finfo);
754     mitem.addActionListener(new ActionListener()
755     {
756
757       @Override
758       public void actionPerformed(ActionEvent e)
759       {
760         Desktop.showUrl(finfo);
761
762       }
763     });
764     mnu.add(mitem);
765     mnu.show(invoker, x, y);
766   }
767
768   URL linkImageURL = getClass().getResource("/images/link.gif");
769
770   Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
771
772   Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
773
774   public Map<String, OptionBox> getOptSet()
775   {
776     return optSet;
777   }
778
779   public void setOptSet(Map<String, OptionBox> optSet)
780   {
781     this.optSet = optSet;
782   }
783
784   public Map<String, ParamBox> getParamSet()
785   {
786     return paramSet;
787   }
788
789   public void setParamSet(Map<String, ParamBox> paramSet)
790   {
791     this.paramSet = paramSet;
792   }
793
794   OptsParametersContainerI poparent;
795
796   OptionBox addOption(OptionI opt)
797   {
798     OptionBox cb = optSet.get(opt.getName());
799     if (cb == null)
800     {
801       cb = new OptionBox(opt);
802       optSet.put(opt.getName(), cb);
803       // jobOptions.add(cb, FlowLayout.LEFT);
804     }
805     return cb;
806   }
807
808   ParamBox addParameter(ParameterI arg)
809   {
810     ParamBox pb = paramSet.get(arg.getName());
811     if (pb == null)
812     {
813       pb = new ParamBox(poparent, arg);
814       paramSet.put(arg.getName(), pb);
815       // paramList.add(pb);
816     }
817     pb.init();
818     // take the defaults from the parameter
819     pb.updateControls(arg);
820     return pb;
821   }
822
823   void selectOption(OptionI option, String string)
824   {
825     OptionBox cb = optSet.get(option.getName());
826     if (cb == null)
827     {
828       cb = addOption(option);
829     }
830     cb.enabled.setSelected(string != null); // initial state for an option.
831     if (string != null)
832     {
833       if (option.getPossibleValues().contains(string))
834       {
835         cb.val.setSelectedItem(string);
836       }
837       else
838       {
839         throw new Error("Invalid value " + string + " for option " + option);
840       }
841
842     }
843     if (option.isRequired() && !cb.enabled.isSelected())
844     {
845       // TODO: indicate paramset is not valid.. option needs to be selected!
846     }
847     cb.setInitialValue();
848   }
849
850   void setParameter(ParameterI arg)
851   {
852     ParamBox pb = paramSet.get(arg.getName());
853     if (pb == null)
854     {
855       addParameter(arg);
856     }
857     else
858     {
859       pb.updateControls(arg);
860     }
861
862   }
863
864   /**
865    * recover options and parameters from GUI
866    * 
867    * @return
868    */
869   public List<ArgumentI> getCurrentSettings()
870   {
871     List<ArgumentI> argSet = new ArrayList<ArgumentI>();
872     for (OptionBox opts : getOptSet().values())
873     {
874       OptionI opt = opts.getOptionIfEnabled();
875       if (opt != null)
876       {
877         argSet.add(opt);
878       }
879     }
880     for (ParamBox parambox : getParamSet().values())
881     {
882       ParameterI parm = parambox.getParameter();
883       if (parm != null)
884       {
885         argSet.add(parm);
886       }
887     }
888
889     return argSet;
890   }
891
892 }