1 2 23 24 package net.fenyo.gnetwatch.GUI; 25 26 import java.net.UnknownHostException ; 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 50 51 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; private int sec = SecurityLevel.AUTH_PRIV; 85 private int retries = 3; 86 private int timeout = 1500; private int port = 161; 88 private String community = "public"; 89 private String username = ""; 90 private String password_auth = ""; 91 private String password_priv = ""; 92 private int pdu_max_size = 1000; 93 94 99 public DialogCredentials(final GUI gui, final Shell parent) { 100 super(parent, 0); 101 this.gui = gui; 102 } 103 104 109 public int getVersion() { 110 return version; 111 } 112 113 118 public int getSec() { 119 return sec; 120 } 121 122 127 public int getRetries() { 128 return retries; 129 } 130 131 136 public int getTimeout() { 137 return timeout; 138 } 139 140 145 public int getPort() { 146 return port; 147 } 148 149 154 public String getCommunity() { 155 return community; 156 } 157 158 163 public String getUsername() { 164 return username; 165 } 166 167 172 public String getPasswordAuth() { 173 return password_auth; 174 } 175 176 181 public String getPasswordPriv() { 182 return password_priv; 183 } 184 185 190 public int getPDUMaxSize() { 191 return pdu_max_size; 192 } 193 194 199 public void setVersion(final int version) { 200 this.version = version; 201 } 202 203 208 public void setSec(final int sec) { 209 this.sec = sec; 210 } 211 212 217 public void setRetries(final int retries) { 218 this.retries = retries; 219 } 220 221 226 public void setTimeout(final int timeout) { 227 this.timeout = timeout; 228 } 229 230 235 public void setPort(final int port) { 236 this.port = port; 237 } 238 239 244 public void setCommunity(final String community) { 245 this.community = community; 246 } 247 248 253 public void setUsername(final String username) { 254 this.username = username; 255 } 256 257 262 public void setPasswordAuth(final String password_auth) { 263 this.password_auth = password_auth; 264 } 265 266 271 public void setPasswordPriv(final String password_priv) { 272 this.password_priv = password_priv; 273 } 274 275 280 public void setPDUMaxSize(final int pdu_max_size) { 281 this.pdu_max_size = pdu_max_size; 282 } 283 284 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 296 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 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 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 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 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 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 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 |