package org.minimalj.frontend.impl.json;

import java.util.Arrays;
import java.util.List;

import org.minimalj.frontend.Frontend.IContent;
import org.minimalj.frontend.Frontend.Search;
import org.minimalj.frontend.Frontend.TableActionListener;
import org.minimalj.frontend.action.Action;
import org.minimalj.frontend.page.IDialog;

public class JsonDialog extends JsonComponent implements IDialog {

	public JsonDialog(String title, IContent content, Action saveAction, Action[] actions) {
		super("Dialog");
		put("title", title);
		put("content", (content));
		
		List<Object> jsonActions = JsonFrontend.getClientSession().createActions(actions);
		put("actions", jsonActions);
		
		// if saveAction is one of the 'normal' actions the json adapter has to be reused
		// (there can be only one listener on the Minimal-J action, if there were two
		// one would not get the notifications for enable / disable)
		int saveActionIndex = Arrays.asList(actions).indexOf(saveAction);
		Object jsonSaveAction = saveActionIndex > -1 ? jsonActions.get(saveActionIndex) : JsonFrontend.getClientSession().createAction(saveAction);
		put("saveAction", jsonSaveAction);
		
		JsonFrontend.getClientSession().openDialog(this);
	}

	private JsonDialog(String type, JsonComponent content) {
		super(type);
		put("title", "Search");
		put("content", content);
		JsonFrontend.getClientSession().openDialog(this);
	}

	@Override
	public void closeDialog() {
		JsonFrontend.getClientSession().closeDialog((String) get("id"));
	}
	
	public static class JsonSearchDialog<T> extends JsonDialog {

		public JsonSearchDialog(Search<T> search, Object[] keys, TableActionListener<T> listener) {
			super("SearchDialog", new JsonSearchPanel(search, keys, listener));
		}
		
	}

}

@@ -676,16 +676,45 @@
 ns);%0A%09%09%0A
+%09%09if (saveAction != null) %7B%0A%09
 %09%09// if 
@@ -791,16 +791,17 @@
 eused%0A%09%09
+%09
 // (ther
@@ -872,16 +872,17 @@
 e two%0A%09%09
+%09
 // one w
@@ -934,16 +934,17 @@
 isable)%0A
+%09
 %09%09int sa
@@ -1005,16 +1005,17 @@
 ion);%0A%09%09
+%09
 Object j
@@ -1146,16 +1146,17 @@
 ction);%0A
+%09
 %09%09put(%22s
@@ -1184,16 +1184,20 @@
 ction);%0A
+%09%09%7D%0A
 %09%09%0A%09%09Jso

allow missing SaveAction (no NPE)