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.midlet.MIDletEventConsumer;
030: import com.sun.midp.lcdui.DisplayEventHandler;
031: import com.sun.midp.lcdui.DisplayEventHandlerFactory;
032: import com.sun.midp.i18n.Resource;
033: import com.sun.midp.i18n.ResourceConstants;
034: import com.sun.midp.security.SecurityToken;
035:
036: import com.sun.midp.midlet.MIDletPeer;
037:
038: import javax.microedition.lcdui.*;
039:
040: /**
041: * This class represents simple dialog form.
042: */
043: class Dialog implements CommandListener {
044:
045: /** Answer that indicates that the dialog was cancelled. */
046: public static final int CANCELLED = -1;
047: /** Answer that indicates successful completion. */
048: public static final int CONFIRMED = 1;
049:
050: /** Caches the display manager reference. */
051: private DisplayEventHandler displayEventHandler;
052:
053: /** Command object for "OK" command. */
054: private Command okCmd = new Command(Resource
055: .getString(ResourceConstants.OK), Command.OK, 1);
056:
057: /** Command object for "Cancel" command. */
058: private Command cancelCmd = new Command(Resource
059: .getString(ResourceConstants.CANCEL), Command.CANCEL, 1);
060:
061: /** Holds the preempt token so the form can end. */
062: private Object preemptToken;
063:
064: /** Holds the answer to the security question. */
065: private int answer = CANCELLED;
066:
067: /** Form object for this dialog. */
068: private Form form;
069:
070: /**
071: * Construct message dialog for informational message or
072: * confirmation request.
073: * @param title dialog title
074: * @param withCancel show Cancel button
075: */
076: Dialog(String title, boolean withCancel) {
077:
078: form = new Form(title);
079: form.addCommand(okCmd);
080: if (withCancel) {
081: form.addCommand(cancelCmd);
082: }
083: form.setCommandListener(this );
084: }
085:
086: /**
087: * Adds an Item into the Form.
088: * @param item the Item to be added
089: */
090: void append(Item item) {
091: form.append(item);
092: }
093:
094: /**
095: * Waits for the user's answer.
096: * @param token security token.
097: * @return user's answer
098: * @throws InterruptedException if interrupted
099: */
100: int waitForAnswer(SecurityToken token) throws InterruptedException {
101: if (displayEventHandler == null) {
102: displayEventHandler = DisplayEventHandlerFactory
103: .getDisplayEventHandler(token);
104: }
105: preemptToken = displayEventHandler.preemptDisplay(form, true);
106:
107: synchronized (this ) {
108: if (preemptToken == null) {
109: return CANCELLED;
110: }
111:
112: try {
113: wait();
114: } catch (Throwable t) {
115: return CANCELLED;
116: }
117:
118: return answer;
119: }
120: }
121:
122: /**
123: * Respond to a command issued on form.
124: * Sets the user's answer and notifies waitForAnswer and
125: * ends the form.
126: * @param c command activiated by the user
127: * @param s the Displayable the command was on.
128: */
129: public void commandAction(Command c, Displayable s) {
130: synchronized (this) {
131: answer = c == okCmd ? CONFIRMED : CANCELLED;
132: displayEventHandler.donePreempting(preemptToken);
133: notify();
134: }
135: }
136: }
|