com.clustercontrol.dialog.CommonDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.dialog.CommonDialog.java

Source

/*
    
Copyright (C) 2006 NTT DATA Corporation
    
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, version 2.
    
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.
    
 */

package com.clustercontrol.dialog;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.clustercontrol.util.FocusUtil;
import com.clustercontrol.util.Messages;
import com.clustercontrol.util.WidgetTestUtil;

/**
 * ????<BR>
 * <p>
 *
 * ???getInitialSize?customizeDialog???? <br>
 * ????validate????
 *
 * @version 2.2.0
 * @since 1.0.0
 */
public abstract class CommonDialog extends Dialog {

    private Composite m_areaComposite;
    private ScrolledComposite m_scrolledComposite;

    // -----  ----- //

    /**
     * parent??????
     *
     * @param parent
     *            Shell
     */
    public CommonDialog(Shell parent) {
        super(parent);
        setShellStyle(getShellStyle() | SWT.RESIZE);
    }

    // ----- instance  ----- //

    /**
     * ????
     * <p>
     *
     * @param composite
     *            ??
     */
    protected void customizeDialog(Composite composite) {
    }

    /**
     * ????
     *
     * @return ?
     */
    protected String getOkButtonText() {
        return null;
    }

    /**
     * ????
     *
     * @return ?
     */
    protected String getCancelButtonText() {
        return null;
    }

    /**
     * ?????
     * <p>
     *
     * ??????????
     *
     * @return ValidateResult
     */
    protected ValidateResult validate() {
        return null;
    }

    /**
     * ??????
     * <p>
     *
     * ????????????
     */
    protected boolean action() {
        return true;
    }

    /**
     * ????????????
     * <p>
     *
     * ?????????
     */
    @Override
    protected void okPressed() {
        ValidateResult result = this.validate();

        if (result == null || result.isValid()) {

            if (this.action()) {
                super.okPressed();
            }

        } else {
            this.displayError(result);
        }
    }

    /**
     * ???
     * <p>
     *
     * ???????
     *
     * @param result
     *            ValidateResult
     */
    protected void displayError(ValidateResult result) {
        MessageDialog.openWarning(null, result.getID(), result.getMessage());
    }

    /**
     * ????????
     *
     * @param result ValidateResult
     * @return?
     */
    protected boolean displayQuestion(ValidateResult result) {
        return MessageDialog.openQuestion(null, Messages.getString("confirmed"), result.getMessage());

    }

    @Override
    public int open() {
        FocusUtil.setDialog(this.getClass().getName());
        return super.open();
    }

    @Override
    public boolean close() {
        FocusUtil.unsetDialog(this.getClass().getName());
        return super.close();
    }

    // ----- Dialog?? ----- //

    @Override
    protected Control createContents(Composite parent) {
        Composite composite = (Composite) super.createContents(parent);
        WidgetTestUtil.setTestId(this, null, composite);
        m_areaComposite = (Composite) this.getDialogArea();
        WidgetTestUtil.setTestId(this, "area", m_areaComposite);

        m_areaComposite.setLayout(new FillLayout(SWT.DEFAULT));
        m_scrolledComposite = new ScrolledComposite(m_areaComposite, SWT.V_SCROLL | SWT.H_SCROLL);
        WidgetTestUtil.setTestId(this, "scrolled", m_scrolledComposite);

        Composite childComposite = new Composite(m_scrolledComposite, SWT.NONE);
        WidgetTestUtil.setTestId(this, "child", childComposite);

        m_scrolledComposite.setExpandHorizontal(true);
        m_scrolledComposite.setExpandVertical(true);
        m_scrolledComposite.setContent(childComposite);

        // ?
        this.customizeDialog(childComposite);

        if (m_areaComposite.getSize().x > 0 && m_areaComposite.getSize().y > 0) {
            // ???pack?????
            m_scrolledComposite.setMinSize(m_areaComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
            // ?????????????????
            // ?????????????
            m_scrolledComposite.setMinWidth(m_areaComposite.getSize().x - 20);
        } else {
            //???pack???????
            /*
              ?????????????????
                scrolledComposite.setMinSize(areaComposite.getShell().getSize().x,
            areaComposite.getShell().getSize().y);
             */
        }

        // ?
        String okText = this.getOkButtonText();
        if (okText != null) {
            Button okButton = this.getButton(IDialogConstants.OK_ID);
            if (okButton != null) {
                WidgetTestUtil.setTestId(this, "ok", okButton);
                okButton.setText(okText);
            }
        }

        // ?
        String cancelText = this.getCancelButtonText();
        if (cancelText != null) {
            Button cancelButton = this.getButton(IDialogConstants.CANCEL_ID);
            if (cancelButton != null) {
                WidgetTestUtil.setTestId(this, "cancel", cancelButton);
                cancelButton.setText(cancelText);
            }
        }

        return composite;
    }

    /**
     * OK?
     */
    @Override
    protected Control createButtonBar(Composite parent) {
        Composite composite = new Composite(parent, SWT.NONE);
        // create a layout with spacing and margins appropriate for the font
        // size.
        GridLayout layout = new GridLayout();
        layout.numColumns = 0; // this is incremented by createButton
        layout.makeColumnsEqualWidth = true;
        layout.marginTop = 0;
        layout.marginBottom = 5;
        layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
        layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
        layout.verticalSpacing = 0;
        composite.setLayout(layout);
        GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_CENTER);
        composite.setLayoutData(data);
        composite.setFont(parent.getFont());

        // Add the buttons to the button bar.
        createButtonsForButtonBar(composite);
        return composite;
    }

    /**
     * AreaComposite??
     * 
     * @return AreaComposite
     */
    protected Composite getAreaComposite() {
        return m_areaComposite;
    }

    /**
     * ScrolledComposite??
     * 
     * @return ScrolledComposite
     */
    protected ScrolledComposite getScrolledComposite() {
        return m_scrolledComposite;
    }
}