001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.auth.callback;
019:
020: import java.awt.Button;
021: import java.awt.Component;
022: import java.awt.Dialog;
023: import java.awt.Frame;
024: import java.awt.GridLayout;
025: import java.awt.Label;
026: import java.awt.TextField;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.awt.event.WindowAdapter;
030: import java.awt.event.WindowEvent;
031: import java.io.IOException;
032:
033: import javax.security.auth.callback.Callback;
034: import javax.security.auth.callback.CallbackHandler;
035: import javax.security.auth.callback.NameCallback;
036: import javax.security.auth.callback.PasswordCallback;
037: import javax.security.auth.callback.UnsupportedCallbackException;
038:
039: public class DialogCallbackHandler implements CallbackHandler {
040:
041: public DialogCallbackHandler() {
042:
043: }
044:
045: public DialogCallbackHandler(Component parentComponent) {
046: }
047:
048: private static class DialogCallbackHandlerDialoge extends Dialog {
049:
050: private static final long serialVersionUID = -598231925400058563L;
051:
052: private Label nameLabel = new Label();
053:
054: private Label passwordLabel = new Label();
055:
056: private TextField nameText = new TextField("", 16);
057:
058: private TextField passwordText = new TextField("", 20);
059:
060: private Button okButton = new Button("OK");
061:
062: private Button cancelButton = new Button("Cancel");
063:
064: private NameCallback nameCallback;
065:
066: private PasswordCallback passwordCallback;
067:
068: public void exitDialog() {
069: this .dispose();
070: }
071:
072: public DialogCallbackHandlerDialoge(Callback[] callbacks)
073: throws UnsupportedCallbackException {
074:
075: super ((Frame) null, "Confirmation");
076: this .setResizable(true);
077: this .setBounds(320, 250, 350, 120);
078: int layoutColumn = 1;
079: okButton.addActionListener(new ActionListener() {
080: public void actionPerformed(ActionEvent e) {
081: if (nameCallback != null)
082: nameCallback.setName(nameText.getText());
083: if (passwordCallback != null)
084: passwordCallback.setPassword(passwordText
085: .getText().toCharArray());
086: exitDialog();
087: }
088: });
089:
090: cancelButton.addActionListener(new ActionListener() {
091: public void actionPerformed(ActionEvent e) {
092: exitDialog();
093: }
094: });
095:
096: for (int i = 0; i < callbacks.length; i++) {
097: if (callbacks[i] instanceof NameCallback) {
098: nameCallback = (NameCallback) callbacks[i];
099: nameLabel.setText(nameCallback.getPrompt());
100: this .add(nameLabel);
101: this .add(nameText);
102: layoutColumn++;
103: } else if (callbacks[i] instanceof PasswordCallback) {
104: passwordCallback = (PasswordCallback) callbacks[i];
105: passwordLabel.setText(passwordCallback.getPrompt());
106: this .add(passwordLabel);
107: this .add(passwordText);
108: layoutColumn++;
109: } else {
110: throw new UnsupportedCallbackException(callbacks[i]);
111: }
112: }
113: passwordText.setEchoChar('*');
114: this .add(okButton);
115: this .add(cancelButton);
116: this .setModal(true);
117: this .addWindowListener(new WindowAdapter() {
118: public void windowClosing(WindowEvent e) {
119:
120: dispose();
121: }
122: });
123: this .setLayout(new GridLayout(layoutColumn, 1));
124: }
125: }
126:
127: public void handle(Callback[] callbacks) throws IOException,
128: UnsupportedCallbackException {
129: Dialog df = new DialogCallbackHandlerDialoge(callbacks);
130: df.setModal(true);
131: df.setVisible(true);
132: }
133: }
|