drop down selection of different parameter sets
authorjprocter <Jim Procter>
Mon, 28 Jun 2010 13:56:13 +0000 (13:56 +0000)
committerjprocter <Jim Procter>
Mon, 28 Jun 2010 13:56:13 +0000 (13:56 +0000)
src/jalview/gui/WsJobParameters.java

index 36266d1..89028ef 100644 (file)
@@ -14,6 +14,8 @@ import java.awt.event.ComponentEvent;
 import java.awt.event.ComponentListener;
 import java.awt.event.ContainerEvent;
 import java.awt.event.ContainerListener;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
 import java.awt.event.MouseEvent;
@@ -66,9 +68,12 @@ import compbio.metadata.ValueConstrain.Type;
 import jalview.ws.jws2.Jws2Discoverer;
 import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
 
-public class WsJobParameters extends JPanel
+public class WsJobParameters extends JPanel implements ItemListener,
+        ActionListener
 {
-  private static final int PARAM_WIDTH = 340, PARAM_HEIGHT = 120;
+  private static final int PARAM_WIDTH = 340, PARAM_HEIGHT = 150,PARAM_CLOSEDHEIGHT=80;
+
+  private static final int OPTSET_HEIGHT = 30;
 
   JPanel setDetails = new JPanel();
 
@@ -76,13 +81,18 @@ public class WsJobParameters extends JPanel
 
   JPanel jobOptions = new JPanel();
 
+  JScrollPane jobOptionsPane = new JScrollPane();
+
   JPanel jobParameters = new JPanel();
 
   JPanel paramButtons = new JPanel();
 
   JPanel paramPanel = new JPanel();
+
   JLabel modified = new JLabel();
+
   JButton startjob = new JButton();
+
   JButton canceljob = new JButton();
 
   JButton exportParams = new JButton();
@@ -97,7 +107,7 @@ public class WsJobParameters extends JPanel
 
   JButton editParam = new JButton();
 
-  JLabel setName = new JLabel();
+  JComboBox setName = new JComboBox();
 
   JTextArea setDescr = new JTextArea();
 
@@ -140,35 +150,38 @@ public class WsJobParameters extends JPanel
     // display in new JFrame attached to parent.
     validate();
   }
-  int response=-1;
-  JDialog frame=null;
-  public boolean showRunDialog() {
-    
-    frame = new JDialog(
-            Desktop.instance, true);
-    
-    frame.setTitle("Edit parameters for "+service.getActionText());
+
+  int response = -1;
+
+  JDialog frame = null;
+
+  public boolean showRunDialog()
+  {
+
+    frame = new JDialog(Desktop.instance, true);
+
+    frame.setTitle("Edit parameters for " + service.getActionText());
     Rectangle deskr = Desktop.instance.getBounds();
     frame.setBounds(new Rectangle((int) (deskr.getCenterX() - 240),
             (int) (deskr.getCenterY() - 92), 380, 385));
     frame.setContentPane(this);
 
     frame.setVisible(true);
-    
-    if (response>0)
+
+    if (response > 0)
     {
       return true;
     }
     return false;
   }
-  
+
   private void jbInit()
   {
-    modified.setText("Job Options Modified");
+    modified.setText("*");
     modified.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
     modified.setForeground(Color.red);
-    modified.setHorizontalAlignment(SwingConstants.CENTER);
-    
+    modified.setHorizontalAlignment(SwingConstants.RIGHT);
+
     exportParams.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
     exportParams.setText("Export");
     exportParams.setToolTipText("Export parameters to a file");
@@ -251,18 +264,26 @@ public class WsJobParameters extends JPanel
     });
 
     setDetails.setBorder(new TitledBorder("Details"));
-    setDetails.setLayout(new GridLayout(2, 1));
+    setDetails.setLayout(new BorderLayout());
     setDescr.setColumns(40);
     setDescr.setWrapStyleWord(true);
     setDescr.setLineWrap(true);
-    setDescr.setPreferredSize(new Dimension(350, 90));
     setDescr.setBackground(getBackground());
     setDescr.setEditable(false);
