package org.orienteer.core.method;

import java.io.Serializable;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.IModel;
import org.orienteer.core.component.BootstrapType;
import org.orienteer.core.component.IBootstrapAware;
import org.orienteer.core.widget.AbstractWidget;

/**
 * 
 * Panel for methods display.
 *
 */
public class MethodsView implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private List<IMethod> methods;

	private MethodPlace place;
	private RepeatingView externalList;
	private IModel<?> displayObjectModel;

	private BootstrapType bootstrapType;
	private boolean bootstrapTypeOverriden = false;
	
	public MethodsView(RepeatingView externalList, IModel<?> displayObjectModel,MethodPlace place) {
		this.externalList = externalList;
		this.displayObjectModel = displayObjectModel;
		this.place = place;
	}
	
	public void loadMethods(){
		AbstractWidget<?> widget = externalList.findParent(AbstractWidget.class);
		methods = MethodManager.get().getMethods(new MethodBaseData(displayObjectModel,widget,place));
		for ( IMethod method : methods) {
			Component component = method.getDisplayComponent(externalList.newChildId()); 
			if (component instanceof IBootstrapAware && bootstrapTypeOverriden){
				((IBootstrapAware)component).setBootstrapType(bootstrapType);
			}
			externalList.add(component);
		}
	}
	
	/**
	 * Use it only before loadMethods
	 * @param type
	 */	
	public MethodsView overrideBootstrapType(BootstrapType bootstrapType){
		this.bootstrapType = bootstrapType;
		bootstrapTypeOverriden = true;
		return this;
	}


	public List<IMethod> getMethods() {
		return methods;
	}
	
}

@@ -1283,16 +1283,43 @@
 Id()); %0A
+%09%09%09if (component !=null)%7B%0A%09
 %09%09%09if (c
@@ -1386,16 +1386,17 @@
 n)%7B%0A%09%09%09%09
+%09
 ((IBoots
@@ -1452,18 +1452,20 @@
 pe);%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09exter
@@ -1488,16 +1488,21 @@
 onent);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%09

fix MethodsView -> empty display component error