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  
21  package javax.microedition.lcdui;
22  
23  public class Command {
24  
25  	public static final int SCREEN = 1;
26  
27  	public static final int BACK = 2;
28  
29  	public static final int CANCEL = 3;
30  
31  	public static final int OK = 4;
32  
33  	public static final int HELP = 5;
34  
35  	public static final int STOP = 6;
36  
37  	public static final int EXIT = 7;
38  
39  	public static final int ITEM = 8;
40  
41  	// this is needed to allow for item commands without 
42  	// breaking the existing interfaces
43  	// (yeah i know it's ugly) 
44  	// Andres Navarro
45  	private Command originalCommand;
46  
47  	private Item focusedItem;
48  
49  	private Command itemCommand;
50  
51  	String label;
52  
53  	int commandType;
54  
55  	int priority;
56  
57  	public Command(String label, int commandType, int priority) {
58  		this.label = label;
59  		this.commandType = commandType;
60  		this.priority = priority;
61  	}
62  
63  	public Command(String shortLabel, String longLabel, int commandType,
64  			int priority) {
65  		// TODO implement
66  		this(shortLabel, commandType, priority);
67  	}
68  
69  	public int getCommandType() {
70  		return commandType;
71  	}
72  
73  	public String getLabel() {
74  		return label;
75  	}
76  
77  	public String getLongLabel() {
78  		// TODO implement;
79  		return label;
80  	}
81  
82  	public int getPriority() {
83  		return priority;
84  	}
85  
86  	Item getFocusedItem() {
87  		if (isRegularCommand()) {
88  			throw new IllegalStateException();
89  		}
90  		// can't be null!
91  		return focusedItem;
92  	}
93  
94  	Command getItemCommand(Item item) {
95  		if (!isRegularCommand()) {
96  			throw new IllegalStateException();
97  		}
98  		if (item == null) {
99  			throw new NullPointerException();
100 		}
101 		// we are allowed by the spec to threat all commands containes
102 		// in an Item as of the type ITEM
103 		if (itemCommand == null) {
104 			itemCommand = new Command(getLabel(), Command.ITEM, getPriority());
105 			itemCommand.originalCommand = this;
106 		}
107 		itemCommand.focusedItem = item;
108 		return itemCommand;
109 	}
110 
111 	Command getOriginalCommand() {
112 		if (isRegularCommand()) {
113 			throw new IllegalStateException();
114 		}
115 		// can't be null!
116 		return originalCommand;
117 	}
118 
119 	boolean isRegularCommand() {
120 		return originalCommand == null;
121 	}
122 }