Source code: org/altara/mars/swingui/HostEditorPanel.java
1 /* MARS Network Monitor Swing User Interface
2 Copyright (C) 1999 Brian H. Trammell
3 Copyright (C) 2002 Leapfrog Research & Development, LLC
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, it is available at
17 http:///www.gnu.org/copyleft/gpl.html, or by writing to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20 */
21
22 package org.altara.mars.swingui;
23
24 import org.altara.util.*;
25 import org.altara.mars.*;
26 import org.altara.mars.engine.*;
27 import java.util.*;
28 import java.net.*;
29 import java.awt.*;
30 import java.awt.event.*;
31 import javax.swing.*;
32 import javax.swing.tree.*;
33 import javax.swing.event.*;
34
35 public class HostEditorPanel extends JPanel implements Editor {
36
37 private MarsModel model;
38 private Host host;
39
40 private JTextField nameField;
41 private JTextField addrField;
42 private String editorTitle;
43
44 public HostEditorPanel(MarsModel model) {
45 // hold on to the model
46 this.model = model;
47 this.host = null;
48
49 // default to new host
50 this.editorTitle = "Add Host";
51
52 // create editable fields
53 nameField = new JTextField(25);
54 addrField = new JTextField(25);
55
56 // set up the layout manager
57 setLayout(new GridBagLayout());
58 GridBagConstraints c = new GridBagConstraints();
59 c.anchor = c.NORTHWEST; c.fill = c.BOTH;
60 c.weightx = 1.0; c.weighty = 0.0;
61 c.insets = new Insets(4,4,6,4);
62
63 // lay out the panel
64 c.gridx = 0; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1;
65 add(new JLabel("Name"),c);
66 c.gridx = 1; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1;
67 add(nameField,c);
68 c.gridx = 0; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1;
69 add(new JLabel("Address"),c);
70 c.gridx = 1; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1;
71 add(addrField,c);
72 c.gridx = 1; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1;
73 JLabel addrHelp = new JLabel("Enter DNS hostname or IP address.");
74 addrHelp.setFont(addrHelp.getFont().deriveFont(Font.PLAIN,9));
75 add(addrHelp,c);
76 }
77
78 public HostEditorPanel(Host host) {
79 // create the UI first
80 this(host.getModel());
81
82 // capture the host to edit
83 this.host = host;
84
85 // change the header label
86 this.editorTitle = "Edit Host";
87
88 // change the text field contents
89 nameField.setText(host.getName());
90 addrField.setText(host.getAddress().getHostName());
91 }
92
93 public void commit() throws UnknownHostException {
94 if (host == null) {
95 host = new Host(model, nameField.getText(),
96 InetAddress.getByName(addrField.getText()));
97 } else {
98 host.setName(nameField.getText());
99 host.setAddress(InetAddress.getByName(addrField.getText()));
100 host.fireHostChanged();
101 }
102 }
103
104 public String getEditorTitle() {
105 return editorTitle;
106 }
107 }