/*
 * Licensed 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.hobsoft.microbrowser.jsoup;

import org.jsoup.nodes.Element;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Form control wrapper for a jsoup {@code Element}.
 */
class JsoupControl
{
	// ----------------------------------------------------------------------------------------------------------------
	// fields
	// ----------------------------------------------------------------------------------------------------------------

	private final Element element;
	
	private String value;

	// ----------------------------------------------------------------------------------------------------------------
	// constructors
	// ----------------------------------------------------------------------------------------------------------------

	public JsoupControl(Element element)
	{
		this.element = checkNotNull(element, "element");
		
		value = element.val();
	}
	
	// ----------------------------------------------------------------------------------------------------------------
	// public methods
	// ----------------------------------------------------------------------------------------------------------------

	public String getName()
	{
		return element.attr("name");
	}

	public String getValue()
	{
		return value;
	}

	public void setValue(String value)
	{
		checkArgument(!isHidden(), "Cannot set hidden control value: %s", getName());
		
		this.value = checkNotNull(value, "value");
	}
	
	// ----------------------------------------------------------------------------------------------------------------
	// private methods
	// ----------------------------------------------------------------------------------------------------------------

	private boolean isHidden()
	{
		return "hidden".equals(getType());
	}

	private String getType()
	{
		return element.attr("type");
	}
}

@@ -1129,32 +1129,8 @@
 ;%0A%09%0A
-%09private String value;%0A%0A
 %09// 
@@ -1473,36 +1473,8 @@
 %22);%0A
-%09%09%0A%09%09value = element.val();%0A
 %09%7D%0A%09
@@ -1829,21 +1829,29 @@
 %09return 
+element.
 val
-ue
+()
 ;%0A%09%7D%0A%0A%09p
@@ -1973,24 +1973,8 @@
 ;%0A%09%09
-%0A%09%09this.value = 
 chec
@@ -1995,24 +1995,49 @@
 , %22value%22);%0A
+%09%09%0A%09%09element.val(value);%0A
 %09%7D%0A%09%0A%09// ---

Use jsoup element to maintain form control value