mt.com.southedge.jclockwork.plugin.wizard.controls.CreateControlObject.java Source code

Java tutorial

Introduction

Here is the source code for mt.com.southedge.jclockwork.plugin.wizard.controls.CreateControlObject.java

Source

package mt.com.southedge.jclockwork.plugin.wizard.controls;

/*******************************************************************************
 * Copyright (c) 2011 SouthEdge Software and Consultancy.  
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Carl Frendo - initial design and implementation
 *******************************************************************************/

import mt.com.southedge.jclockwork.plugin.messages.ClockWorkWizardMessages;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.ui.IJavaElementSearchConstants;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.SelectionDialog;

public class CreateControlObject extends CreateControlText {

    private static final int ONE = 1;
    private static final int TWO = 2;

    private Button button;
    private String buttonText;

    /**
     * 
     * @param labelText the label text
     * @param buttonText the buuton text to be displayed
     */
    public CreateControlObject(String labelText, String buttonText) {
        super(labelText);
        this.buttonText = buttonText;
    }

    /**
     * 
     * @param buttonText the text to be displayed on the button
     */
    public void setButtonLabel(String buttonText) {
        this.buttonText = buttonText;
    }

    /**
     * Create control object method.
     * 
     * @param composite the composite
     * @param nColumns the number of columns
     */
    public Control[] createBeanControls(Composite composite, int nColumns) {
        assertEnoughColumns(nColumns);
        getLabelControl(composite).setLayoutData(CreateControlLabel.gridDataForLabel(ONE));

        getTextControl(composite).setLayoutData(gridDataForText(nColumns - TWO));

        button = getChangeControl(composite);
        button.setLayoutData(gridDataForButton(ONE));

        return new Control[] { getLabel(), getTextbox(), button };
    }

    /**
     * 
     * @param span the columns to span
     * @return the grid data
     */
    protected static GridData gridDataForButton(int span) {
        GridData gd = new GridData();
        gd.horizontalAlignment = GridData.FILL;
        gd.grabExcessHorizontalSpace = false;
        gd.horizontalSpan = span;
        return gd;
    }

    /**
     * 
     * @param parent the composite
     * @return the button control object
     */
    protected Button getChangeControl(Composite parent) {
        if (button == null) {
            Assert.isNotNull(parent, "Null composite");

            button = new Button(parent, SWT.PUSH);
            button.setFont(parent.getFont());
            button.setText(buttonText);
            if (isOkToUse(button)) {
                button.setEnabled(true);
            } else {
                button.setEnabled(false);
            }
            button.addSelectionListener(new SelectionListener() {

                public void widgetSelected(SelectionEvent e) {
                    handleButtonInvocation();
                }

                public void widgetDefaultSelected(SelectionEvent e) {

                }
            });
        }
        return button;
    }

    /**
     * Handles and displays the selection dialog
     */
    protected void handleButtonInvocation() {
        try {
            SelectionDialog selDialog = JavaUI.createTypeDialog(
                    PlatformUI.getWorkbench().getDisplay().getActiveShell(),
                    PlatformUI.getWorkbench().getProgressService(), SearchEngine.createWorkspaceScope(),
                    IJavaElementSearchConstants.CONSIDER_CLASSES, false);
            selDialog.setTitle(ClockWorkWizardMessages.ClockWorkWizard_Selection_Dialog_Title);
            if (selDialog.open() != IDialogConstants.CANCEL_ID) {
                processSelection(selDialog);
            }

        } catch (JavaModelException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 
     * @param selDialog the selection dialog
     */
    protected void processSelection(SelectionDialog selDialog) {
        Object[] lstSelectionObjects = selDialog.getResult();
        setTextWithoutUpdate(((IType) lstSelectionObjects[0]).getFullyQualifiedName());
    }

    /**
     * @return the fButton
     */
    public Button getButton() {
        return button;
    }

    /**
     * @param fButton the fButton to set
     */
    public void setButton(Button button) {
        this.button = button;
    }

}