-    setDetails.add(setName);
-    setDetails.add(setDescr);
-    setDetails.setPreferredSize(new Dimension(360, 100));
-    settingsPanel.setLayout(new BorderLayout());
-    jobOptions.setBorder(new TitledBorder("Options"));
+    JScrollPane setDescrView = new JScrollPane();
+    // setDescrView.setPreferredSize(new Dimension(350, 200));
+    setDescrView.getViewport().setView(setDescr);
+    setName.setEditable(true);
+    setName.addItemListener(this);
+    setName.getEditor().addActionListener(this);
+    modified.setPreferredSize(new Dimension(15, 15));
+    JPanel SetNamePanel = new JPanel();
+    SetNamePanel.setLayout(new BorderLayout());
+    SetNamePanel.add(setName, BorderLayout.WEST);
+    SetNamePanel.add(modified, BorderLayout.EAST);
+    setDetails.add(SetNamePanel, BorderLayout.NORTH);
+    setDetails.add(setDescrView, BorderLayout.CENTER);
+    // setDetails.setPreferredSize(new Dimension(360, 100));
     jobParameters.setBorder(new TitledBorder("Parameters"));
     jobParameters.setLayout(jparamLayout = new BorderLayout());
     paramPane.setPreferredSize(new Dimension(360, 300));
@@ -278,11 +299,16 @@ public class WsJobParameters extends JPanel
     paramButtons.add(resetParams);
     paramPane.getViewport().setView(paramList);
     jobParameters.add(paramPane, BorderLayout.NORTH);
-    //jobParameters.add(paramButtons, BorderLayout.SOUTH);
-    settingsPanel.add(modified, BorderLayout.NORTH);
-    settingsPanel.add(jobOptions, BorderLayout.CENTER);
+    // jobParameters.add(paramButtons, BorderLayout.SOUTH);
+    settingsPanel.setLayout(new BorderLayout());
+    // settingsPanel.add(modified, BorderLayout.NORTH);
+    JPanel jobOptionsPanel = new JPanel();
+    jobOptionsPanel.setBorder(new TitledBorder("Options"));
+    jobOptionsPane.getViewport().setView(jobOptions);
+    jobOptionsPanel.add(jobOptionsPane);
+    settingsPanel.add(jobOptionsPanel, BorderLayout.NORTH);
     settingsPanel.add(jobParameters, BorderLayout.SOUTH);
-    
+
     setLayout(new BorderLayout());
     // setPreferredSize(new Dimension(400, 600));
     // setSize(new Dimension(400, 600));
@@ -296,16 +322,18 @@ public class WsJobParameters extends JPanel
 
   protected void canceljob_actionPerformed(ActionEvent e)
   {
-    response=0;
-    if (frame!=null) {
+    response = 0;
+    if (frame != null)
+    {
       frame.setVisible(false);
     }
   }
 
   protected void startjob_actionPerformed(ActionEvent e)
   {
-    response=1;
-    if (frame!=null) {
+    response = 1;
+    if (frame != null)
+    {
       frame.setVisible(false);
     }
   }
@@ -347,11 +375,29 @@ public class WsJobParameters extends JPanel
   }
 
   Jws2Instance service;
+
   void initForService(Jws2Instance service, Preset p,
           List<Argument> jobArgset)
   {
-    this.service=service;
+    this.service = service;
     serviceOptions = service.getRunnerConfig();
+    // add any presets not already added.
+    String curname = (p == null ? "" : p.getName());
+    PresetManager prman = service.getPresets();
+    if (prman != null)
+    {
+      List prList = service.getPresets().getPresets();
+      if (prList != null)
+      {
+        for (Object pr : prList)
+        {
+          if (p == null || !curname.equals(((Preset) pr).getName()))
+          {
+            setName.addItem(((Preset) pr).getName());
+          }
+        }
+      }
+    }
     updateTable(p, jobArgset);
   }
 
@@ -399,13 +445,16 @@ public class WsJobParameters extends JPanel
           }
         }
       }
