JAL-867 - rejigged layout to a single splitpane wide enough for longest combobox...
authorjprocter <jprocter@compbio.dundee.ac.uk>
Wed, 17 Aug 2011 09:20:15 +0000 (10:20 +0100)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Wed, 17 Aug 2011 09:20:15 +0000 (10:20 +0100)
src/jalview/gui/WsJobParameters.java

index 440a714..6017765 100644 (file)
  */
 package jalview.gui;
 
+import jalview.ws.jws2.JabaParamStore;
+import jalview.ws.jws2.JabaPreset;
+import jalview.ws.jws2.Jws2Discoverer;
+import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
+import jalview.ws.params.ArgumentI;
+import jalview.ws.params.OptionI;
+import jalview.ws.params.ParamDatastoreI;
+import jalview.ws.params.ParameterI;
+import jalview.ws.params.ValueConstrainI;
+import jalview.ws.params.WsParamSetI;
+
 import java.awt.BorderLayout;
-import java.awt.Color;
 import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
@@ -29,30 +39,20 @@ import java.awt.GridLayout;
 import java.awt.Rectangle;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-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;
 import java.awt.event.MouseListener;
 import java.awt.event.WindowEvent;
 import java.awt.event.WindowListener;
-import java.awt.event.WindowStateListener;
 import java.net.URL;
 import java.util.ArrayList;
-import java.util.EventObject;
-import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Vector;
 
-import javax.swing.InputVerifier;
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
@@ -60,7 +60,6 @@ import javax.swing.JComponent;
 import javax.swing.JDialog;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
-import javax.swing.JList;
 import javax.swing.JMenuItem;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
@@ -69,19 +68,13 @@ import javax.swing.JScrollPane;
 import javax.swing.JSlider;
 import javax.swing.JSplitPane;
 import javax.swing.JTabbedPane;
-import javax.swing.JTable;
 import javax.swing.JTextArea;
 import javax.swing.JTextField;
-import javax.swing.ListSelectionModel;
-import javax.swing.SwingConstants;
-import javax.swing.SwingUtilities;
 import javax.swing.border.TitledBorder;
-import javax.swing.event.CellEditorListener;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentListener;
-import javax.swing.table.*;
 
 import compbio.metadata.Argument;
 import compbio.metadata.Option;
@@ -89,25 +82,8 @@ import compbio.metadata.Parameter;
 import compbio.metadata.Preset;
 import compbio.metadata.PresetManager;
 import compbio.metadata.RunnerConfig;
-import compbio.metadata.ValueConstrain;
-import compbio.metadata.WrongParameterException;
 import compbio.metadata.ValueConstrain.Type;
 
