blob: a23758e4ba9dad05aea882b77bca36830c48163e [file] [log] [blame]
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +00001//
2// Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
3// Copyright (C) 2002-2006 Constantin Kaplinsky. All Rights Reserved.
4//
5// This is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This software is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this software; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18// USA.
19//
20
Adam Tkacf53e62a2009-03-13 13:20:26 +000021package com.tigervnc.vncviewer;
Constantin Kaplinsky90d8a502008-04-14 09:45:50 +000022
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +000023import java.awt.*;
24import java.awt.event.*;
25
26//
27// The panel which implements the user authentication scheme
28//
29
30class AuthPanel extends Panel implements ActionListener {
31
32 TextField passwordField;
33 Button okButton;
Adam Tkac71def362010-11-11 13:25:14 +000034 boolean AskPassword;
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +000035
36 //
37 // Constructor.
38 //
39
Adam Tkac71def362010-11-11 13:25:14 +000040 public AuthPanel(VncViewer viewer, boolean askpassword)
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +000041 {
Adam Tkac71def362010-11-11 13:25:14 +000042 AskPassword = askpassword;
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +000043 Label titleLabel = new Label("VNC Authentication", Label.CENTER);
44 titleLabel.setFont(new Font("Helvetica", Font.BOLD, 18));
45
Adam Tkac71def362010-11-11 13:25:14 +000046 Label promptLabel;
47 if (AskPassword)
48 promptLabel = new Label("Password:", Label.CENTER);
49 else
50 promptLabel = new Label("User:", Label.CENTER);
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +000051
52 passwordField = new TextField(10);
53 passwordField.setForeground(Color.black);
54 passwordField.setBackground(Color.white);
Adam Tkac71def362010-11-11 13:25:14 +000055 if (AskPassword)
56 passwordField.setEchoChar('*');
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +000057
58 okButton = new Button("OK");
59
60 GridBagLayout gridbag = new GridBagLayout();
61 GridBagConstraints gbc = new GridBagConstraints();
62
63 setLayout(gridbag);
64
65 gbc.gridwidth = GridBagConstraints.REMAINDER;
66 gbc.insets = new Insets(0,0,20,0);
67 gridbag.setConstraints(titleLabel,gbc);
68 add(titleLabel);
69
70 gbc.fill = GridBagConstraints.NONE;
71 gbc.gridwidth = 1;
72 gbc.insets = new Insets(0,0,0,0);
73 gridbag.setConstraints(promptLabel,gbc);
74 add(promptLabel);
75
76 gridbag.setConstraints(passwordField,gbc);
77 add(passwordField);
78 passwordField.addActionListener(this);
79
80 // gbc.ipady = 10;
81 gbc.gridwidth = GridBagConstraints.REMAINDER;
82 gbc.fill = GridBagConstraints.BOTH;
83 gbc.insets = new Insets(0,20,0,0);
84 gbc.ipadx = 30;
85 gridbag.setConstraints(okButton,gbc);
86 add(okButton);
87 okButton.addActionListener(this);
88 }
89
90 //
91 // Move keyboard focus to the default object, that is, the password
92 // text field.
93 //
94
95 public void moveFocusToDefaultField()
96 {
97 passwordField.requestFocus();
98 }
99
100 //
101 // This method is called when a button is pressed or return is
102 // pressed in the password text field.
103 //
104
105 public synchronized void actionPerformed(ActionEvent evt)
106 {
107 if (evt.getSource() == passwordField || evt.getSource() == okButton) {
108 passwordField.setEnabled(false);
109 notify();
110 }
111 }
112
113 //
114 // Wait for user entering a password, and return it as String.
115 //
116
117 public synchronized String getPassword() throws Exception
118 {
119 try {
120 wait();
121 } catch (InterruptedException e) { }
122 return passwordField.getText();
123 }
124
125}