Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 1 | // |
| 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 Tkac | f53e62a | 2009-03-13 13:20:26 +0000 | [diff] [blame] | 21 | package com.tigervnc.vncviewer; |
Constantin Kaplinsky | 90d8a50 | 2008-04-14 09:45:50 +0000 | [diff] [blame] | 22 | |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 23 | import java.awt.*; |
| 24 | import java.awt.event.*; |
| 25 | |
| 26 | // |
| 27 | // The panel which implements the user authentication scheme |
| 28 | // |
| 29 | |
| 30 | class AuthPanel extends Panel implements ActionListener { |
| 31 | |
| 32 | TextField passwordField; |
| 33 | Button okButton; |
Adam Tkac | 71def36 | 2010-11-11 13:25:14 +0000 | [diff] [blame^] | 34 | boolean AskPassword; |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 35 | |
| 36 | // |
| 37 | // Constructor. |
| 38 | // |
| 39 | |
Adam Tkac | 71def36 | 2010-11-11 13:25:14 +0000 | [diff] [blame^] | 40 | public AuthPanel(VncViewer viewer, boolean askpassword) |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 41 | { |
Adam Tkac | 71def36 | 2010-11-11 13:25:14 +0000 | [diff] [blame^] | 42 | AskPassword = askpassword; |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 43 | Label titleLabel = new Label("VNC Authentication", Label.CENTER); |
| 44 | titleLabel.setFont(new Font("Helvetica", Font.BOLD, 18)); |
| 45 | |
Adam Tkac | 71def36 | 2010-11-11 13:25:14 +0000 | [diff] [blame^] | 46 | Label promptLabel; |
| 47 | if (AskPassword) |
| 48 | promptLabel = new Label("Password:", Label.CENTER); |
| 49 | else |
| 50 | promptLabel = new Label("User:", Label.CENTER); |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 51 | |
| 52 | passwordField = new TextField(10); |
| 53 | passwordField.setForeground(Color.black); |
| 54 | passwordField.setBackground(Color.white); |
Adam Tkac | 71def36 | 2010-11-11 13:25:14 +0000 | [diff] [blame^] | 55 | if (AskPassword) |
| 56 | passwordField.setEchoChar('*'); |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 57 | |
| 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 | } |