-import jalview.util.jarInputStreamProvider;
-import jalview.ws.jws2.JabaParamStore;
-import jalview.ws.jws2.JabaPreset;
-import jalview.ws.jws2.Jws2Discoverer;
-import jalview.ws.jws2.ParameterUtils;
-import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
-import jalview.ws.jws2.dm.JabaOption;
-import jalview.ws.jws2.dm.JabaParameter;
-import jalview.ws.params.ArgumentI;
-import jalview.ws.params.OptionI;
-import jalview.ws.params.ParamDatastoreI;
-import jalview.ws.params.ParameterI;
-import jalview.ws.params.ValueConstrainI;
-import jalview.ws.params.WsParamSetI;
-
 /**
  * job parameter editing/browsing dialog box. User can browse existing settings
  * (user + presets + Defaults), and any changes to parameters creates a modified
@@ -135,8 +111,6 @@ public class WsJobParameters extends JPanel implements ItemListener,
   private static final int PARAM_WIDTH = 340, PARAM_HEIGHT = 150,
           PARAM_CLOSEDHEIGHT = 80;
 
-  private static final int OPTSET_HEIGHT = 30;
-
   JPanel SetNamePanel = new JPanel();
 
   JPanel setDetails = new JPanel();
@@ -145,10 +119,6 @@ public class WsJobParameters extends JPanel implements ItemListener,
 
   JSplitPane jobPanel = new JSplitPane();
 
-  boolean split = true;
-
-  JTabbedPane tabpanels = new JTabbedPane();
-
   JPanel jobOptions = new JPanel();
 
   JScrollPane jobOptionsPane = new JScrollPane();
@@ -175,10 +145,14 @@ public class WsJobParameters extends JPanel implements ItemListener,
 
   JPanel paramList = new JPanel();
 
+  JPanel optsAndparams = new JPanel();
+
   RunnerConfig serviceOptions;
 
   ParamDatastoreI paramStore;
 
+  private int MAX_OPTWIDTH = 200;
+
   WsJobParameters(Jws2Instance service)
   {
     this(service, null);
@@ -244,30 +218,23 @@ public class WsJobParameters extends JPanel implements ItemListener,
 
     frame.setTitle("Edit parameters for " + service.getActionText());
     Rectangle deskr = Desktop.instance.getBounds();
-    frame.setBounds(new Rectangle((int) (deskr.getCenterX() - 240),
-            (int) (deskr.getCenterY() - 250), 480, 500));
+    Dimension pref = this.getPreferredSize();
+    frame.setBounds(new Rectangle((int) (deskr.getCenterX() - pref.width/2),
+            (int) (deskr.getCenterY() - pref.height/2), pref.width, pref.height));
     frame.setContentPane(this);
-    // should recover defaults from user prefs.
-    // settingsPanel.setDividerLocation(0.4);
-    // jobPanel.setDividerLocation(0.5);
+    
+    // should perhaps recover defaults from user prefs.
+
     frame.validate();
-    if (split)
+    javax.swing.SwingUtilities.invokeLater(new Runnable()
     {
-      javax.swing.SwingUtilities.invokeLater(new Runnable()
-      {
         public void run()
         {
-          settingsPanel.setDividerLocation(0.5);
-          jobPanel.setDividerLocation(setDescr.getLineCount() > 3 ? 0.35
-                  : 0.16);
+          jobPanel.setDividerLocation(0.25); 
+
         }
       });
-    }
-    else
-    {
-      tabpanels.setSelectedComponent(jobOptionsPane);
-    }
-    frame.setVisible(true);
+  frame.setVisible(true);
 
     if (response > 0)
     {
@@ -353,12 +320,13 @@ public class WsJobParameters extends JPanel implements ItemListener,
     JPanel setNameInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
     GridBagLayout gbl = new GridBagLayout();
     SetNamePanel.setLayout(gbl);
-    SetNamePanel.setMinimumSize(new Dimension(300, 60));
+
     JLabel setNameLabel = new JLabel("Current parameter set name :");
     setNameLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
-    // setNameLabel.setHorizontalAlignment(FlowLayout.LEFT);
+    
     setNameInfo.add(setNameLabel);
     setNameInfo.add(setName);
+    
     // initial button visibility
     updatepref.setVisible(false);
     deletepref.setVisible(false);
@@ -368,11 +336,14 @@ public class WsJobParameters extends JPanel implements ItemListener,
     setsavebuts.setLayout(new FlowLayout(FlowLayout.LEFT)); // GridLayout(1,2));
     ((FlowLayout) setsavebuts.getLayout()).setHgap(10);
     ((FlowLayout) setsavebuts.getLayout()).setVgap(0);
+    JPanel spacer = new JPanel();
+    spacer.setPreferredSize(new Dimension(2,30));
+    setsavebuts.add(spacer);
     setsavebuts.add(deletepref);
     setsavebuts.add(revertpref);
     setsavebuts.add(createpref);
     setsavebuts.add(updatepref);
-    setsavebuts.setSize(new Dimension(150, 20));
+//    setsavebuts.setSize(new Dimension(150, 30));
     JPanel buttonArea = new JPanel(new GridLayout(1, 1));
     buttonArea.add(setsavebuts);
     SetNamePanel.add(setNameInfo);
@@ -381,46 +352,35 @@ public class WsJobParameters extends JPanel implements ItemListener,
     gbl.setConstraints(setNameInfo, gbc);
     SetNamePanel.add(buttonArea);
     gbc = new GridBagConstraints();
-    gbc.gridx=0;
-    gbc.gridy=2;
+    gbc.gridx = 0;
+    gbc.gridy = 2;
     gbc.gridheight = 1;
     gbl.setConstraints(buttonArea, gbc);
     setDetails.add(setDescrView, BorderLayout.CENTER);
-    // setDetails.setPreferredSize(new Dimension(360, 100));
-    jobParameters.setBorder(new TitledBorder("Parameters"));
-    jobParameters.setLayout(new BorderLayout());
-    paramPane.setPreferredSize(new Dimension(360, 300));
-    paramPane.getVerticalScrollBar().setUnitIncrement(20);
-    // paramPanel.setPreferredSize(new Dimension(360, 300));
-    // TODO: relayout buttons nicely
-    paramPane.getViewport().setView(paramList);
-    jobParameters.add(paramPane, BorderLayout.CENTER);
-    JPanel jobOptionsPanel = new JPanel();
-    jobOptionsPanel.setLayout(new BorderLayout());
-    jobOptionsPanel.setBorder(new TitledBorder("Options"));
-    jobOptionsPane.getViewport().setView(jobOptions);
-    jobOptionsPanel.add(jobOptionsPane, BorderLayout.CENTER);
 
+    jobParameters.setBorder(new TitledBorder("Parameters"));
+    paramPane.setPreferredSize(new Dimension(360, 400));
+    jobOptions.setBorder(new TitledBorder("Options"));
+    
+    paramList.setBorder(new TitledBorder("Parameters"));
+    
+    JPanel bjo=new JPanel(new BorderLayout()),bjp=new JPanel(new BorderLayout());
+    bjo.add(jobOptions, BorderLayout.CENTER);
+    bjp.add(paramList, BorderLayout.CENTER);
+    optsAndparams.setLayout(new BorderLayout()); 
+
+    optsAndparams.add(bjo, BorderLayout.CENTER);
+    optsAndparams.add(bjp, BorderLayout.SOUTH);
+    paramPane.setViewportView(optsAndparams);
     setLayout(new BorderLayout());
     jobPanel.setLeftComponent(setDetails);
-    if (split)
-    {
-      settingsPanel.setLeftComponent(jobOptionsPanel);
-      settingsPanel.setRightComponent(jobParameters);
-      settingsPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
-      jobPanel.setRightComponent(settingsPanel);
-    }
-    else
-    {
-      jobPanel.setRightComponent(tabpanels);
-      jobOptionsPane.setName("Options");
-      tabpanels.add(jobOptionsPane);
-      paramPane.setName("Parameters");
-      tabpanels.add(paramPane);
-    }
+    jobPanel.setRightComponent(paramPane);
     jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
+
     add(SetNamePanel, BorderLayout.NORTH);
     add(jobPanel, BorderLayout.CENTER);
+
     JPanel dialogpanel = new JPanel();
     dialogpanel.add(startjob);
     dialogpanel.add(canceljob);
@@ -520,9 +480,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
       p = jabap; // (jabap != null) ? paramStore.getPreset(jabap.getName()) :
                  // null;
     }
-    // TODO: Recover window geometry prefs for this service
-    // jobPanel.setDividerLocation(proportionalLocation)
-    // settingsPanel.setDividerLocation(proportionalLocation)
+
     Hashtable exnames = new Hashtable();
     for (int i = 0, iSize = setName.getItemCount(); i < iSize; i++)
     {
@@ -597,7 +555,13 @@ public class WsJobParameters extends JPanel implements ItemListener,
           if (myarg instanceof OptionI)
           {
             OptionI opt = (OptionI) myarg;
-            addOption(opt).resetToDefault();
+            OptionBox ob = addOption(opt);
+            ob.resetToDefault();
+            if (MAX_OPTWIDTH < ob.getPreferredSize().width)
+            {
+              MAX_OPTWIDTH = ob.getPreferredSize().width;
+            }
+
           }
           else
           {
@@ -664,13 +628,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
       }
     }
 
-    jobOptions.setPreferredSize(new Dimension(PARAM_WIDTH, optSet.size()
-            * OPTSET_HEIGHT));
-    FlowLayout fl;
-    jobOptions.setLayout(fl=new FlowLayout(FlowLayout.LEFT));
-    
     refreshParamLayout();
-    paramPane.validate();
     revalidate();
   }
 
@@ -906,13 +864,12 @@ public class WsJobParameters extends JPanel implements ItemListener,
       showDesc.setText("+");
       string.setFont(new Font("Verdana", Font.PLAIN, 11));
       string.setBackground(getBackground());
-      // string.setSize(new Dimension(PARAM_WIDTH, 80));
+
       string.setEditable(false);
       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;
       finfo = parm.getFurtherDetails();
       if (finfo != null)
@@ -939,6 +896,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
         {
           descisvisible = !descisvisible;
           descPanel.setVisible(descisvisible);
+          descPanel.getVerticalScrollBar().setValue(0);
           me.setPreferredSize(new Dimension(PARAM_WIDTH,
                   (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
           me.validate();
@@ -1001,8 +959,6 @@ public class WsJobParameters extends JPanel implements ItemListener,
     {
       adjusting = true;
       boolean init = (choicebox == null && valueField == null);
-      float fVal = 0f;
-      int iVal = 0;
       if (init)
       {
         if (choice)
@@ -1244,7 +1200,17 @@ public class WsJobParameters extends JPanel implements ItemListener,
       // TODO Auto-generated method stub
 
     }
+    // from http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
+    // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
+    @Override
+    public Component.BaselineResizeBehavior getBaselineResizeBehavior() {
+        return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
+    }
 
+    @Override
+    public int getBaseline(int width, int height) {
+        return 0;
+    }
   }
 
   Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
@@ -1277,17 +1243,14 @@ public class WsJobParameters extends JPanel implements ItemListener,
       if (finfo != null)
       {
         hasLink = true;
-        // optlabel.setToolTipText("<html><p>"+opt.getDescription()+"</p><img src=\""+linkImageURL+"\"/></html>");
         enabled.setToolTipText("<html>"
                 + JvSwingUtils.wrapTooltip(opt.getDescription()
                         + "<br><img src=\"" + linkImageURL + "\"/>")
                 + "</html>");
-        // optlabel.addMouseListener(this);
         enabled.addMouseListener(this);
       }
       else
       {
-        // optlabel.setToolTipText(opt.getDescription());
         enabled.setToolTipText("<html>"
                 + JvSwingUtils.wrapTooltip(opt.getDescription())
                 + "</html>");
@@ -1306,7 +1269,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
       }
       // TODO: add actionListeners for popup (to open further info),
       // and to update list of parameters if an option is enabled
-      // that takes a value.
+      // that takes a value. JBPNote: is this TODO still valid ?
       setInitialValue();
     }
 
@@ -1426,7 +1389,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
     {
       cb = new OptionBox(opt);
       optSet.put(opt.getName(), cb);
-      jobOptions.add(cb);
+      jobOptions.add(cb, FlowLayout.LEFT);
     }
     return cb;
   }
@@ -1453,14 +1416,58 @@ public class WsJobParameters extends JPanel implements ItemListener,
 
   protected void refreshParamLayout()
   {
-    FlowLayout fl = new FlowLayout();
-    paramList.setLayout(fl);
-    int s = 2 * fl.getVgap();
-    for (ParamBox pbox : paramSet.values())
+    FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
+    int sep=fl.getVgap();
+    int os=0,s = jobOptions.getBorder().getBorderInsets(jobOptions).bottom+jobOptions.getBorder().getBorderInsets(jobOptions).top+2 * sep;
+    int w = 2 * fl.getHgap() + (MAX_OPTWIDTH > PARAM_WIDTH ? MAX_OPTWIDTH : PARAM_WIDTH);
+    jobOptions.setLayout(fl);
+    if (optSet.size() > 0)
+    {
+      
+      for (OptionBox pbox : optSet.values())
+      {
+         pbox.revalidate();
+        s += sep + pbox.getPreferredSize().height; 
+      }
+      jobOptions.setPreferredSize(new Dimension(w, s));
+      jobOptions.setLayout(new GridLayout(optSet.size(),1));
+      os=s;
+    }
+    else
+    {
+      jobOptions.setVisible(false);
+    }
+
+    // Now layout the parameters assuming they occupy one column - to calculate total height of options+parameters
+    fl = new FlowLayout(FlowLayout.CENTER);
+    // helpful hint from http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
+    fl.setAlignOnBaseline(true);
+    if (paramSet.size() > 0)
     {
-      s += fl.getVgap() + pbox.getBoxHeight(); // getBoxHeight();
+      paramList.setLayout(fl);
+
+      s = 2 * sep;
+      for (ParamBox pbox : paramSet.values())
+      {
+        pbox.validate();
+        s += sep + pbox.getPreferredSize().height+pbox.getBorder().getBorderInsets(pbox).bottom; 
+      }
+      
+      paramList.setPreferredSize(new Dimension(w, s));
+      os+=s+2*sep+paramList.getBorder().getBorderInsets(paramList).bottom+paramList.getBorder().getBorderInsets(paramList).top;
     }
-    paramList.setPreferredSize(new Dimension(PARAM_WIDTH, s));
+    else
+    {
+      paramList.setVisible(false);
+    }
+    // TODO: waste some time trying to eliminate any unnecessary .validate calls here
+    paramList.validate();
+    jobOptions.validate();
+    // System.out.println("Size will be : "+w+","+os);
+    optsAndparams.validate();
+    paramPane.getViewport().validate();
+    paramPane.getVerticalScrollBar().setBlockIncrement(PARAM_CLOSEDHEIGHT*2);
+    paramPane.validate();
     validate();
   }
 
@@ -1474,7 +1481,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
     jalview.ws.jws2.Jws2Discoverer disc = jalview.ws.jws2.Jws2Discoverer
             .getDiscoverer();
     int p = 0;
-    if (args.length > 3)
+    if (args.length > 0)
     {
       Vector<String> services = new Vector<String>();
       services.addElement(args[p++]);
@@ -1621,10 +1628,13 @@ public class WsJobParameters extends JPanel implements ItemListener,
                     new JabaPreset(lastserv, pr));
             JFrame jf = new JFrame("Parameters for "
                     + lastserv.getActionText());
-            JPanel cont = new JPanel();
-            // jf.setPreferredSize(new Dimension(600, 800));
-            cont.add(pgui);
-            jf.add(cont);
+            JPanel cont = new JPanel(new BorderLayout());
+            pgui.validate();
+            cont.setPreferredSize(pgui.getPreferredSize());
+            cont.add(pgui, BorderLayout.CENTER);
+            jf.setLayout(new BorderLayout());
+            jf.add(cont, BorderLayout.CENTER);
+            jf.validate();
             final Thread thr = Thread.currentThread();
             jf.addWindowListener(new WindowListener()
             {
@@ -1826,6 +1836,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
     initArgSetModified();
     syncSetNamesWithStore();
     setName.setSelectedItem(lastParmSet);
+    SetNamePanel.validate();
     validate();
     settingDialog = false;