View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19   
20  package javax.microedition.lcdui;
21  
22  import java.util.Vector;
23  
24  import org.microemu.device.Device;
25  import org.microemu.device.DeviceFactory;
26  import org.microemu.device.ui.DisplayableUI;
27  
28  
29  
30  public abstract class Displayable
31  {
32  	Device device;
33  	
34  	boolean sizeChangedDeferredRequest;
35  	
36  	Display currentDisplay = null;
37      
38  	boolean fullScreenMode;
39  
40      Ticker ticker;
41      
42      // TODO make private
43      int viewPortY;
44      // TODO make private
45      int viewPortHeight;
46      
47      protected DisplayableUI ui;
48      
49      private String title;
50      
51      /**
52       * @associates Command 
53       */
54  	private Vector commands = new Vector();
55  	
56  	private CommandListener listener = null;
57  
58      
59      Displayable(String title) 
60      {
61          this.device = DeviceFactory.getDevice();
62          this.sizeChangedDeferredRequest = false;        
63          this.fullScreenMode = false;
64          this.title = title;
65      }
66      
67      
68      void setUI(DisplayableUI ui) {
69      	this.ui = ui;
70      }
71    
72  
73  	public void addCommand(Command cmd) {
74  		// Check that its not the same command
75  		for (int i = 0; i < commands.size(); i++) {
76  			if (cmd == (Command) commands.elementAt(i)) {
77  				// Its the same just return
78  				return;
79  			}
80  		}
81  
82  		// Now insert it in order
83  		boolean inserted = false;
84  		for (int i = 0; i < commands.size(); i++) {
85  			if (cmd.getPriority() < ((Command) commands.elementAt(i)).getPriority()) {
86  				commands.insertElementAt(cmd, i);
87  				inserted = true;
88  				break;
89  			}
90  		}
91  		if (inserted == false) {
92  			// Not inserted just place it at the end
93  			commands.addElement(cmd);
94  		}
95  		
96  		ui.addCommand(cmd);
97  
98  		if (isShown()) {
99  			currentDisplay.updateCommands();
100 		}
101 	}
102 
103 
104 	public void removeCommand(Command cmd)
105 	{
106 		commands.removeElement(cmd);
107 		
108 		ui.removeCommand(cmd);
109 
110 		if (isShown()) {
111 			currentDisplay.updateCommands();
112 		}
113 	}
114     
115     
116     public int getWidth()
117     {
118     	if (fullScreenMode) {
119     		return device.getDeviceDisplay().getFullWidth();
120     	} else {
121     		return device.getDeviceDisplay().getWidth();
122     	}
123     }
124 
125 
126     public int getHeight()
127     {
128     	if (fullScreenMode) {
129     		return device.getDeviceDisplay().getFullHeight();
130     	} else {
131     		return device.getDeviceDisplay().getHeight();
132     	}
133     }
134 
135 
136 	public boolean isShown()
137 	{
138 		if (currentDisplay == null) {
139 			return false;
140 		}
141 		return currentDisplay.isShown(this);
142 	}
143 
144     
145     public Ticker getTicker() 
146     {
147         return ticker;
148     }
149 
150     
151     public void setTicker(Ticker ticker) 
152     {
153         this.ticker = ticker;
154 
155         repaint();
156     }
157 
158     
159     public String getTitle() 
160     {
161         return title;
162     }
163 
164     
165     public void setTitle(String s) 
166     {
167         this.title = s;
168         
169         // TODO move to the native UI component
170         ui.invalidate();
171     }        
172     
173 
174 	public void setCommandListener(CommandListener l)
175 	{
176 		listener = l;
177 		
178 		ui.setCommandListener(l);
179 	}
180 	
181 	
182 	protected void sizeChanged(int w, int h)
183 	{		
184 	}
185 
186 
187 	CommandListener getCommandListener()
188 	{
189 		return listener;
190 	}
191 
192 
193 	Vector getCommands()
194 	{
195 		// in Form this is overriden to allow for the inclusion
196 		// of item contained commands 
197 		// Andres Navarro
198 		return commands;
199 	}
200 
201 
202 	void hideNotify()
203 	{
204 	}
205 
206 
207 	final void hideNotify(Display d)
208 	{		
209 		ui.hideNotify();
210 
211 		hideNotify();
212 	}
213 
214 
215 	void keyPressed(int keyCode)
216 	{
217 	}
218 
219 
220 	void keyRepeated(int keyCode)
221 	{
222 	}
223 
224 
225 	void keyReleased(int keyCode)
226 	{
227 	}
228 
229 
230 	void pointerPressed(int x, int y) 
231 	{
232 	}
233 
234 	
235 	void pointerReleased(int x, int y) 
236 	{
237 	}
238 
239 	
240 	void pointerDragged(int x, int y) 
241 	{
242 	}
243 
244 	
245 	abstract void paint(Graphics g);
246 
247 
248 	void repaint()
249 	{
250 		if (currentDisplay != null) {
251 			repaint(0, 0, getWidth(), getHeight());
252 		}
253 	}
254 
255 
256 	void repaint(int x, int y, int width, int height)
257     {
258 		if (currentDisplay != null) {
259 			currentDisplay.repaint(this, x, y, width, height);
260 		}
261     }
262 	
263 	
264 	void showNotify()
265 	{        
266 	}
267 
268 
269 	final void showNotify(Display d)
270 	{
271 		currentDisplay = d;
272         viewPortY = 0;
273         // TODO remove this StringComponent object when native UI is completed
274         StringComponent title = new StringComponent(getTitle());
275         viewPortHeight = getHeight() - title.getHeight() - 1;
276         if (ticker != null) {
277         		viewPortHeight -= this.ticker.getHeight();
278         }
279         
280         if (sizeChangedDeferredRequest) {
281         	sizeChanged(getWidth(), getHeight());
282         	sizeChangedDeferredRequest = false;
283         }
284 		
285 		showNotify();
286 
287 		ui.showNotify();		
288 	}
289 
290 }