com.dawg6.gwt.client.ApplicationPanel.java Source code

Java tutorial

Introduction

Here is the source code for com.dawg6.gwt.client.ApplicationPanel.java

Source

/*******************************************************************************
 * Copyright (c) 2014, 2015 Scott Clarke (scott@dawg6.com).
 *
 * This file is part of Dawg6's Common GWT Libary.
 *
 * Dawg6's Common GWT Libary is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dawg6's Common GWT Libary is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package com.dawg6.gwt.client;

import com.dawg6.gwt.client.util.ClientData;
import com.dawg6.gwt.client.util.LocalStorageAdapter;
import com.dawg6.gwt.common.util.AsyncTaskHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ApplicationPanel extends Composite {

    public static final int OK = 1;
    public static final int YES = 2;
    public static final int NO = 4;
    public static final int APPLY_CHANGES = 8;
    public static final int REVERT = 16;
    public static final int DEFAULT = 32;
    public static final int SAVE = 64;
    public static final int RESET = 128;
    public static final int CANCEL = 256;
    public static final int CUSTOM1 = 512;
    public static final int CUSTOM2 = 1024;
    public static final int CUSTOM3 = 2048;

    protected ClientData clientData;

    private static WidgetFactory widgetFactory;

    protected static final AsyncTaskHandler DEFAULT_HANDLER = AsyncTaskHandler.Default;

    public ApplicationPanel() {
        this(null);
    }

    public ApplicationPanel(String applicationName) {
        clientData = new LocalStorageAdapter(applicationName);
    }

    public static WidgetFactory getWidgetFactory() {
        if (widgetFactory == null)
            widgetFactory = new DefaultWidgetFactory();

        return widgetFactory;
    }

    public static void setWidgetFactory(WidgetFactory f) {
        widgetFactory = f;
    }

    public static DialogBox showErrorDialog(String title, String message) {
        return showDialogBox(title, message);
    }

    public static DialogBox showErrorDialog(String message) {
        return showErrorDialog("Error", message);
    }

    public static DialogBox showInfoDialog(String title, String message) {
        return showDialogBox(title, message);
    }

    public static DialogBox showInfoDialog(String message) {
        return showInfoDialog("Information", message);
    }

    public static DialogBox showConfirmDialog(String title, String message, int buttons,
            DialogBoxResultHandler handler) {
        return showDialogBox(title, message, buttons, handler);
    }

    public interface DialogBoxResultHandler {
        void dialogBoxResult(int result);
    }

    public static DialogBox showDialogBox(String title, String message) {
        return showDialogBox(title, message, OK, null);
    }

    public static DialogBox showDialogBox(String title, String message, int buttons,
            final DialogBoxResultHandler handler) {
        Label label = new Label(message);
        return showDialogBox(title, label, buttons, handler);
    }

    protected static class ButtonInfo {
        public int id;
        public String label;

        public ButtonInfo(int id, String name) {
            this.id = id;
            this.label = name;
        }
    }

    protected static void setButtonLabel(int button, String label) {
        for (ButtonInfo b : allButtons) {
            if (b.id == button)
                b.label = label;
        }
    }

    protected static final ButtonInfo[] allButtons = { new ButtonInfo(OK, "OK"), new ButtonInfo(YES, "Yes"),
            new ButtonInfo(APPLY_CHANGES, "Apply Changes"), new ButtonInfo(SAVE, "Save"), new ButtonInfo(NO, "No"),
            new ButtonInfo(DEFAULT, "Default"), new ButtonInfo(REVERT, "Revert"), new ButtonInfo(RESET, "Reset"),
            new ButtonInfo(CANCEL, "Cancel"), new ButtonInfo(CUSTOM1, "Custom1"),
            new ButtonInfo(CUSTOM2, "Custom2"), new ButtonInfo(CUSTOM3, "Custom3"), };

    public static DialogBox showDialogBox(String title, IsWidget component, int buttons,
            final DialogBoxResultHandler handler) {
        // Create the popup dialog box
        final DialogBox dialogBox = createDialogBox();
        dialogBox.setText(title);
        dialogBox.setAnimationEnabled(true);
        dialogBox.setModal(false);
        FlexTable table = new FlexTable();
        table.setWidget(0, 0, component);
        dialogBox.setWidget(table);

        HorizontalPanel row = new HorizontalPanel();
        row.setSpacing(5);
        row.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        row.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        table.setWidget(1, 0, row);

        boolean firstButton = true;

        if (buttons == 0)
            buttons = OK;

        for (final ButtonInfo b : allButtons) {
            if ((buttons > 0) && ((buttons & b.id) != 0)) {
                final Button button = createDialogBoxButton(b.label);

                button.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        dialogBox.hide();

                        if (handler != null)
                            handler.dialogBoxResult(b.id);
                    }
                });
                row.add(button);

                if (firstButton) {
                    button.setFocus(true);
                    firstButton = false;
                }
            }
        }

        dialogBox.center();

        return dialogBox;
    }

    protected static Button createDialogBoxButton(String text) {
        return getWidgetFactory().createDialogBoxButton(text);
    }

    protected static DialogBox createDialogBox() {
        return getWidgetFactory().createDialogBox();
    }

    public static AsyncTaskHandler showWaitDialogBox(String title, String message) {

        VerticalPanel panel = new VerticalPanel();
        HorizontalPanel row = new HorizontalPanel();
        row.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        row.setSpacing(5);
        panel.add(row);

        Image image = new Image("images/Progressbar.gif");
        image.setSize("64px", "16px");
        row.add(image);

        if (message != null) {
            Label label = new Label(message);
            row.add(label);
        }

        final DialogBox dialogBox = showDialogBox(title, panel, -1, null);

        return new AsyncTaskHandler() {

            @Override
            public void taskCompleted() {
                dialogBox.hide();
            }
        };

    }

    public static void showWaitCursor() {
        // DOM.setStyleAttribute(RootPanel.getBodyElement(), "cursor", "wait");
    }

    public static void showDefaultCursor() {
        // DOM.setStyleAttribute(RootPanel.getBodyElement(), "cursor",
        // "default");
    }

    public static String escapeText(String text) {
        SafeHtmlBuilder builder = new SafeHtmlBuilder();
        builder.appendEscaped(text);
        return builder.toSafeHtml().asString();
    }
}