001: /* MessageboxDlg.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed Aug 17 16:42:20 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 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: package org.zkoss.zul.impl;
020:
021: import org.zkoss.mesg.Messages;
022: import org.zkoss.zul.mesg.MZul;
023:
024: import org.zkoss.zk.ui.UiException;
025:
026: import org.zkoss.zul.Window;
027: import org.zkoss.zul.Messagebox;
028:
029: /**
030: * Used with {@link Messagebox} to implement a message box.
031: *
032: * @author tomyeh
033: */
034: public class MessageboxDlg extends Window {
035: /** A OK button. */
036: public static final int OK = Messagebox.OK;
037: /** A Cancel button. */
038: public static final int CANCEL = Messagebox.CANCEL;
039: /** A Yes button. */
040: public static final int YES = Messagebox.YES;
041: /** A No button. */
042: public static final int NO = Messagebox.NO;
043: /** A Abort button. */
044: public static final int ABORT = Messagebox.ABORT;
045: /** A Retry button. */
046: public static final int RETRY = Messagebox.RETRY;
047: /** A IGNORE button. */
048: public static final int IGNORE = Messagebox.IGNORE;
049:
050: /** What buttons are allowed. */
051: private int _buttons;
052: /** Which button is pressed. */
053: private int _result;
054:
055: public void onOK() {
056: if ((_buttons & OK) != 0)
057: endModal(OK);
058: else if ((_buttons & YES) != 0)
059: endModal(YES);
060: else if ((_buttons & RETRY) != 0)
061: endModal(RETRY);
062: }
063:
064: public void onCancel() {
065: if (_buttons == OK)
066: endModal(OK);
067: else if ((_buttons & CANCEL) != 0)
068: endModal(CANCEL);
069: else if ((_buttons & NO) != 0)
070: endModal(NO);
071: else if ((_buttons & ABORT) != 0)
072: endModal(ABORT);
073: }
074:
075: /** Sets what buttons are allowed. */
076: public void setButtons(int buttons) {
077: _buttons = buttons;
078: }
079:
080: /** Sets the focus.
081: * @param button the button to gain the focus. If 0, the default one
082: * (i.e., the first one) is assumed.
083: * @since 3.0.0
084: */
085: public void setFocus(int button) {
086: if (button > 0) {
087: final Button btn = (Button) getFellowIfAny("btn" + button);
088: if (btn != null)
089: btn.focus();
090: }
091: }
092:
093: /** Called only internally.
094: */
095: public void endModal(int button) {
096: _result = button;
097: detach();
098: }
099:
100: /** Returns the result which is the button being pressed.
101: */
102: public int getResult() {
103: return _result;
104: }
105:
106: /**
107: * Represents a button on the message box.
108: * @since 3.0.0
109: */
110: public static class Button extends org.zkoss.zul.Button {
111: private int _button;
112:
113: /** Sets the identity.
114: */
115: public void setIdentity(int button) {
116: _button = button;
117:
118: final int label;
119: switch (button) {
120: case YES:
121: label = MZul.YES;
122: break;
123: case NO:
124: label = MZul.NO;
125: break;
126: case RETRY:
127: label = MZul.RETRY;
128: break;
129: case ABORT:
130: label = MZul.ABORT;
131: break;
132: case IGNORE:
133: label = MZul.IGNORE;
134: break;
135: case CANCEL:
136: label = MZul.CANCEL;
137: break;
138: default:
139: label = MZul.OK;
140: }
141: setLabel(Messages.get(label));
142: setId("btn" + _button);
143: }
144:
145: public void onClick() {
146: ((MessageboxDlg) getSpaceOwner()).endModal(_button);
147: }
148: }
149: }
|