001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.control.gui;
020:
021: import java.awt.BorderLayout;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.util.Arrays;
025: import java.util.Collection;
026:
027: import javax.swing.Box;
028: import javax.swing.JButton;
029: import javax.swing.JLabel;
030: import javax.swing.JPanel;
031: import javax.swing.JTextField;
032:
033: import org.apache.jmeter.control.gui.LogicControllerGui;
034: import org.apache.jmeter.gui.JMeterGUIComponent;
035: import org.apache.jmeter.gui.UnsharedComponent;
036: import org.apache.jmeter.gui.util.HorizontalPanel;
037: import org.apache.jmeter.gui.util.MenuFactory;
038: import org.apache.jmeter.protocol.http.control.HttpMirrorControl;
039: import org.apache.jmeter.testelement.TestElement;
040: import org.apache.jmeter.util.JMeterUtils;
041: import org.apache.jorphan.logging.LoggingManager;
042: import org.apache.log.Logger;
043:
044: public class HttpMirrorControlGui extends LogicControllerGui implements
045: JMeterGUIComponent, ActionListener, UnsharedComponent {
046: private static transient Logger log = LoggingManager
047: .getLoggerForClass();
048:
049: private JTextField portField;
050:
051: private JButton stop, start;
052:
053: private static final String ACTION_STOP = "stop"; // $NON-NLS-1$
054:
055: private static final String ACTION_START = "start"; // $NON-NLS-1$
056:
057: private HttpMirrorControl mirrorController;
058:
059: public HttpMirrorControlGui() {
060: super ();
061: log.debug("Creating HttpMirrorControlGui");
062: init();
063: }
064:
065: public TestElement createTestElement() {
066: mirrorController = new HttpMirrorControl();
067: log.debug("creating/configuring model = " + mirrorController);
068: modifyTestElement(mirrorController);
069: return mirrorController;
070: }
071:
072: /**
073: * Modifies a given TestElement to mirror the data in the gui components.
074: *
075: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
076: */
077: public void modifyTestElement(TestElement el) {
078: configureTestElement(el);
079: if (el instanceof HttpMirrorControl) {
080: mirrorController = (HttpMirrorControl) el;
081: mirrorController.setPort(portField.getText());
082: }
083: }
084:
085: public String getLabelResource() {
086: return "httpmirror_title"; // $NON-NLS-1$
087: }
088:
089: public Collection getMenuCategories() {
090: return Arrays
091: .asList(new String[] { MenuFactory.NON_TEST_ELEMENTS });
092: }
093:
094: public void configure(TestElement element) {
095: log.debug("Configuring gui with " + element);
096: super .configure(element);
097: mirrorController = (HttpMirrorControl) element;
098: portField.setText(mirrorController.getPortString());
099: repaint();
100: }
101:
102: public void actionPerformed(ActionEvent action) {
103: String command = action.getActionCommand();
104:
105: if (command.equals(ACTION_STOP)) {
106: mirrorController.stopHttpMirror();
107: stop.setEnabled(false);
108: start.setEnabled(true);
109: } else if (command.equals(ACTION_START)) {
110: modifyTestElement(mirrorController);
111: mirrorController.startHttpMirror();
112: start.setEnabled(false);
113: stop.setEnabled(true);
114: }
115: }
116:
117: private void init() {
118: setLayout(new BorderLayout(0, 5));
119: setBorder(makeBorder());
120:
121: add(makeTitlePanel(), BorderLayout.NORTH);
122:
123: JPanel mainPanel = new JPanel(new BorderLayout());
124:
125: Box myBox = Box.createVerticalBox();
126: myBox.add(createPortPanel());
127: mainPanel.add(myBox, BorderLayout.NORTH);
128:
129: mainPanel.add(createControls(), BorderLayout.CENTER);
130:
131: add(mainPanel, BorderLayout.CENTER);
132: }
133:
134: private JPanel createControls() {
135: start = new JButton(JMeterUtils.getResString("start")); // $NON-NLS-1$
136: start.addActionListener(this );
137: start.setActionCommand(ACTION_START);
138: start.setEnabled(true);
139:
140: stop = new JButton(JMeterUtils.getResString("stop")); // $NON-NLS-1$
141: stop.addActionListener(this );
142: stop.setActionCommand(ACTION_STOP);
143: stop.setEnabled(false);
144:
145: JPanel panel = new JPanel();
146: panel.add(start);
147: panel.add(stop);
148: return panel;
149: }
150:
151: private JPanel createPortPanel() {
152: portField = new JTextField(HttpMirrorControl.DEFAULT_PORT_S, 8);
153: portField.setName(HttpMirrorControl.PORT);
154:
155: JLabel label = new JLabel(JMeterUtils.getResString("port")); // $NON-NLS-1$
156: label.setLabelFor(portField);
157:
158: HorizontalPanel panel = new HorizontalPanel();
159: panel.add(label);
160: panel.add(portField);
161:
162: panel.add(Box.createHorizontalStrut(10));
163:
164: return panel;
165: }
166:
167: public void clearGui() {
168: super.clearGui();
169: portField.setText(HttpMirrorControl.DEFAULT_PORT_S);
170: }
171: }
|