View Javadoc

1   /*
2    * This file is part of NetPisteur.
3    * 
4    * Copyright 2001-2002 : Olivier CORNEC, David BOUANCHEAU, Loic LOPEZ,
5    *                       Gaetan BOUDARD, Mael LE LANNOU, Julien ROBINEAU
6    * 
7    * Copyright 2002-2003 : Olivier BRIENS, Simon DEZE, Florence FRIGOULT,
8    *                       Olivier JOURNEAULT, Francois MARTINIER, Damien RAUDE-MORVAN
9    * 
10   * Copyright 2004-2007 : Damien RAUDE-MORVAN
11   * 
12   * NetPisteur is free software; you can redistribute it and/or modify
13   * it under the terms of the GNU General Public License as published by
14   * the Free Software Foundation; either version 2 of the License, or
15   * (at your option) any later version.
16   *
17   * NetPisteur is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   * GNU General Public License for more details.
21   *
22   * You should have received a copy of the GNU General Public License
23   * along with NetPisteur; if not, write to the Free Software
24   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
25   *
26   */
27  package com.drazzib.netpisteur.ui;
28  
29  import java.awt.BorderLayout;
30  import java.awt.Font;
31  import java.awt.GridLayout;
32  import java.awt.event.ActionEvent;
33  import java.awt.event.ActionListener;
34  import java.awt.event.MouseAdapter;
35  import java.awt.event.MouseEvent;
36  import java.net.MalformedURLException;
37  import java.net.URL;
38  
39  import javax.swing.JButton;
40  import javax.swing.JComboBox;
41  import javax.swing.JFrame;
42  import javax.swing.JLabel;
43  import javax.swing.JOptionPane;
44  import javax.swing.JPanel;
45  import javax.swing.JTextField;
46  
47  import org.apache.commons.logging.Log;
48  import org.apache.commons.logging.LogFactory;
49  
50  import com.drazzib.netpisteur.configuration.Configuration;
51  import com.drazzib.netpisteur.configuration.ListUtilisateurs;
52  import com.drazzib.netpisteur.configuration.Utilisateur;
53  import com.drazzib.netpisteur.util.Messages;
54  
55  /**
56   * This class open a frame wich allows to change the banner page and the
57   * separator of a user.
58   * 
59   * @author Netpisteur v.2 Team
60   */
61  public class AdvancedOptionsFrame extends JFrame {
62  	
63  	private static Log log = LogFactory.getLog(AdvancedOptionsFrame.class);
64  
65  	/**
66  	 * 
67  	 */
68  	private static final long serialVersionUID = 2509402800493155326L;
69  
70  	private JPanel saisieSeparateur, panelAll, panelNorth, panelWest,
71  			panelEast, panelSouth, panelCenter, optionPanel, boutonPanel;
72  
73  	private JLabel pageLabel, pageAcLabel, separateurLabel, separaLabel,
74  			expliPageLabel, expliSeparateurLabel, expliPageLabel2,
75  			expliSeparateurLabel2, sep, sep2;
76  
77  	private JTextField pageTextField, separateurTextField;
78  
79  	private JComboBox choixSep;
80  
81  	private JButton okBouton, annulerBouton, parcBouton;
82  
83  	// //////////////////////////////
84  	// different Variables
85  	private boolean combobox;
86  
87  	private String page = "";
88  
89  	private String separ = "";
90  
91  	private Utilisateur user;
92  
93  	public static String _REP = Configuration.getHandle().getRootNom() + "_" //$NON-NLS-1$
94  			+ Configuration.getHandle().getRootNom();
95  
96  	// //////////////////////////////
97  	// constructor
98  
99  	/**
100 	 * Creates new form Config
101 	 * 
102 	 * @param utilisat
103 	 *            the user who will be change
104 	 */
105 	public AdvancedOptionsFrame(Utilisateur utilisat) {
106 		this.page = utilisat.getPage();
107 		this.separ = utilisat.getSeparateur();
108 		this.user = utilisat;
109 		initComponents();
110 	}
111 
112 	// //////////////////////////////
113 	// operations
114 
115 	private void initComponents() {
116 
117 		// Basic Panels
118 		this.panelAll = new JPanel(new BorderLayout());
119 		this.panelNorth = new JPanel();
120 		this.panelWest = new JPanel();
121 		this.panelEast = new JPanel();
122 		this.panelSouth = new JPanel();
123 		this.panelCenter = new JPanel(new BorderLayout());
124 		// Button Panel
125 		this.boutonPanel = new JPanel();
126 		// Principal Panel
127 		this.optionPanel = new JPanel();
128 		// Separator Panel
129 		this.saisieSeparateur = new JPanel();
130 
131 		// Banner page variables
132 		this.pageLabel = new JLabel();
133 		this.pageAcLabel = new JLabel();
134 		this.expliPageLabel = new JLabel();
135 		this.expliPageLabel2 = new JLabel();
136 		this.pageTextField = new JTextField();
137 
138 		// Separator variables
139 		this.separateurLabel = new JLabel();
140 		this.separaLabel = new JLabel();
141 		this.expliSeparateurLabel = new JLabel();
142 		this.expliSeparateurLabel2 = new JLabel();
143 		this.choixSep = new JComboBox();
144 		this.separateurTextField = new JTextField();
145 
146 		// Creation of the differents fonts used and the separator
147 		Font titre = new Font("Arial", Font.BOLD, 15); //$NON-NLS-1$
148 		this.sep = new JLabel("____________________________________"); //$NON-NLS-1$
149 		this.sep2 = new JLabel("____________________________________"); //$NON-NLS-1$
150 
151 		// Advanced options :
152 
153 		// size of the two panels used
154 		this.optionPanel.setLayout(new GridLayout(11, 2));
155 		this.saisieSeparateur.setLayout(new GridLayout(1, 2));
156 		this.boutonPanel.setLayout(new GridLayout(1, 2));
157 
158 		// Banner Page option :
159 		this.pageLabel.setText(Messages
160 				.getString("OptionAvance.HOMEPAGE_LABEL")); //$NON-NLS-1$
161 		this.pageLabel.setFont(titre);
162 		// pageLabel.setName("pageLabel"); //$NON-NLS-1$
163 		this.optionPanel.add(this.pageLabel);
164 		this.optionPanel.add(this.sep);
165 
166 		this.expliPageLabel.setText(Messages.getString("OptionAvance.ADDRESS")); //$NON-NLS-1$
167 		this.expliPageLabel2.setText(Messages
168 				.getString("OptionAvance.HOMEPAGE")); //$NON-NLS-1$
169 
170 		this.optionPanel.add(this.expliPageLabel);
171 		this.optionPanel.add(new JLabel());
172 		this.optionPanel.add(this.expliPageLabel2);
173 		this.optionPanel.add(new JLabel());
174 
175 		this.pageAcLabel
176 				.setText(Messages.getString("OptionAvance.FOR_EXEMPLE")); //$NON-NLS-1$
177 		this.optionPanel.add(this.pageAcLabel);
178 		// pageTextField.setName("pageTextField"); //$NON-NLS-1$
179 		this.pageTextField.setText(this.page);
180 		this.optionPanel.add(this.pageTextField);
181 
182 		this.parcBouton = new JButton();
183 		this.parcBouton.setText(Messages.getString("OptionAvance.BROWSE")); //$NON-NLS-1$
184 		// parcBouton.setName("parcourir"); //$NON-NLS-1$
185 		this.parcBouton.addMouseListener(new MouseAdapter() {
186 			public void mouseReleased(MouseEvent evt) {
187 				parcourirFrameBouton(evt);
188 
189 			}
190 		});
191 		this.optionPanel.add(new JLabel());
192 		this.optionPanel.add(this.parcBouton);
193 
194 		// Separator option :
195 		this.separateurLabel.setText(Messages
196 				.getString("OptionAvance.SEPARATOR")); //$NON-NLS-1$
197 		this.separateurLabel.setFont(titre);
198 		// separateurLabel.setName("separateurLabel"); //$NON-NLS-1$
199 		this.optionPanel.add(this.separateurLabel);
200 
201 		this.optionPanel.add(this.sep2);
202 
203 		this.expliSeparateurLabel.setText(Messages
204 				.getString("OptionAvance.TYPE_OF_SEPARATOR")); //$NON-NLS-1$
205 		this.expliSeparateurLabel2.setText(Messages
206 				.getString("OptionAvance.IN_OUTFILES")); //$NON-NLS-1$
207 
208 		this.optionPanel.add(this.expliSeparateurLabel);
209 		this.optionPanel.add(new JLabel());
210 		this.optionPanel.add(this.expliSeparateurLabel2);
211 
212 		this.choixSep.addItem(Messages.getString("OptionAvance.COMMA")); //$NON-NLS-1$
213 		this.choixSep.addItem(Messages.getString("OptionAvance.TAB")); //$NON-NLS-1$
214 		this.choixSep.addItem(Messages.getString("OptionAvance.SPACE")); //$NON-NLS-1$
215 		this.choixSep.addItem(Messages.getString("OptionAvance.SLASH")); //$NON-NLS-1$
216 		this.choixSep.addItem(Messages.getString("OptionAvance.OTHER")); //$NON-NLS-1$
217 		this.choixSep.addActionListener(new ActionListener() {
218 			public void actionPerformed(ActionEvent evt) {
219 				switchComboBox(evt);
220 
221 			}
222 		});
223 
224 		this.optionPanel.add(this.choixSep);
225 		this.loadSeparator();
226 
227 		this.optionPanel.add(new JLabel());
228 		this.optionPanel.add(new JLabel());
229 		this.separaLabel.setText(Messages
230 				.getString("OptionAvance.ENTER_SEPARATOR")); //$NON-NLS-1$
231 		this.optionPanel.add(this.separaLabel);
232 
233 		this.separateurTextField.setColumns(5);
234 		// separateurTextField.setName("separateurTextField"); //$NON-NLS-1$
235 		this.saisieSeparateur.add(this.separateurTextField);
236 		this.saisieSeparateur.add(new JLabel());
237 		this.optionPanel.add(this.saisieSeparateur);
238 		this.optionPanel.add(new JLabel());
239 		this.optionPanel.add(new JLabel());
240 
241 		// Button ok and Cancel
242 
243 		this.boutonPanel = new JPanel();
244 		// Buttons creation :
245 		this.okBouton = new JButton();
246 		this.annulerBouton = new JButton();
247 
248 		// Button Ok (definition and method launched :okBoutonMouseReleased)
249 		this.okBouton.setText(Messages.getString("OptionAvance.OK")); //$NON-NLS-1$
250 		// okBouton.setName("ok"); //$NON-NLS-1$
251 		this.okBouton.addMouseListener(new MouseAdapter() {
252 			public void mouseReleased(MouseEvent evt) {
253 				okBoutonMouseReleased(evt);
254 
255 			}
256 		});
257 
258 		// Button cancel (definition and method
259 		// launched:annulerBoutonMouseReleased)
260 		this.annulerBouton.setText(Messages.getString("OptionAvance.CANCEL")); //$NON-NLS-1$
261 		// annulerBouton.setName("annuler"); //$NON-NLS-1$
262 		this.annulerBouton.addMouseListener(new MouseAdapter() {
263 			public void mouseReleased(MouseEvent evt) {
264 				annulerBoutonMouseReleased(evt);
265 
266 			}
267 		});
268 
269 		// addition of the two buttons to the frame
270 		this.boutonPanel.add(this.okBouton);
271 		this.boutonPanel.add(this.annulerBouton);
272 
273 		// Basic configuration
274 
275 		// frame's title
276 		setTitle(Messages.getString("OptionAvance.ADVANCED_OPTIONS")); //$NON-NLS-1$
277 
278 		// Panels configuration
279 		this.panelCenter.add(this.optionPanel, BorderLayout.CENTER);
280 		this.panelCenter.add(this.boutonPanel, BorderLayout.SOUTH);
281 		this.panelAll.add(this.panelNorth, BorderLayout.NORTH);
282 		this.panelAll.add(this.panelWest, BorderLayout.WEST);
283 		this.panelAll.add(this.panelEast, BorderLayout.EAST);
284 		this.panelAll.add(this.panelSouth, BorderLayout.SOUTH);
285 		this.panelAll.add(this.panelCenter, BorderLayout.CENTER);
286 		this.getContentPane().add(this.panelAll);
287 		this.setLocationRelativeTo(null);
288 		pack();
289 
290 	}
291 
292 	/**
293 	 * Allows to put the good separator when the frame opening.
294 	 */
295 	private void loadSeparator() {
296 
297 		if ((this.separ.equals(";")) || (this.separ.equals("\t")) || (this.separ.equals(" ")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
298 				|| (this.separ.equals("/"))) { //$NON-NLS-1$
299 			if (this.separ.equals(";")) { //$NON-NLS-1$
300 				this.choixSep.setSelectedIndex(0);
301 			}
302 			if (this.separ.equals("\t")) { //$NON-NLS-1$
303 				this.choixSep.setSelectedIndex(1);
304 			}
305 			if (this.separ.equals(" ")) { //$NON-NLS-1$
306 				this.choixSep.setSelectedIndex(2);
307 			}
308 			if (this.separ.equals("/")) { //$NON-NLS-1$
309 				this.choixSep.setSelectedIndex(3);
310 			}
311 			this.combobox = false;
312 			this.separaLabel.setVisible(false);
313 			this.separateurTextField.setVisible(false);
314 		} else {
315 			this.choixSep.setSelectedIndex(4);
316 			this.combobox = true;
317 			log.debug("Separator : "+this.separ); //$NON-NLS-1$
318 			this.separateurTextField.setText(this.separ);
319 			this.separaLabel.setVisible(true);
320 			this.separateurTextField.setVisible(true);
321 			this.validate();
322 		}
323 	}
324 
325 	// Methods used by the buttons Ok and Cancel
326 
327 	/**
328 	 * Allows to close the frame when the button cancel is pushed
329 	 * 
330 	 * @param MouseEvent
331 	 *            evt
332 	 */
333 	void annulerBoutonMouseReleased(MouseEvent evt) {
334 		this.dispose();
335 	}
336 
337 	/**
338 	 * Allows to browse your disk to select home page
339 	 * 
340 	 * @param MouseEvent
341 	 *            evt
342 	 */
343 	void parcourirFrameBouton(MouseEvent evt) {
344 		ExplorerFrame exp = new ExplorerFrame(this);
345 		exp.setVisible(true);
346 	}
347 
348 	/**
349 	 * Allows to close the frame and save the modification when the button ok is
350 	 * pushed
351 	 * 
352 	 * @param MouseEvent
353 	 *            evt
354 	 */
355 	void okBoutonMouseReleased(MouseEvent evt) {
356 
357 		// Separator
358 		if (this.combobox == true) {
359 			boolean nonnull = !(this.separateurTextField.getText().equals("")); //$NON-NLS-1$
360 			if (nonnull == true) {
361 				Configuration.getHandle().setRootSeparator(
362 						this.separateurTextField.getText());
363 				this.dispose();
364 			} else {
365 				JOptionPane
366 						.showMessageDialog(
367 								null,
368 								Messages
369 										.getString("OptionAvance.WRONG_SEPARATOR"), Messages.getString("OptionAvance.ERROR"), //$NON-NLS-1$ //$NON-NLS-2$
370 								JOptionPane.ERROR_MESSAGE);
371 
372 			}
373 		} else {
374 			if (this.choixSep.getSelectedItem().equals(
375 					Messages.getString("OptionAvance.COMMA"))) { //$NON-NLS-1$
376 				Configuration.getHandle().setRootSeparator(";"); //$NON-NLS-1$
377 			}
378 			if (this.choixSep.getSelectedItem().equals(
379 					Messages.getString("OptionAvance.TAB"))) { //$NON-NLS-1$
380 				Configuration.getHandle().setRootSeparator("\t"); //$NON-NLS-1$
381 			}
382 			if (this.choixSep.getSelectedItem().equals(
383 					Messages.getString("OptionAvance.SPACE"))) { //$NON-NLS-1$
384 				Configuration.getHandle().setRootSeparator(" "); //$NON-NLS-1$
385 			}
386 			if (this.choixSep.getSelectedItem().equals(
387 					Messages.getString("OptionAvance.SLASH"))) { //$NON-NLS-1$
388 				Configuration.getHandle().setRootSeparator("/"); //$NON-NLS-1$
389 			}
390 		}
391 
392 		try {
393 			// Banner Page
394 			Configuration.getHandle().setRootPage(this.pageTextField.getText());
395 
396 			// verification de l'url
397 			URL url = new URL(this.pageTextField.getText());
398 
399 			log.info("URL Valide : " + url); //$NON-NLS-1$
400 
401 			ListUtilisateurs.changeSep(this.user, Configuration.getHandle().getRootSeparator());
402 			ListUtilisateurs.changePage(this.user, Configuration.getHandle().getRootPage());
403 			JOptionPane
404 					.showMessageDialog(
405 							null,
406 							Messages.getString("OptionAvance.MODIF_OK"), //$NON-NLS-1$
407 							Messages.getString("OptionAvance.SUCCESS"), JOptionPane.INFORMATION_MESSAGE); //$NON-NLS-1$
408 			this.dispose();
409 		} catch (MalformedURLException ex) {
410 			JOptionPane
411 					.showMessageDialog(
412 							null,
413 							Messages.getString("OptionAvance.WRONG_URL"), Messages.getString("OptionAvance.EROOR"), //$NON-NLS-1$ //$NON-NLS-2$
414 							JOptionPane.ERROR_MESSAGE);
415 		}
416 	}
417 
418 	public void setAccueilPage(String myPage) {
419 		this.pageTextField.setText(myPage);
420 	}
421 
422 	/**
423 	 * Allows to make visible the Textfield when the icons "autres" is selected.
424 	 * 
425 	 * @param ActionEvent
426 	 *            evt
427 	 */
428 	void switchComboBox(ActionEvent evt) {
429 
430 		if (this.combobox == true
431 				&& !(this.choixSep.getSelectedItem().equals(Messages
432 						.getString("OptionAvance.OTHER")))) { //$NON-NLS-1$
433 			this.separaLabel.setVisible(false);
434 			this.separateurTextField.setVisible(false);
435 			this.validate();
436 		}
437 		if (this.choixSep.getSelectedItem().equals(
438 				Messages.getString("OptionAvance.OTHER"))) { //$NON-NLS-1$
439 			this.combobox = true;
440 			this.separaLabel.setVisible(true);
441 			this.separateurTextField.setVisible(true);
442 			this.validate();
443 		}
444 
445 	}
446 
447 }