com.netxforge.screens.editing.base.dialogs.MessageDialogWithResult.java Source code

Java tutorial

Introduction

Here is the source code for com.netxforge.screens.editing.base.dialogs.MessageDialogWithResult.java

Source

/*******************************************************************************
 * Copyright (c) 24 feb. 2014 NetXForge.
 * 
 * This program 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.
 * 
 * This program 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/>
 * 
 * Contributors: Christophe Bouhier - initial API and implementation and/or
 * initial documentation
 *******************************************************************************/
package com.netxforge.screens.editing.base.dialogs;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Shell;

/**
 * We need this dialog, as the base implementation doesn't differentiate between
 * {@link IDialogConstants#CANCEL_ID} and {@link IDialogConstants#NO_ID}
 * 
 * @author Christophe Bouhier
 */
public class MessageDialogWithResult extends MessageDialog {

    public MessageDialogWithResult(Shell parentShell, String dialogTitle, Image dialogTitleImage,
            String dialogMessage, int dialogImageType, String[] dialogButtonLabels, int defaultIndex) {
        super(parentShell, dialogTitle, dialogTitleImage, dialogMessage, dialogImageType, dialogButtonLabels,
                defaultIndex);
        super.setShellStyle(this.getShellStyle() | SWT.RESIZE);
    }

    /**
     * Convenience method to open a simple dialog as specified by the
     * <code>kind</code> flag.
     * 
     * @param kind
     *            the kind of dialog to open, one of {@link #ERROR},
     *            {@link #INFORMATION}, {@link #QUESTION}, {@link #WARNING},
     *            {@link #CONFIRM}, or {@link #QUESTION_WITH_CANCEL}.
     * @param parent
     *            the parent shell of the dialog, or <code>null</code> if none
     * @param title
     *            the dialog's title, or <code>null</code> if none
     * @param message
     *            the message
     * @param style
     *            {@link SWT#NONE} for a default dialog, or {@link SWT#SHEET}
     *            for a dialog with sheet behavior
     * @return <code>true</code> if the user presses the OK or Yes button,
     *         <code>false</code> otherwise
     * @since 3.5
     */
    public static int openWithResult(int kind, Shell parent, String title, String message, int style) {
        MessageDialog dialog = new MessageDialog(parent, title, null, message, kind, getButtonLabels(kind), 0);
        return dialog.open();
    }

    public static String[] getButtonLabels(int kind) {
        String[] dialogButtonLabels;
        switch (kind) {
        case ERROR:
        case INFORMATION:
        case WARNING: {
            dialogButtonLabels = new String[] { IDialogConstants.OK_LABEL };
            break;
        }
        case CONFIRM: {
            dialogButtonLabels = new String[] { IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL };
            break;
        }
        case QUESTION: {
            dialogButtonLabels = new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL };
            break;
        }
        case QUESTION_WITH_CANCEL: {
            dialogButtonLabels = new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL,
                    IDialogConstants.CANCEL_LABEL };
            break;
        }
        default: {
            throw new IllegalArgumentException("Illegal value for kind in MessageDialog.open()"); //$NON-NLS-1$
        }
        }
        return dialogButtonLabels;
    }
}