View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2005 Andres Navarro
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   *  Contributor(s):
21   *    3GLab
22   */
23   
24  package javax.microedition.lcdui;
25  
26  import java.util.Vector;
27  
28  import org.microemu.device.DeviceFactory;
29  
30  public abstract class Item
31  {
32  
33  	static final int OUTOFITEM = Integer.MAX_VALUE;
34  
35  	public static final int LAYOUT_DEFAULT          = 0x0000;
36  
37      public static final int LAYOUT_LEFT             = 0x0001;
38      public static final int LAYOUT_RIGHT            = 0x0002;
39      public static final int LAYOUT_CENTER           = 0x0003;
40  
41      public static final int LAYOUT_TOP              = 0x0010;
42      public static final int LAYOUT_BOTTOM           = 0x0020;
43      public static final int LAYOUT_VCENTER          = 0x0030;
44  
45      public static final int LAYOUT_NEWLINE_BEFORE   = 0x0100;
46      public static final int LAYOUT_NEWLINE_AFTER    = 0x0200;
47  
48      public static final int LAYOUT_SHRINK           = 0x0400;
49      public static final int LAYOUT_EXPAND           = 0x0800;
50      public static final int LAYOUT_VSHRINK          = 0x1000;
51      public static final int LAYOUT_VEXPAND          = 0x2000;
52  
53      public static final int LAYOUT_2                = 0x4000;
54  
55  
56      public static final int PLAIN = 0;
57      public static final int HYPERLINK = 1;
58      public static final int BUTTON = 2;
59  
60  	StringComponent labelComponent;
61  	Screen owner = null;
62  	private boolean focus = false;
63  	
64  	// MIDP2
65  	int layout;
66  	Vector commands;
67      Command defaultCommand;
68      ItemCommandListener commandListener;
69      // -1 means unlocked, otherwise it is the 
70      // application requested preffered size
71      // for the actual one use the getPrefXXXX() method
72      // package access
73      private int prefWidth, prefHeight;
74    
75    
76    Item(String label)
77    {
78  		labelComponent = new StringComponent(label);
79  		commands = new Vector();
80    }
81    
82  	public void addCommand(Command cmd) {
83  	    if (cmd == null)
84  	        throw new NullPointerException();
85  	
86  	    if (!commands.contains(cmd)) {
87  	        // Now insert it in order
88  	        boolean inserted = false;
89  	          
90  	        for (int i = 0; i < commands.size(); i++) {
91  	            if (cmd.getPriority() < ((Command)commands.elementAt(i)).getPriority()) {
92  	                commands.insertElementAt(cmd, i);
93  	                inserted = true;
94  	                break;
95  	            }
96  	        }
97  	        if (!inserted) {
98  	          // Not inserted just place it at the end
99  	              commands.addElement(cmd);
100 	        }
101 	    	repaintOwner();
102         }
103 
104 	}
105   
106 	public String getLabel()
107 	{
108 		return labelComponent.getText();
109 	}
110 
111 	public int getLayout() {
112 		return layout;
113 	}
114 	
115 	public int getMinimumHeight() {
116 		if (labelComponent != null)
117 			return labelComponent.getHeight();
118 		else 
119 			return 0;
120     }
121 
122     public int getMinimumWidth() {
123     	return getMaximumWidth();
124     }
125     
126     public int getPreferredHeight() {
127         int ret = prefHeight;
128         int min = getMinimumHeight();
129         int max = getMaximumHeight();
130 
131         if (ret == -1)
132         	return min;
133         
134         if (ret < min)
135         	ret = min;
136         else if (ret > max)
137         	ret = max;
138     	return ret;
139     }
140 
141     public int getPreferredWidth() {
142         int ret = prefWidth;
143         int min = getMinimumWidth();
144         int max = getMaximumWidth();
145         
146         if (ret == -1)
147         	return max;
148         	
149         if (ret < min)
150         	ret = min;
151         else if (ret > max)
152         	ret = max;
153     	return ret;
154     }
155 
156 	public void notifyStateChanged() {
157 		Screen owner = getOwner();
158 		if (owner != null && owner instanceof Form) {
159 			Form form = (Form) owner;
160 			form.fireItemStateListener(this);
161 		}
162 		
163     }
164 
165 	public void removeCommand(Command cmd) {
166         commands.removeElement(cmd);
167         if (defaultCommand == cmd)
168         	defaultCommand = null;
169         repaintOwner();
170     }
171 	
172     public void setDefaultCommand(Command cmd) {
173         this.defaultCommand = cmd;
174         if (cmd != null) {
175             // we should repaint even if the command was added
176             // because the command layout could become different
177             if (commands.contains(cmd))
178             	addCommand(cmd);
179             else 
180             	repaintOwner();
181         } else {
182         	repaintOwner();
183         }
184     }
185 
186     public void setItemCommandListener(ItemCommandListener l) {
187         this.commandListener = l;
188     }
189     
190     public void setLabel(String label)	
191 	{
192 		labelComponent.setText(label);
193 		repaint();
194 	}
195 
196     public void setLayout(int layout) {
197     	// TODO validate container is not Alert
198     	// on add to Alert validate this is default
199     	
200     	// notice that the vertical and the horizontal
201     	// layout policies can't generate conflict
202     	// because the center is the or of the two
203     	// others (ie VCENTER == (LEFT | RIGHT))
204     	if ((( (layout & LAYOUT_SHRINK) != 0) &&
205     		  ((layout & LAYOUT_EXPAND) != 0)) ||
206     			( ((layout & LAYOUT_VSHRINK) != 0) &&
207     	    	(layout & LAYOUT_VEXPAND) != 0) )
208     		throw new IllegalArgumentException(
209     				"Bad combination of layout policies");
210 		this.layout = layout;
211     	repaint();
212     }
213 
214     public void setPreferredSize(int width, int height) {
215         if (width < -1 || height < -1) {
216             throw new IllegalArgumentException();
217         }
218         this.prefWidth = width;
219         this.prefHeight = height;
220         repaint();
221     }
222 
223     //
224     // package access methods
225     //
226     
227     // repaint the owner of this item (if any)
228     void repaintOwner() {
229         Screen owner = getOwner();
230         if (owner != null)
231         	owner.repaint();
232     }
233 	
234   int getHeight()
235 	{
236 		return labelComponent.getHeight();
237 	}
238 	
239 	
240 	boolean isFocusable()
241 	{
242 		return false;
243 	}
244 	
245 	
246   void keyPressed(int keyCode)
247   {
248   }
249   
250     
251   abstract int paint(Graphics g);
252 	
253 	
254 	void paintContent(Graphics g)
255 	{
256 		labelComponent.paint(g);
257 	}
258 	
259 	
260 	void repaint()
261 	{
262 		if (owner != null) {
263 			owner.repaint();
264 		}
265 	}
266 	
267   
268 	boolean hasFocus()
269 	{
270 		return focus;
271 	}
272   
273   
274 	void setFocus(boolean state)
275 	{
276 		focus = state;
277 	}
278   
279   
280   Screen getOwner()
281   {
282     return owner;
283   }
284 	
285 	
286   	void setOwner(Screen owner) 
287   	{
288 		this.owner = owner;
289 
290 		if (owner == null) {
291 			setFocus(false);
292 		}
293 	}
294 	
295 	
296 	boolean select()
297 	{
298 		// call the default command (if there is one)
299 		// however subclasses may override this behaviour
300 		// (ie popup choices uses select to bring the popup)
301 		if (defaultCommand != null && commandListener != null) {
302 			commandListener.commandAction(defaultCommand, this);
303 			return true;
304 		} else {
305 			return false;
306 		}
307 	}
308   
309 
310 	int traverse(int gameKeyCode, int top, int bottom, boolean action)
311 	{
312 		return 0;
313 	}
314 
315 	int getMaximumHeight() {
316 		if (owner != null) {
317 			return owner.getHeight() * 10;
318 		} else {
319 			return DeviceFactory.getDevice().getDeviceDisplay().getHeight() * 10;
320 		}
321 	}
322 	
323 	int getMaximumWidth() {
324 		if (owner != null) {
325 			return owner.getWidth() - 3;
326 		} else {
327 			return DeviceFactory.getDevice().getDeviceDisplay().getWidth() - 3;
328 		}
329 	}
330 
331 	ItemCommandListener getItemCommandListener() {
332 		return this.commandListener;
333 	}
334 }