/* use a stack to push and pop displayable objects which will include * public void pushDisplayable(Displayable) * public void popDisplayable() * public void home() methods */ /************************************** * Source & Further Reading: J. W. Muchow (2002), Core J2ME Technology & MIDP, Sun MICROSYSTEMS ***************************************/ import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import java.util.*; public class DisplayManager extends Stack { private Display display; // refer to a Display object private Displayable mainDisplayable; // the main Displayable for the MIDlet private Alert alSE; // Any stack error to be displyed by this Alert object // MyDisplayManager Constructor public DisplayManager(Display display, Displayable mainDisplayable){ // You can only display one Displayable object per MIdlet, "this" this.display = display; this.mainDisplayable = mainDisplayable; // create the alert display if an error occurs alSE = new Alert("Unable to Perform Task"); // Stack error Alert alSE.setTimeout(Alert.FOREVER); // Modal } // Pop displayable from stack and activate public void popDisplayable() { // if stack is not empty show next displayable if(empty() == false) display.setCurrent((Displayable) pop()); else // if error occurs alert, and set the main displayable active display.setCurrent(alSE, mainDisplayable); } /* Push the displayable object onto stack and set the passed in displayable as active */ public void pushDisplayable(Displayable newDisplayable){ push(display.getCurrent()); display.setCurrent(newDisplayable); } // Return to the main displayable object public void home(){ while (elementCount > 1) { pop(); display.setCurrent(mainDisplayable); } } }