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