/*
 * Copyright 2008-2011 the original author or authors.
 *
 * 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 info.joseluismartin.gui.form;

import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.util.ArrayList;

/**
 * FocusTraversalProvider for FormBuilder
 * 
 * @author Jose Luis Martin - (jlm@joseluismartin.info)
 */
public class FormFocusTransversalPolicy extends FocusTraversalPolicy {
	
	ArrayList<Component> components = new ArrayList<Component>();
	
	@Override
	public Component getComponentAfter(Container aContainer,
			Component aComponent) {
	
		int index = components.indexOf(aComponent);
		index++;
		
		if (index < components.size() && index >= 0) {
			Component c =  components.get(index);
			if (c.isEnabled()) 
				return c;
			else 
				return getComponentAfter(aContainer, c);
		}
		
		return getFirstComponent(aContainer);
	}

	@Override
	public Component getComponentBefore(Container aContainer,
			Component aComponent) {
		
		int index = components.indexOf(aComponent);
		index--;
		
		if (index < components.size() && index >= 0) {
			Component c =  components.get(index);
			if (c.isEnabled()) 
				return c;
			else 
				return getComponentBefore(aContainer, c);
		}
		
		return getLastComponent(aContainer);
	}

	@Override
	public Component getDefaultComponent(Container aContainer) {
		if (components.size() > 0) {
			Component c = components.get(0);
			if (c.isEnabled())
				return c;
			else 
				return getComponentAfter(aContainer, c);
		}
		
		return null;
	}

	@Override
	public Component getFirstComponent(Container aContainer) {
		return getDefaultComponent(aContainer);
	}

	@Override
	public Component getLastComponent(Container aContainer) {
		if (!components.isEmpty()) {
			Component c = components.get(components.size() - 1);
			if (c.isEnabled())
				return c;
			else
				return getComponentBefore(aContainer, c);
		}
		
		return null;
	}

	public void add(Component c) {
		components.add(c);
	}
}

@@ -774,16 +774,47 @@
 yList;%0A%0A
+import javax.swing.JComboBox;%0A%0A
 /**%0A * F
@@ -1102,34 +1102,33 @@
 After(Container 
-aC
+c
 ontainer,%0A%09%09%09Com
@@ -1134,29 +1134,249 @@
 mponent 
-aComponent) %7B
+component) %7B%0A%09%09%0A%09%09// Fix awt bug looking for ComboBoxEditor instead ComboBox%0A%09%09// see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6205817%0A%09%09if (component.getParent() instanceof JComboBox)%0A%09%09%09%09component = component.getParent();
 %0A%09%0A%09%09int
@@ -1395,34 +1395,33 @@
 ponents.indexOf(
-aC
+c
 omponent);%0A%09%09ind
@@ -1586,34 +1586,33 @@
 tComponentAfter(
-aC
+c
 ontainer, c);%0A%09%09
@@ -1635,34 +1635,33 @@
 tFirstComponent(
-aC
+c
 ontainer);%0A%09%7D%0A%0A%09

Fix error bug with editable JComboBox.