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

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.sensia.relaxNG.RNGOptional;
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.panels.IRefreshHandler;
import com.sensia.tools.client.swetools.editors.sensorml.renderer.editor.panels.element.EditElementPanel;
import com.sensia.tools.client.swetools.editors.sensorml.renderer.editor.panels.element.EditSectionElementPanel;
import com.sensia.tools.client.swetools.editors.sensorml.utils.Utils;

public class RNGOptionalPanel extends AbstractPanel<RNGOptional>{

	private Panel patternContainer;
	private Panel headerPanel;
	private Label addButton;
	
	public RNGOptionalPanel(final RNGOptional tag,final IRefreshHandler refreshHandler) {
		super(tag);
		container = new HorizontalPanel();
		patternContainer = new HorizontalPanel();
		
		final String label = Utils.findLabel(tag);
		
		addButton = new Label();
		headerPanel = new HorizontalPanel();
		container.add(headerPanel);
		container.add(patternContainer);
		
		if(tag.isSelected()) {
			addButton.addStyleName("rng-optional-select-remove");
			headerPanel.add(addButton);
		} else {
			addButton.addStyleName("rng-optional-select-add");
			headerPanel.add(new Label(label));
			headerPanel.add(addButton);
		}
		
		addButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(addButton.getStyleName().contains("rng-optional-select-add")){
					tag.setSelected(true);
				} else {
					tag.setSelected(false);
				}
				
				if(refreshHandler != null) {
					refreshHandler.refresh();
				}
				
			}
		});
		
		patternContainer.addStyleName("rng-optional-pattern");
	}
	
	@Override
	public String getName() {
		return "";
	}

	@Override
	protected void addInnerElement(IPanel<? extends RNGTag> element) {
		headerPanel.addStyleName("rng-disclosure");
		headerPanel.clear();
		headerPanel.add(addButton);
		headerPanel.add(element.getPanel());
	}

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

}

@@ -1999,16 +1999,314 @@
 %09%7D%0A%09%09%09%09%0A
+%09%09%09%09/*RNGRendererSML newRenderer = new RNGRendererSML();%0A%09%09%09%09for(RNGTag child : tag.getChildren()) %7B%0A%09%09%09%09%09child.accept(newRenderer);%0A%09%09%09%09%7D%0A%09%09%09%09patternContainer.add(newRenderer.getRoot().getPanel());*/%0A%09%09%09%09//TODO: use MVC or MVP to update the view%0A%09%09%09%09//ViewerPanel.getInstance(null).redraw();%0A%09%09%09%09%0A
 %09%09%09%09if(r
@@ -2326,24 +2326,49 @@
  != null) %7B%0A
+%09%09%09%09%09GWT.log(%22refresh%22);%0A
 %09%09%09%09%09refresh
@@ -2607,16 +2607,105 @@
 ment) %7B%0A
+%09%09if(element instanceof EditSectionElementPanel %7C%7C element instanceof RNGChoicePanel) %7B%0A%09
 %09%09header
@@ -2742,16 +2742,21 @@
 sure%22);%0A
+%09%09%7D %0A
 %09%09header
@@ -2839,16 +2839,62 @@
 nel());%0A
+%09%09//patternContainer.add(element.getPanel());%0A
 %09%7D%0A%0A%09@Ov

Apply optional for each child panels