1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
/*
* Wireless Host Monitor
*
* Copyright (C) 2003, Jonathan Sevy <jsevy@mcs.drexel.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package airporthostmon;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class PreferencesDialog extends JDialog
implements ActionListener
{
JTextField ipAddressField;
JPasswordField passwordField;
JTextField intervalField;
JLabel ipAddressLabel;
JLabel passwordLabel;
JLabel intervalLabel;
JLabel accessPointTypeLabel;
JComboBox accessPointTypeComboBox;
JCheckBox logCheckBox;
JLabel logFileLabel;
JTextField logFileField;
JButton chooseLogFileButton;
JRadioButton logAllButton, logUnknownButton;
JCheckBox notifyCheckBox;
JRadioButton notifyAllButton, notifyUnknownButton;
JCheckBox notifyBeepCheckBox;
JCheckBox notifyEmailCheckBox;
JLabel notifyEmailLabel;
JTextField notifyEmailField;
JLabel notifySMTPServerLabel;
JTextField notifySMTPServerField;
JButton okButton, cancelButton;
boolean cancelled = false;
Preferences2 newPrefs;
public PreferencesDialog(Frame owner, Preferences2 currentPreferences, String[] accessPointTypes)
{
// create as modal dialog
super(owner, "Preferences", true);
// set new prefs to current
newPrefs = currentPreferences;
ipAddressField = new JTextField(12);
ipAddressLabel = new JLabel("Base station IP address:");
ipAddressField.setText(currentPreferences.ipAddress);
ipAddressField.setMinimumSize(ipAddressField.getPreferredSize());
passwordField = new JPasswordField(12);
passwordLabel = new JLabel("Base station password:");
passwordField.setText("");
passwordField.setMinimumSize(passwordField.getPreferredSize());
intervalField = new JTextField(12);
intervalLabel = new JLabel("Base station query interval (seconds):");
intervalField.setText("" + currentPreferences.queryInterval);
intervalField.setMinimumSize(intervalField.getPreferredSize());
logCheckBox = new JCheckBox("Log associated host information");
logCheckBox.setSelected(currentPreferences.logFlag);
logAllButton = new JRadioButton("Log all associated hosts");
logUnknownButton = new JRadioButton("Log only unknown associated hosts");
ButtonGroup logButtonGroup = new ButtonGroup();
logButtonGroup.add(logAllButton);
logButtonGroup.add(logUnknownButton);
if (currentPreferences.logMode == Preferences2.LOG_ALL)
{
logAllButton.setSelected(true);
}
else
{
logUnknownButton.setSelected(true);
}
logFileField = new JTextField(12);
logFileLabel = new JLabel("Log file:");
logFileField.setText(currentPreferences.logFile);
logFileField.setMinimumSize(logFileField.getPreferredSize());
chooseLogFileButton = new JButton("Choose file...");
chooseLogFileButton.setActionCommand("choose log file");
chooseLogFileButton.addActionListener(this);
notifyCheckBox = new JCheckBox("Notify when new hosts appear");
notifyCheckBox.setSelected(currentPreferences.notifyFlag);
notifyAllButton = new JRadioButton("Notify for all associated hosts");
notifyUnknownButton = new JRadioButton("Notify only for unknown associated hosts");
ButtonGroup notifyButtonGroup = new ButtonGroup();
notifyButtonGroup.add(notifyAllButton);
notifyButtonGroup.add(notifyUnknownButton);
if (currentPreferences.notifyMode == Preferences2.NOTIFY_ALL)
{
notifyAllButton.setSelected(true);
}
else
{
notifyUnknownButton.setSelected(true);
}
notifyBeepCheckBox = new JCheckBox("Beep when new hosts appear");
notifyBeepCheckBox.setSelected(currentPreferences.beepNotifyFlag);
notifyEmailCheckBox = new JCheckBox("Send email when new hosts appear");
notifyEmailCheckBox.setSelected(currentPreferences.emailNotifyFlag);
notifyEmailField = new JTextField(12);
notifyEmailLabel = new JLabel("Email address:");
notifyEmailField.setText(currentPreferences.emailAddress);
notifyEmailField.setMinimumSize(notifyEmailField.getPreferredSize());
notifySMTPServerField = new JTextField(12);
notifySMTPServerLabel = new JLabel("Email server (SMTP):");
notifySMTPServerField.setText(currentPreferences.emailSMTPHost);
notifySMTPServerField.setMinimumSize(notifySMTPServerField.getPreferredSize());
okButton = new JButton("OK");
okButton.setActionCommand("ok");
okButton.addActionListener(this);
this.getRootPane().setDefaultButton(okButton);
cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("cancel");
cancelButton.addActionListener(this);
// populate combo box with access point types; set selected index to
accessPointTypeComboBox = new JComboBox(accessPointTypes);
accessPointTypeComboBox.setSelectedItem(currentPreferences.accessPointType);
accessPointTypeLabel = new JLabel("Base station type:");
// set params for layout manager
GridBagLayout theLayout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.NONE;
c.ipadx = 0;
c.ipady = 0;
Insets theMargin = new Insets(2,2,2,2);
c.insets = theMargin;
c.anchor = GridBagConstraints.CENTER;
c.weightx = .5;
c.weighty = .5;
// set up panel for logging info
InfoPanel logPanel = new InfoPanel();
// put stuff in panel
logPanel.setLayout(theLayout);
c.gridwidth = 3;
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(logUnknownButton, c);
logPanel.add(logUnknownButton);
c.gridx = 1;
c.gridy = 2;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(logAllButton, c);
logPanel.add(logAllButton);
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 3;
c.anchor = GridBagConstraints.EAST;
theLayout.setConstraints(logFileLabel, c);
logPanel.add(logFileLabel);
c.gridx = 2;
c.gridy = 3;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(logFileField, c);
logPanel.add(logFileField);
c.gridx = 3;
c.gridy = 3;
c.anchor = GridBagConstraints.CENTER;
theLayout.setConstraints(chooseLogFileButton, c);
logPanel.add(chooseLogFileButton);
// make highlighting of panel dependent on logCheckBox
logCheckBox.addItemListener(logPanel);
logPanel.setEnabled(logCheckBox.isSelected());
// set up panel for email notifying when new host appears
InfoPanel emailNotifyPanel = new InfoPanel();
emailNotifyPanel.setLayout(theLayout);
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
theLayout.setConstraints(notifyEmailLabel, c);
emailNotifyPanel.add(notifyEmailLabel);
c.gridx = 2;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(notifyEmailField, c);
emailNotifyPanel.add(notifyEmailField);
c.gridx = 1;
c.gridy = 2;
c.anchor = GridBagConstraints.EAST;
theLayout.setConstraints(notifySMTPServerLabel, c);
emailNotifyPanel.add(notifySMTPServerLabel);
c.gridx = 2;
c.gridy = 2;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(notifySMTPServerField, c);
emailNotifyPanel.add(notifySMTPServerField);
// make highlighting of panel dependent on logCheckBox
logCheckBox.addItemListener(emailNotifyPanel);
emailNotifyPanel.setEnabled(logCheckBox.isSelected());
// make highlighting of panel dependent on notifyCheckBox
notifyEmailCheckBox.addItemListener(emailNotifyPanel);
emailNotifyPanel.setEnabled(notifyEmailCheckBox.isSelected());
// set up panel for notifying when new host appears
InfoPanel notifyPanel = new InfoPanel();
notifyPanel.setLayout(theLayout);
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(notifyUnknownButton, c);
notifyPanel.add(notifyUnknownButton);
c.gridx = 1;
c.gridy = 2;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(notifyAllButton, c);
notifyPanel.add(notifyAllButton);
c.gridx = 1;
c.gridy = 3;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(notifyBeepCheckBox, c);
notifyPanel.add(notifyBeepCheckBox);
c.gridx = 1;
c.gridy = 4;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(notifyEmailCheckBox, c);
notifyPanel.add(notifyEmailCheckBox);
c.gridx = 1;
c.gridy = 5;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(emailNotifyPanel, c);
notifyPanel.add(emailNotifyPanel);
// make highlighting of panel dependent on notifyCheckBox
notifyCheckBox.addItemListener(notifyPanel);
notifyPanel.setEnabled(notifyCheckBox.isSelected());
this.getContentPane().setLayout(theLayout);
c.anchor = GridBagConstraints.CENTER;
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
theLayout.setConstraints(ipAddressLabel, c);
this.getContentPane().add(ipAddressLabel);
c.gridx = 2;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(ipAddressField, c);
this.getContentPane().add(ipAddressField);
c.gridx = 1;
c.gridy = 2;
c.anchor = GridBagConstraints.EAST;
theLayout.setConstraints(passwordLabel, c);
this.getContentPane().add(passwordLabel);
c.gridx = 2;
c.gridy = 2;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(passwordField, c);
this.getContentPane().add(passwordField);
c.gridx = 1;
c.gridy = 3;
c.anchor = GridBagConstraints.EAST;
theLayout.setConstraints(accessPointTypeLabel, c);
this.getContentPane().add(accessPointTypeLabel);
c.gridx = 2;
c.gridy = 3;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(accessPointTypeComboBox, c);
this.getContentPane().add(accessPointTypeComboBox);
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 4;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(logCheckBox, c);
this.getContentPane().add(logCheckBox);
c.gridx = 1;
c.gridy = 5;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(logPanel, c);
this.getContentPane().add(logPanel);
c.gridx = 1;
c.gridy = 6;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(notifyCheckBox, c);
this.getContentPane().add(notifyCheckBox);
c.gridx = 1;
c.gridy = 7;
c.anchor = GridBagConstraints.WEST;
theLayout.setConstraints(notifyPanel, c);
this.getContentPane().add(notifyPanel);
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 8;
c.anchor = GridBagConstraints.CENTER;
theLayout.setConstraints(okButton, c);
this.getContentPane().add(okButton);
c.gridx = 2;
c.gridy = 8;
c.anchor = GridBagConstraints.CENTER;
theLayout.setConstraints(cancelButton, c);
this.getContentPane().add(cancelButton);
this.pack();
this.show();
}
public void actionPerformed(ActionEvent theEvent)
// respond to button pushes, menu selections
{
String command = theEvent.getActionCommand();
if (command.equals("ok"))
{
if (validateNewPreferences())
this.hide();
}
if (command == "cancel")
{
cancelled = true;
this.hide();
}
if (command == "choose log file")
{
FileDialog fd = new FileDialog((Frame)this.getOwner(), "Select log file...", FileDialog.LOAD);
fd.setFile(logFileField.getText());
fd.show();
if (fd.getFile() != null)
{
logFileField.setText(fd.getFile());
}
}
}
private boolean validateNewPreferences()
{
int interval;
// check the interval parameter
try
{
interval = Integer.parseInt(intervalField.getText());
if (interval <= 0)
throw new NumberFormatException();
newPrefs = new Preferences2();
newPrefs.ipAddress = ipAddressField.getText();
newPrefs.password = passwordField.getText();
newPrefs.queryInterval = interval;
newPrefs.accessPointType = (String)accessPointTypeComboBox.getSelectedItem();
if (logCheckBox.isSelected())
newPrefs.logFlag = true;
else
newPrefs.logFlag = false;
if (logAllButton.isSelected())
newPrefs.logMode = Preferences2.LOG_ALL;
else if (logUnknownButton.isSelected())
newPrefs.logMode = Preferences2.LOG_UNKNOWN;
else
newPrefs.logMode = Preferences2.LOG_NONE;
newPrefs.logFile = logFileField.getText();
if (notifyCheckBox.isSelected())
newPrefs.notifyFlag = true;
else
newPrefs.notifyFlag = false;
if (notifyAllButton.isSelected())
newPrefs.notifyMode = Preferences2.NOTIFY_ALL;
else if (notifyUnknownButton.isSelected())
newPrefs.notifyMode = Preferences2.NOTIFY_UNKNOWN;
else
newPrefs.notifyMode = Preferences2.NOTIFY_NONE;
if (notifyBeepCheckBox.isSelected())
newPrefs.beepNotifyFlag = true;
else
newPrefs.beepNotifyFlag = false;
if (notifyEmailCheckBox.isSelected())
newPrefs.emailNotifyFlag = true;
else
newPrefs.emailNotifyFlag = false;
newPrefs.emailAddress = notifyEmailField.getText();
newPrefs.emailSMTPHost = notifySMTPServerField.getText();
return true;
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(this, "Value supplied must be a positive integer.");
return false;
}
}
public Preferences2 getPreferences()
{
return newPrefs;
}
public boolean isCancelled()
{
return cancelled;
}
}
|