package imagej.plugin.gui.swing;

import imagej.plugin.Plugin;
import imagej.plugin.PluginModule;
import imagej.plugin.gui.AbstractInputHarvester;
import imagej.plugin.gui.InputPanel;
import imagej.plugin.process.PluginPreprocessor;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * SwingInputHarvester is a plugin preprocessor that collects input parameter
 * values from the user using a {@link SwingInputPanel} dialog box.
 */
@Plugin(type = PluginPreprocessor.class)
public class SwingInputHarvester extends AbstractInputHarvester {

	@Override
	public InputPanel createInputPanel() {
		return new SwingInputPanel();
	}

	@Override
	public boolean showDialog(final InputPanel inputPanel,
		final PluginModule<?> module)
	{
		final JOptionPane optionPane = new JOptionPane(null);
		optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
		final JDialog dialog = optionPane.createDialog(module.getInfo().getLabel());
		final JPanel mainPane = (JPanel) optionPane.getComponent(0);
		final JPanel widgetPane = (JPanel) mainPane.getComponent(0);
		widgetPane.add((SwingInputPanel) inputPanel);
		dialog.setModal(true);
		dialog.pack();
		dialog.setVisible(true);
		final Integer rval = (Integer) optionPane.getValue();
		return rval != null && rval == JOptionPane.OK_OPTION;
	}

}

@@ -27,16 +27,69 @@
 swing;%0A%0A
+import java.awt.Dimension;%0Aimport java.awt.Toolkit;%0A%0A
 import i
@@ -1228,16 +1228,54 @@
 pack();%0A
+%09%09ensureDialogSizeReasonable(dialog);%0A
 %09%09dialog
@@ -1409,10 +1409,504 @@
 ON;%0A%09%7D%0A%0A
+%09private void ensureDialogSizeReasonable(JDialog dialog)%0A%09%7B%0A%09%09Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();%0A%09%09%0A%09%09Dimension dialogSize = dialog.getSize();%0A%0A%09%09int newWidth = dialogSize.width;%0A%09%09int newHeight = dialogSize.height;%0A%09%09%0A%09%09if (dialogSize.width %3E (0.75*screenSize.width))%0A%09%09%09newWidth = (int)(0.75 * screenSize.width);%0A%09%09%0A%09%09if (dialogSize.height %3E (0.75*screenSize.height))%0A%09%09%09newHeight = (int)(0.75 * screenSize.height);%0A%0A%09%09dialog.setSize(newWidth, newHeight);%0A%09%7D%0A
 %7D%0A

fix for superwide spinners going off the screen. this fix is not sufficient in that the entry field gets clipped by the new width of the dialog. thus one can enter values into the field but cannot see them. will address with a follow on fix.