view com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/NewBundleDialog.java @ 90:8f2dd0dbdb87

Externalize strings
author Omair Majid <omajid@redhat.com>
date Tue, 21 Jan 2014 19:51:04 -0500
parents b788a26c0d4d
children 10ffc183f39e
line wrap: on
line source

package com.redhat.thermostat.tools.eclipse.plugin.editor;

import java.io.IOException;

import org.eclipse.jface.dialogs.TitleAreaDialog;
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.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.redhat.thermostat.tools.eclipse.plugin.BundleInformationExtractor;
import com.redhat.thermostat.tools.eclipse.plugin.Messages;
import com.redhat.thermostat.tools.eclipse.plugin.model.Bundle;

/**
 * Prompt the user to enter or select a bundle name/version.
 *
 * Used by the command edit page.
 */
public class NewBundleDialog extends TitleAreaDialog {

    private Bundle bundleInfo;

    private Button manualSelectionButton;
    private Text nameText;
    private Text versionText;

    private Button fromJarButton;
    private Text fileLocationText;
    private Button btnFromFile;

    public NewBundleDialog(Shell parentShell) {
        super(parentShell);
    }

    @Override
    public void create() {
        super.create();

        setTitle(Messages.NewBundleDialog_title);
        setMessage(Messages.NewBundleDialog_message);
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        Composite area = (Composite) super.createDialogArea(parent);

        Composite container = new Composite(area, SWT.NONE);
        container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true));

        GridLayout containerLayout = new GridLayout(3, false);
        containerLayout.marginTop = 5;
        containerLayout.marginRight = 5;
        containerLayout.marginLeft = 5;
        container.setLayout(containerLayout);

        manualSelectionButton = new Button(container, SWT.RADIO);
        manualSelectionButton.setSelection(true);
        manualSelectionButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                nameText.setEnabled(true);
                versionText.setEnabled(true);

                fileLocationText.setEnabled(false);
                btnFromFile.setEnabled(false);
            }
        });
        manualSelectionButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
        manualSelectionButton.setText(Messages.NewBundleDialog_manualSectionTitle);

        Label nameLabel = new Label(container, SWT.None);
        nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        nameLabel.setText(Messages.NewBundleDialog_symbolicNameLabel);
        nameText = new Text(container, SWT.None);
        GridData nameTextLayoutData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
        nameTextLayoutData.horizontalSpan = 2;
        nameText.setLayoutData(nameTextLayoutData);

        Label versionLabel = new Label(container, SWT.None);
        versionLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        versionLabel.setText(Messages.NewBundleDialog_versionLabel);
        versionText = new Text(container, SWT.None);
        GridData versionTextLayoutData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
        versionTextLayoutData.horizontalSpan = 2;
        versionText.setLayoutData(versionTextLayoutData);

        fromJarButton = new Button(container, SWT.RADIO);
        fromJarButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                nameText.setEnabled(false);
                versionText.setEnabled(false);

                fileLocationText.setEnabled(true);
                btnFromFile.setEnabled(true);
            }
        });
        fromJarButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
        fromJarButton.setText(Messages.NewBundleDialog_fromJarSectionTitle);

        Label locationLabel = new Label(container, SWT.NONE);
        locationLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        locationLabel.setText(Messages.NewBundleDialog_pathLabel);

        fileLocationText = new Text(container, SWT.BORDER);
        fileLocationText.setEnabled(false);
        fileLocationText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        btnFromFile = new Button(container, SWT.NONE);
        btnFromFile.setText(Messages.NewBundleDialog_findOnDisk);
        btnFromFile.setEnabled(false);
        btnFromFile.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
                dialog.setFilterExtensions(new String[] { "*.jar" }); //$NON-NLS-1$
                String result = dialog.open();
                if (result != null) {
                    fileLocationText.setText(result);
                }
            }
        });

        // TODO implement extracting data from bundle/jar on disk

        return area;
    }

    @Override
    protected void okPressed() {
        saveInput();
        super.okPressed();
    }

    private void saveInput() {
        if (manualSelectionButton.getSelection()) {
            bundleInfo = new Bundle(nameText.getText(), versionText.getText());
        } else {
            try {
                BundleInformationExtractor extractor = new BundleInformationExtractor();
                bundleInfo = extractor.extract(fileLocationText.getText());
            } catch (IOException e) {
                // FIXME doing something saner with this exception
                e.printStackTrace();
            }
        }
    }

    public Bundle getBundle() {
        return bundleInfo;
    }

}