James Moger
2012-11-25 c8b26c51aa67fc9345b624e36aab6f819e7eed74
commit | author | age
4ad1eb 1 /*
JM 2  * Copyright 2012 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.authority;
17
18 import java.awt.BorderLayout;
19 import java.awt.Frame;
20 import java.awt.GridLayout;
21 import java.awt.Insets;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.util.Date;
25
26 import javax.swing.JButton;
27 import javax.swing.JCheckBox;
28 import javax.swing.JDialog;
29 import javax.swing.JLabel;
30 import javax.swing.JOptionPane;
31 import javax.swing.JPanel;
32 import javax.swing.JPasswordField;
33 import javax.swing.JTextField;
34
35 import org.bouncycastle.util.Arrays;
36
37 import com.gitblit.client.HeaderPanel;
38 import com.gitblit.client.Translation;
39 import com.gitblit.utils.StringUtils;
40 import com.toedter.calendar.JDateChooser;
41
42 public class NewClientCertificateDialog extends JDialog {
43
44     private static final long serialVersionUID = 1L;
45     
46     JDateChooser expirationDate;
47     JPasswordField pw1;
48     JPasswordField pw2;
49     JTextField hint;
50     JCheckBox sendEmail;
51     boolean isCanceled = true;
52
c8b26c 53     public NewClientCertificateDialog(Frame owner, String displayname, Date defaultExpiration, boolean allowEmail) {
4ad1eb 54         super(owner);
JM 55         
56         setTitle(Translation.get("gb.newCertificate"));
57         
c8b26c 58         JPanel content = new JPanel(new BorderLayout(Utils.MARGIN, Utils.MARGIN)) {            
4ad1eb 59             private static final long serialVersionUID = 1L;
JM 60
61             @Override
62             public Insets getInsets() {
63                 
64                 return Utils.INSETS;
65             }
66         };
67         content.add(new HeaderPanel(Translation.get("gb.newCertificate") + " (" + displayname + ")", "rosette_16x16.png"), BorderLayout.NORTH);
68         
69         expirationDate = new JDateChooser(defaultExpiration);
70         pw1 = new JPasswordField(20);
71         pw2 = new JPasswordField(20);
72         hint = new JTextField(20);
73         sendEmail = new JCheckBox(Translation.get("gb.sendEmail"));
74         
c8b26c 75         JPanel panel = new JPanel(new GridLayout(0, 2, Utils.MARGIN, Utils.MARGIN));
4ad1eb 76         
JM 77         panel.add(new JLabel(Translation.get("gb.expires")));
78         panel.add(expirationDate);
79         
80         panel.add(new JLabel(Translation.get("gb.password")));
81         panel.add(pw1);
82
83         panel.add(new JLabel(Translation.get("gb.confirmPassword")));
84         panel.add(pw2);
85         
86         panel.add(new JLabel(Translation.get("gb.passwordHint")));
87         panel.add(hint);
88         
c8b26c 89         if (allowEmail) {
JM 90             panel.add(new JLabel(""));
91             panel.add(sendEmail);
92         }
4ad1eb 93
JM 94         content.add(panel, BorderLayout.CENTER);
95         
96         JButton ok = new JButton(Translation.get("gb.ok"));
97         ok.addActionListener(new ActionListener() {
98             public void actionPerformed(ActionEvent e) {
99                 if (validateInputs()) {
100                     isCanceled = false;
101                     setVisible(false);
102                 }
103             }
104         });
105         JButton cancel = new JButton(Translation.get("gb.cancel"));
106         cancel.addActionListener(new ActionListener() {
107             public void actionPerformed(ActionEvent e) {
108                 isCanceled = true;
109                 setVisible(false);
110             }
111         });
112         
113         JPanel controls = new JPanel();
114         controls.add(ok);
115         controls.add(cancel);
116         
117         content.add(controls, BorderLayout.SOUTH);
118         
119         getContentPane().add(content, BorderLayout.CENTER);
120         pack();
121         
122         setLocationRelativeTo(owner);
123     }
124     
125     private boolean validateInputs() {
126         if (getExpiration().getTime() < System.currentTimeMillis()) {
127             // expires before now
c8b26c 128             JOptionPane.showMessageDialog(this, Translation.get("gb.invalidExpirationDate"),
4ad1eb 129                     Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);
JM 130             return false;
131         }
132         if (pw1.getPassword().length == 0 || !Arrays.areEqual(pw1.getPassword(), pw2.getPassword())) {
133             // password mismatch
134             JOptionPane.showMessageDialog(this, Translation.get("gb.passwordsDoNotMatch"),
135                     Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);
136             return false;
137         }
138         if (StringUtils.isEmpty(getPasswordHint())) {
139             // must have hint
140             JOptionPane.showMessageDialog(this, Translation.get("gb.passwordHintRequired"),
141                     Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);
142             return false;
143         }
144         return true;
145     }
146     
147     public String getPassword() {
148         return new String(pw1.getPassword());
149     }
150     
151     public String getPasswordHint() {
152         return hint.getText();
153     }
154     
155     public Date getExpiration() {
156         return expirationDate.getDate();
157     }
158     
159     public boolean sendEmail() {
160         return sendEmail.isSelected();
161     }
162     
163     public boolean isCanceled() {
164         return isCanceled;
165     }
166 }