001: /*
002: *
003: *
004: * Copyright 1990-2007 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.satsa.pki;
028:
029: import com.sun.midp.i18n.Resource;
030: import com.sun.midp.i18n.ResourceConstants;
031:
032: import com.sun.midp.security.SecurityToken;
033:
034: import javax.microedition.lcdui.*;
035:
036: /**
037: * Provides methods for simple messages and requests.
038: */
039: public class MessageDialog {
040:
041: /**
042: * Displays dialog with informational message or confirmation
043: * request.
044: * @param token security token.
045: * @param title dialog title
046: * @param message message text
047: * @param withCancel show Cancel button
048: * @return -1 if user cancelled dialog, 1 otherwise
049: * @throws InterruptedException if interrupted
050: */
051: public static int showMessage(SecurityToken token, String title,
052: String message, boolean withCancel)
053: throws InterruptedException {
054:
055: Dialog d = new Dialog(title, withCancel);
056: d.append(new StringItem(message, null));
057: return d.waitForAnswer(token);
058: }
059:
060: /**
061: * Displays dialog that allows user to select one element
062: * from the list.
063: * @param token security token.
064: * @param title dialog title
065: * @param label list label
066: * @param list elements of the list
067: * @return -1 if user cancelled dialog, index of chosen item
068: * otherwise
069: * @throws InterruptedException if interrupted
070: */
071: public static int chooseItem(SecurityToken token, String title,
072: String label, String[] list) throws InterruptedException {
073:
074: Dialog d = new Dialog(title, true);
075: ChoiceGroup choice = new ChoiceGroup(label, Choice.EXCLUSIVE,
076: list, null);
077: d.append(choice);
078: return d.waitForAnswer(token) == Dialog.CANCELLED ? -1 : choice
079: .getSelectedIndex();
080: }
081:
082: /**
083: * Displays dialog with new PIN parameters.
084: * @param token security token.
085: * @return array of strings or null if cancelled. Array contains new
086: * PIN label and PIN value.
087: * @throws InterruptedException if interrupted
088: */
089: public static String[] enterNewPIN(SecurityToken token)
090: throws InterruptedException {
091:
092: Dialog d = new Dialog(
093: // "New PIN",
094: Resource
095: .getString(ResourceConstants.JSR177_PINDIALOG_TITLE_NEWPIN),
096: true);
097:
098: TextField label = new TextField(
099: // "PIN label ",
100: Resource
101: .getString(ResourceConstants.JSR177_PINDIALOG_LABEL)
102: + " ", "", 32, 0);
103: TextField pin1 = new TextField(
104: // "Enter PIN ",
105: Resource
106: .getString(ResourceConstants.JSR177_PINDIALOG_ENTERPIN)
107: + " ", "", 8, TextField.PASSWORD
108: | TextField.NUMERIC);
109: TextField pin2 = new TextField(
110: // "Confirm PIN ",
111: Resource
112: .getString(ResourceConstants.JSR177_PINDIALOG_CONFIRMPIN)
113: + " ", "", 8, TextField.PASSWORD
114: | TextField.NUMERIC);
115:
116: d.append(label);
117: d.append(pin1);
118: d.append(pin2);
119:
120: while (true) {
121: if (d.waitForAnswer(token) == -1) {
122: return null;
123: }
124:
125: String s = label.getString().trim();
126: if (s.equals("")) {
127: continue;
128: }
129:
130: String h1 = pin1.getString().trim();
131: String h2 = pin2.getString().trim();
132:
133: if (h1.equals("") || !h1.equals(h2) || h1.length() < 4) {
134: pin1.setString("");
135: pin2.setString("");
136: continue;
137: }
138: return new String[] { s, h1 };
139: }
140: }
141: }
|