/*
 * Copyright (C) 2009 JavaRosa
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.javarosa.j2me.view;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;

public class J2MEDisplay {
	private static Display display;
	
	public static void init (MIDlet m) {
		setDisplay(Display.getDisplay(m));
	}
	
	public static void setDisplay (Display display) {
		J2MEDisplay.display = display;
	}

	public static Display getDisplay () {
		if (display == null) {
			throw new RuntimeException("Display has not been set from MIDlet; call J2MEDisplay.init()");
		}
		
		return display;
	}
	
	public void setView (Displayable d) {
		display.setCurrent(d);
	}
	
	
//    //good utility function
//    private void showError(String title, String message) {
//    	//#style mailAlert
//    	Alert alert = new Alert(title, message, null, AlertType.ERROR);
//    	alert.setTimeout(Alert.FOREVER);
//    	//alert.setCommandListener(this);
//    	
//    	// Clayton Sims - Apr 27, 2009 : 
//    	//Really not-thrilled with how this works. Unfortunately this is an instance of views not being able
//    	//to propogate up new views. Maybe there should be a centralized way to use alerts? Or are we fine
//    	//with using the existing elements?
//    	Alert.setCurrent((Display)JavaRosaServiceProvider.instance().getDisplay().getDisplayObject(), alert, this);
//    }
//    
//    
//	private void showError(String s, String message) {
//		showError(s, message, this);
//	}
//
//	private void showError(String s, String message, CommandListener cl) {
//		//#style mailAlert
//		final Alert alert = new Alert(s, message, null, AlertType.ERROR);
//		alert.setTimeout(Alert.FOREVER);
//		if (cl != null) {
//			//alert.setCommandListener(cl);
//		}
//		J2MEDisplay.getDisplay().setCurrent(alert, view);
//	}
	
}

@@ -621,16 +621,147 @@
 iew;%0D%0A%0D%0A
+import javax.microedition.lcdui.Alert;%0Aimport javax.microedition.lcdui.AlertType;%0Aimport javax.microedition.lcdui.CommandListener;%0A
 import j
@@ -1311,16 +1311,23 @@
 %09public 
+static 
 void set
@@ -1385,57 +1385,136 @@
 %0A%09%0A%09
-%0A//    //good utility function%0A//    private void
+public static void showError (String title, String message) %7B%0A%09%09showError(title, message, null, null);%0A%09%7D%0A%09%0A%09public static Alert
  sho
@@ -1511,32 +1511,33 @@
  Alert showError
+ 
 (String title, S
@@ -1553,18 +1553,63 @@
 sage
-) %7B%0A//    
+, Displayable next, CommandListener customListener) %7B%0A%09
 %09//#
@@ -1624,23 +1624,24 @@
 ilAlert%0A
-//    %09
+%09%09final 
 Alert al
@@ -1696,22 +1696,17 @@
 ERROR);%0A
-//    
+%09
 %09alert.s
@@ -1735,803 +1735,317 @@
 R);%0A
-//    %09//alert.setCommandListener(this);%0A//    %09%0A//    %09// Clayton Sims - Apr 27, 2009 : %0A//    %09//Really not-thrilled with how this works. Unfortunately this is an instance of views not being able%0A//    %09//to propogate up new views. Maybe there should be a centralized way to use alerts? Or are we fine%0A//    %09//with using the existing elements?%0A//    %09Alert.setCurrent((Display)JavaRosaServiceProvider.instance().getDisplay().getDisplayObject(), alert, this);%0A//    %7D%0A//    %0A//    %0A//%09private void showError(String s, String message) %7B%0A//%09%09showError(s, message, this);%0A//%09%7D%0A//%0A//%09private void showError(String s, String message, CommandListener cl) %7B%0A//%09%09//#style mailAlert%0A//%09%09final Alert alert = new Alert(s, message, null, AlertType.ERROR);%0A//%09%09alert.setTimeout(Alert.FOREVER);%0A//%09%09if (cl !
+%0A%09%09if (customListener != null) %7B%0A%09%09%09if (next != null) %7B%0A%09%09%09%09System.err.println(%22Warning: alert invoked with both custom listener and 'next' displayable. 'next' will be ignored; it must be switched to explicitly in the custom handler%22);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09alert.setCommandListener(customListener);%0A%09%09%7D%0A%09%09%0A%09%09if (next =
 = nu
@@ -2054,79 +2054,59 @@
 ) %7B%0A
-//
 %09%09%09
-//alert.setCommandListener(cl
+display.setCurrent(alert
 );%0A
-//
 %09%09%7D
-%0A//%09%09J2MEDisplay.getD
+ else %7B%0A%09%09%09d
 isplay
-()
 .set
@@ -2124,22 +2124,40 @@
 rt, 
-view);%0A//
+next);%0A%09
 %09%7D%0A%09
-%0D
+%09%0A%09%09return alert;%0A%09%7D
 %0A%7D%0D%0A

add alert utility function