package com.sensia.tools.client.swetools.editors.sensorml.panels.rng;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.ListBox;
import com.sensia.relaxNG.RNGChoice;
import com.sensia.relaxNG.RNGTag;
import com.sensia.tools.client.swetools.editors.sensorml.panels.AbstractPanel;
import com.sensia.tools.client.swetools.editors.sensorml.panels.IPanel;
import com.sensia.tools.client.swetools.editors.sensorml.utils.Utils;

public class RNGChoicePanel extends AbstractPanel<RNGChoice>{

	private ListBox choices;
	
	public RNGChoicePanel(RNGChoice tag) {
		super(tag);
		
		choices = new ListBox();
		container.add(choices);
		
		choices.addItem("");
		
		// add children name into the choice and
		List<RNGTag> children = tag.getItems();
		if(children != null){
			for(RNGTag child : children) {
				String label = Utils.findLabel(child);
				choices.addItem(label);
			}
		}
		
		if(getTag().getSelectedIndex() != -1) {
			choices.setSelectedIndex(getTag().getSelectedIndex()+1);
		}
		
		choices.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(choices.getSelectedIndex() == 0) {
					RNGChoicePanel.this.getTag().setSelectedIndex(-1);
				} else {
					RNGChoicePanel.this.getTag().setSelectedIndex(choices.getSelectedIndex()-1);
				}
			}
		});
	}
	
	@Override
	public String getName() {
		return "";
	}

	@Override
	protected void addInnerElement(IPanel<? extends RNGTag> element) {
		choices.addItem(element.getName());
	}

	@Override
	protected AbstractPanel<RNGChoice> newInstance() {
		// TODO Auto-generated method stub
		return null;
	}

}

@@ -238,16 +238,112 @@
 istBox;%0A
+import com.google.gwt.user.client.ui.Panel;%0Aimport com.google.gwt.user.client.ui.VerticalPanel;%0A
 import c
@@ -405,16 +405,57 @@
 RNGTag;%0A
+import com.sensia.relaxNG.RNGTagVisitor;%0A
 import c
@@ -757,16 +757,48 @@
 hoices;%0A
+%09private Panel paternContainer;%0A
 %09%0A%09publi
@@ -814,16 +814,22 @@
 cePanel(
+final 
 RNGChoic
@@ -837,23 +837,93 @@
  tag
-) %7B%0A%09%09super(tag
+, final RNGTagVisitor visitor) %7B%0A%09%09super(tag);%0A%09%09paternContainer = new VerticalPanel(
 );%0A%09
@@ -977,16 +977,50 @@
 oices);%0A
+%09%09container.add(paternContainer);%0A
 %09%09%0A%09%09cho
@@ -1478,24 +1478,53 @@
 nt event) %7B%0A
+%09%09%09%09paternContainer.clear();%0A
 %09%09%09%09if(choic
@@ -1700,24 +1700,71 @@
 Index()-1);%0A
+%09%09%09%09%09tag.getSelectedPattern().accept(visitor);%0A
 %09%09%09%09%7D%0A%09%09%09%7D%0A%09
@@ -1909,31 +1909,35 @@
 nt) %7B%0A%09%09
-choices.addItem
+paternContainer.add
 (element
@@ -1940,20 +1940,21 @@
 ment.get
-Name
+Panel
 ());%0A%09%7D%0A

refresh visitor when a value is selected