-      args=null; // no more args to process.
+      args = null; // no more args to process.
     }
     if (p != null)
     {
+      if (lastParmSet == null)
+      {
+        // initialise setname
+        setName.addItem(p.getName());
+      }
       setDescr.setText(p.getDescription());
-      setName.setText(p.getName());
-      setDetails.setVisible(true);
       // TODO - URL link
       try
       {
@@ -414,10 +463,17 @@ public class WsJobParameters extends JPanel
       {
         e.printStackTrace();
       }
-      // TODO: check if args should be unselected prior to resetting using the preset
+      // TODO: check if args should be unselected prior to resetting using the
+      // preset
       setargs.clear();
-    } else {
-      setDetails.setVisible(false);
+    }
+    else
+    {
+      if (lastParmSet == null)
+      {
+        // first call - so create a dummy name
+        setName.addItem("Defaults *");
+      }
     }
     if (jobArgset != null)
     {
@@ -446,27 +502,39 @@ public class WsJobParameters extends JPanel
 
     // mdl.setData(setargs);
     // jobOptions.setLayout(new GridLayout(optSet.size() / 2 + 1, 2));
+    jobOptions.setPreferredSize(new Dimension(PARAM_WIDTH, optSet.size()
+            * OPTSET_HEIGHT));
     jobOptions.setLayout(new GridLayout(optSet.size(), 1));
+    refreshParamLayout();
     // paramPanel.setLayout(new GridLayout(paramSet.size(), 1));
     // paramList.va
     // paramPane.getViewport().validate();
-    paramList.setPreferredSize(new Dimension(350, paramSet.size()
-            * PARAM_HEIGHT));
-    paramList.setLayout(new GridLayout(paramSet.size(), 1));
-    paramList.validate();
     paramPane.validate();
     validate();
   }
 
+  private boolean isModified()
+  {
+    return modified.isVisible();
+  }
+
   private void argSetModified(boolean b)
   {
-    
     modified.setVisible(b);
-    if (isVisible())
-    {
-      validate();
-    }
-   
+    /*
+     * String setn = (String)setName.getSelectedItem(); if (setn==null) return;
+     * int p = setName.getSelectedIndex(); if (b) { if (setn.length()<2 ||
+     * setn.lastIndexOf(" *")!=setn.length()-2) { setn = setn+" *"; } } else {
+     * if (setn.lastIndexOf(" *")==setn.length()-2) { setn =
+     * setn.substring(0,setn.length()-2); } } // edit the list
+     * 
+     * Object[] objects = new Object[setName.getItemCount()]; for (int
+     * i=0;i<objects.length; i++) { objects[i] = setName.getItemAt(i); }
+     * objects[p] = setn; setName.removeAllItems(); for (int
+     * i=0;i<objects.length; i++) { setName.addItem(objects[i]); }
+     * setName.setSelectedIndex(p);
+     */
+    validate();
   }
 
   private void addParameter(Parameter parm)
@@ -474,7 +542,7 @@ public class WsJobParameters extends JPanel
     ParamBox pb = paramSet.get(parm.getName());
     if (pb == null)
     {
-      pb = new ParamBox(parm);
+      pb = new ParamBox(this, parm);
       paramSet.put(parm.getName(), pb);
       paramList.add(pb);
     }
@@ -520,55 +588,87 @@ public class WsJobParameters extends JPanel
   public class ParamBox extends JPanel implements ChangeListener,
           ActionListener
   {
-    JLabel name = new JLabel();
+    JCheckBox showDesc = new JCheckBox();
 
     JTextArea string = new JTextArea();
     JScrollPane descPanel = new JScrollPane();
+
     JSlider slider = null;
 
     JTextField valueField = null;
 
     ValueConstrain validator = null;
-
+    JPanel settingPanel = new JPanel();
     boolean integ = false;
-    boolean choice=false;
-    public ParamBox(Parameter parm)
+
+    boolean choice = false;
+
+    public ParamBox(final WsJobParameters pmlayout, Parameter parm)
     {
-      setLayout(new FlowLayout());
+      setLayout(new FlowLayout(FlowLayout.LEFT));
       setBorder(new TitledBorder(parm.getName()));
-      setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_HEIGHT));
+      setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
       string.setFont(new Font("Verdana", Font.PLAIN, 11));
       string.setBackground(getBackground());
-      //string.setSize(new Dimension(PARAM_WIDTH, 80));
+      // string.setSize(new Dimension(PARAM_WIDTH, 80));
       string.setEditable(false);
-      descPanel.setPreferredSize(new Dimension(PARAM_WIDTH, 70));
+      showDesc.setText("Show Description");
+      showDesc.setSelected(false);
+      showDesc.setToolTipText(parm.getDescription());
+      //showDesc.setLocation(2,2);
+      descPanel.setPreferredSize(new Dimension(PARAM_WIDTH-20, 90));
       descPanel.getViewport().setView(string);
+      //descPanel.setLocation(2,17);
+      descPanel.setVisible(false);
       // string.setMinimumSize(new Dimension(140,80));
       // string.setMaximumSize(new Dimension(280,80));
+      final ParamBox me=this;
+      showDesc.addActionListener(new ActionListener() {
+
+        @Override
+        public void actionPerformed(ActionEvent e)
+        {
+          boolean show = showDesc.isSelected();
+          descPanel.setVisible(show);
+          me.setPreferredSize(new Dimension(PARAM_WIDTH, (show) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
+          me.validate();
+          pmlayout.refreshParamLayout();
+        }
+      });
       string.setWrapStyleWord(true);
       string.setLineWrap(true);
-      string.setColumns(40);
+      string.setColumns(32);
       string.setText(parm.getDescription());
-      descPanel.validate();
       // name.setToolTipText(parm.getDescription());
-      add(descPanel); // , BorderLayout.NORTH);
+      add(showDesc);
       validator = parm.getValidValue();
-      parameter=parm;
+      parameter = parm;
       if (validator != null)
       {
         integ = validator.getType() == Type.Integer;
-      } else {
-        if (parameter.getPossibleValues()!=null)
+      }
+      else
+      {
+        if (parameter.getPossibleValues() != null)
         {
-          choice=true;
+          choice = true;
         }
       }
       updateControls(parm);
+      add(descPanel); // , BorderLayout.NORTH);
+      validate();
     }
 
+
     boolean adjusting = false;
+
     Parameter parameter;
+
     JComboBox choicebox;
+    public int getBoxHeight()
+    {
+      return (showDesc.isSelected() ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
+    }
     public void updateControls(Parameter parm)
     {
       adjusting = true;
@@ -582,46 +682,57 @@ public class WsJobParameters extends JPanel
           choicebox = new JComboBox();
           choicebox.addActionListener(this);
           add(choicebox);
-        } else {
-        slider = new JSlider();
-        slider.addChangeListener(this);
-        valueField = new JTextField();
-        valueField.addActionListener(this);
-        valueField.setPreferredSize(new Dimension(80, 25));
-        JPanel container = new JPanel();
-        // container.setLayout(new FlowLayout());
-        add(slider);// , BorderLayout.SOUTH);
-        add(valueField);// ,BorderLayout.CENTER);
-        // add(container, BorderLayout.EAST);
-        // container.validate();
-
-      }}
-      
+        }
+        else
+        {
+          slider = new JSlider();
+          slider.addChangeListener(this);
+          valueField = new JTextField();
+          valueField.addActionListener(this);
+          valueField.setPreferredSize(new Dimension(80, 25));
+          JPanel container = new JPanel();
+          // container.setLayout(new FlowLayout());
+          add(slider);// , BorderLayout.SOUTH);
+          add(valueField);// ,BorderLayout.CENTER);
+          // add(container, BorderLayout.EAST);
+          // container.validate();
+
+        }
+      }
+
       if (parm != null)
       {
-        if (choice) {
+        if (choice)
+        {
           List vals = parm.getPossibleValues();
-          for (Object val: vals)
+          for (Object val : vals)
           {
             choicebox.addItem(val);
           }
-          if (parm.getDefaultValue()!=null) {
+          if (parm.getDefaultValue() != null)
+          {
             choicebox.setSelectedItem(parm.getDefaultValue());
           }
-        } else {
+        }
+        else
+        {
           valueField.setText(parm.getDefaultValue());
         }
       }
       updateSliderFromValueField();
       adjusting = false;
     }
-    public Parameter getParameter() {
-      try {
+
+    public Parameter getParameter()
+    {
+      try
+      {
         if (choice)
         {
-          parameter.setDefaultValue((String)choicebox.getSelectedItem());
+          parameter.setDefaultValue((String) choicebox.getSelectedItem());
         }
-        else {
+        else
+        {
           parameter.setDefaultValue(valueField.getText());
         }
       } catch (WrongParameterException e)
@@ -631,6 +742,7 @@ public class WsJobParameters extends JPanel
       }
       return parameter;
     }
+
     public Object updateSliderFromValueField()
     {
       int iVal;
@@ -687,13 +799,16 @@ public class WsJobParameters extends JPanel
       }
       else
       {
-        if (!choice) {
+        if (!choice)
+        {
           slider.setVisible(false);
-        return new String[]
-        { valueField.getText().trim() };
-      }
-        else {
-          return new String[] { (String) choicebox.getSelectedItem()};
+          return new String[]
+          { valueField.getText().trim() };
+        }
+        else
+        {
+          return new String[]
+          { (String) choicebox.getSelectedItem() };
         }
       }
 
@@ -715,7 +830,8 @@ public class WsJobParameters extends JPanel
     {
       if (choice)
       {
-      } else if (!adjusting)
+      }
+      else if (!adjusting)
       {
         updateSliderFromValueField();
       }
@@ -731,7 +847,9 @@ public class WsJobParameters extends JPanel
     JComboBox val = new JComboBox();
 
     JCheckBox enabled = new JCheckBox();
+
     Option option;
+
     public OptionBox(Option opt)
     {
       option = opt;
@@ -756,13 +874,16 @@ public class WsJobParameters extends JPanel
       // and to update list of parameters if an option is enabled
       // that takes a value.
     }
-    public Option getOptionIfEnabled() {
+
+    public Option getOptionIfEnabled()
+    {
       if (!enabled.isSelected())
       {
         return null;
       }
-      try {
-        if (val.getSelectedItem()!=null)
+      try
+      {
+        if (val.getSelectedItem() != null)
         {
           option.setDefaultValue((String) val.getSelectedItem());
         }
@@ -773,6 +894,7 @@ public class WsJobParameters extends JPanel
       }
       return option;
     }
+
     @Override
     public void actionPerformed(ActionEvent e)
     {
@@ -793,6 +915,17 @@ public class WsJobParameters extends JPanel
     return cb;
   }
 
+  protected void refreshParamLayout()
+  {
+    int s=100;
+    for (ParamBox pbox : paramSet.values()){
+      s+=pbox.getBoxHeight();
+    }
+    paramList.setPreferredSize(new Dimension(PARAM_WIDTH, s));
+    paramList.setLayout(new FlowLayout());
+    validate();
+  }
+
   /**
    * testing method - grab a service and parameter set and show the window
    * 
@@ -935,23 +1068,89 @@ public class WsJobParameters extends JPanel
   {
     List<Argument> argSet = new ArrayList<Argument>();
     // recover options and parameters from GUI
-    for (OptionBox opts: optSet.values())
+    for (OptionBox opts : optSet.values())
     {
       Option opt = opts.getOptionIfEnabled();
-      if (opt!=null)
+      if (opt != null)
       {
         argSet.add(opt);
       }
     }
-    for (ParamBox parambox: paramSet.values())
+    for (ParamBox parambox : paramSet.values())
     {
       Parameter parm = parambox.getParameter();
-      if (parm!=null)
+      if (parm != null)
       {
         argSet.add(parm);
       }
     }
-    
+
     return argSet;
   }
+
+  String lastParmSet = null;
+
+  Hashtable<String, Object[]> editedParams = new Hashtable<String, Object[]>();
+
+  @Override
+  public void itemStateChanged(ItemEvent e)
+  {
+    if (e.getSource() == setName)
+    {
+      String setname = (String) setName.getSelectedItem();
+      if (setname == null)
+      {
+        return;
+      }
+      // if modified - warn user - or at least save current edited set.
+      if (lastParmSet != null)
+      {
+        // save current state
+        Object[] pset;
+        editedParams.put(lastParmSet, pset = new Object[3]);
+        pset[0] = lastParmSet;
+        pset[1] = setDescr.getText();
+        pset[2] = getJobParams();
+      }
+      Object[] pset = editedParams.get(setname);
+      if (pset != null)
+      {
+        setDescr.setText((String) pset[1]);
+        updateTable(null, (List<Argument>) pset[2]);
+        lastParmSet = setname;
+        validate();
+      }
+      else
+      {
+        // must be a default preset from service
+        Preset p = null;
+        try
+        {
+          p = service.getPresets().getPresetByName(setname);
+        } catch (Exception ex)
+        {
+          ex.printStackTrace();
+        }
+        if (p != null)
+        {
+          updateTable(p, null);
+          validate();
+          lastParmSet = setname;
+        }
+      }
+
+    }
+  }
+
+  @Override
+  public void actionPerformed(ActionEvent e)
+  {
+    if (e.getSource() == setName)
+    {
+      // rename any existing records we know about for this set.
+      String newname = (String) setName.getSelectedItem();
+      System.err.println("Command " + newname + " : "
+              + e.getActionCommand());
+    }
+  }
 }