1 | package br.gov.serpro.ui; |
---|
2 | |
---|
3 | import br.gov.serpro.setup.Setup; |
---|
4 | import java.awt.BorderLayout; |
---|
5 | import java.awt.Dimension; |
---|
6 | import java.awt.FlowLayout; |
---|
7 | import java.awt.Frame; |
---|
8 | import java.awt.Toolkit; |
---|
9 | import java.awt.event.ActionEvent; |
---|
10 | import java.awt.event.ActionListener; |
---|
11 | import java.awt.event.KeyEvent; |
---|
12 | import java.awt.event.KeyListener; |
---|
13 | import java.awt.event.WindowEvent; |
---|
14 | import java.awt.event.WindowListener; |
---|
15 | import java.beans.PropertyChangeEvent; |
---|
16 | import java.beans.PropertyChangeListener; |
---|
17 | import java.lang.reflect.InvocationTargetException; |
---|
18 | |
---|
19 | import java.util.ArrayList; |
---|
20 | import java.util.List; |
---|
21 | import java.util.Map; |
---|
22 | import java.util.Set; |
---|
23 | import javax.swing.BorderFactory; |
---|
24 | import javax.swing.JButton; |
---|
25 | import javax.swing.JDialog; |
---|
26 | import javax.swing.JLabel; |
---|
27 | import javax.swing.JList; |
---|
28 | import javax.swing.JOptionPane; |
---|
29 | import javax.swing.JPanel; |
---|
30 | import javax.swing.JPasswordField; |
---|
31 | import javax.swing.JScrollPane; |
---|
32 | import javax.swing.ListSelectionModel; |
---|
33 | import javax.swing.SwingUtilities; |
---|
34 | import javax.swing.WindowConstants; |
---|
35 | |
---|
36 | public final class DialogBuilder extends JDialog implements PropertyChangeListener { |
---|
37 | |
---|
38 | private Setup setup; |
---|
39 | private static Object lock = new Object(); |
---|
40 | private String pin = null; |
---|
41 | private String certificateSubject = null; |
---|
42 | //private List<String> certificatelist = new ArrayList<String>(); |
---|
43 | private boolean ok = false; |
---|
44 | private boolean locked = true; |
---|
45 | private JOptionPane optionPane; |
---|
46 | // private DialogBuilder myself; |
---|
47 | private EventManager em = new EventManager(); |
---|
48 | /** |
---|
49 | * Gerado automaticamente |
---|
50 | */ |
---|
51 | private static final long serialVersionUID = 1003857725229120014L; |
---|
52 | private int dialogType = -1; |
---|
53 | //JPanel pNorte, pCentro, pSul; |
---|
54 | //JButton btOk, btCancel; |
---|
55 | //JLabel lTitle, lTitle2, lPin; |
---|
56 | JPasswordField pfPin; |
---|
57 | JList lCertificatesList; |
---|
58 | public static final int PIN_NEEDED_DIALOG = 0; |
---|
59 | public static final int CERTIFICATE_SELECTOR_DIALOG = 1; |
---|
60 | |
---|
61 | private DialogBuilder(Frame parent, Setup setup) { |
---|
62 | |
---|
63 | super(parent, true); |
---|
64 | this.setup = setup; |
---|
65 | this.setResizable(false); |
---|
66 | // this.myself = this; |
---|
67 | |
---|
68 | //this.setVisible(true); |
---|
69 | |
---|
70 | } |
---|
71 | |
---|
72 | private void buildPinDialog() { |
---|
73 | |
---|
74 | this.dialogType = DialogBuilder.PIN_NEEDED_DIALOG; |
---|
75 | |
---|
76 | this.setContentPane(new JPanel()); |
---|
77 | this.setLayout(new BorderLayout()); |
---|
78 | this.setTitle(this.setup.getLang("ExpressoCertMessages", "pin")); |
---|
79 | this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
---|
80 | this.setModal(true); |
---|
81 | |
---|
82 | JPanel pNorte = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
---|
83 | this.add(pNorte, BorderLayout.NORTH); |
---|
84 | JPanel pCentro = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
---|
85 | this.add(pCentro, BorderLayout.CENTER); |
---|
86 | JPanel pSul = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
---|
87 | this.add(pSul, BorderLayout.SOUTH); |
---|
88 | |
---|
89 | //JLabel lTitle = new JLabel("Entre com o seu PIN:"); |
---|
90 | JLabel lTitle = new JLabel(this.setup.getLang("ExpressoCertMessages", "DialogBuilder001")); |
---|
91 | pNorte.add(lTitle); |
---|
92 | |
---|
93 | JLabel lPin = new JLabel(this.setup.getLang("ExpressoCertMessages", "pin") + ":"); |
---|
94 | this.pfPin = new JPasswordField(30); |
---|
95 | this.pfPin.requestFocusInWindow(); |
---|
96 | this.pfPin.addKeyListener(this.em); |
---|
97 | pCentro.add(lPin); |
---|
98 | pCentro.add(this.pfPin); |
---|
99 | |
---|
100 | JButton btOk = new JButton(this.setup.getLang("ExpressoCertMessages", "ok")); |
---|
101 | btOk.setMnemonic(KeyEvent.VK_ENTER); |
---|
102 | btOk.setActionCommand("ok"); |
---|
103 | |
---|
104 | btOk.addActionListener(this.em); |
---|
105 | |
---|
106 | JButton btCancel = new JButton(this.setup.getLang("ExpressoCertMessages", "cancel")); |
---|
107 | btCancel.setMnemonic(KeyEvent.VK_ESCAPE); |
---|
108 | btCancel.setActionCommand("cancel"); |
---|
109 | btCancel.addActionListener(this.em); |
---|
110 | |
---|
111 | pSul.add(btOk); |
---|
112 | pSul.add(btCancel); |
---|
113 | |
---|
114 | this.addWindowListener(this.em); |
---|
115 | this.pack(); |
---|
116 | |
---|
117 | //Posicionando no centro da tela. |
---|
118 | Dimension mySize = this.getSize(); |
---|
119 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
---|
120 | //System.out.println("ScreenSize: " + screenSize.toString()+"\nMySize: " +mySize.toString()); |
---|
121 | this.setLocation(screenSize.width / 2 - (mySize.width / 2), screenSize.height / 2 - (mySize.height / 2)); |
---|
122 | |
---|
123 | setVisible(true); |
---|
124 | //this.repaint(); |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | private void buildCertificateSelector(List<String> subjectList) { |
---|
129 | |
---|
130 | dialogType = DialogBuilder.CERTIFICATE_SELECTOR_DIALOG; |
---|
131 | |
---|
132 | this.setContentPane(new JPanel()); |
---|
133 | this.setLayout(new BorderLayout()); |
---|
134 | this.setTitle(this.setup.getLang("ExpressoCertMessages", "certificate")); |
---|
135 | this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
---|
136 | this.setModal(true); |
---|
137 | |
---|
138 | JPanel pNorte = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
---|
139 | this.add(pNorte, BorderLayout.NORTH); |
---|
140 | JPanel pCentro = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
---|
141 | this.add(pCentro, BorderLayout.CENTER); |
---|
142 | JPanel pSul = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
---|
143 | this.add(pSul, BorderLayout.SOUTH); |
---|
144 | |
---|
145 | JLabel lTitle = new JLabel(this.setup.getLang("ExpressoCertMessages", "DialogBuilder002")); |
---|
146 | pNorte.add(lTitle); |
---|
147 | |
---|
148 | lCertificatesList = new JList(subjectList.toArray()); |
---|
149 | lCertificatesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
---|
150 | lCertificatesList.setLayoutOrientation(JList.HORIZONTAL_WRAP); |
---|
151 | lCertificatesList.setBorder(BorderFactory.createLoweredBevelBorder()); |
---|
152 | JScrollPane listScroller = new JScrollPane(lCertificatesList); |
---|
153 | listScroller.setPreferredSize(new Dimension(500, 80)); |
---|
154 | listScroller.setAlignmentX(LEFT_ALIGNMENT); |
---|
155 | |
---|
156 | pCentro.add(listScroller); |
---|
157 | //pCentro.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); |
---|
158 | |
---|
159 | JButton bSelect = new JButton(this.setup.getLang("ExpressoCertMessages", "select")); |
---|
160 | bSelect.setMnemonic(KeyEvent.VK_ENTER); |
---|
161 | bSelect.setActionCommand("select"); |
---|
162 | bSelect.addActionListener(em); |
---|
163 | pSul.add(bSelect); |
---|
164 | |
---|
165 | JButton btCancel = new JButton(this.setup.getLang("ExpressoCertMessages", "cancel")); |
---|
166 | btCancel.setMnemonic(KeyEvent.VK_ESCAPE); |
---|
167 | btCancel.setActionCommand("cancel"); |
---|
168 | btCancel.addActionListener(this.em); |
---|
169 | pSul.add(btCancel); |
---|
170 | |
---|
171 | this.addWindowListener(em); |
---|
172 | this.pack(); |
---|
173 | |
---|
174 | //Posicionando no centro da tela. |
---|
175 | Dimension mySize = this.getSize(); |
---|
176 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
---|
177 | //System.out.println("ScreenSize: " + screenSize.toString()+"\nMySize: " +mySize.toString()); |
---|
178 | this.setLocation(screenSize.width / 2 - (mySize.width / 2), screenSize.height / 2 - (mySize.height / 2)); |
---|
179 | |
---|
180 | this.setVisible(true); |
---|
181 | |
---|
182 | } |
---|
183 | |
---|
184 | // TODO: Lançar e implementar exceção ActionCanceled |
---|
185 | private void cancelButtonActionPerformed() { |
---|
186 | this.setVisible(false); |
---|
187 | this.pin = null; // hack para saber que diálogo foi cancelado. |
---|
188 | this.certificateSubject = null; |
---|
189 | this.unlock(); |
---|
190 | //this.dispose(); |
---|
191 | } |
---|
192 | |
---|
193 | private void okButtonActionPerformed() { |
---|
194 | this.setVisible(false); |
---|
195 | this.unlock(); |
---|
196 | //this.dispose(); |
---|
197 | } |
---|
198 | |
---|
199 | protected String getPin() { |
---|
200 | return this.pin; |
---|
201 | } |
---|
202 | |
---|
203 | protected void setPin(String pin) { |
---|
204 | this.pin = pin; |
---|
205 | } |
---|
206 | |
---|
207 | protected String getCertificateSubject() { |
---|
208 | return certificateSubject; |
---|
209 | } |
---|
210 | |
---|
211 | protected void setCertificateSubject(String certificateSubject) { |
---|
212 | this.certificateSubject = certificateSubject; |
---|
213 | } |
---|
214 | |
---|
215 | protected boolean isLocked() { |
---|
216 | synchronized (DialogBuilder.lock) { |
---|
217 | return this.locked; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | private void setLocked(boolean locked) { |
---|
222 | synchronized (DialogBuilder.lock) { |
---|
223 | this.locked = locked; |
---|
224 | } |
---|
225 | |
---|
226 | } |
---|
227 | |
---|
228 | private void unlock() { |
---|
229 | synchronized (DialogBuilder.lock) { |
---|
230 | setLocked(false); |
---|
231 | DialogBuilder.lock.notifyAll(); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | static public int showMessageDialog(Frame parent, Object message, Setup setup) { |
---|
236 | |
---|
237 | return DialogBuilder.showDialog(parent, message, JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, setup); |
---|
238 | |
---|
239 | } |
---|
240 | |
---|
241 | static public int showMessageDialog(Frame parent, Object message, int messageType, Setup setup) { |
---|
242 | |
---|
243 | return DialogBuilder.showDialog(parent, message, messageType, JOptionPane.DEFAULT_OPTION, setup); |
---|
244 | |
---|
245 | } |
---|
246 | |
---|
247 | static public int showConfirmDialog(Frame parent, Object message, int messageType, int optionType, Setup setup) { |
---|
248 | |
---|
249 | return DialogBuilder.showDialog(parent, message, messageType, optionType, setup); |
---|
250 | |
---|
251 | } |
---|
252 | |
---|
253 | static public int showDialog(Frame parent, Object message, int messageType, int optionType, Setup setup) { |
---|
254 | |
---|
255 | DialogBuilder dialog = new DialogBuilder(parent, setup); |
---|
256 | int valor = dialog.buildDialog(message, messageType, optionType); |
---|
257 | |
---|
258 | dialog.dispose(); |
---|
259 | |
---|
260 | return valor; |
---|
261 | |
---|
262 | } |
---|
263 | |
---|
264 | private int buildDialog(Object message, int messageType, int optionType) { |
---|
265 | |
---|
266 | this.optionPane = new JOptionPane(message, messageType, optionType); |
---|
267 | this.setModal(true); |
---|
268 | this.setContentPane(this.optionPane); |
---|
269 | this.optionPane.addPropertyChangeListener(this); |
---|
270 | this.pack(); |
---|
271 | |
---|
272 | //Posicionando no centro da tela. |
---|
273 | Dimension mySize = this.getSize(); |
---|
274 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
---|
275 | this.setLocation(screenSize.width / 2 - (mySize.width / 2), screenSize.height / 2 - (mySize.height / 2)); |
---|
276 | this.setVisible(true); |
---|
277 | |
---|
278 | Object selectedValue = this.optionPane.getValue(); |
---|
279 | int resultado = JOptionPane.CLOSED_OPTION; |
---|
280 | if (selectedValue != null) { |
---|
281 | resultado = JOptionPane.CLOSED_OPTION; |
---|
282 | } else if (selectedValue instanceof Integer) { |
---|
283 | resultado = ((Integer) selectedValue).intValue(); |
---|
284 | } |
---|
285 | |
---|
286 | //this.dispose(); |
---|
287 | |
---|
288 | return resultado; |
---|
289 | |
---|
290 | } |
---|
291 | |
---|
292 | public void propertyChange(PropertyChangeEvent evt) { |
---|
293 | // TODO Stub de método gerado automaticamente |
---|
294 | String property = evt.getPropertyName(); |
---|
295 | |
---|
296 | if (this.isVisible() && |
---|
297 | evt.getSource() == optionPane && |
---|
298 | JOptionPane.VALUE_PROPERTY.equals(property)) { |
---|
299 | |
---|
300 | this.setVisible(false); |
---|
301 | } |
---|
302 | |
---|
303 | } |
---|
304 | |
---|
305 | static public String showPinDialog(Frame parent, Setup setup) { |
---|
306 | DialogBuilder pinCodeDialog = new DialogBuilder(parent, setup); |
---|
307 | |
---|
308 | try { |
---|
309 | SwingUtilities.invokeAndWait(pinCodeDialog.new PinCodeNeededBuilder()); |
---|
310 | } catch (InterruptedException e1) { |
---|
311 | // TODO Bloco catch gerado automaticamente |
---|
312 | e1.printStackTrace(); |
---|
313 | } catch (InvocationTargetException e1) { |
---|
314 | // TODO Bloco catch gerado automaticamente |
---|
315 | e1.printStackTrace(); |
---|
316 | } |
---|
317 | |
---|
318 | synchronized (DialogBuilder.lock) { |
---|
319 | |
---|
320 | while (pinCodeDialog.isLocked()) { |
---|
321 | try { |
---|
322 | DialogBuilder.lock.wait(); |
---|
323 | } catch (InterruptedException e) { |
---|
324 | // TODO Bloco catch gerado automaticamente |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | String pin = pinCodeDialog.getPin(); |
---|
329 | pinCodeDialog.dispose(); |
---|
330 | pinCodeDialog = null; |
---|
331 | |
---|
332 | return pin; |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | static public String showCertificateSelector(Frame parent, Setup setup, Map<String, String> certificateMap) { |
---|
337 | DialogBuilder certificateSelectorDialog = new DialogBuilder(parent, setup); |
---|
338 | |
---|
339 | Set<Map.Entry<String, String>> entries = certificateMap.entrySet(); |
---|
340 | List<String> certificateList = new ArrayList<String>(); |
---|
341 | |
---|
342 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
343 | System.out.println("Entradas:"); |
---|
344 | } |
---|
345 | |
---|
346 | for (Map.Entry<String, String> entry : entries){ |
---|
347 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
348 | System.out.println("[" + entry.getKey() + " : " + entry.getValue() + "]"); |
---|
349 | } |
---|
350 | certificateList.add(entry.getValue()); |
---|
351 | } |
---|
352 | |
---|
353 | try { |
---|
354 | SwingUtilities.invokeAndWait(certificateSelectorDialog.new CertificateSelectorBuilder(certificateList)); |
---|
355 | } catch (InterruptedException e1) { |
---|
356 | // TODO Bloco catch gerado automaticamente |
---|
357 | e1.printStackTrace(); |
---|
358 | } catch (InvocationTargetException e1) { |
---|
359 | // TODO Bloco catch gerado automaticamente |
---|
360 | e1.printStackTrace(); |
---|
361 | } |
---|
362 | |
---|
363 | synchronized (DialogBuilder.lock) { |
---|
364 | |
---|
365 | while (certificateSelectorDialog.isLocked()) { |
---|
366 | try { |
---|
367 | DialogBuilder.lock.wait(); |
---|
368 | } catch (InterruptedException e) { |
---|
369 | // TODO Bloco catch gerado automaticamente |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | String subject = certificateSelectorDialog.getCertificateSubject(); |
---|
374 | String label = null; |
---|
375 | |
---|
376 | for (Map.Entry<String, String> entry : entries){ |
---|
377 | if (entry.getValue().equals(subject)){ |
---|
378 | label = entry.getKey(); |
---|
379 | break; |
---|
380 | } |
---|
381 | } |
---|
382 | |
---|
383 | certificateSelectorDialog.dispose(); |
---|
384 | certificateSelectorDialog = null; |
---|
385 | |
---|
386 | return label; |
---|
387 | } |
---|
388 | } |
---|
389 | |
---|
390 | synchronized private boolean isOk() { |
---|
391 | return ok; |
---|
392 | } |
---|
393 | |
---|
394 | synchronized private void setOk(boolean ok) { |
---|
395 | this.ok = ok; |
---|
396 | } |
---|
397 | |
---|
398 | private class PinCodeNeededBuilder implements Runnable { |
---|
399 | |
---|
400 | public void run() { |
---|
401 | buildPinDialog(); |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | private class CertificateSelectorBuilder implements Runnable{ |
---|
406 | |
---|
407 | private List<String> certificateList; |
---|
408 | |
---|
409 | public CertificateSelectorBuilder(List<String> certificateList){ |
---|
410 | this.certificateList = certificateList; |
---|
411 | } |
---|
412 | |
---|
413 | public void run() { |
---|
414 | buildCertificateSelector(this.certificateList); |
---|
415 | } |
---|
416 | } |
---|
417 | |
---|
418 | private final class EventManager implements ActionListener, KeyListener, WindowListener { |
---|
419 | |
---|
420 | public void keyPressed(KeyEvent keyEvt) { |
---|
421 | int keyPressed = keyEvt.getKeyCode(); |
---|
422 | |
---|
423 | if (keyPressed == KeyEvent.VK_ENTER) { |
---|
424 | //System.out.println("Tecla ENTER pressionada"); |
---|
425 | if (dialogType == CERTIFICATE_SELECTOR_DIALOG) { |
---|
426 | this.actionPerformed(new ActionEvent(keyEvt.getSource(), keyEvt.getID(), "select")); |
---|
427 | } else { |
---|
428 | this.actionPerformed(new ActionEvent(keyEvt.getSource(), keyEvt.getID(), "ok")); |
---|
429 | } |
---|
430 | } else if (keyPressed == KeyEvent.VK_ESCAPE) { |
---|
431 | //System.out.println("Tecla ESC pressionada"); |
---|
432 | this.actionPerformed(new ActionEvent(keyEvt.getSource(), keyEvt.getID(), "cancel")); |
---|
433 | } |
---|
434 | } |
---|
435 | |
---|
436 | public void keyReleased(KeyEvent arg0) { |
---|
437 | } |
---|
438 | |
---|
439 | public void keyTyped(KeyEvent keyEvt) { |
---|
440 | } |
---|
441 | |
---|
442 | public void windowActivated(WindowEvent arg0) { |
---|
443 | } |
---|
444 | |
---|
445 | public void windowClosed(WindowEvent arg0) { |
---|
446 | //System.out.println("Window Closed: Fechando diálogo!"); |
---|
447 | } |
---|
448 | |
---|
449 | public void windowClosing(WindowEvent arg0) { |
---|
450 | //System.out.println("Window Closing: Fechando diálogo!"); |
---|
451 | cancelButtonActionPerformed(); |
---|
452 | } |
---|
453 | |
---|
454 | public void windowDeactivated(WindowEvent arg0) { |
---|
455 | } |
---|
456 | |
---|
457 | public void windowDeiconified(WindowEvent arg0) { |
---|
458 | } |
---|
459 | |
---|
460 | public void windowIconified(WindowEvent arg0) { |
---|
461 | } |
---|
462 | |
---|
463 | public void windowOpened(WindowEvent arg0) { |
---|
464 | } |
---|
465 | |
---|
466 | public void actionPerformed(ActionEvent evt) { |
---|
467 | |
---|
468 | String command = evt.getActionCommand(); |
---|
469 | |
---|
470 | // Atribui o valor digitado para o campo |
---|
471 | // senha e esconde a interface e assina o certificado |
---|
472 | if (command.equals("ok")) { |
---|
473 | |
---|
474 | //if (pfPin != null && pfPin.getPassword().length <= 0){ |
---|
475 | // Mostra um aviso de que o campo PIN não foi preenchido |
---|
476 | //JOptionPane.showMessageDialog(null, "Campo PIN não foi preenchido!", |
---|
477 | // "Aviso", JOptionPane.INFORMATION_MESSAGE); |
---|
478 | //} |
---|
479 | //else { |
---|
480 | setPin(String.valueOf(pfPin.getPassword())); |
---|
481 | okButtonActionPerformed(); |
---|
482 | //} |
---|
483 | |
---|
484 | } else if (command.equals("select")){ |
---|
485 | setCertificateSubject((String) lCertificatesList.getSelectedValue()); |
---|
486 | okButtonActionPerformed(); |
---|
487 | } |
---|
488 | else if (command.equals("cancel")) { |
---|
489 | cancelButtonActionPerformed(); |
---|
490 | } else if (command.equals("prosseguir")) { |
---|
491 | setOk(true); |
---|
492 | okButtonActionPerformed(); |
---|
493 | } |
---|
494 | } |
---|
495 | } |
---|
496 | } |
---|