Commit 3f7a67de authored by Andrey Filippov's avatar Andrey Filippov
Browse files

Implemented/tested zoom correction and simple (static) tabbed

replacement for IJ GenericDialog
parent ce517a59
Loading
Loading
Loading
Loading
+12 −58
Original line number Original line Diff line number Diff line
@@ -622,16 +622,7 @@ public class AlignmentCorrection {
			int tileY = s.tile / tilesX;
			int tileY = s.tile / tilesX;
			double centerX = tileX * qc.tp.getTileSize() + qc.tp.getTileSize()/2;// - shiftX;
			double centerX = tileX * qc.tp.getTileSize() + qc.tp.getTileSize()/2;// - shiftX;
			double centerY = tileY * qc.tp.getTileSize() + qc.tp.getTileSize()/2;//- shiftY;
			double centerY = tileY * qc.tp.getTileSize() + qc.tp.getTileSize()/2;//- shiftY;
/*

			double [][] centersXY_disp = qc.geometryCorrection.getPortsCoordinates(
					centerX,
					centerY,
					disp_strength[2 * s.series + 0][s.tile]/magic_coeff); // disparity
			double [][] centersXY_inf = qc.geometryCorrection.getPortsCoordinates(
					centerX,
					centerY,
					0.0); // disparity
*/
			double [][] centersXY_disp = qc.geometryCorrection.getPortsCoordinatesAndDerivatives(
			double [][] centersXY_disp = qc.geometryCorrection.getPortsCoordinatesAndDerivatives(
					corr_rots, // Matrix []   rots,
					corr_rots, // Matrix []   rots,
					null,      //  Matrix [][] deriv_rots,
					null,      //  Matrix [][] deriv_rots,
@@ -2094,6 +2085,7 @@ B = |+dy0 -dy1 -2*dy3 |
					clt_parameters.ly_inf_en,    // boolean use_disparity,     // if true will ignore disparity data even if available (was false)
					clt_parameters.ly_inf_en,    // boolean use_disparity,     // if true will ignore disparity data even if available (was false)
					clt_parameters.ly_inf_force, // boolean force_convergence, // if true try to adjust convergence (disparity, symmetrical parameter 0) even with no disparity
					clt_parameters.ly_inf_force, // boolean force_convergence, // if true try to adjust convergence (disparity, symmetrical parameter 0) even with no disparity
					clt_parameters.ly_com_roll,  // boolean    common_roll,    // Enable common roll (valid for high disparity range only)
					clt_parameters.ly_com_roll,  // boolean    common_roll,    // Enable common roll (valid for high disparity range only)
					clt_parameters.ly_focalLength,// boolean    corr_focalLength,     // Correct scales (focal length temperature? variations)


					mismatch_list,                          // ArrayList<Mismatch> mismatch_list,
					mismatch_list,                          // ArrayList<Mismatch> mismatch_list,
					qc.geometryCorrection,                  // GeometryCorrection geometryCorrection,
					qc.geometryCorrection,                  // GeometryCorrection geometryCorrection,
@@ -2250,15 +2242,6 @@ B = |+dy0 -dy1 -2*dy3 |
			double [] pXY = mm.getPXY();
			double [] pXY = mm.getPXY();
			double [][] deriv = new double [2 * NUM_SENSORS][];
			double [][] deriv = new double [2 * NUM_SENSORS][];
			int dbg_index =dbg_index (pXY, dbg_decimate);
			int dbg_index =dbg_index (pXY, dbg_decimate);
//			double [][] f =
/*
			geometryCorrection.getPortsCoordinatesAndDerivatives_old(
					corr_vector, // CorrVector corr_vector,
					deriv,   // 	boolean calc_deriv,
					pXY[0],      // double px,
					pXY[1],      // double py,
					mm.getDisparityMeas()); // getDisparityTask()); // double disparity)
*/
			geometryCorrection.getPortsCoordinatesAndDerivatives(
			geometryCorrection.getPortsCoordinatesAndDerivatives(
					corr_rots,   //  Matrix []   rots,
					corr_rots,   //  Matrix []   rots,
					deriv_rots,  //  Matrix [][] deriv_rots,
					deriv_rots,  //  Matrix [][] deriv_rots,
@@ -2421,14 +2404,6 @@ B = |+dy0 -dy1 -2*dy3 |
		for (int indx = 0; indx<mismatch_list.size(); indx++){ // need indx value
		for (int indx = 0; indx<mismatch_list.size(); indx++){ // need indx value
			Mismatch mm = mismatch_list.get(indx);
			Mismatch mm = mismatch_list.get(indx);
			double [] pXY = mm.getPXY();
			double [] pXY = mm.getPXY();
			/*
			double [][] f = geometryCorrection.getPortsCoordinatesAndDerivatives_old( // 4x2
					corr_vector, // CorrVector corr_vector,
					null,        //	boolean calc_deriv,
					pXY[0],      // double px,
					pXY[1],      // double py,
					mm.getDisparityMeas()); // getDisparityTask()); // double disparity)
*/
			double [][] f = geometryCorrection.getPortsCoordinatesAndDerivatives( // 4x2
			double [][] f = geometryCorrection.getPortsCoordinatesAndDerivatives( // 4x2
					corr_rots,    //  Matrix []   rots,
					corr_rots,    //  Matrix []   rots,
					null, //  Matrix [][] deriv_rots,
					null, //  Matrix [][] deriv_rots,
@@ -2593,6 +2568,8 @@ B = |+dy0 -dy1 -2*dy3 |
			boolean force_convergence, // if true try to adjust convergence (disparity, symmetrical parameter 0) even with no disparity
			boolean force_convergence, // if true try to adjust convergence (disparity, symmetrical parameter 0) even with no disparity
			                           // data, using just radial distortions
			                           // data, using just radial distortions
	  		boolean    common_roll,    // Enable common roll (valid for high disparity range only)
	  		boolean    common_roll,    // Enable common roll (valid for high disparity range only)
			boolean    corr_focalLength,     // Correct scales (focal length temperature? variations)



			ArrayList<Mismatch> mismatch_list,
			ArrayList<Mismatch> mismatch_list,
			GeometryCorrection geometryCorrection,
			GeometryCorrection geometryCorrection,
@@ -2608,35 +2585,21 @@ B = |+dy0 -dy1 -2*dy3 |
				break;
				break;
			}
			}
		}
		}
//		boolean [] par_mask = new boolean[10];

//		for (int i = (has_disparity ? 0 : 1); i < par_mask.length; i++){
//			par_mask[i] = true;
//		}
		boolean [] par_mask = geometryCorrection.getParMask(
		boolean [] par_mask = geometryCorrection.getParMask(
				has_disparity, // boolean use_disparity,
				has_disparity, // boolean use_disparity,
				common_roll); // boolean common_roll);
				common_roll,// boolean common_roll,
/*			{ // TODO: move to GeometryCorrection
				corr_focalLength); // boolean corr_focalLength);
				has_disparity, //sym0
				true,          //sym1
				true,          //sym2
				true,          //sym3
				true,          //sym4
				true,          //sym5
				common_roll,   //sym6 // common roll
				true,          //sym7
				true,          //sym8
				true           //sym9
		};
		*/


		double [][] jta = getJacobianTransposed(
		double [][] jta = getJacobianTransposed(
				par_mask,           // boolean [] par_mask,
				par_mask,           // boolean [] par_mask,
				mismatch_list,      // ArrayList<Mismatch> mismatch_list,
				mismatch_list,      // ArrayList<Mismatch> mismatch_list,
				geometryCorrection, // GeometryCorrection geometryCorrection,
				geometryCorrection, // GeometryCorrection geometryCorrection,
				corr_vector,        // GeometryCorrection.CorrVector corr_vector)
				corr_vector,        // GeometryCorrection.CorrVector corr_vector)
//				debugLevel);		// int debugLevel)
				debugLevel);		// int debugLevel)
				debugLevel);		// int debugLevel)


//		debugLevel = 2;

		// convert Jacobian outputs to symmetrical measurement vectors (last one is non-zero only if disparity should be adjusted)
		// convert Jacobian outputs to symmetrical measurement vectors (last one is non-zero only if disparity should be adjusted)




@@ -2653,7 +2616,7 @@ B = |+dy0 -dy1 -2*dy3 |
				mismatch_list); // ArrayList<Mismatch> mismatch_list)
				mismatch_list); // ArrayList<Mismatch> mismatch_list)
		double [] y_minus_fx_a_weighted = mulWeight(y_minus_fx_a, weights);
		double [] y_minus_fx_a_weighted = mulWeight(y_minus_fx_a, weights);
		double rms0 = getRMS	(y_minus_fx_a, weights);
		double rms0 = getRMS	(y_minus_fx_a, weights);
		if (debugLevel > -1){
		if (debugLevel > -2){
			System.out.println("--- solveCorr(): initial RMS = " + rms0);
			System.out.println("--- solveCorr(): initial RMS = " + rms0);
		}
		}


@@ -2661,14 +2624,8 @@ B = |+dy0 -dy1 -2*dy3 |
			old_new_rms[0] =rms0;
			old_new_rms[0] =rms0;
		}
		}
		Matrix y_minus_fx_weighted = new Matrix(y_minus_fx_a_weighted, y_minus_fx_a_weighted.length);
		Matrix y_minus_fx_weighted = new Matrix(y_minus_fx_a_weighted, y_minus_fx_a_weighted.length);
//		double [][] jtja = getJTJ(jta, weights);
		double [][] jtja = getJTJ(jta_mv, weights);
		double [][] jtja = getJTJ(jta_mv, weights);
		Matrix jtj = new Matrix(jtja); // getJTJ(jta, weights)); // less operations than jt.times(jt.transpose());
		Matrix jtj = new Matrix(jtja); // getJTJ(jta, weights)); // less operations than jt.times(jt.transpose());
//		double [] jt_trace_null_dbg = getJtJTrace(jta,null);
//		double [] jt_trace_dbg =      getJtJTrace(jta,weights);
//		double [] jt_mv_trace_null_dbg = getJtJTrace(jta_mv,null);
//		double [] jt_mv_trace_dbg =      getJtJTrace(jta_mv,weights);
//
		boolean dbg_images = debugLevel>1;
		boolean dbg_images = debugLevel>1;
		int dbg_decimate = 64; // just for the debug image
		int dbg_decimate = 64; // just for the debug image
		int dbg_width =  qc.tp.getTilesX()*qc.tp.getTileSize();
		int dbg_width =  qc.tp.getTilesX()*qc.tp.getTileSize();
@@ -2676,8 +2633,7 @@ B = |+dy0 -dy1 -2*dy3 |
		int dbg_owidth = dbg_width/dbg_decimate;
		int dbg_owidth = dbg_width/dbg_decimate;
		int dbg_oheight = dbg_height/dbg_decimate;
		int dbg_oheight = dbg_height/dbg_decimate;
		int dbg_length = dbg_owidth*dbg_oheight;
		int dbg_length = dbg_owidth*dbg_oheight;
//		String [] dbg_titles_tar=GeometryCorrection.CORR_NAMES;
		String [] dbg_titles_sym= {"sym0","sym1","sym2","sym3","sym4","sym5","sroll0","sroll1","sroll2","sroll3", "zoom0", "zoom1", "zoom2"};
		String [] dbg_titles_sym= {"sym0","sym1","sym2","sym3","sym4","sym5","sroll0","sroll1","sroll2","sroll3"};
		String [] dbg_titles_xy=  {"x0","y0","x1","y1","x2","y2","x3","y3"};
		String [] dbg_titles_xy=  {"x0","y0","x1","y1","x2","y2","x3","y3"};
		String [] dbg_titles_mv=  {"dy0","dy1","dx2","dx3","dx1-dx0","dy3-dy2","dh-dv","dhy+dv"};
		String [] dbg_titles_mv=  {"dy0","dy1","dx2","dx3","dx1-dx0","dy3-dy2","dh-dv","dhy+dv"};
		double [][] dbg_xy = null;  // jacobian dmv/dsym
		double [][] dbg_xy = null;  // jacobian dmv/dsym
@@ -2699,8 +2655,6 @@ B = |+dy0 -dy1 -2*dy3 |
			dbg_dmv_dsym =        doubleNaN(dbg_titles_mv.length * dbg_titles_sym.length,    dbg_length); // jacobian dmv/dsym
			dbg_dmv_dsym =        doubleNaN(dbg_titles_mv.length * dbg_titles_sym.length,    dbg_length); // jacobian dmv/dsym
			dbg_dmv_dsym_delta =  doubleNaN(dbg_titles_mv.length * dbg_titles_sym.length,    dbg_length); // jacobian dmv/dsym
			dbg_dmv_dsym_delta =  doubleNaN(dbg_titles_mv.length * dbg_titles_sym.length,    dbg_length); // jacobian dmv/dsym
			dbg_dmv_dsym_diff =   doubleNaN(dbg_titles_mv.length * dbg_titles_sym.length,    dbg_length); // jacobian dmv/dsym
			dbg_dmv_dsym_diff =   doubleNaN(dbg_titles_mv.length * dbg_titles_sym.length,    dbg_length); // jacobian dmv/dsym
			// dbg_xy =        new double [dbg_titles_xy.length]                           [dbg_length]; // jacobian dmv/dsym
			// dbg_mv =        new double [dbg_titles_mv.length]                           [dbg_length]; // jacobian dmv/dsym


			String [] dbg_dmv_dsym_titles = new String [dbg_titles_mv.length * dbg_titles_sym.length];
			String [] dbg_dmv_dsym_titles = new String [dbg_titles_mv.length * dbg_titles_sym.length];
			for (int i = 0; i < dbg_titles_mv.length; i++){
			for (int i = 0; i < dbg_titles_mv.length; i++){
@@ -2764,7 +2718,7 @@ B = |+dy0 -dy1 -2*dy3 |
			drslt[i] *= -1.0;
			drslt[i] *= -1.0;
		}
		}
		GeometryCorrection.CorrVector rslt = geometryCorrection.getCorrVector(drslt, par_mask);
		GeometryCorrection.CorrVector rslt = geometryCorrection.getCorrVector(drslt, par_mask);
		if (debugLevel > -1){
		if (debugLevel > -2){
			System.out.println("solveCorr() rslt:");
			System.out.println("solveCorr() rslt:");
			System.out.println(rslt.toString());
			System.out.println(rslt.toString());
		}
		}
+1504 −0

File changed.

Preview size limit exceeded, changes collapsed.

+10 −7
Original line number Original line Diff line number Diff line
@@ -568,10 +568,13 @@ private Panel panel1,
		if (DCT_MODE) {
		if (DCT_MODE) {
			panelClt3 = new Panel();
			panelClt3 = new Panel();
			panelClt3.setLayout(new GridLayout(1, 0, 5, 5)); // rows, columns, vgap, hgap
			panelClt3.setLayout(new GridLayout(1, 0, 5, 5)); // rows, columns, vgap, hgap
			addButton("Setup CLT parameters",       panelClt3, color_configure);
			addButton("Setup CLT",                  panelClt3, color_configure);
			addButton("Infinity offset",            panelClt3, color_configure);
			addButton("Infinity offset",            panelClt3, color_configure);
			addButton("Setup CLT Batch parameters", panelClt3, color_configure);
			addButton("Setup CLT Batch parameters", panelClt3, color_configure);
			addButton("CLT batch process",          panelClt3, color_process);
			addButton("CLT batch process",          panelClt3, color_process);
//			addButton("JTabbed",                    panelClt3, color_stop);
//			addButton("Demo",                       panelClt3, color_process);

			add(panelClt3);
			add(panelClt3);
		}
		}
		pack();
		pack();
@@ -1148,7 +1151,7 @@ private Panel panel1,
/* ======================================================================== */
/* ======================================================================== */
    } else if (label.equals("Configure correction")) {
    } else if (label.equals("Configure correction")) {
        DEBUG_LEVEL=MASTER_DEBUG_LEVEL;
        DEBUG_LEVEL=MASTER_DEBUG_LEVEL;
    	CORRECTION_PARAMETERS.showDialog("Correction parameters");
    	CORRECTION_PARAMETERS.showJDialog("Correction parameters");
    	return;
    	return;




@@ -3751,6 +3754,10 @@ private Panel panel1,
    } else if (label.equals("Setup CLT parameters")) {
    } else if (label.equals("Setup CLT parameters")) {
    	CLT_PARAMETERS.showDialog();
    	CLT_PARAMETERS.showDialog();
        return;
        return;
/* ======================================================================== */
    } else if (label.equals("Setup CLT")) {
    	CLT_PARAMETERS.showJDialog();
        return;
/* ======================================================================== */
/* ======================================================================== */
    } else if (label.equals("Infinity offset")) {
    } else if (label.equals("Infinity offset")) {
    	while (true) {
    	while (true) {
@@ -5155,11 +5162,7 @@ private Panel panel1,
        }
        }
        return;
        return;



//JTabbedTest



//
// End of buttons code
// End of buttons code
    }
    }
    DEBUG_LEVEL=MASTER_DEBUG_LEVEL;
    DEBUG_LEVEL=MASTER_DEBUG_LEVEL;
