001: /* Command.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Jun 1, 2007 11:09:09 AM, Created by henrichen
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019:
020: package org.zkoss.mil;
021:
022: import org.zkoss.lang.Objects;
023: import org.zkoss.xml.HTMLs;
024: import org.zkoss.zk.ui.Component;
025: import org.zkoss.zk.ui.UiException;
026:
027: /**
028: * A command on the Mobile. Whenever a Command is "clicked" on the Mobile, it will fire onCommand to
029: * its parent component.
030: *
031: * @author henrichen
032: */
033: public class Command extends MilComponent {
034: private final static int BACK = 2;
035: private final static int CANCEL = 3;
036: private final static int EXIT = 7;
037: private final static int HELP = 5;
038: private final static int ITEM = 8;
039: private final static int OK = 4;
040: private final static int SCREEN = 1;
041: private final static int STOP = 6;
042:
043: private final static int SELECT = 0x100; //special command for JavaME List.setSelectCommand() only
044:
045: private static final long serialVersionUID = 200706011201L;
046: private String _label = "OK";
047: private String _llabel;
048: private String _type = "ok";
049: private int _priority = 1;
050:
051: public Command() {
052: }
053:
054: /**
055: * A Command on the Mobile (the command priority is default to 1).
056: *
057: * @param label label appear on the command
058: * @param type The command type on the Mobile (back, cancel, exit, help, item, ok, screen, stop).
059: */
060: public Command(String label, String type) {
061: this (label, null/*longLabel*/, type, 1);
062: }
063:
064: /**
065: * A Command on the Mobile.
066: *
067: * @param label label appear on the command
068: * @param type The command type on the Mobile (back, cancel, exit, help, item, ok, screen, stop).
069: */
070: public Command(String label, String type, int priority) {
071: this (label, null/*longLabel*/, type, priority);
072: }
073:
074: /**
075: * A Command on the Mobile.
076: *
077: * @param label label appear on the command
078: * @param longLabel long label apper on the command
079: * @param type The command type on the Mobile (back, cancel, exit, help, item, ok, screen, stop).
080: */
081: public Command(String label, String longLabel, String type,
082: int priority) {
083: setLabel(label);
084: setLongLabel(longLabel);
085: setType(type);
086: setPriority(priority);
087: }
088:
089: public String getLabel() {
090: return _label;
091: }
092:
093: public void setLabel(String label) {
094: if (label != null && label.length() == 0) {
095: label = null;
096: }
097: if (label == null) {
098: throw new NullPointerException(
099: "Label cannot be empty or null");
100: }
101: if (!Objects.equals(_label, label)) {
102: _label = label;
103: smartUpdate("lb", label);
104: }
105: }
106:
107: public String getLongLabel() {
108: return _llabel;
109: }
110:
111: public void setLongLabel(String label) {
112: if (label != null && label.length() == 0) {
113: label = null;
114: }
115: if (!Objects.equals(_llabel, label)) {
116: _llabel = label;
117: smartUpdate("ll", label);
118: }
119: }
120:
121: public String getType() {
122: return _type;
123: }
124:
125: public void setType(String type) {
126: if (type != null && type.length() == 0) {
127: type = null;
128: }
129: if (!Objects.equals(_type, type)) {
130: _type = type;
131: smartUpdate("tp", getCommandType(type));
132: }
133: }
134:
135: public int getPriority() {
136: return _priority;
137: }
138:
139: public void setPriority(int priority) {
140: if (_priority != priority) {
141: _priority = priority;
142: smartUpdate("pr", priority);
143: }
144: }
145:
146: //--Component--//
147: public void setParent(Component parent) {
148: if ((parent != null && !(parent instanceof MilComponent))
149: || parent instanceof Listitem) {
150: throw new UiException("Unsupported parent for command: "
151: + parent);
152: }
153: super .setParent(parent);
154: }
155:
156: /** Not childable. */
157: public boolean isChildable() {
158: return false;
159: }
160:
161: public String getInnerAttrs() {
162: final StringBuffer sb = new StringBuffer(64);
163: HTMLs.appendAttribute(sb, "tp", getCommandType(_type));
164: HTMLs.appendAttribute(sb, "lb", encodeString(getLabel()));
165: HTMLs.appendAttribute(sb, "ll", encodeString(getLongLabel()));
166: HTMLs.appendAttribute(sb, "pr", getPriority());
167: return sb.toString();
168: }
169:
170: private int getCommandType(String type) {
171: if ("back".equals(type)) {
172: return BACK;
173: } else if ("cancel".equalsIgnoreCase(type)) {
174: return CANCEL;
175: } else if ("exit".equalsIgnoreCase(type)) {
176: return EXIT;
177: } else if ("help".equalsIgnoreCase(type)) {
178: return HELP;
179: } else if ("item".equalsIgnoreCase(type)) {
180: return ITEM;
181: } else if ("ok".equalsIgnoreCase(type)) {
182: return OK;
183: } else if ("screen".equalsIgnoreCase(type)) {
184: return SCREEN;
185: } else if ("stop".equalsIgnoreCase(type)) {
186: return STOP;
187: } else if ("select".equalsIgnoreCase(type)) {
188: return SELECT;
189: } else {
190: throw new IllegalArgumentException(
191: "Unsupport Command type: " + type);
192: }
193: }
194: }
|