Package name.abuchen.portfolio.ui.wizards.client

Source Code of name.abuchen.portfolio.ui.wizards.client.NewAccountPage

package name.abuchen.portfolio.ui.wizards.client;

import name.abuchen.portfolio.model.Account;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.ui.Messages;
import name.abuchen.portfolio.ui.PortfolioPlugin;
import name.abuchen.portfolio.ui.wizards.AbstractWizardPage;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;

public class NewAccountPage extends AbstractWizardPage
{
    private Client client;
    private TableViewer tViewer;

    public NewAccountPage(Client client)
    {
        super(NewAccountPage.class.getSimpleName());
        this.client = client;
        setTitle(Messages.NewFileWizardAccountTitle);
        setDescription(Messages.NewFileWizardAccountDescription);
    }

    @Override
    public void beforePage()
    {
        tViewer.refresh();
    }

    @Override
    public void createControl(Composite parent)
    {
        Composite container = new Composite(parent, SWT.NULL);
        setControl(container);
        GridLayoutFactory.fillDefaults().numColumns(3).applyTo(container);

        Label lblAcc = new Label(container, SWT.NULL);
        lblAcc.setText(Messages.ColumnAccount);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).applyTo(lblAcc);

        final Text accountName = new Text(container, SWT.BORDER | SWT.SINGLE);
        GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(accountName);

        Button button = new Button(container, SWT.PUSH);
        button.setText(Messages.NewFileWizardButtonAdd);
        GridDataFactory.fillDefaults().applyTo(button);

        Composite tableContainer = new Composite(container, SWT.NONE);
        GridDataFactory.fillDefaults().span(3, 1).grab(true, true).applyTo(tableContainer);
        TableColumnLayout layout = new TableColumnLayout();
        tableContainer.setLayout(layout);

        tViewer = new TableViewer(tableContainer);

        button.addSelectionListener(new SelectionAdapter()
        {
            @Override
            public void widgetSelected(SelectionEvent e)
            {
                String acnName = accountName.getText();
                if (acnName.length() > 0)
                {
                    Account currentAccount = new Account();
                    currentAccount.setName(acnName);
                    client.addAccount(currentAccount);
                    tViewer.refresh();

                    accountName.setText(""); //$NON-NLS-1$
                    accountName.setFocus();
                }
            }
        });

        Table table = tViewer.getTable();
        table.setHeaderVisible(true);
        table.setEnabled(false);

        tViewer.setContentProvider(ArrayContentProvider.getInstance());
        tViewer.setInput(client.getAccounts());
        TableViewerColumn aCol = new TableViewerColumn(tViewer, SWT.NONE);
        layout.setColumnData(aCol.getColumn(), new ColumnWeightData(50));
        aCol.getColumn().setText(Messages.ColumnAccount);
        aCol.setLabelProvider(new ColumnLabelProvider()
        {
            @Override
            public String getText(Object element)
            {
                return ((Account) element).getName();
            }

            @Override
            public Image getImage(Object element)
            {
                return PortfolioPlugin.image(PortfolioPlugin.IMG_ACCOUNT);
            }
        });
        container.pack();
        setPageComplete(true);
    }

}
TOP

Related Classes of name.abuchen.portfolio.ui.wizards.client.NewAccountPage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.