source: branches/2.2/security/ExpressoCert/src/br/gov/serpro/ui/DialogBuilder.java @ 1174

Revision 1174, 15.7 KB checked in by rafaelraymundo, 15 years ago (diff)

Ticket #558 - Altera nome de diretório seguranca para security.

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