package org.minimalj.frontend.edit;

import java.util.List;

import org.minimalj.frontend.toolkit.ClientToolkit;
import org.minimalj.frontend.toolkit.IComponent;
import org.minimalj.frontend.toolkit.IDialog;
import org.minimalj.frontend.toolkit.ResourceAction;
import org.minimalj.frontend.toolkit.ClientToolkit.Search;
import org.minimalj.frontend.toolkit.ITable.TableActionListener;

public abstract class SearchDialogAction<T> extends ResourceAction implements Search<T> {
	private final IComponent source;
	private final Object[] keys;
	private IDialog dialog;
	
	protected SearchDialogAction(IComponent source, Object... keys) {
		this.source = source;
		this.keys = keys;
	}
	
	@Override
	public void action(IComponent context) {
		try {
			showPageOn(source);
		} catch (Exception x) {
			// TODO show dialog
			x.printStackTrace();
		}
	}

	protected int getColumnWidthPercentage() {
		return 100;
	}

	protected void run(IComponent source) {
		
	}
	
	private void showPageOn(IComponent source) {
		dialog = ClientToolkit.getToolkit().createSearchDialog(source, this, keys, new SearchClickListener());
		dialog.openDialog();
	}
	
	public abstract List<T> search(String query);

	protected abstract void save(T object);
	
	private class SearchClickListener implements TableActionListener<T> {
		@Override
		public void action(T selectedObject, List<T> selectedObjects) {
			save(selectedObject);
			dialog.closeDialog();
		}
	}
	
}

@@ -107,24 +107,84 @@
 ntToolkit;%0D%0A
+import org.minimalj.frontend.toolkit.ClientToolkit.Search;%0D%0A
 import org.m
@@ -309,82 +309,34 @@
 kit.
-ResourceAction;%0D%0Aimport org.minimalj.frontend.toolkit.ClientToolkit.Search
+ITable.TableActionListener
 ;%0D%0Ai
@@ -375,34 +375,22 @@
 kit.
-ITable.TableActionListener
+ResourceAction
 ;%0D%0A%0D
@@ -942,61 +942,8 @@
 %0D%0A%0D%0A
-%09protected void run(IComponent source) %7B%0D%0A%09%09%0D%0A%09%7D%0D%0A%09%0D%0A
 %09pri

remove dead method