KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > GUI > DialogCredentials


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch.GUI;
25
26 import java.net.UnknownHostException JavaDoc;
27
28 import net.fenyo.gnetwatch.AlgorithmException;
29 import net.fenyo.gnetwatch.GenericTools;
30 import net.fenyo.gnetwatch.targets.TargetIPv4Subnet;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import org.eclipse.swt.*;
36 import org.eclipse.swt.widgets.*;
37 import org.eclipse.swt.layout.*;
38 import org.eclipse.swt.custom.*;
39 import org.eclipse.swt.events.*;
40 import org.eclipse.swt.graphics.*;
41 import org.eclipse.swt.browser.*;
42
43 import org.snmp4j.security.*;
44
45 /**
46  * This class manages the "credentials" dialog.
47  * @author Alexandre Fenyo
48  * @version $Id: DialogCredentials.java,v 1.15 2007/03/03 00:38:19 fenyo Exp $
49  */

50
51 /*
52  * snmp :
53  * version
54  * security level
55  * retries
56  * timeout
57  * port
58  * max size request pdu
59  * v1 et v2c : community
60  * v3 : username / password pour authentication, password pour privacy
61  */

62
63 public class DialogCredentials extends Dialog {
64   private static Log log = LogFactory.getLog(DialogCredentials.class);
65
66   private final GUI gui;
67
68   private final int bglevel = 100;
69
70   private GridLayout layout = null;
71   private Composite groups_composite = null;
72   private Composite bottom_composite = null;
73   private RowLayout groups_composite_layout = null;
74   private RowLayout bottom_composite_layout = null;
75   private GridData groups_composite_grid_data = null;
76   private Group group_network_parameters = null, group_credentials = null, group_credentials_v2c = null;
77   private GridLayout group_network_parameters_layout = null, group_credentials_layout = null, group_credentials_v2c_layout = null;
78   private Text group_credentials_value = null;
79   private Text group_credentials_value2 = null;
80   private Text group_credentials_value3 = null;
81   private Text group_credentials_v2c_value = null;
82
83   private int version = 0; // SNMPv1
84
private int sec = SecurityLevel.AUTH_PRIV;
85   private int retries = 3;
86   private int timeout = 1500; // microsec
87
private int port = 161;
88   private String JavaDoc community = "public";
89   private String JavaDoc username = "";
90   private String JavaDoc password_auth = "";
91   private String JavaDoc password_priv = "";
92   private int pdu_max_size = 1000;
93
94   /**
95    * Constructor.
96    * @param gui current GUI instance.
97    * @param parent parent shell.
98    */

99   public DialogCredentials(final GUI gui, final Shell parent) {
100     super(parent, 0);
101     this.gui = gui;
102   }
103
104   /**
105    * Gets SNMP version.
106    * @param none.
107    * @return int SNMP version.
108    */

109   public int getVersion() {
110     return version;
111   }
112
113   /**
114    * Gets security level.
115    * @param none.
116    * @return int security level.
117    */

118   public int getSec() {
119     return sec;
120   }
121
122   /**
123    * Gets number of retries.
124    * @param none.
125    * @return int retries.
126    */

127   public int getRetries() {
128     return retries;
129   }
130
131   /**
132    * Gets SNMP timeout per try.
133    * @param none.
134    * @return int timeout.
135    */

136   public int getTimeout() {
137     return timeout;
138   }
139
140   /**
141    * Returns the SNMP agent UDP port.
142    * @param none.
143    * @return int SNMP agent UDP port.
144    */

145   public int getPort() {
146     return port;
147   }
148
149   /**
150    * Returns the community string.
151    * @param none.
152    * @return String community string.
153    */

154   public String JavaDoc getCommunity() {
155     return community;
156   }
157
158   /**
159    * Returns the username.
160    * @param none.
161    * @return String username.
162    */

163   public String JavaDoc getUsername() {
164     return username;
165   }
166
167   /**
168    * Returns the password authentication.
169    * @param none.
170    * @return String password authentication.
171    */

172   public String JavaDoc getPasswordAuth() {
173     return password_auth;
174   }
175
176   /**
177    * Returns the password privacy.
178    * @param none.
179    * @return String password privacy.
180    */

181   public String JavaDoc getPasswordPriv() {
182     return password_priv;
183   }
184
185   /**
186    * Returns the maximum PDU size.
187    * @param none.
188    * @return int maximum PDU size.
189    */

190   public int getPDUMaxSize() {
191     return pdu_max_size;
192   }
193
194   /**
195    * Sets the SNMP version.
196    * @param version SNMP version.
197    * @return void.
198    */

199   public void setVersion(final int version) {
200     this.version = version;
201   }
202
203   /**
204    * Sets the security level.
205    * @param sec security level.
206    * @return void.
207    */

208   public void setSec(final int sec) {
209     this.sec = sec;
210   }
211
212   /**
213    * Sets the number of SNMP retries.
214    * @param int number of SNMP retries.
215    * @return void.
216    */

217   public void setRetries(final int retries) {
218     this.retries = retries;
219   }
220
221   /**
222    * Sets the SNMP timeout per try.
223    * @param timeout timeout per try.
224    * @return void.
225    */

226   public void setTimeout(final int timeout) {
227     this.timeout = timeout;
228   }
229
230   /**
231    * Sets the SNMP agent UDP port.
232    * @param port SNMP agent UDP port.
233    * @return void.
234    */

235   public void setPort(final int port) {
236     this.port = port;
237   }
238
239   /**
240    * Sets the community string.
241    * @param community community string.
242    * @return void.
243    */

244   public void setCommunity(final String JavaDoc community) {
245     this.community = community;
246   }
247
248   /**
249    * Sets the username.
250    * @param username username.
251    * @return void.
252    */

253   public void setUsername(final String JavaDoc username) {
254     this.username = username;
255   }
256
257   /**
258    * Sets the password authentication.
259    * @param password_auth password authentication.
260    * @return void.
261    */

262   public void setPasswordAuth(final String JavaDoc password_auth) {
263     this.password_auth = password_auth;
264   }
265
266   /**
267    * Sets the password privacy.
268    * @param password_priv password privacy.;
269    * @return void.
270    */

271   public void setPasswordPriv(final String JavaDoc password_priv) {
272     this.password_priv = password_priv;
273   }
274
275   /**
276    * Sets the PDU maximum size.
277    * @param pdu_max_size PDU maximum size.
278    * @return void.
279    */

280   public void setPDUMaxSize(final int pdu_max_size) {
281     this.pdu_max_size = pdu_max_size;
282   }
283
284   /**
285    * Displays the dialog.
286    * @param none.
287    * @return void.
288    */

289   public void open() {
290     final Shell parent = getParent();
291     final Display display = parent.getDisplay();
292     final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
293     shell.setText(gui.getConfig().getString("gnetwatch_cred"));
294 // shell.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
295

296     // Your code goes here (widget creation, set result, etc).
297
// Composite for groups at left
298
layout = new GridLayout();
299     layout.numColumns = 1;
300     layout.marginHeight = 2;
301     layout.marginWidth = 2;
302     layout.verticalSpacing = 1;
303     shell.setLayout(layout);
304
305     groups_composite = new Composite(shell, SWT.FLAT);
306     groups_composite_layout = new RowLayout(SWT.VERTICAL);
307     groups_composite_layout.fill = true;
308     groups_composite_layout.marginTop = 0;
309     groups_composite_layout.marginBottom = 0;
310     groups_composite.setLayout(groups_composite_layout);
311     groups_composite_grid_data = new GridData(GridData.FILL_VERTICAL);
312     groups_composite.setLayoutData(groups_composite_grid_data);
313
314     // Group for network parameters
315

316     group_network_parameters = new Group(groups_composite, SWT.SHADOW_ETCHED_IN);
317     group_network_parameters_layout = new GridLayout();
318     group_network_parameters_layout.numColumns = 2;
319     group_network_parameters.setLayout(group_network_parameters_layout);
320     group_network_parameters.setText(gui.getConfig().getString("network_parameters"));
321
322     final Label label_version = new Label(group_network_parameters, SWT.SHADOW_IN);
323     label_version.setText(gui.getConfig().getString("protocol_version"));
324     label_version.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
325     final Combo cversion = new Combo(group_network_parameters, SWT.SHADOW_IN | SWT.READ_ONLY);
326     cversion.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
327 // version.setBackground(new Color(display, bglevel, bglevel, bglevel));
328
// version.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
329
cversion.add("SNMP v1");
330     cversion.add("SNMP v2c");
331     cversion.add("SNMP v3");
332     if (version == 0) cversion.setText("SNMP v1");
333     else if (version == 1) cversion.setText("SNMP v2c");
334     else cversion.setText("SNMP v3");
335
336     final Label label_sec = new Label(group_network_parameters, SWT.SHADOW_IN);
337     label_sec.setText(gui.getConfig().getString("security_level"));
338     label_sec.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
339     final Combo csec = new Combo(group_network_parameters, SWT.SHADOW_IN | SWT.READ_ONLY);
340     csec.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
341     csec.add("open");
342     csec.add("auth only");
343     csec.add("auth+priv");
344     if (sec == SecurityLevel.AUTH_PRIV) csec.setText("auth+priv");
345     else if (sec == SecurityLevel.AUTH_NOPRIV) csec.setText("auth only");
346     else csec.setText("open");
347     label_sec.setEnabled(version == 2);
348     csec.setEnabled(version == 2);
349
350     final Label label_packet_size = new Label(group_network_parameters, SWT.SHADOW_IN);
351     label_packet_size.setText(gui.getConfig().getString("pdu_max_size"));
352     label_packet_size.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
353     final Spinner packet_size = new Spinner(group_network_parameters, SWT.WRAP);
354     packet_size.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
355     packet_size.setMinimum(0);
356     packet_size.setMaximum(10000);
357     packet_size.setSelection(1400);
358     // packet_size.setBackground(new Color(display, bglevel, bglevel, bglevel));
359
// packet_size.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
360

361     final Label label_retries = new Label(group_network_parameters, SWT.SHADOW_IN);
362     label_retries.setText(gui.getConfig().getString("retries"));
363     label_retries.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
364     final Spinner retries = new Spinner(group_network_parameters, SWT.WRAP);
365     retries.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
366     retries.setMinimum(0);
367     retries.setMaximum(15);
368     retries.setSelection(this.retries);
369
370     final Label label_timeout = new Label(group_network_parameters, SWT.SHADOW_IN);
371     label_timeout.setText(gui.getConfig().getString("timeout_microsec"));
372     label_timeout.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
373     final Spinner timeout = new Spinner(group_network_parameters, SWT.WRAP);
374     timeout.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
375     timeout.setMinimum(0);
376     timeout.setMaximum(10000);
377     timeout.setSelection(this.timeout);
378
379     final Label label_port = new Label(group_network_parameters, SWT.SHADOW_IN);
380     label_port.setText(gui.getConfig().getString("destination_port"));
381     label_port.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
382     final Spinner port = new Spinner(group_network_parameters, SWT.WRAP);
383     port.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
384     port.setMinimum(1);
385     port.setMaximum(65535);
386     port.setSelection(this.port);
387
388     // Group for SNMPv1 & SNMPv2c credentials
389

390     group_credentials_v2c = new Group(groups_composite, SWT.SHADOW_ETCHED_IN);
391     group_credentials_v2c_layout = new GridLayout();
392     group_credentials_v2c_layout.numColumns = 2;
393     group_credentials_v2c.setLayout(group_credentials_v2c_layout);
394     group_credentials_v2c.setText(gui.getConfig().getString("v1_v2c_cred"));
395     group_credentials_v2c.setEnabled(version != 2);
396
397     final Label label1_v2c = new Label(group_credentials_v2c, SWT.SHADOW_IN);
398     label1_v2c.setText(gui.getConfig().getString("community_string"));
399     label1_v2c.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
400     label1_v2c.setEnabled(version != 2);
401     group_credentials_v2c_value = new Text(group_credentials_v2c, SWT.SINGLE);
402     group_credentials_v2c_value.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.FILL_VERTICAL));
403     group_credentials_v2c_value.setBackground(new Color(display, bglevel, bglevel, bglevel));
404     group_credentials_v2c_value.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
405     group_credentials_v2c_value.setEnabled(version != 2);
406
407     group_credentials_v2c_value.setText(community);
408     final GC gc = new GC(group_credentials_v2c_value);
409     gc.setFont(group_credentials_v2c_value.getFont());
410     ((GridData) (group_credentials_v2c_value.getLayoutData())).widthHint = gc.stringExtent(" ").x;
411
412     // Group for SNMPv3 credentials
413

