001: /*
002: *
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.j2me.dialog;
028:
029: import com.sun.j2me.i18n.Resource;
030: import com.sun.j2me.i18n.ResourceConstants;
031:
032: import java.io.IOException;
033:
034: /**
035: * This class represents simple dialog form.
036: */
037: public class Dialog {
038:
039: /** Answer that indicates that the dialog was cancelled. */
040: public static final int CANCELLED = -1;
041: /** Answer that indicates successful completion. */
042: public static final int CONFIRMED = 1;
043:
044: /* Confirmation string. */
045: private String okCmd = ":o";//Resource.getString(ResourceConstants.OK);
046:
047: /* "Cancel" string. */
048: private String cancelCmd = ":q";//Resource.getString(ResourceConstants.CANCEL);
049:
050: private boolean withCancel;
051:
052: /**
053: * Construct message dialog for informational message or
054: * confirmation request.
055: * @param title dialog title
056: * @param withCancel show Cancel button
057: */
058: public Dialog(String title, boolean withCancel) {
059: System.out.println();
060: System.out.println(title);
061: System.out.println("Print '" + okCmd
062: + "' for operation confirmation");
063: if (withCancel) {
064: System.out.println("Print '" + cancelCmd
065: + "' for operation cancellation");
066: this .withCancel = withCancel;
067: }
068: System.out.println();
069: }
070:
071: /**
072: * Adds an Item into the Form.
073: * @return input string
074: */
075: String append() {
076: String strInput = "";
077:
078: while (true) {
079: try {
080: int tmp = System.in.read();
081: if (tmp == -1)
082: break;
083: strInput = strInput + (char) tmp;
084: } catch (IOException e) {
085: }
086: }
087:
088: System.out.println();
089: return strInput;
090: }
091:
092: /**
093: * Waits for the user's answer.
094: * @return user's answer
095: */
096: public int waitForAnswer() {
097: if (withCancel) {
098: while (true) {
099: String strInput = "";
100: System.out.println("Please enter result of operation: "
101: + okCmd + "/" + cancelCmd);
102: strInput = append();
103: if (strInput.equals(okCmd)) {
104: return CONFIRMED;
105: }
106: if (strInput.equals(cancelCmd)) {
107: return CANCELLED;
108: }
109: System.out.println("Result is invalid");
110: }
111: } else {
112: return CONFIRMED;
113: }
114: }
115: }
|