JAL-591 adapt option layout to width of dialog box
authorjprocter <jprocter@compbio.dundee.ac.uk>
Mon, 3 Sep 2012 20:30:40 +0000 (21:30 +0100)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Mon, 3 Sep 2012 20:30:40 +0000 (21:30 +0100)
src/jalview/gui/WsJobParameters.java

index 689534c..b936efb 100644 (file)
@@ -27,12 +27,10 @@ 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.Component;
-import java.awt.Container;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
 import java.awt.Font;
@@ -42,50 +40,40 @@ import java.awt.GridLayout;
 import java.awt.Rectangle;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.HierarchyBoundsListener;
+import java.awt.event.HierarchyEvent;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
-import java.awt.event.MouseEvent;
-import java.awt.event.MouseListener;
 import java.awt.event.WindowEvent;
 import java.awt.event.WindowListener;
 import java.net.URL;
-import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 import java.util.Vector;
 
 import javax.swing.JButton;
-import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
-import javax.swing.JComponent;
 import javax.swing.JDialog;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
-import javax.swing.JMenuItem;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
-import javax.swing.JPopupMenu;
 import javax.swing.JScrollPane;
-import javax.swing.JSlider;
 import javax.swing.JSplitPane;
-import javax.swing.JTabbedPane;
 import javax.swing.JTextArea;
-import javax.swing.JTextField;
 import javax.swing.border.TitledBorder;
-import javax.swing.event.ChangeEvent;
-import javax.swing.event.ChangeListener;
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentListener;
 
+import net.miginfocom.swing.MigLayout;
+
 import compbio.metadata.Argument;
 import compbio.metadata.Option;
 import compbio.metadata.Parameter;
 import compbio.metadata.Preset;
 import compbio.metadata.PresetManager;
 import compbio.metadata.RunnerConfig;
-import compbio.metadata.ValueConstrain.Type;
 
 /**
  * job parameter editing/browsing dialog box. User can browse existing settings
@@ -134,13 +122,11 @@ public class WsJobParameters extends JPanel implements ItemListener,
 
   JSplitPane settingsPanel = new JSplitPane();
 
-  JSplitPane jobPanel = new JSplitPane();
+  JPanel jobPanel = new JPanel();
 
 
   JScrollPane jobOptionsPane = new JScrollPane();
 
-  JPanel jobParameters = new JPanel();
-
   JButton createpref = new JButton();
 
   JButton deletepref = new JButton();
@@ -160,8 +146,8 @@ public class WsJobParameters extends JPanel implements ItemListener,
   JScrollPane paramPane = new JScrollPane();
 
 
+//  ScrollablePanel optsAndparams = new ScrollablePanel();
   JPanel optsAndparams = new JPanel();
-
   RunnerConfig serviceOptions;
 
   ParamDatastoreI paramStore;
@@ -245,7 +231,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
     {
         public void run()
         {
-          jobPanel.setDividerLocation(0.25); 
+          //jobPanel.setDividerLocation(0.25); 
 
         }
       });
@@ -260,6 +246,22 @@ public class WsJobParameters extends JPanel implements ItemListener,
 
   private void jbInit()
   {
+    this.addHierarchyBoundsListener(new HierarchyBoundsListener()
+    {
+      
+      @Override
+      public void ancestorResized(HierarchyEvent arg0)
+      {
+        refreshParamLayout();
+      }
+      
+      @Override
+      public void ancestorMoved(HierarchyEvent arg0)
+      {
+        // TODO Auto-generated method stub
+        
+      }
+    });
     updatepref = JvSwingUtils.makeButton("Update",
             "Update this existing user parameter set.",
             new ActionListener()
@@ -373,25 +375,33 @@ public class WsJobParameters extends JPanel implements ItemListener,
     gbl.setConstraints(buttonArea, gbc);
     setDetails.add(setDescrView, BorderLayout.CENTER);
 
-    jobParameters.setBorder(new TitledBorder("Parameters"));
-    paramPane.setPreferredSize(new Dimension(360, 400));
+    // paramPane.setPreferredSize(new Dimension(360, 400));
+    // paramPane.setPreferredSize(null);
     jobOptions.setBorder(new TitledBorder("Options"));
-    
+    jobOptions.setOpaque(true);
     paramList.setBorder(new TitledBorder("Parameters"));
-    
+    paramList.setOpaque(true);
     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);
+    bjp.setOpaque(true);
+    bjo.setOpaque(true);
+    //optsAndparams.setScrollableWidth(ScrollableSizeHint.FIT);
+    // optsAndparams.setScrollableHeight(ScrollableSizeHint.NONE);
+    // optsAndparams.setLayout(new BorderLayout()); 
+    optsAndparams.setLayout(new BorderLayout());
+    optsAndparams.add(jobOptions, BorderLayout.NORTH);
+    optsAndparams.add(paramList, BorderLayout.CENTER);
+    JPanel jp=new JPanel(new BorderLayout());
+    jp.add(optsAndparams, BorderLayout.CENTER);
+    paramPane.getViewport().setView(jp);
+    paramPane.setBorder(null);
     setLayout(new BorderLayout());
-    jobPanel.setLeftComponent(setDetails);
-    jobPanel.setRightComponent(paramPane);
-    jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
+    jobPanel.setPreferredSize(null);
+    jobPanel.setLayout(new BorderLayout());
+    jobPanel.add(setDetails, BorderLayout.NORTH);
+    jobPanel.add(paramPane, BorderLayout.CENTER);
+//    jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
 
     add(SetNamePanel, BorderLayout.NORTH);
     add(jobPanel, BorderLayout.CENTER);
@@ -400,6 +410,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
     dialogpanel.add(startjob);
     dialogpanel.add(canceljob);
     add(dialogpanel, BorderLayout.SOUTH);
+    validate();
   }
 
   protected void revert_actionPerformed(ActionEvent e)
@@ -557,13 +568,14 @@ public class WsJobParameters extends JPanel implements ItemListener,
 
     // split to params and required arguments
     {
+      int cw=0;
       for (ArgumentI myarg : args)
       {
         // Ideally, Argument would implement isRequired !
         if (myarg instanceof ParameterI)
         {
           ParameterI parm = (ParameterI) myarg;
-          paramList.add(opanp.addParameter(parm));
+          opanp.addParameter(parm).validate();
         }
         else
         {
@@ -571,13 +583,13 @@ public class WsJobParameters extends JPanel implements ItemListener,
           {
             OptionI opt = (OptionI) myarg;
             OptionBox ob = opanp.addOption(opt);
-            jobOptions.add(ob, FlowLayout.LEFT);
             ob.resetToDefault();
             if (MAX_OPTWIDTH < ob.getPreferredSize().width)
             {
               MAX_OPTWIDTH = ob.getPreferredSize().width;
             }
-
+            ob.validate();
+            cw+=ob.getPreferredSize().width+5;
           }
           else
           {
@@ -787,22 +799,46 @@ public class WsJobParameters extends JPanel implements ItemListener,
 
   public void refreshParamLayout()
   {
+//    optsAndparams.setPreferredSize(null);
     FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
     int sep=fl.getVgap();
+    boolean fh=true;
     int os=0,s = jobOptions.getBorder().getBorderInsets(jobOptions).bottom+jobOptions.getBorder().getBorderInsets(jobOptions).top+2 * sep;
+    /**
+     * final height for viewport
+     */
+    int finalh=s;
+    int panewidth = paramPane.getViewport().getSize().width-120-jobOptions.getBorder().getBorderInsets(jobOptions).left+jobOptions.getBorder().getBorderInsets(jobOptions).right;
+    
     int w = 2 * fl.getHgap() + (MAX_OPTWIDTH > OptsAndParamsPage.PARAM_WIDTH ? MAX_OPTWIDTH : OptsAndParamsPage.PARAM_WIDTH);
