001: /* Item.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: May 22, 2007 6:31:32 PM, 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: * Generic Item component under the {@link Frame} component.
029: * @author henrichen
030: */
031: abstract public class Item extends MilComponent {
032: //JavaME Item's appearance mode
033: protected final static int BUTTON = 2;
034: protected final static int HYPERLINK = 1;
035: protected final static int PLAIN = 0;
036:
037: //JavaME Item's layout constants
038: protected final static int LAYOUT_DEFAULT = 0; //follow the default layout policy of its container
039: protected final static int LAYOUT_2 = 0x4000; //indicating that new MIDP 2.0 layout rules are in effect.
040:
041: //align
042: protected final static int LAYOUT_LEFT = 1;
043: protected final static int LAYOUT_RIGHT = 2;
044: protected final static int LAYOUT_CENTER = 3;
045:
046: //valign
047: protected final static int LAYOUT_TOP = 0x10;
048: protected final static int LAYOUT_BOTTOM = 0x20;
049: protected final static int LAYOUT_VCENTER = 0x30;
050:
051: //flow
052: protected final static int LAYOUT_NEWLINE_BEFORE = 0x100; //the first item for a new line or row.
053: protected final static int LAYOUT_NEWLINE_AFTER = 0x200; //next Item on a new line or row.
054:
055: //widthHint
056: protected final static int LAYOUT_SHRINK = 0x400; //width may be reduced to its minimum width
057: protected final static int LAYOUT_EXPAND = 0x800; //width may be increased to fill available space
058:
059: //heightHint
060: protected final static int LAYOUT_VSHRINK = 0x1000; //height may be reduced to its minimum height
061: protected final static int LAYOUT_VEXPAND = 0x2000; //height may be increase to fill available space
062:
063: protected String _label;
064: protected String _align; //left, center, right
065: protected String _valign; //top, middle, bottom
066: protected String _flow; //(change row) first, last, only
067: protected String _heightHint; //shrink, expand
068: protected String _widthHint; //shrink, expand
069:
070: protected int _preferredHeight = -1; //-1 means default
071: protected int _preferredWidth = -1; //-1 means default
072:
073: /**
074: * Get the label of this Item.
075: *
076: * @return the label of this Item.
077: */
078: public String getLabel() {
079: return _label;
080: }
081:
082: /**
083: * Set the new label of this Item.
084: * @param label the new label of this Item.
085: */
086: public void setLabel(String label) {
087: if (label != null && label.length() == 0) {
088: label = null;
089: }
090: if (!Objects.equals(_label, label)) {
091: _label = label;
092: smartUpdate("lb", encodeString(label));
093: }
094: }
095:
096: /**
097: * Get preferred height. -1 means use defualt.
098: * @return the preferred height.
099: */
100: public int getPreferredHeight() {
101: return _preferredHeight;
102: }
103:
104: /**
105: * Set preferred height. -1 means use default
106: * @param preferredHeight the preferred height.
107: */
108: public void setPreferredHeight(int preferredHeight) {
109: if (_preferredHeight != preferredHeight) {
110: _preferredHeight = preferredHeight;
111: smartUpdatePreferredSize();
112: }
113: }
114:
115: /**
116: * Get preferred width. -1 means use default.
117: * @return the preferred width;
118: */
119: public int getPreferredWidth() {
120: return _preferredWidth;
121: }
122:
123: /**
124: * Set preferred width. -1 means use default.
125: * @param preferredWidth the preferred width.
126: */
127: public void setPreferredWidth(int preferredWidth) {
128: if (_preferredWidth != preferredWidth) {
129: _preferredWidth = preferredWidth;
130: smartUpdatePreferredSize();
131: }
132: }
133:
134: private String getPreferredSize() {
135: return "" + _preferredWidth + "," + _preferredHeight;
136: }
137:
138: private void smartUpdatePreferredSize() {
139: smartUpdate("ps", getPreferredSize());
140: }
141:
142: /**
143: * Get the horizontal alignment of this Item (left, center, right).
144: * <ul>
145: * <li>left: align to left.</li>
146: * <li>center: align to center.</li>
147: * <li>right: align to right</li>
148: * </ul>
149: * null means use default alignment.
150: * @return current horizontal alignment setting
151: */
152: public String getAlign() {
153: return _align;
154: }
155:
156: /**
157: * Set the horizontal alignment of this Item (left, center, right);
158: * <ul>
159: * <li>left: align to left.</li>
160: * <li>center: align to center.</li>
161: * <li>right: align to right</li>
162: * </ul>
163: * null means use default alignment.
164: * @param align
165: */
166: public void setAlign(String align) {
167: if (align != null && align.length() == 0) {
168: align = null;
169: }
170: if (!Objects.equals(_align, align)) {
171: _align = align;
172: smartUpdateLayout();
173: }
174: }
175:
176: private int getAlignLayout() {
177: if ("left".equalsIgnoreCase(_align))
178: return LAYOUT_LEFT;
179: else if ("center".equalsIgnoreCase(_align))
180: return LAYOUT_CENTER;
181: else if ("right".equalsIgnoreCase(_align))
182: return LAYOUT_RIGHT;
183: else
184: return LAYOUT_LEFT;
185: }
186:
187: /**
188: * Get the vertical alignment of this Item (top, middle, bottom).
189: * <ul>
190: * <li>top: align to top.</li>
191: * <li>middle: align to middle.</li>
192: * <li>bottom: align to bottom.</li>
193: * </ul>
194: * null means use default vertical alignment.
195: * @return the vertical alignment.
196: */
197: public String getValign() {
198: return _valign;
199: }
200:
201: /**
202: * Set the vertical alignment of this Item (top, middle, bottom).
203: * <ul>
204: * <li>top: align to top.</li>
205: * <li>middle: align to middle.</li>
206: * <li>bottom: align to bottom.</li>
207: * </ul>
208: * null means use default vertical alignment.
209: * @param valign (top, middle, bottom)
210: */
211: public void setValign(String valign) {
212: if (valign != null && valign.length() == 0) {
213: valign = null;
214: }
215: if (!Objects.equals(_valign, valign)) {
216: _valign = valign;
217: smartUpdateLayout();
218: }
219: }
220:
221: private int getValignLayout() {
222: if ("top".equalsIgnoreCase(_valign))
223: return LAYOUT_TOP;
224: else if ("middle".equalsIgnoreCase(_valign))
225: return LAYOUT_VCENTER;
226: else if ("bottom".equalsIgnoreCase(_valign))
227: return LAYOUT_BOTTOM;
228: else
229: return LAYOUT_TOP;
230: }
231:
232: /**
233: * Set the width hint of this Item (shrink, expand).
234: * <ul>
235: * <li>shrink: means width can be reduced to its minimum.</li>
236: * <li>expand: means width can be expanded to available space.</li>
237: * </ul>
238: * null means use default.
239: * @return the current width hint.
240: */
241: public String getWidthHint() {
242: return _widthHint;
243: }
244:
245: /**
246: * Set the width hint of this Item (shrink, expand).
247: * <ul>
248: * <li>shrink: means width can be reduced to its minimum.</li>
249: * <li>expand: means width can be expanded to available space.</li>
250: * </ul>
251: * null means use default.
252: * @param widthHint the current width hint.
253: */
254: public void setWidthHint(String widthHint) {
255: if (widthHint != null && widthHint.length() == 0) {
256: widthHint = null;
257: }
258: if (!Objects.equals(_widthHint, widthHint)) {
259: _widthHint = widthHint;
260: smartUpdateLayout();
261: }
262: }
263:
264: private int getWidthHintLayout() {
265: if ("shrink".equalsIgnoreCase(_widthHint))
266: return LAYOUT_SHRINK;
267: else if ("expand".equalsIgnoreCase(_widthHint))
268: return LAYOUT_EXPAND;
269: else
270: return 0;
271: }
272:
273: /**
274: * Set the hieght hint of this Item (shrink, expand).
275: * <ul>
276: * <li>shrink: means height can be reduced to its minimum.</li>
277: * <li>expand: means height can be expanded to available space.</li>
278: * </ul>
279: * null means use default.
280: * @return the current height hint.
281: */
282: public String getHeightHint() {
283: return _heightHint;
284: }
285:
286: /**
287: * Set the hieght hint of this Item (shrink, expand).
288: * <ul>
289: * <li>shrink: means height can be reduced to its minimum.</li>
290: * <li>expand: means height can be expanded to available space.</li>
291: * </ul>
292: * null means use default.
293: * @param heightHint the current height hint.
294: */
295: public void setHeightHint(String heightHint) {
296: if (heightHint != null && heightHint.length() == 0) {
297: heightHint = null;
298: }
299: if (!Objects.equals(_heightHint, heightHint)) {
300: _heightHint = heightHint;
301: smartUpdateLayout();
302: }
303: }
304:
305: private int getHeightHintLayout() {
306: if ("shrink".equalsIgnoreCase(_heightHint))
307: return LAYOUT_VSHRINK;
308: else if ("expand".equalsIgnoreCase(_heightHint))
309: return LAYOUT_VEXPAND;
310: else
311: return 0;
312: }
313:
314: /**
315: * Get the layout flow of this Item (first, last, only).
316: * <ul>
317: * <li>first: means this Item must be the first in a row.</li>
318: * <li>last: means this Item must be the last in a row.</li>
319: * <li>only: means this Item is the only Item in a row.</li>
320: * </ul>
321: * null means the default flow rule.
322: * @return the layout flow of this Item.
323: */
324: public String getFlow() {
325: return _flow;
326: }
327:
328: /**
329: * Set the layout flow of this Item (first, last, only).
330: * <ul>
331: * <li>first: means this Item must be the first in a row.</li>
332: * <li>last: means this Item must be the last in a row.</li>
333: * <li>only: means this Item is the only Item in a row.</li>
334: * </ul>
335: * null means use the default flow rule.
336: * @param flow the layout flow of this Item.
337: */
338: public void setFlow(String flow) {
339: if (flow != null && flow.length() == 0) {
340: flow = null;
341: }
342: if (!Objects.equals(_flow, flow)) {
343: _flow = flow;
344: smartUpdateLayout();
345: }
346: }
347:
348: //--Component--//
349: public void setParent(Component parent) {
350: if (parent != null && !(parent instanceof Frame)) {
351: throw new UiException("Unsupported parent for item: "
352: + this + ", parent: " + parent);
353: }
354: super .setParent(parent);
355: }
356:
357: public String getInnerAttrs() {
358: final StringBuffer sb = new StringBuffer(64);
359: HTMLs.appendAttribute(sb, "ps", getPreferredSize());
360: HTMLs.appendAttribute(sb, "lb", encodeString(getLabel()));
361: HTMLs.appendAttribute(sb, "lo", getLayout());
362: return sb.toString();
363: }
364:
365: private int getFlowLayout() {
366: if ("first".equalsIgnoreCase(_flow))
367: return LAYOUT_NEWLINE_BEFORE;
368: else if ("last".equalsIgnoreCase(_flow))
369: return LAYOUT_NEWLINE_AFTER;
370: else if ("only".equalsIgnoreCase(_flow))
371: return (LAYOUT_NEWLINE_BEFORE | LAYOUT_NEWLINE_AFTER);
372: else
373: return 0;
374: }
375:
376: private int getLayout() {
377: return LAYOUT_2 | getAlignLayout() | getValignLayout()
378: | getWidthHintLayout() | getHeightHintLayout()
379: | getFlowLayout();
380: }
381:
382: private void smartUpdateLayout() {
383: smartUpdate("lo", getLayout());
384: }
385: }
|