1 /* 
  2  * Lancaster University 
  3  * Computing Department 
  4  *  
  5  * Created by Eduardo Figueiredo 
  6  * Date: 22 Jun 2007 
  7  *  
  8  */ 
  9 package ubc.midp.mobilephoto.core.ui.controller; 
 10  
 11 import javax.microedition.lcdui.Alert; 
 12 import javax.microedition.lcdui.Command; 
 13 import javax.microedition.lcdui.CommandListener; 
 14 import javax.microedition.lcdui.Display; 
 15 import javax.microedition.lcdui.Displayable; 
 16  
 17 import ubc.midp.mobilephoto.core.ui.MainUIMidlet; 
 18 import ubc.midp.mobilephoto.core.ui.datamodel.AlbumData; 
 19 import ubc.midp.mobilephoto.core.ui.screens.AlbumListScreen; 
 20  
 21 /** 
 22  * Purpose: (i) to structure controllers and (ii) simplify method handleCommand. 
 23  * @author Eduardo Figueiredo 
 24  * 
 25  */ 
 26 public abstract class AbstractController implements CommandListener, ControllerInterface { 
 27  
 28   protected MainUIMidlet midlet; 
 29    
 30   //Define a successor to implement the Chain of Responsibility design pattern 
 31   private ControllerInterface nextController; 
 32  
 33   private AlbumData albumData; 
 34  
 35   //Define the basic screens 
 36   private AlbumListScreen albumListScreen; 
 37  
 38   /** 
 39    * @param midlet 
 40    * @param nextController 
 41    * @param albumData 
 42    * @param albumListScreen 
 43    * @param currentScreenName 
 44    */ 
 45   public AbstractController(MainUIMidlet midlet, AlbumData albumData, AlbumListScreen albumListScreen) { 
 46     this.midlet = midlet; 
 47     this.albumData = albumData; 
 48     this.albumListScreen = albumListScreen; 
 49     // [EF] Senario 04: A singleton ScreenSingleton was created in order to all other access it.  
 50     // [EF] I think some data need to be unique (e.g. currentScreenName) to make them consistent for all controllers. 
 51   } 
 52    
 53   /* (non-Javadoc) 
 54    * @see ubc.midp.mobilephoto.core.ui.controller.ControllerInterface#postCommand(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable) 
 55    */ 
 56   public void postCommand(Command command) { 
 57         System.out.println("AbstractController::postCommand - Current controller is: " + this.getClass().getName()); 
 58         //If the current controller cannot handle the command, pass it to the next 
 59         //controller in the chain. 
 60         if (handleCommand(command) == false) { 
 61           ControllerInterface next = getNextController(); 
 62             if (next != null) { 
 63                 System.out.println("Passing to next controller in chain: " + next.getClass().getName()); 
 64                 next.postCommand(command); 
 65             } else { 
 66                 System.out.println("AbstractController::postCommand - Reached top of chain. No more handlers for command: " + command); 
 67             } 
 68         } 
 69  
 70   } 
 71  
 72   /*  
 73    * Handle events. For now, this just passes control off to a 'wrapper' 
 74    * so we can ensure, in order to use it in the aspect advice 
 75    * (non-Javadoc) 
 76    * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable) 
 77    */ 
 78   public void commandAction(Command c, Displayable d) { 
 79     postCommand(c); 
 80   } 
 81  
 82  
 83     public void setAlbumListAsCurrentScreen(Alert a) { 
 84       setCurrentScreen(a, albumListScreen); 
 85     } 
 86    
 87     /** 
 88    * Set the current screen for display, after alert 
 89    */ 
 90     public void setCurrentScreen(Alert a, Displayable d) { 
 91         Display.getDisplay(midlet).setCurrent(a, d); 
 92     }  
 93  
 94     /** 
 95      * [EF] RENAMED in Scenario 04: remove "Name". Purpose: avoid method name conflict 
 96    * Get the current screen name that is displayed 
 97    */ 
 98     public Displayable getCurrentScreen() { 
 99         return Display.getDisplay(midlet).getCurrent(); 
100     }  
101      
102     /** 
103    * Set the current screen for display 
104    */ 
105     public void setCurrentScreen(Displayable d) { 
106         Display.getDisplay(midlet).setCurrent(d); 
107     }  
108  
109   /** 
110    * @return the albumData 
111    */ 
112   public AlbumData getAlbumData() { 
113     return albumData; 
114   } 
115  
116   /** 
117    * @param albumData the albumData to set 
118    */ 
119   public void setAlbumData(AlbumData albumData) { 
120     this.albumData = albumData; 
121   } 
122    
123   /** 
124    * @return the nextController 
125    */ 
126   public ControllerInterface getNextController() { 
127     return nextController; 
128   } 
129  
130   /** 
131    * @param nextController the nextController to set 
132    */ 
133   public void setNextController(ControllerInterface nextController) { 
134     this.nextController = nextController; 
135   } 
136  
137   /** 
138    * [EF] Scenario 04: Just forward method. 
139    * @return the currentStoreName 
140    */ 
141   public String getCurrentStoreName() { 
142     return ScreenSingleton.getInstance().getCurrentStoreName(); 
143   } 
144  
145   /** 
146    * @return the albumListScreen 
147    */ 
148   public AlbumListScreen getAlbumListScreen() { 
149     return albumListScreen; 
150   } 
151 }    
    |