View Javadoc

1   package es.caib.signatura.impl.ui;
2   
3   import java.awt.Dimension;
4   import java.awt.Point;
5   import java.awt.Rectangle;
6   import java.awt.Toolkit;
7   import java.awt.event.ActionEvent;
8   import java.awt.event.ActionListener;
9   import java.io.File;
10  import java.io.FileInputStream;
11  import java.io.FileOutputStream;
12  import java.util.Properties;
13  
14  import javax.swing.JButton;
15  import javax.swing.JCheckBox;
16  import javax.swing.JComboBox;
17  import javax.swing.JDesktopPane;
18  import javax.swing.JDialog;
19  import javax.swing.JLabel;
20  
21  import es.caib.signatura.impl.SigDebug;
22  
23  
24  public class FirefoxSignerProfileChooser extends JDialog implements ActionListener {
25  
26  	private static final long serialVersionUID = 1L;
27  	private JDesktopPane jDesktopPane = null;
28  	private JCheckBox jCheckBox = null;
29  	private JLabel jLabel = null;
30  	private JComboBox jComboBox = null;
31  	private JLabel jLabel1 = null;
32  	private JButton jButton = null;
33  	
34  	public static final String FIREFOX_PROPERTIES = System.getProperty("user.home") + "/.signaturacaib/firefox.properties";
35  
36  	/**
37  	 * This is the default constructor
38  	 */
39  	public FirefoxSignerProfileChooser() {
40  		super();
41  		initialize();
42  	}
43  
44  	/**
45  	 * This method initializes this
46  	 * 
47  	 * @return void
48  	 */
49  	private void initialize() {
50  		this.setSize(300, 175);
51  		this.setTitle("Seleccionar perfil de Firefox");
52  		this.setModal(true);
53  		this.setPreferredSize(new Dimension(300, 175));
54  		this.setContentPane(getJDesktopPane());
55  		centerWindow ();
56  		this.toFront();
57  	}
58  	
59      private void centerWindow() {
60          Rectangle screen = new Rectangle(
61  	    Toolkit.getDefaultToolkit().getScreenSize());
62          Point center     = new Point(
63  	    (int)screen.getCenterX(), (int)screen.getCenterY());
64          Point newLocation = new Point(
65  	    center.x - this.getWidth()/2, center.y - this.getHeight()/2);
66          if(screen.contains(newLocation.x, newLocation.y, 
67  	                   this.getWidth(), this.getHeight())) {
68              this.setLocation(newLocation);
69          }
70      } // centerWindow()
71  	/**
72  	 * This method initializes jDesktopPane
73  	 * 
74  	 * @return javax.swing.JDesktopPane
75  	 */
76  	private JDesktopPane getJDesktopPane() {
77  		if (jDesktopPane == null) {
78  			jLabel1 = new JLabel();
79  			jLabel1.setBounds(new Rectangle(174, 62, 90, 16));
80  			jLabel1.setText("Recordar opción");
81  			jLabel = new JLabel();
82  			jLabel.setBounds(new Rectangle(7, 8, 280, 16));
83  			jLabel.setText("Perfil de Firefox para buscar certificados:");
84  			jDesktopPane = new JDesktopPane();
85  			jDesktopPane.add(getJCheckBox(), null);
86  			jDesktopPane.add(jLabel, null);
87  			jDesktopPane.add(getJComboBox(), null);
88  			jDesktopPane.add(jLabel1, null);
89  			jDesktopPane.add(getJButton(), null);
90  		}
91  		return jDesktopPane;
92  	}
93  
94  	/**
95  	 * This method initializes jCheckBox
96  	 * 
97  	 * @return javax.swing.JCheckBox
98  	 */
99  	private JCheckBox getJCheckBox() {
100 		if (jCheckBox == null) {
101 			jCheckBox = new JCheckBox();
102 			jCheckBox.setBounds(new Rectangle(265, 61, 21, 21));
103 			jCheckBox.addActionListener(this);
104 		}
105 		return jCheckBox;
106 	}
107 
108 	/**
109 	 * This method initializes jComboBox
110 	 * 
111 	 * @return javax.swing.JComboBox
112 	 */
113 	private JComboBox getJComboBox() {
114 		if (jComboBox == null) {
115 			jComboBox = new JComboBox();
116 			jComboBox.setBounds(new Rectangle(6, 29, 280, 25));
117 			jComboBox.addActionListener(this);
118 		}
119 		return jComboBox;
120 	}
121 
122 	/**
123 	 * This method initializes jButton
124 	 * 
125 	 * @return javax.swing.JButton
126 	 */
127 	private JButton getJButton() {
128 		if (jButton == null) {
129 			jButton = new JButton();
130 			jButton.setBounds(new Rectangle(103, 102, 97, 35));
131 			jButton.setText("Aceptar");
132 			jButton.addActionListener(this);
133 		}
134 		return jButton;
135 	}
136 
137 	public void actionPerformed(ActionEvent e) {
138 
139 		if (e.getSource().equals(jButton)) {
140 
141 			if (e.getID() == ActionEvent.ACTION_PERFORMED) {
142 				dispose();
143 			}
144 
145 		} else if (e.getSource().equals(jComboBox)) {
146 
147 			if (e.getID() == ActionEvent.ACTION_PERFORMED) {
148 				// System.out.println("Evento... " +
149 				// jComboBox.getSelectedIndex() );
150 			}
151 
152 		} else if (e.getSource().equals(jCheckBox)) {
153 
154 			if (e.getID() == ActionEvent.ACTION_PERFORMED) {
155 				// System.out.println("Evento... " + jCheckBox.isSelected());
156 			}
157 
158 		}
159 
160 	}
161 
162 	public int getComboValue(File firefoxPropsFile) throws Exception {
163 		
164 		SigDebug.write("START getComboValue");
165 	
166 		if ( jCheckBox.isSelected() ) {
167 			
168 			SigDebug.write("START getComboValue CHECK SELECTED");
169 							
170 			Properties props = new Properties();
171 			props.setProperty("firefox.chosenprofile", (String)jComboBox.getSelectedItem());
172 			props.store(new FileOutputStream(firefoxPropsFile), "Archivo de propiedades para el signer de Firefox");
173 							
174 		}
175 		
176 		SigDebug.write("END getComboValue");
177 		
178 		return (int)jComboBox.getSelectedIndex();
179 	
180 	}
181 
182 	public void fillItemList(File[] items) {
183 
184 		for (int i = 0; i < items.length; i++)
185 			jComboBox.addItem(items[i].getName().substring(9));
186 
187 	}
188 
189 	public static int getFirefoxProfile(File[] profiles) throws Exception {
190 		
191 		FirefoxSignerProfileChooser p = new FirefoxSignerProfileChooser();
192 		File firefoxPropsFile = new File(FIREFOX_PROPERTIES);
193 		
194 		// Si existe el archivo de properties, devolvemos el valor guardado en esta "caché"
195 		if ( firefoxPropsFile.exists() ) {
196 			
197 			SigDebug.write("Existe " + firefoxPropsFile.getAbsolutePath());
198 			
199 			Properties props = new Properties();
200 			
201 			try {
202 				
203 				props.load(new FileInputStream(firefoxPropsFile));
204 				String value = props.getProperty("firefox.chosenprofile");
205 				
206 				// Comprobamos el valor de la clave obtenida...
207 				if (value == null || "".equals(value))
208 					throw new Exception("Clave 'firefox.chosenprofile' nula o vacía en el archivo " +
209 							"firefox.properties");
210 					
211 				for (int i = 0; i < profiles.length; i++) {
212 					
213 					if ( value.equals(profiles[i].getName().substring(9)) )
214 						return i;
215 					
216 				}
217 				
218 				// Si llegamos aquí, hay un problema...
219 				throw new Exception(
220 						"El perfil guardado en el archivo " + FIREFOX_PROPERTIES + " no coincide con " +
221 						"ninguno de los que se ofrecen como candidatos. Se sugiere borrar este " +
222 						"archivo e intentarlo de nuevo.");
223 
224 			} catch (Exception e) {
225 				
226 				e.printStackTrace();
227 				throw e;
228 				
229 			}
230 		
231 		// Si no existe el archivo de properties, preguntamos qué perfil cargar...
232 		} else {
233 					
234 			p.fillItemList(profiles);
235 			p.setVisible(true);
236 			return p.getComboValue(firefoxPropsFile);
237 			
238 		}
239 		
240 	}
241 }