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