/* 
 * JBoss, Home of Professional Open Source 
 * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @author tags. All rights reserved. 
 * See the copyright.txt in the distribution for a 
 * full listing of individual contributors.
 *
 * This copyrighted material is made available to anyone wishing to use, 
 * modify, copy, or redistribute it subject to the terms and conditions 
 * of the GNU Lesser General Public License, v. 2.1. 
 * This program is distributed in the hope that it will be useful, but WITHOUT A 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details. 
 * You should have received a copy of the GNU Lesser General Public License, 
 * v.2.1 along with this distribution; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 */
package org.jboss.as.console.client.shared.subsys.logging;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import org.jboss.as.console.client.Console;
import org.jboss.ballroom.client.widgets.common.DefaultButton;
import org.jboss.ballroom.client.widgets.forms.FormAdapter;
import org.jboss.ballroom.client.widgets.forms.FormValidation;
import org.jboss.ballroom.client.widgets.window.DefaultWindow;

/**
 * Generic popup window that executes a command on the commandAdapter when the user clicks the Save button.
 *
 * @author Stan Silvert ssilvert@redhat.com (C) 2011 Red Hat Inc.
 */
public abstract class LoggingPopupWindow<T> extends DefaultWindow {
    
    protected EntityBridge<T> bridge;
    protected FormAdapter<T> form;
    
    public LoggingPopupWindow(String title, FormAdapter<T> form, EntityBridge<T> bridge) {
        super(title);
        this.form = form;
        this.bridge = bridge;
        setWidth(320);
        setHeight(260);

        addCloseHandler(new CloseHandler<PopupPanel>() {
            @Override
            public void onClose(CloseEvent<PopupPanel> event) {
                LoggingPopupWindow.this.hide();
            }
        });
        
        setWidget(makeWidget());
        setGlassEnabled(true);
        center();
        hide();
    }
    
    private Widget makeWidget() {
        VerticalPanel layout = new VerticalPanel();
        layout.getElement().setAttribute("style", "width:95%, margin:15px;");
        
        layout.add(form.asWidget());

        Label cancel = new Label(Console.CONSTANTS.common_label_cancel());
        cancel.setStyleName("html-link");
        cancel.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                LoggingPopupWindow.this.hide();
            }
        });

        DefaultButton submit = new DefaultButton(Console.CONSTANTS.common_label_save(), new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                FormValidation validation = form.validate();
                if (!validation.hasErrors()) {
                    Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
                        @Override
                        public void execute() {
                            doCommand(form);
                            LoggingPopupWindow.this.hide();
                        }
                    });
                }
            } // end if
        });

        HorizontalPanel options = new HorizontalPanel();
        options.getElement().setAttribute("style", "margin-top:10px;width:100%");

        HTML spacer = new HTML("&nbsp;");
        options.add(spacer);

        options.add(submit);
        options.add(spacer);
        options.add(cancel);
        cancel.getElement().getParentElement().setAttribute("style", "vertical-align:middle");
        submit.getElement().getParentElement().setAttribute("align", "right");
        submit.getElement().getParentElement().setAttribute("width", "100%");

        layout.add(options);
        
        return layout;
    }

    public void setNewBean() {
        T newBean = bridge.newEntity().as();
        form.edit(newBean);
    }
    
    public void setBean(T bean) {
        form.edit(bean);
    }
    
    /**
     * Execute a command from the EntityAdapter
     * @param form The form that was just edited.
     */
    protected abstract void doCommand(FormAdapter<T> form);
}

@@ -1333,363 +1333,115 @@
 .ui.
-HTML;%0Aimport com.google.gwt.user.client.ui.HorizontalPanel;%0Aimport com.google.gwt.user.client.ui.Label;%0Aimport com.google.gwt.user.client.ui.PopupPanel;%0Aimport com.google.gwt.user.client.ui.VerticalPanel;%0Aimport com.google.gwt.user.client.ui.Widget;%0Aimport org.jboss.as.console.client.Console;%0Aimport org.jboss.ballroom.client.widgets.common.DefaultButton
+PopupPanel;%0Aimport com.google.gwt.user.client.ui.VerticalPanel;%0Aimport com.google.gwt.user.client.ui.Widget
 ;%0Aim
@@ -1623,16 +1623,151 @@
 tWindow;
+%0Aimport org.jboss.ballroom.client.widgets.window.DialogueOptions;%0Aimport org.jboss.ballroom.client.widgets.window.WindowContentBuilder;
 %0A%0A/**%0A *
@@ -2753,266 +2753,124 @@
 out.
-getElement().setAttribute(%22style%22, %22width:95%25, margin:15px;%22);%0A        %0A        layout.add(form.asWidget());%0A%0A        Label cancel = new Label(Console.CONSTANTS.common_label_cancel());%0A        cancel.setStyleName(%22html-link%22);%0A        cancel.addClick
+setStyleName(%22default-window-content%22);%0A%0A        layout.add(form.asWidget());%0A%0A        ClickHandler cancel
 Handler
-(
+ = 
 new 
@@ -3024,33 +3024,32 @@
      %7D%0A        %7D
-)
 ;%0A%0A        Defau
@@ -3047,87 +3047,36 @@
     
-DefaultButton submit = new DefaultButton(Console.CONSTANTS.common_label_save(),
+ClickHandler submitHandler =
  new
@@ -3651,17 +3651,16 @@
        %7D
-)
 ;%0A%0A     
@@ -3666,402 +3666,120 @@
     
-HorizontalPanel options = new HorizontalPanel();%0A        options.getElement().setAttribute(%22style%22, %22margin-top:10px;width:100%25%22);%0A%0A        HTML spacer = new HTML(%22&nbsp;%22);%0A        options.add(spacer);%0A%0A        options.add(submit);%0A        options.add(spacer);%0A        options.add(cancel);%0A        cancel.getElement().getParentElement().setAttribute(%22style%22, %22vertical-align:middle%22
+DialogueOptions options = new DialogueOptions(%0A                submitHandler, cancelHandler%0A        
 );%0A
+%0A
         
 subm
@@ -3778,185 +3778,56 @@
     
-submit.getElement().getParentElement().setAttribute(%22align%22, %22right%22);%0A        submit.getElement().getParentElement().setAttribute(%22width%22, %22100%25%22);%0A%0A        
+return new WindowContentBuilder(
 layout
-.add(
+, 
 options)
 ;%0A  
@@ -3826,41 +3826,18 @@
 ons)
-;%0A        %0A        return layout;
+.build();%0A
 %0A   

use WinowContentBuilder form logging windows