414     group_credentials = new Group(groups_composite, SWT.SHADOW_ETCHED_IN);
415     group_credentials_layout = new GridLayout();
416     group_credentials_layout.numColumns = 2;
417     group_credentials.setLayout(group_credentials_layout);
418     group_credentials.setText(gui.getConfig().getString("v3_cred"));
419     group_credentials.setEnabled(version == 2);
420
421     final Label label1 = new Label(group_credentials, SWT.SHADOW_IN);
422     label1.setText(gui.getConfig().getString("username"));
423     label1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
424     label1.setEnabled(version == 2);
425     group_credentials_value = new Text(group_credentials, SWT.SINGLE);
426     group_credentials_value.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.FILL_VERTICAL));
427     group_credentials_value.setBackground(new Color(display, bglevel, bglevel, bglevel));
428     group_credentials_value.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
429     group_credentials_value.setText(username);
430     ((GridData) (group_credentials_value.getLayoutData())).widthHint = gc.stringExtent(" ").x;
431     group_credentials_value.setEnabled(version == 2);
432
433     final Label label2 = new Label(group_credentials, SWT.SHADOW_IN);
434     label2.setText(gui.getConfig().getString("authentication_password"));
435     label2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
436     label2.setEnabled(version == 2);
437     group_credentials_value2 = new Text(group_credentials, SWT.SINGLE);
438     group_credentials_value2.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.FILL_VERTICAL));
439     group_credentials_value2.setBackground(new Color(display, bglevel, bglevel, bglevel));
440     group_credentials_value2.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
441     group_credentials_value2.setText(password_auth);
442     ((GridData) (group_credentials_value2.getLayoutData())).widthHint = gc.stringExtent(" ").x;
443     group_credentials_value2.setEnabled(version == 2);
444
445     final Label label3 = new Label(group_credentials, SWT.SHADOW_IN);
446     label3.setText(gui.getConfig().getString("privacy_password"));
447     label3.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
448     label3.setEnabled(version == 2);
449     group_credentials_value3 = new Text(group_credentials, SWT.SINGLE);
450     group_credentials_value3.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.FILL_VERTICAL));
451     group_credentials_value3.setBackground(new Color(display, bglevel, bglevel, bglevel));
452     group_credentials_value3.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
453     group_credentials_value3.setText(password_priv);
454     ((GridData) (group_credentials_value3.getLayoutData())).widthHint = gc.stringExtent(" ").x;
455     group_credentials_value3.setEnabled(version == 2);
456
457     // bottom buttons
458

