Tuesday, July 22, 2008

Java: IP Analyzer Update #1

Up to this point the IP Analyzer has been used only by myself as I find it easier than an Excel spreadsheet. However that has just changed. The Sales team is now in possession of the application, and some changes have become necessary. It turns out several of them have used the Add button instead of the Compare button and now IPs that do not belong on the restricted list are in fact there. The temporary solution is easy, I can erase the misplaced information as it is only a text file. However a more permanent solution is needed.

And this is where the JPasswordField compoent comes into play. I decided to password protect the Add button so that I was the only one with access to this feature. At first I would have to wrap the entire Add method into an statement checking the password, that is until I realized I was making this so much harder than they had to be.

There were two things that needed to be done, obtain the password input from the user and then decide whether or not to display the Add dialogue window. The first portion took some doing to organize the dialogue window correctly.

JOptionPane.showConfirmDialog(textPanel, passPanel, "Enter Password", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE);


Choosing a dialogue method was a bit difficult, and took several attempts before the above one was decided upon. The passPanel is a small JPanel that contains a JLabel and the input JPasswordField.

Once the password was input an IF statement was created that would decide what to do next.

if (password.compareTo(String.valueOf(input)) > 0 || password.compareTo(String.valueOf(input)) <>

If the password was correct (value = 0) then this statement is bypassed and the rest of the Add method is acted upon. However if the password is incorrect then the If statement does two things: displays a message stating the password is incorrect and Returns out of the Add method completely.

The change was fairly simple but should ensure that none of the Sales team adds anymore values into the restricted IP list without approval.

No comments: