com.kopson.cite.actions.AddSmartLogRowDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.kopson.cite.actions.AddSmartLogRowDialog.java

Source

/*******************************************************************************
 * Copyright (c) 2012 Piotr Kopka
 * 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.kopson.cite.com/legal/epl-v10.html
 *
 * Contributors:
 *     Piotr Kopka
 *******************************************************************************/

package com.kopson.cite.actions;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
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.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.kopson.cite.application.CITE;
import com.kopson.cite.smartlogmodel.SmartLogEntry;
import com.kopson.cite.smartlogmodel.SmartLogProvider;

/**
 * Add log dialog.
 * 
 * @author kopson
 *
 */
public class AddSmartLogRowDialog extends TitleAreaDialog {

    /**
     * Row
     */
    private SmartLogEntry row;

    /**
     * Row fields.
     */
    private Text tagText;
    private Text objectText;
    private Text assertionText;
    private Combo typeCombo;
    private Text messageText;

    /**
     * Ge row.
     * 
     * @return row.
     */
    public SmartLogEntry getRow() {
        return row;
    }

    /**
     * The constructor.
     * 
     * @param parentShell
     */
    public AddSmartLogRowDialog(Shell parentShell) {
        super(parentShell);
    }

    @Override
    protected Control createContents(Composite parent) {
        Control contents = super.createContents(parent);
        setTitle(CITE.ADDLOGROW_DIALOG_TITLE);
        setMessage(CITE.ADDLOGROW_DIALOG_MSG, IMessageProvider.INFORMATION);
        return contents;
    }

    @Override
    protected Control createDialogArea(Composite parent) {

        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        parent.setLayout(layout);

        Label typeLabel = new Label(parent, SWT.NONE);
        typeLabel.setText(CITE.SMARTLOG_COLUMN_TYPE);
        typeCombo = new Combo(parent, SWT.READ_ONLY);

        for (SmartLogProvider.TYPES types : SmartLogProvider.TYPES.values()) {
            typeCombo.add(types.name());
        }

        Label tagLabel = new Label(parent, SWT.NONE);
        tagLabel.setText(CITE.SMARTLOG_COLUMN_TAG);
        tagText = new Text(parent, SWT.BORDER);

        Label messageLabel = new Label(parent, SWT.NONE);
        messageLabel.setText(CITE.SMARTLOG_COLUMN_MESSAGE);
        messageText = new Text(parent, SWT.BORDER);

        Label objectLabel = new Label(parent, SWT.NONE);
        objectLabel.setText(CITE.SMARTLOG_COLUMN_OBJECT);
        objectText = new Text(parent, SWT.BORDER);

        Label assertionLabel = new Label(parent, SWT.NONE);
        assertionLabel.setText(CITE.SMARTLOG_COLUMN_ASSERT);
        assertionText = new Text(parent, SWT.BORDER);

        GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
        gd.horizontalSpan = 2;

        return parent;
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {

        ((GridLayout) parent.getLayout()).numColumns++;

        Button button = new Button(parent, SWT.PUSH);
        button.setText(CITE.DIALOG_OK);
        button.setFont(JFaceResources.getDialogFont());
        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                if (tagText.getText().length() != 0 && messageText.getText().length() != 0
                        && typeCombo.getItem(typeCombo.getSelectionIndex()).length() != 0
                        && objectText.getText().length() != 0 && assertionText.getText().length() != 0) {
                    row = new SmartLogEntry(SmartLogProvider.ROWID++,
                            typeCombo.getItem(typeCombo.getSelectionIndex()), tagText.getText(),
                            messageText.getText(), objectText.getText(), assertionText.getText());
                    close();

                } else {
                    setErrorMessage(CITE.DIALOG_ERROR_MSG);
                }
            }
        });
    }
}