459     bottom_composite = new Composite(shell, SWT.FLAT);
460     bottom_composite_layout = new RowLayout();
461     bottom_composite_layout.fill = true;
462     bottom_composite_layout.marginTop = 0;
463     bottom_composite_layout.marginBottom = 0;
464     bottom_composite_layout.wrap = false;
465     bottom_composite_layout.pack = false;
466     bottom_composite_layout.justify = true;
467     bottom_composite_layout.type = SWT.HORIZONTAL;
468     bottom_composite_layout.marginLeft = 5;
469     bottom_composite_layout.marginTop = 5;
470     bottom_composite_layout.marginRight = 5;
471     bottom_composite_layout.marginBottom = 5;
472     bottom_composite_layout.spacing = 0;
473     bottom_composite.setLayout(bottom_composite_layout);
474     final GridData bottom_composite_grid_data = new GridData(GridData.FILL_HORIZONTAL);
475     bottom_composite.setLayoutData(bottom_composite_grid_data);
476
477     final Button button_ok = new Button(bottom_composite, SWT.PUSH);
478     button_ok.setText("Ok");
479     final DialogCredentials _this = this;
480     button_ok.addSelectionListener(new SelectionListener() {
481       public void widgetDefaultSelected(SelectionEvent e) {
482         _this.version = cversion.getSelectionIndex();
483
484         if (csec.getSelectionIndex() == 0) _this.sec = SecurityLevel.NOAUTH_NOPRIV;
485         else if (csec.getSelectionIndex() == 1) _this.sec = SecurityLevel.AUTH_NOPRIV;
486         else _this.sec = SecurityLevel.AUTH_PRIV;
487
488         _this.retries = retries.getSelection();
489         _this.timeout = timeout.getSelection();
490         _this.port = port.getSelection();
491         _this.community = group_credentials_v2c_value.getText();
492         _this.username = group_credentials_value.getText();
493         _this.password_auth = group_credentials_value2.getText();
494         _this.password_priv = group_credentials_value3.getText();
495         _this.pdu_max_size = packet_size.getSelection();
496
497         shell.dispose();
498       }
499
500       public void widgetSelected(SelectionEvent e) {
501         widgetDefaultSelected(e);
502       }
503     });
504
505     final Button button_cancel = new Button(bottom_composite, SWT.PUSH);
506     button_cancel.setText("Cancel");
507     button_cancel.addSelectionListener(new SelectionListener() {
508       public void widgetDefaultSelected(SelectionEvent e) {
509         shell.dispose();
510       }
511
512       public void widgetSelected(SelectionEvent e) {
513         widgetDefaultSelected(e);
514       }
515     });
516
517     cversion.addModifyListener(new ModifyListener() {
518       public void modifyText(final ModifyEvent e) {
519         if (cversion.getSelectionIndex() == 2) {
520           group_credentials_v2c.setEnabled(false);
521           label1_v2c.setEnabled(false);
522           group_credentials_v2c_value.setEnabled(false);
523
524           label_sec.setEnabled(true);
525           csec.setEnabled(true);
526
527           group_credentials.setEnabled(true);
528           label1.setEnabled(true);
529           label2.setEnabled(true);
530           label3.setEnabled(true);
531           group_credentials_value.setEnabled(true);
532           group_credentials_value2.setEnabled(true);
533           group_credentials_value3.setEnabled(true);
534
535           groups_composite.pack(true);
536         } else {
537           group_credentials_v2c.setEnabled(true);
538           label1_v2c.setEnabled(true);
539           group_credentials_v2c_value.setEnabled(true);
540
541           label_sec.setEnabled(false);
542           csec.setEnabled(false);
543
544           group_credentials.setEnabled(false);
545           label1.setEnabled(false);
546           label2.setEnabled(false);
547           label3.setEnabled(false);
548           group_credentials_value.setEnabled(false);
549           group_credentials_value2.setEnabled(false);
550           group_credentials_value3.setEnabled(false);
551
552           groups_composite.pack(true);
553         }
554       }
555     });
556
557     shell.pack(true);
558     shell.open();
559     while (!shell.isDisposed())
560       if (!display.readAndDispatch()) display.sleep();
561   }
562 }
563
Popular Tags