+419 −0
Original line number Original line Diff line number Diff line
/*
 * TabbedPaneDemo.java requires one additional file:
 *   images/middle.gif.
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.Border;

import ij.IJ;

public class GenericJTabbedDialog implements ActionListener {
	static final int   DIVIDE_COLUMNS_BY    = 2; // JTextField.setColumns() seem to make it twice wider?
	static final int   DEFAULT_STRING_WIDTH = 8;
    static final Color COMMENT_COLOR = new Color(0, 150, 0);
	static final int   COMMENT_PADY = 20; // make comment/message line wider

	//0: label:'LabelUI', input:'TextFieldUI', inp_units:'PanelUI'
	ArrayList<ArrayList<JComponent>> components = new ArrayList<ArrayList<JComponent>>(); // null component means a message, not am input
	ArrayList<ArrayList<JLabel>> labels = new ArrayList<ArrayList<JLabel>>();
	ArrayList<JComponent> tabs = new ArrayList<JComponent>();
	ArrayList<JButton> buttons = new ArrayList<JButton>();
	ArrayList<JScrollPane> scrollPanes = new ArrayList<JScrollPane>();
	private JFrame frame = new JFrame();
	public int width, height;
	private int read_tab=0,read_component=0; // current number of tab/component to be read next
	String result=null;
	private JDialog jd;
	public GenericJTabbedDialog(String title) {
		this(title, 600, 800);
	}
	public GenericJTabbedDialog(String title, int width, int height) {
//		final JFrame frame= new JFrame();
		jd = new JDialog(frame , title, true);
		components.add(new ArrayList<JComponent>());
		labels.add(new ArrayList<JLabel>());
		tabs.add (new JPanel(false)); // first (yet nameless tab)
		this.width = width;
		this.height = height;
	}

	public void addTab(String tab_name) {
		addTab(tab_name, "");

	}
	public void addTab(String tab_name, String tab_tooltip) {
		// See if there are any components for the current tab. If none (such as when none are yet added, just rename existing)
		// If there are some components already - start a new tab
		int ntab = tabs.size()-1;
		if (!components.get(ntab).isEmpty()) {
			components.add(new ArrayList<JComponent>());
			labels.add(new ArrayList<JLabel>());
			tabs.add (new JPanel(false)); // starting next tab
			ntab++;
		}
		tabs.get(ntab).putClientProperty("title",  tab_name);    // to be later used for tab name
		tabs.get(ntab).putClientProperty("tooltip",tab_tooltip); // to be later used for tab tooltip
	}

	public void addButtons(
			String [] labels,
			String [] actions,
			String [] tooltips) {

		for (int i = 0; i < labels.length; i++) {
			JButton button = new JButton(labels[i]);
			if ((actions != null) && (actions[i] != null)) button.setActionCommand(actions[i]);
			if ((tooltips != null) && (tooltips[i] != null)) button.setToolTipText(tooltips[i]);
			buttons.add(button);
		}
	}

	public void addDefaultButtons() {
		String [] labels = {"OK", "Cancel"};
		addButtons(labels, labels, labels);

	}

	// Common part of adding messages/input fields
	private void addLine(String prompt,
			             JComponent component, // may be null
			             String tooltip) {
		JLabel label;
   		if (prompt.indexOf('_') == -1) {
   			label = new JLabel(prompt);
   		} else {
   			label = new JLabel(prompt.replace('_', ' '));

   		}
		if (tooltip != null) {
			label.setToolTipText(tooltip);
			if (component != null) component.setToolTipText(tooltip);
		}
		labels.get(labels.size()-1).add(label);
		components.get(components.size()-1).add(component);
	}

	public void addMessage(String message) {
		addMessage(message, null);
	}
	public void addMessage(String message, String tooltip) {
		addLine(message, null, tooltip);
	}

	public void addStringField (String label, String value) {
		addStringField(label, value, DEFAULT_STRING_WIDTH, null);
	}


	public void addStringField (String label, String value,	String tooltip) {
		addStringField(label, value, DEFAULT_STRING_WIDTH, tooltip);
	}

	public void addStringField (String label, String value,	int width) {
		addStringField(label, value, width, null);
	}

	public void addStringField ( // no units here
			String label,
			String value,
			int    width,
			String tooltip) {
		JTextField inp = new JTextField();
		inp.setText(value);
		inp.setColumns(width/DIVIDE_COLUMNS_BY + 2); ///2);
		JPanel inp_units = new JPanel(false);
		inp_units.add(inp);
		inp_units.putClientProperty("type",  "String");
		inp_units.setLayout(new FlowLayout(FlowLayout.LEFT));
		addLine(label, inp_units, tooltip);
	}

	public void addNumericField(String label, double defaultValue, int digits) { // as in IJ
		addNumericField(label, defaultValue, digits, 6, null, null);
	}

	public void addNumericField(String label, double defaultValue, int digits, int columns, String units) { // as in IJ
		addNumericField(label, defaultValue, digits, columns, units, null);
	}

	public void addNumericField(String label, double defaultValue, int digits, int columns, String units, String tooltip) {
		String svalue = IJ.d2s(defaultValue, digits); // , columns
		JTextField inp = new JTextField();
		inp.setText(svalue);
		inp.setColumns(columns/DIVIDE_COLUMNS_BY + 2);
		JPanel inp_units = new JPanel(false);
		inp_units.add(inp);
		if ((units != null) && !units.isEmpty()) {
			JLabel junits = new JLabel(units);
			Font fnt = junits.getFont();
			junits.setFont(fnt.deriveFont(fnt.getStyle() | Font.ITALIC));
			inp_units.add(junits);
		}
		inp_units.putClientProperty("type",  "double");
		inp_units.setLayout(new FlowLayout(FlowLayout.LEFT));
		addLine(label, inp_units, tooltip);
//		Component[] comps = inp_units.getComponents();
	}

	public void addCheckbox(String label, boolean defaultValue) {
		addCheckbox(label, defaultValue, null);
	}

    public void addCheckbox(String label, boolean defaultValue, String tooltip) { // no support for preview functionality
    	JCheckBox checkbox = new JCheckBox(null, null, defaultValue); // text, icon, selected
		checkbox.putClientProperty("type",  "boolean");
    	addLine(label, checkbox, tooltip);
    }
 // Add choice too.
    public void addChoice(String label, String[] items, String defaultItem) {
    	addChoice(label, items, defaultItem, null);
    }
    public void addChoice(String label, String[] items, String defaultItem, String tooltip) {
    	int index = 0;
    	if (defaultItem != null) {
    		for (int i = 0; i < items.length; i++) if (items[i].equals(defaultItem)) {
    			index = i;
    			break;
    		}
    	}
    	JComboBox<String> combo = new JComboBox<String>(items);
    	combo.setSelectedIndex(index);

		JPanel inp_units = new JPanel(false);
		inp_units.add(combo);

		inp_units.putClientProperty("type",  "combo");
//		combo.setPreferredSize(new Dimension(200));
//		combo.setSize(200, combo.getPreferredSize().height);
//		combo.setMaximumSize(20); //  combo.getPreferredSize() );
		inp_units.setLayout(new FlowLayout(FlowLayout.LEFT));

    	addLine(label, inp_units, tooltip);
    }


    public void buildDialog() { // non-blocking, does not show

    	if (buttons.isEmpty()) {
    		addDefaultButtons();
    	}

		jd.setLayout(new BorderLayout()); //  new GridLayout(2, 2) );
		// prepare each panel first, then optionally make a tabbed layout
		for (int ntab = 0; ntab < tabs.size(); ntab++) {
			JComponent            tab = tabs.get(ntab);
			ArrayList<JComponent> tabComponents = components.get(ntab);
			ArrayList<JLabel>     tabLabels = labels.get(ntab);
	        tab.setLayout(new GridBagLayout());
	        GridBagConstraints gbc = new GridBagConstraints();
	        gbc.fill = GridBagConstraints.HORIZONTAL;
	    	gbc.weightx = 0.5;
	    	gbc.weighty = 0.0;
			for (int ncomp = 0; ncomp < tabLabels.size(); ncomp++) {
				JLabel label =       tabLabels.get(ncomp);
				JComponent component = tabComponents.get(ncomp);
	        	gbc.gridx = 0;
	        	gbc.gridy = ncomp;
	        	gbc.gridwidth = 1;
	        	gbc.ipady = 0;
				if (component == null) { // message/comment line
					label.setHorizontalAlignment(JLabel.CENTER);
					label.setForeground(COMMENT_COLOR);
	        		Font fnt = label.getFont();
	        		label.setFont(fnt.deriveFont(fnt.getStyle() | Font.ITALIC));
	        		gbc.gridwidth = 2;
	            	gbc.ipady = COMMENT_PADY;
	            	tab.add(label,gbc);
				} else {
					label.setHorizontalAlignment(JLabel.RIGHT);
	            	tab.add(label,gbc);
	        		gbc.gridx = 1;
	        		tab.add(component,gbc);
				}
			}
			// Add empty label that would push up all other rows
	        JLabel label_push = new JLabel("");
	    	gbc.weightx = 0.5;
	    	gbc.weighty = 0.5;
	    	gbc.gridx = 0;
	    	gbc.gridy = tabLabels.size();
	    	tab.add(label_push,gbc);

	    	// Wrap each panel with scroll panel
	        JScrollPane scrollPane = new JScrollPane(tab);
	        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); // HORIZONTAL_SCROLLBAR_NEVER);
	        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
	    	scrollPanes.add(scrollPane);
		}
        Border padding = BorderFactory.createEmptyBorder(20,20,5,20);
		if (tabs.size() == 1) {
	        jd.add(new JScrollPane(scrollPanes.get(0)), BorderLayout.CENTER); // no tabs
		} else { // tabbed
			JTabbedPane tabbedPane = new JTabbedPane();
			for (int ntab = 0; ntab < tabs.size(); ntab++) {
				JScrollPane scrollPane = scrollPanes.get(ntab);
				JComponent tab = tabs.get(ntab);
				scrollPane.setBorder(padding);
				tabbedPane.addTab(
						(String) tab.getClientProperty("title"),
						null, // icon
						scrollPane,
						(String) tab.getClientProperty("tooltip"));
			}
	        jd.add(tabbedPane, BorderLayout.CENTER); //
		}
		// Add buttons
        JPanel panelButtons = new JPanel(false);
        panelButtons.setLayout(new FlowLayout());
        for (JButton b : buttons) {
        	panelButtons.add(b);
        	 b.addActionListener(this);
        }
        jd.add(panelButtons, BorderLayout.PAGE_END);
        jd.setSize(width,height);
    }

        public String showDialogAny() {
  			buildDialog();
        	jd.setVisible(true);
            return result;
        }
        public boolean showDialog() {
  			buildDialog();
        	jd.setVisible(true);
        	if ((result != null) && !result.equals("Cancel")) return true;
            return false;
        }

    @Override
	public void actionPerformed(ActionEvent e) {
    	System.out.println(e.getActionCommand());
    	if (e.getActionCommand().equals("Cancel")) {
    		result = e.getActionCommand();
    		jd.dispose();
    	} else if (e.getActionCommand().equals("OK")) {
    		result = e.getActionCommand();
    		jd.dispose();
    	}

    }

    public boolean wasCanceled() {
    	return (result == null) || (result.equals("Cancel"));
    }

    private boolean skipComments (
    		boolean incTab) { // increment tab if nothing left in this one (false - keep read_component equal to tab's components length
    	boolean got_it = false;
//    	boolean nothing_left = false;
//    	read_component ++;
    	while (!got_it) {
    		if (read_component >= labels.get(read_tab).size()) {
    			if (!incTab || (read_tab >= labels.size())) break;
    			read_tab++;
    			read_component = 0;
    		}
    		if (components.get(read_tab).get(read_component) != null) {
    			got_it = true;
    		} else {
    			read_component++;
    		}
    	}
    	return got_it;
    }

    public void checkType (String type) { // type == null for null components OK
    	JComponent component = components.get(read_tab).get(read_component);
    	String this_type = "null";
    	if (component != null) {
    		this_type = (String) component.getClientProperty("type");
    		if (this_type.equals(type)) return;
    	} else if (type == null) return;
    	// component type mismatch - report
    	String msg="Dialog components mismatch for tab="+read_tab+", component="+read_component+
    			", label='"+labels.get(read_tab).get(read_component).getText()+"': expected "+type+", got "+this_type;
    	IJ.showMessage(msg);
    	throw new IllegalArgumentException (msg);
    }

    public boolean getNextBoolean() {
    	skipComments(true);
    	checkType("boolean");
    	boolean rslt = ((JCheckBox) components.get(read_tab).get(read_component)).isSelected();
    	read_component ++;
    	return rslt;

    }

    public String getNextString() {
    	skipComments(true); // add testing for checkbox?
    	checkType("String");
    	String rslt =  ((JTextField) components.get(read_tab).get(read_component).getComponents()[0]).getText();
    	read_component ++;
    	return rslt;
    }

    public double getNextNumber() {
    	skipComments(true); // add testing for checkbox?
    	checkType("double");
    	double rslt =  Double.parseDouble(((JTextField) components.get(read_tab).get(read_component).getComponents()[0]).getText());
    	read_component ++;
    	return rslt;
    }

    public int getNextChoiceIndex() {
    	skipComments(true); // add testing for checkbox?
    	checkType("combo");
    	@SuppressWarnings("unchecked")
		JComboBox<String> combo = (JComboBox<String>) components.get(read_tab).get(read_component).getComponents()[0];
    	String selectedItem = (String)combo.getSelectedItem();
//    	Object [] items = combo.getSelectedObjects();
    	int size = combo.getItemCount();
    	int index = 0;
    	if (selectedItem != null) {
    		for (int i = 0; i < size; i++) if (combo.getItemAt(i).equals(selectedItem)) {
    			index = i;
    			break;
    		}
    	}


    	System.out.println(combo.getSelectedItem());
//    	for (int i = 0; i < selectedItem.length; i++){
 //   	    System.out.println(String.format("item %s = %s", i, selectedItem[i]));
  //  	}

    	read_component ++;
    	return index;
    }

/*
String[] selectedItem = (String[])combo.getSelectedItem();
for (int i = 0; i < selectedItem.length; i++){
    System.out.println(String.format("item %s = %s", i, selectedItem[i]));
}
 */

}
 No newline at end of file
+333 −765

File changed.

Preview size limit exceeded, changes collapsed.

Loading