-    jobOptions.setLayout(fl);
+    int hgap=fl.getHgap(),cw=hgap;
+    
     if (opanp.getOptSet().size() > 0)
     {
       
+      jobOptions.setLayout(new MigLayout("","", ""));
+      jobOptions.removeAll();
+      
       for (OptionBox pbox : opanp.getOptSet().values())
       {
-         pbox.revalidate();
-        s += sep + pbox.getPreferredSize().height; 
+        pbox.validate();
+        cw+=pbox.getSize().width+hgap;
+      if (cw+120>panewidth)
+      {
+        jobOptions.add(pbox, "wrap");
+        // System.out.println("Wrap on "+pbox.option.getName());
+        cw=hgap+pbox.getSize().width;
+        fh=true;
+      } else {
+        jobOptions.add(pbox);
+      }
+      if (fh)
+      {
+        finalh+=pbox.getSize().height+fl.getVgap();
+        fh=false;
       }
-      jobOptions.setPreferredSize(new Dimension(w, s));
-      jobOptions.setLayout(new GridLayout(opanp.getOptSet().size(),1));
-      os=s;
+      }      
+    jobOptions.revalidate();
     }
     else
     {
@@ -810,36 +846,55 @@ public class WsJobParameters extends JPanel implements ItemListener,
     }
 
     // Now layout the parameters assuming they occupy one column - to calculate total height of options+parameters
-    fl = new FlowLayout(FlowLayout.CENTER);
+    fl = new FlowLayout(FlowLayout.LEFT);
     // helpful hint from http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
     fl.setAlignOnBaseline(true);
     if (opanp.getParamSet().size() > 0)
     {
-      paramList.setLayout(fl);
+      paramList.removeAll();
+      paramList.setLayout(new MigLayout("","",""));
+      fh=true;
+      for (ParamBox pbox:opanp.getParamSet().values())
+      {
+        pbox.validate();
+        cw+=pbox.getSize().width+hgap;
+        if (cw+160>panewidth) {
+          paramList.add(pbox, "wrap");
+          cw=pbox.getSize().width+hgap;
+          fh=true;
+        } else {
+          paramList.add(pbox);
+        }
+        if (fh)
+        {
+          finalh+=pbox.getSize().height+fl.getVgap();
+          fh=false;
+        }
 
-      s = 2 * sep;
+      }
+/*      s = 2 * sep;
       for (ParamBox pbox : opanp.getParamSet().values())
       {
         pbox.validate();
         s += sep + pbox.getPreferredSize().height+pbox.getBorder().getBorderInsets(pbox).bottom; 
       }
       
-      paramList.setPreferredSize(new Dimension(w, s));
+//      paramList.setPreferredSize(new Dimension(w, s));
       os+=s+2*sep+paramList.getBorder().getBorderInsets(paramList).bottom+paramList.getBorder().getBorderInsets(paramList).top;
+      */
+      paramList.revalidate();
     }
     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(OptsAndParamsPage.PARAM_CLOSEDHEIGHT*2);
-    paramPane.validate();
-    validate();
+    // optsAndparams.setPreferredSize(null);
+    // paramPane.getViewport().setView(optsAndparams);
+    paramPane.getViewport().setAutoscrolls(true);
+    paramPane.revalidate();
+    revalidate();
   }
 
   /**