raptor.swt.ErrorDialog.java Source code

Java tutorial

Introduction

Here is the source code for raptor.swt.ErrorDialog.java

Source

/**
 * New BSD License
 * http://www.opensource.org/licenses/bsd-license.php
 * Copyright 2009-2016 RaptorProject (https://github.com/Raptor-Fics-Interface/Raptor)
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * Neither the name of the RaptorProject nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package raptor.swt;

import org.apache.commons.lang.StringUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import raptor.international.L10n;

public class ErrorDialog extends InputDialog {
    protected StyledText errorText;

    /**
     * InputDialog constructor
     * 
     * @param parent
     *            the parent
     * @param style
     *            the style
     */
    public ErrorDialog(Shell parent, String errorText) {
        // Let users override the default styles
        super(parent, L10n.getInstance().getString("raptorError"), "");
        setText(L10n.getInstance().getString("raptorError"));
        setMessage(errorText);
    }

    /**
     * Opens the dialog and returns the input
     * 
     * @return String
     */
    @Override
    public String open() {
        // Create the dialog window
        Shell shell = new Shell(getParent(), getStyle());
        shell.setText(getText());
        createContents(shell);
        shell.pack();
        errorText.setText(getMessage());
        SWTUtils.center(shell);
        shell.open();
        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        // Return the entered value, or null
        return input;
    }

    /**
     * Creates the dialog's contents
     * 
     * @param shell
     *            the dialog window
     */
    @Override
    protected void createContents(final Shell shell) {
        shell.setLayout(new GridLayout(1, false));

        // Display the input box
        errorText = new StyledText(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.BORDER);
        errorText.setEditable(false);
        String line = StringUtils.rightPad("", 80) + "\n";
        errorText.setText(line + line + line + line + line + line + line + line + line + line + line);
        errorText.setWordWrap(true);
        errorText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

        Button ok = new Button(shell, SWT.PUSH);
        ok.setText("OK");
        ok.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false, 1, 1));
        ok.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent event) {
                input = "";
                shell.close();
            }
        });
    }
}