Source code: org/altara/mars/swingui/ServiceEditorPanel.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.awt.*;
29 import java.awt.event.*;
30 import javax.swing.*;
31 import javax.swing.tree.*;
32 import javax.swing.event.*;
33 import javax.swing.border.*;
34
35 public class ServiceEditorPanel extends JPanel implements Editor {
36
37 private final static String[] notacNames = {
38 "never",
39 "immediately",
40 "on second attempt",
41 "on third attempt"
42 };
43
44 private Host host;
45 private Service service;
46
47 private JTextField nameField;
48 private ServiceTypeComboBox typeBox;
49 private JComboBox notacBox;
50 private ServiceParamEditor paramEditor;
51 private JTextField portField;
52 private JTextField timeoutField;
53 private JTextField periodField;
54 private String editorTitle;
55
56 private GridBagConstraints c;
57
58 public ServiceEditorPanel(Host host) {
59 // hold on to the host
60 this.host = host;
61 this.service = null;
62
63 // default editor title to creation
64 this.editorTitle = "Add Service";
65
66 // create editable fields
67 nameField = new JTextField(25);
68 timeoutField = new JTextField(5);
69 periodField = new JTextField(5);
70 portField = new JTextField(5);
71 typeBox = new ServiceTypeComboBox(portField);
72 notacBox = new JComboBox(notacNames);
73
74 // null out the params editor for now
75 paramEditor = null;
76
77 // fill in default timeout/period
78 double defTimeout = Service.DEFAULT_TIMEOUT / 1000.0;
79 timeoutField.setText(String.valueOf(defTimeout));
80 double defPeriod = Service.DEFAULT_PERIOD / 1000.0;
81 periodField.setText(String.valueOf(defPeriod));
82
83 // fill in default notac
84 notacBox.setSelectedIndex(Service.DEFAULT_NOTAC);
85
86 // set up the layout manager
87 setLayout(new GridBagLayout());
88 c = new GridBagConstraints();
89 c.anchor = c.NORTHWEST; c.fill = c.BOTH;
90 c.weightx = 1.0; c.weighty = 0.0;
91 c.insets = new Insets(4,4,6,4);
92
93 // lay out the panel
94 c.gridx = 0; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1;
95 add(new JLabel("Name",JLabel.RIGHT),c);
96 c.gridx = 1; c.gridy = 0; c.gridwidth = 5; c.gridheight = 1;
97 add(nameField,c);
98 c.gridx = 0; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1;
99 add(new JLabel("Type",JLabel.RIGHT),c);
100 c.gridx = 1; c.gridy = 1; c.gridwidth = 2; c.gridheight = 1;
101 add(typeBox,c);
102 c.gridx = 3; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1;
103 add(new JLabel("Port",JLabel.RIGHT),c);
104 c.gridx = 4; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1;
105 add(portField,c);
106 c.gridx = 0; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1;
107 add(new JLabel("Timeout",JLabel.RIGHT),c);
108 c.gridx = 1; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1;
109 add(timeoutField,c);
110 c.gridx = 2; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1;
111 add(new JLabel("sec."),c);
112 c.gridx = 3; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1;
113 add(new JLabel("Check ea.",JLabel.RIGHT),c);
114 c.gridx = 4; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1;
115 add(periodField,c);
116 c.gridx = 5; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1;
117 add(new JLabel("sec."),c);
118 c.gridx = 0; c.gridy = 3; c.gridwidth = 1; c.gridheight = 1;
119 add(new JLabel("Notify",JLabel.RIGHT),c);
120 c.gridx = 1; c.gridy = 3; c.gridwidth = 5; c.gridheight = 1;
121 add(notacBox,c);
122
123 // add trigger for service parameters panel
124 typeBox.addActionListener(new ActionListener() {
125 public void actionPerformed(ActionEvent ae) {
126 createParamEditor();
127 }
128 });
129
130 // trigger an action event on the type box to fill in default port
131 // and param panel
132 typeBox.fireActionEvent();
133 }
134
135 public ServiceEditorPanel(Service service) {
136 // create the UI first
137 this(service.getHost());
138
139 // capture the service to edit
140 this.service = service;
141
142 // change the editor title
143 this.editorTitle = "Edit Service";
144
145 // fill in the text field values
146 nameField.setText(service.getName());
147 typeBox.setSvcType(service.getSvcType());
148 portField.setText(String.valueOf(service.getPort()));
149 double timeoutDbl = service.getTimeout() / 1000.0;
150 timeoutField.setText(String.valueOf(timeoutDbl));
151 double periodDbl = service.getPeriod() / 1000.0;
152 periodField.setText(String.valueOf(periodDbl));
153 notacBox.setSelectedIndex(service.getNotac());
154
155 // create the editor panel and fill in values
156 createParamEditor();
157 paramEditor.fillParams(service);
158 }
159
160 public void commit() throws NumberFormatException,
161 InvalidServiceTypeException {
162 // try validating the fields first
163 int port = Integer.parseInt(portField.getText());
164 if (port < 0 || port > 65535)
165 throw new NumberFormatException("Port out of range");
166
167 double timeoutDbl = Double.parseDouble(timeoutField.getText());
168 if (timeoutDbl < 0 || timeoutDbl > 65535)
169 throw new NumberFormatException("Timeout out of range");
170 long timeout = (long)(timeoutDbl*1000);
171
172 double periodDbl = Double.parseDouble(periodField.getText());
173 if (periodDbl < 0 || periodDbl > 65535)
174 throw new NumberFormatException("Check period out of range");
175 long period = (long)(periodDbl*1000);
176
177 if (service == null) {
178 // then create a new service if necessary
179 service = new Service(host,nameField.getText(),
180 typeBox.getSvcType(),port,timeout,period,
181 notacBox.getSelectedIndex(),
182 paramEditor.getParamMap());
183 } else {
184 // otherwise, just save the changes into this service
185 service.setName(nameField.getText());
186 service.setSvcType(typeBox.getSvcType());
187 service.setPort(port);
188 service.setTimeout(timeout);
189 service.setPeriod(period);
190 service.setNotac(notacBox.getSelectedIndex());
191 paramEditor.commitParams(service);
192 service.fireServiceChanged();
193 }
194 }
195
196 private void createParamEditor() {
197 // remove old param editor if necessary
198 if (paramEditor != null) remove(paramEditor);
199
200 // create a new editor panel of the appropriate type
201 String svcname = (String)typeBox.getSelectedItem();
202 paramEditor =
203 new ServiceParamEditor(ProbeFactory.getFactory(svcname));
204 paramEditor.setBorder(
205 new TitledBorder("Parameters for "+svcname));
206
207 // add it to the main panel
208 c.gridx = 0; c.gridy = 4; c.gridwidth = 6; c.gridheight = 6;
209 add(paramEditor,c);
210
211 // revalidate the container and fix up window size
212 validate();
213 packParentWindow();
214 }
215
216 public String getEditorTitle() {
217 return editorTitle;
218 }
219
220 private void packParentWindow() {
221 Component curComponent = this;
222 while (curComponent != null && !(curComponent instanceof Window)) {
223 curComponent = curComponent.getParent();
224 }
225 if (curComponent != null) ((Window)curComponent).pack();
226 }
227 }