01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.jmeter.control;
20:
21: import java.io.Serializable;
22:
23: import org.apache.jmeter.testelement.property.StringProperty;
24:
25: public class SwitchController extends InterleaveControl implements
26: Serializable {
27: private final static String SWITCH_VALUE = "SwitchController.value";
28:
29: public SwitchController() {
30: super ();
31: this .setStyle(USE_SUB_CONTROLLERS);
32: }
33:
34: public void reInitialize() {
35: super .reInitialize();
36: current = getSelectionAsInt();
37: }
38:
39: /**
40: * @see org.apache.jmeter.control.GenericController#resetCurrent()
41: */
42: protected void resetCurrent() {
43: int c = getSubControllers().size();
44: if (c > 0) {
45: current = getSelectionAsInt();
46: } else {
47: current = 0;
48: }
49: }
50:
51: /**
52: * @see org.apache.jmeter.control.GenericController#incrementCurrent()
53: */
54: protected void incrementCurrent() {
55: super .incrementCurrent();
56: current = getSelectionAsInt();
57: }
58:
59: public void setSelection(String inputValue) {
60: setProperty(new StringProperty(SWITCH_VALUE, inputValue));
61: }
62:
63: private int getSelectionAsInt() {
64: int ret;
65: getProperty(SWITCH_VALUE).recoverRunningVersion(null);
66: String sel = getSelection();
67: try {
68: ret = Integer.parseInt(sel);
69: } catch (NumberFormatException e) {
70: ret = 0;
71: }
72: if (ret < 0 || ret >= getSubControllers().size()) {
73: ret = 0;
74: }
75: return ret;
76: }
77:
78: public String getSelection() {
79: return getPropertyAsString(SWITCH_VALUE);
80: }
81: }
|