/*
 * Copyright 2010 JBoss Inc
 *
 * 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.drools.guvnor.client.util;

import java.util.Iterator;

import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.HasCloseHandlers;
import com.google.gwt.event.logical.shared.HasOpenHandlers;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

/**
 * Disclosure panel with rounded corners in header.
 * 
 * Using this class we don't need to set the header and event handlers for the header everywhere we use DisclosurePanels.
 */
public class DecoratedDisclosurePanel extends Composite
    implements
    HasWidgets,
    HasOpenHandlers<DisclosurePanel>,
    HasCloseHandlers<DisclosurePanel> {

    private final DisclosurePanel widget = new DisclosurePanel();

    public DecoratedDisclosurePanel(String headerText) {

        widget.setAnimationEnabled( true );
        widget.setHeader( createHeader( headerText ) );

        initWidget( widget );
    }

    private LazyStackPanelHeader createHeader(String headerText) {
        final LazyStackPanelHeader header = new LazyStackPanelHeader( headerText );

        widget.addOpenHandler( new OpenHandler<DisclosurePanel>() {
            public void onOpen(OpenEvent<DisclosurePanel> event) {
                header.expand();
            }
        } );
        widget.addCloseHandler( new CloseHandler<DisclosurePanel>() {
            public void onClose(CloseEvent<DisclosurePanel> event) {
                header.collapse();
            }
        } );

        return header;
    }

    public void add(Widget w) {
        widget.add( w );
    }

    public void setContent(Widget content) {
        widget.setContent( content );
    }

    public HandlerRegistration addOpenHandler(OpenHandler<DisclosurePanel> openHandler) {
        return widget.addOpenHandler( openHandler );
    }

    public HandlerRegistration addCloseHandler(CloseHandler<DisclosurePanel> handler) {
        return widget.addCloseHandler( handler );
    }

    public void clear() {
        widget.clear();
    }

    public Iterator<Widget> iterator() {
        return widget.iterator();
    }

    public boolean remove(Widget w) {
        return widget.remove( w );
    }

    public void setOpen(boolean b) {
        widget.setOpen( b );
    }

    public boolean isOpen() {
        return widget.isOpen();
    }
}
