View Javadoc

1   package org.eparapher.rcp.dialog;
2   
3   
4   import org.eclipse.swt.SWT;
5   import org.eclipse.swt.events.KeyEvent;
6   import org.eclipse.swt.events.KeyListener;
7   import org.eclipse.swt.layout.GridLayout;
8   import org.eclipse.swt.widgets.Button;
9   import org.eclipse.swt.widgets.Composite;
10  import org.eclipse.swt.widgets.Dialog;
11  import org.eclipse.swt.widgets.Display;
12  import org.eclipse.swt.widgets.Label;
13  import org.eclipse.swt.widgets.Shell;
14  import org.eclipse.swt.widgets.Text;
15  import org.eparapher.rcp.tools.GUIIcons;
16  
17  public class PINDialog extends Dialog {
18  
19  	private static final long serialVersionUID = 1L;
20  
21  	private String PINorPassword;
22  
23      public boolean recieved;
24      //GUI Settings
25      private static final int panelheight = 100;
26      private static final int panelwidth = 350;
27     
28      //GUI Objects
29      private Text   txtPassword;
30      private Label  txtLabel;
31      private Button butOK;
32      private Button butCancel;
33  
34      private static PINDialog singleton = null;
35      
36      public static PINDialog getInstance() {
37      	if (singleton == null)
38      		singleton = new PINDialog(Display.getDefault().getActiveShell());
39      	return singleton;
40  
41      }
42  
43  	public PINDialog(Shell activeShell) {
44  		
45  		super(activeShell);
46  		String title   = "Secret Protecting your Keystore";
47  		String message = "Enter you passphrase : ";
48  		/*
49  		Display display = Display.getDefault();
50  		Shell shell = new Shell(SWT.NONE);
51  	    shell.setSize(panelwidth, panelheight);*/
52  		activeShell.setLocation(0,0);
53  		activeShell.setText(title);
54  		activeShell.setLayout(new GridLayout());
55  
56  	    final Composite composite = new Composite(activeShell, SWT.BORDER );
57  	    final GridLayout gridLayout = new GridLayout();
58  	    //gridLayout.makeColumnsEqualWidth = true;
59  	    gridLayout.verticalSpacing = 15;
60  	    gridLayout.numColumns = 2;
61  	    composite.setLayout(gridLayout);
62  	    
63          //GUI Elements
64          txtPassword = new Text(composite,SWT.PASSWORD | SWT.BORDER |SWT.SINGLE);
65          txtLabel = new Label(composite,SWT.NONE);
66          txtLabel.setText("Code PIN :");
67          
68          butOK = new Button(composite,SWT.NONE);
69          butOK.setText("OK");
70          butOK.setImage(GUIIcons.OK_ICON_IMAGE);
71          butCancel = new Button(composite,SWT.NONE);
72          butCancel.setText("Cancel");
73          butCancel.setImage(GUIIcons.KO_ICON_IMAGE);
74          
75          //GUI Settings        
76          //PINDialogActionListener actionlistener = new PINDialogActionListener();
77          //butOK.addActionListener(actionlistener);
78          //butCancel.addActionListener(actionlistener);
79          
80          //gestion du bouton entree
81          PINDialogKeyAdapter keyadapter = new PINDialogKeyAdapter();
82          txtPassword.addKeyListener(keyadapter);
83          
84          // Gestion du bouton "fermer la fenetre"
85          //PINDialogWindowAdapter windowadapter = new PINDialogWindowAdapter(); 
86          //addWindowListener(windowadapter);
87          
88          //setResizable(false);
89          //Dimension dimension = getToolkit().getScreenSize();
90          //Rectangle rectangle = getBounds();
91          //setLocation((dimension.width - rectangle.width) / 2, (dimension.height - rectangle.height) / 2);
92          //setVisible(false);
93  	}
94  
95  	public void prompt() {
96      	//setVisible(true);
97      	//toFront();
98      	//requestFocusInWindow();
99  	}
100 
101 	public String getPINorPassword() {
102 		String myPIN = this.PINorPassword;
103 		this.PINorPassword = null;
104 		this.txtPassword.setText("");
105 		return myPIN;
106 	}
107 	class PINDialogKeyAdapter implements KeyListener {
108 
109         public void keyPressed(KeyEvent keyevent) {
110         	//Catch a key : validate if enter is pressed
111             if(keyevent.keyCode == 10) {
112                	PINorPassword = new String ( txtPassword.getText() );
113                 //setVisible(false);
114                 recieved = true;
115             }
116         }
117 
118 		public void keyReleased(KeyEvent e) {
119 			
120 		}
121 
122     }
123 	/*
124     //GUI Events
125 	class  PINDialogActionListener   implements ActionListener {
126 
127 	    public void actionPerformed(ActionEvent actionevent) {
128 	            Object obj = actionevent.getSource();
129 	            if(obj == butOK)
130 	            {
131 	                try
132 	                {
133 	                	PINorPassword = new String ( txtPassword.getPassword() );
134 	                }
135 	                catch(Exception _ex)
136 	                {
137 	                	PINorPassword = null;
138 	                }
139 	            }
140 	            if(obj == butCancel)
141 	            {
142 	            	PINorPassword = null;
143 	            }
144                 setVisible(false);
145                 recieved = true;
146 	            
147 	        }
148 
149 	    }*/
150 	/*
151     //GUI Events
152 	class PINDialogWindowAdapter extends  WindowAdapter {
153 
154         public void windowClosing(WindowEvent windowevent)
155         {
156         	PINorPassword = null;
157             recieved = true;
158             setVisible(false);
159             dispose();
160         }
161 
162     }*/
163 }
164