/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.modifiers.gui;

import javax.swing.JCheckBox;

import org.apache.jmeter.modifiers.CounterConfig;
import org.apache.jmeter.processor.gui.AbstractPreProcessorGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.gui.JLabeledTextField;
import org.apache.jorphan.gui.layout.VerticalLayout;

/**
 * @version $Revision$ on $Date$
 */
public class CounterConfigGui extends AbstractPreProcessorGui {
	private JLabeledTextField startField, incrField, endField, varNameField, formatField;

	private JCheckBox perUserField;

	public CounterConfigGui() {
		super();
		init();
	}

	public String getLabelResource() {
		return "counter_config_title";
	}

	/**
	 * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
	 */
	public TestElement createTestElement() {
		CounterConfig config = new CounterConfig();
		modifyTestElement(config);
		return config;
	}

	/**
	 * Modifies a given TestElement to mirror the data in the gui components.
	 * 
	 * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
	 */
	public void modifyTestElement(TestElement c) {
		if (c instanceof CounterConfig) {
			CounterConfig config = (CounterConfig) c;
			config.setStart(startField.getText());
			// Bug 22820 if (endField.getText().length() > 0)
			{
				config.setEnd(endField.getText());
			}
			config.setIncrement(incrField.getText());
			config.setVarName(varNameField.getText());
            config.setFormat(formatField.getText());
			config.setIsPerUser(perUserField.isSelected());
		}
		super.configureTestElement(c);
	}
    
    /**
     * Implements JMeterGUIComponent.clear
     */
    public void clear() {
        super.clear();
        
        startField.setText(""); //$NON-NLS-1$
        incrField.setText(""); //$NON-NLS-1$
        endField.setText(""); //$NON-NLS-1$
        varNameField.setText(""); //$NON-NLS-1$
        formatField.setText(""); //$NON-NLS-1$
        perUserField.setSelected(false);
    }

	public void configure(TestElement element) {
		super.configure(element);
		CounterConfig config = (CounterConfig) element;
		startField.setText(config.getPropertyAsString(CounterConfig.START));
		endField.setText(config.getPropertyAsString(CounterConfig.END));
		incrField.setText(config.getPropertyAsString(CounterConfig.INCREMENT));
        formatField.setText(config.getFormat());
		varNameField.setText(config.getVarName());
		perUserField.setSelected(config.isPerUser());
	}

	private void init() {
		setBorder(makeBorder());
		setLayout(new VerticalLayout(5, VerticalLayout.LEFT));

		startField = new JLabeledTextField(JMeterUtils.getResString("start") + "          ");// TODO
																								// proper
																								// alignment
		incrField = new JLabeledTextField(JMeterUtils.getResString("increment"));
		endField = new JLabeledTextField(JMeterUtils.getResString("max"));
		varNameField = new JLabeledTextField(JMeterUtils.getResString("var_name"));
        formatField = new JLabeledTextField(JMeterUtils.getResString("format"));
		perUserField = new JCheckBox(JMeterUtils.getResString("counter_per_user"));

		add(makeTitlePanel());
		add(startField);
		add(incrField);
		add(endField);
        add(formatField);
		add(varNameField);
		add(perUserField);
	}
}

@@ -1185,49 +1185,8 @@
 t;%0A%0A
-/**%0A * @version $Revision$ on $Date$%0A */%0A
 publ
@@ -1489,16 +1489,29 @@
 _title%22;
+//$NON-NLS-1$
 %0A%09%7D%0A%0A%09/*
@@ -3389,12 +3389,12 @@
 out.
-LEFT
+BOTH
 ));%0A
@@ -3468,103 +3468,23 @@
 rt%22)
- + %22          %22);// TODO%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09// proper%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09// alignment
+);//$NON-NLS-1$
 %0A%09%09i
@@ -3555,16 +3555,29 @@
 ment%22));
+//$NON-NLS-1$
 %0A%09%09endFi
@@ -3637,16 +3637,29 @@
 %22max%22));
+//$NON-NLS-1$
 %0A%09%09varNa
@@ -3728,16 +3728,29 @@
 name%22));
+//$NON-NLS-1$
 %0A       
@@ -3822,16 +3822,29 @@
 rmat%22));
+//$NON-NLS-1$
 %0A%09%09perUs
@@ -3913,16 +3913,29 @@
 user%22));
+//$NON-NLS-1$
 %0A%0A%09%09add(

Fix GUI alignment