File: PreferencesDialog.java

package info (click to toggle)
airport-utils 2-6
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 4,040 kB
  • sloc: java: 30,844; xml: 571; sh: 553; makefile: 10
file content (222 lines) | stat: -rw-r--r-- 5,682 bytes parent folder | download | duplicates (5)
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
/*
 * IPInspector
 *
 * Copyright (C) 2002, 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 airportipinspector; 

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;




public class PreferencesDialog extends JDialog
                        implements ActionListener
{

    JTextField ipAddressField = new JTextField(10);
    JPasswordField passwordField = new JPasswordField(10);
    JTextField smtpHostField = new JTextField(20);
    JTextField emailAddressField = new JTextField(20);
    JTextField intervalField = new JTextField(10);
    JLabel ipAddressLabel = new JLabel("Base station address:");
    JLabel passwordLabel = new JLabel("Base station password:");
    JLabel smtpHostLabel = new JLabel("SMTP (email) host:");
    JLabel emailAddressLabel = new JLabel("Email address for notices:");
    JLabel intervalLabel = new JLabel("Base station query interval (seconds):");
    
    JButton okButton, cancelButton;
    
    boolean cancelled = false;
    Preferences newPrefs;
    
    
    public PreferencesDialog(Frame owner, Preferences currentPreferences)
    {
        // create as modal dialog
        super(owner, "Preferences", true);
        
        // set new prefs to current
        newPrefs = currentPreferences;
        
        ipAddressField.setText(currentPreferences.ipAddress);
        passwordField.setText("");
        smtpHostField.setText(currentPreferences.smtpHost);
        emailAddressField.setText(currentPreferences.emailAddress);
        intervalField.setText("" + currentPreferences.queryInterval);
       
        
        okButton = new JButton("OK");
		okButton.setActionCommand("ok");
		okButton.addActionListener(this);
		this.getRootPane().setDefaultButton(okButton);
		
		cancelButton = new JButton("Cancel");
		cancelButton.setActionCommand("cancel");
		cancelButton.addActionListener(this);
        
        // 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;
		
		
		this.getContentPane().setLayout(theLayout);
		
		
		c.gridx = 1;
		c.gridy = 1;
		theLayout.setConstraints(ipAddressLabel, c);
		this.getContentPane().add(ipAddressLabel);
		
		c.gridx = 2;
		c.gridy = 1;
		theLayout.setConstraints(ipAddressField, c);
		this.getContentPane().add(ipAddressField);
		
		c.gridx = 1;
		c.gridy = 2;
		theLayout.setConstraints(passwordLabel, c);
		this.getContentPane().add(passwordLabel);
		
		c.gridx = 2;
		c.gridy = 2;
		theLayout.setConstraints(passwordField, c);
		this.getContentPane().add(passwordField);
		
		c.gridx = 1;
		c.gridy = 3;
		theLayout.setConstraints(emailAddressLabel, c);
		this.getContentPane().add(emailAddressLabel);
		
		c.gridx = 2;
		c.gridy = 3;
		theLayout.setConstraints(emailAddressField, c);
		this.getContentPane().add(emailAddressField);
		
		c.gridx = 1;
		c.gridy = 4;
		theLayout.setConstraints(smtpHostLabel, c);
		this.getContentPane().add(smtpHostLabel);
		
		c.gridx = 2;
		c.gridy = 4;
		theLayout.setConstraints(smtpHostField, c);
		this.getContentPane().add(smtpHostField);
		
		c.gridx = 1;
		c.gridy = 5;
		theLayout.setConstraints(okButton, c);
		this.getContentPane().add(okButton);
		
		c.gridx = 2;
		c.gridy = 5;
		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();
		}
		
		
	}
	
	
	
	private boolean validateNewPreferences()
	{
	    int interval;
	    
	    // check the interval parameter
	    try
	    {
	        interval = Integer.parseInt(intervalField.getText());
	        if (interval <= 0)
			    throw new NumberFormatException();
			
			newPrefs = new Preferences();
        
            newPrefs.ipAddress = ipAddressField.getText();
            newPrefs.password = passwordField.getText();
            newPrefs.smtpHost = smtpHostField.getText();
            newPrefs.emailAddress = emailAddressField.getText();
            newPrefs.queryInterval = interval;
            
    	    return true;
    	    
	    }
	    catch(NumberFormatException e)
	    {
	        JOptionPane.showMessageDialog(this, "Value supplied must be a positive integer.");
	        return false;
	    }
	    
	    
	}
    
    
    public Preferences getPreferences()
    {
        return newPrefs;
    }
    
    
    
    public boolean isCancelled()
    {
        return cancelled;
    }

}