//
// SwingInputHarvester.java
//

/*
ImageJ software for multidimensional image processing and analysis.

Copyright (c) 2010, ImageJDev.org.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the names of the ImageJDev.org developers nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

package imagej.plugin.ui.swing;

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

import java.awt.Dimension;
import java.awt.Toolkit;

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.
 *
 * @author Curtis Rueden
 * @author Barry DeZonia
 */
@Plugin(type = PluginPreprocessor.class)
public class SwingInputHarvester extends AbstractInputHarvester {

	@Override
	public SwingInputPanel 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);
		// TODO - use JScrollPane in case there are many widgets
		widgetPane.add(((SwingInputPanel) inputPanel).getPanel());
		dialog.setModal(true);
		dialog.pack();
		ensureDialogSizeReasonable(dialog);
		dialog.setVisible(true);
		final Integer rval = (Integer) optionPane.getValue();
		return rval != null && rval == JOptionPane.OK_OPTION;
	}

	private void ensureDialogSizeReasonable(final JDialog dialog) {
		final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();		
		final Dimension dialogSize = dialog.getSize();

		int newWidth = dialogSize.width;
		int newHeight = dialogSize.height;

		final int maxWidth = 3 * screenSize.width / 4;
		final int maxHeight = 3 * screenSize.height / 4;

		if (newWidth > maxWidth) newWidth = maxWidth;
		if (newHeight > maxHeight) newHeight = maxHeight;

		dialog.setSize(newWidth, newHeight);
	}
}

@@ -1668,16 +1668,48 @@
 swing;%0A%0A
+import imagej.plugin.MenuEntry;%0A
 import i
@@ -1764,16 +1764,104 @@
 Module;%0A
+import imagej.plugin.PluginModuleInfo;%0Aimport imagej.plugin.process.PluginPreprocessor;%0A
 import i
@@ -1939,57 +1939,8 @@
 nel;
-%0Aimport imagej.plugin.process.PluginPreprocessor;
 %0A%0Aim
@@ -1988,16 +1988,39 @@
 Toolkit;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -2803,16 +2803,83 @@
 bel());%0A
+%09%09final String title = getTitle(module);%0A%09%09dialog.setTitle(title);%0A
 %09%09final 
@@ -3339,16 +3339,673 @@
 ON;%0A%09%7D%0A%0A
+%09private String getTitle(final PluginModule%3C?%3E module) %7B%0A%09%09final PluginModuleInfo%3C?%3E moduleInfo = module.getInfo();%0A%09%09final String label = moduleInfo.getLabel();%0A%09%09if (label != null && !label.isEmpty()) return label;%0A%09%09final List%3CMenuEntry%3E menuPath = moduleInfo.getPluginEntry().getMenuPath();%0A%09%09if (menuPath != null && menuPath.size() %3E 0) %7B%0A%09%09%09final MenuEntry menuEntry = menuPath.get(menuPath.size() - 1);%0A%09%09%09final String menuName = menuEntry.getName();%0A%09%09%09if (menuName != null && !menuName.isEmpty()) return menuName;%0A%09%09%7D%0A%09%09final String name = moduleInfo.getName();%0A%09%09if (name != null && !name.isEmpty()) return name;%0A%09%09return %22Choose Parameters%22;%0A%09%7D%0A%0A
 %09private

Set dialog title when prompting for parameter values.