Source code: org/altara/mars/swingui/ServiceParamEditor.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 ServiceParamEditor extends JPanel {
36
37 private String[] paramNames;
38 private JTextField[] paramValFields;
39
40 public ServiceParamEditor(ProbeFactory factory) {
41
42 // set up the layout manager
43 setLayout(new GridBagLayout());
44 GridBagConstraints c = new GridBagConstraints();
45 c.anchor = c.NORTHWEST; c.fill = c.BOTH;
46 c.weightx = 1.0; c.weighty = 0.0;
47 c.insets = new Insets(4,4,6,4);
48
49 // Get lists of variables and values
50 paramNames = factory.getServiceParamNames();
51 String[] paramLabels = factory.getServiceParamLabels();
52
53 // check for null
54 if (paramNames == null) {
55 c.gridx = 0; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1;
56 add(new JLabel("This service has no parameters"),c);
57 return;
58 }
59
60 // layout labels and param values in panel
61 paramValFields = new JTextField[paramNames.length];
62 c.gridwidth = 1; c.gridheight = 1;
63 for (int i = 0; i < paramNames.length; i++) {
64 paramValFields[i] = new JTextField(15);
65 c.gridy = i;
66 c.gridx = 0;
67 add(new JLabel(paramLabels[i]),c);
68 c.gridy = i;
69 c.gridx = 1;
70 add(paramValFields[i],c);
71 }
72 }
73
74 public void fillParams(Service service) {
75 // check for no params
76 if (paramNames == null) return;
77
78 // otherwise fill in required parameters from the service
79 for (int i = 0; i < paramNames.length; i++) {
80 String val = service.getParameter(paramNames[i]);
81 if (val == null) val = "";
82 paramValFields[i].setText(val);
83 }
84 }
85
86 public void commitParams(Service service) {
87 // check for no params
88 if (paramNames == null) return;
89
90 // otherwise fill in the required parameters to the service
91 for (int i = 0; i < paramNames.length; i++) {
92 String val = paramValFields[i].getText();
93 if (val.length() == 0) val = null;
94 service.setParameter(paramNames[i],val);
95 }
96 }
97
98 public HashMap getParamMap() {
99 HashMap out = new HashMap();
100 if (paramNames == null) {
101 return out;
102 }
103
104 for (int i = 0; i < paramNames.length; i++) {
105 String val = paramValFields[i].getText();
106 if (val.length() == 0) val = null;
107 out.put(paramNames[i],val);
108 }
109 return out;
110 }
111 }