Package com.gwesm.ui

Source Code of com.gwesm.ui.WelcomeView

package com.gwesm.ui;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.gwesm.core.Account;
import com.gwesm.core.GWESM;
import com.gwesm.dao.CastorDAO;
import com.gwesm.dao.DAO;
import com.gwesm.dao.DAOException;
import com.gwesm.presenters.AccountPresenter;

public class WelcomeView extends SWTView {

  private Composite leftComposite;

  private Composite rightComposite;

  private Combo accountsCombo;

  private Text input;

  private final AccountPresenter presenter;

  public WelcomeView(final Composite parent, final int style,
      final AccountPresenter presenter) {
    super(parent, style, presenter);
    this.presenter = presenter;

    initialize();
  }

  public WelcomeView(final Composite parent, final AccountPresenter presenter) {
    super(parent, presenter);
    this.presenter = presenter;

    initialize();
  }

  private void initialize() {
    this.setLayout(new GridLayout(3, false));

    this.leftComposite = new Composite(this, SWT.NONE);

    Label separator = new Label(this, SWT.SEPARATOR | SWT.VERTICAL);

    this.rightComposite = new Composite(this, SWT.NONE);

    GridData leftCompositeLayoutData = new GridData();
    this.leftComposite.setLayoutData(leftCompositeLayoutData);

    GridData separatorLayoutData = new GridData();
    separator.setLayoutData(separatorLayoutData);

    GridData rightCompositeLayoutData = new GridData();
    this.rightComposite.setLayoutData(rightCompositeLayoutData);

    this.leftComposite.setLayout(new RowLayout(SWT.VERTICAL));
    Label accountSelect = new Label(this.leftComposite, SWT.NONE);
    accountSelect.setText("Select Account :");

    this.accountsCombo = new Combo(this.leftComposite, SWT.NONE);
    Button selectBtn = new Button(this.leftComposite, SWT.NONE);
    selectBtn.setText("Select");
    selectBtn.addListener(SWT.Selection, new Listener() {
      public void handleEvent(final Event event) {
        selectBtnClicked(event);
      }
    });

    this.rightComposite.setLayout(new RowLayout(SWT.VERTICAL));
    Label accountCreate = new Label(this.rightComposite, SWT.NONE);
    accountCreate.setText("New Account Name :");

    this.input = new Text(this.rightComposite, SWT.BORDER);

    Button createBtn = new Button(this.rightComposite, SWT.NONE);
    createBtn.setText("Create");
    createBtn.addListener(SWT.Selection, new Listener() {
      public void handleEvent(final Event event) {
        createBtnClicked(event);
      }
    });
  }

  @Override
  public void refresh() {
    this.accountsCombo.removeAll();
    for (String name : this.presenter.getAccountsName()) {
      this.accountsCombo.add(name);
    }
  }

  public static void main(final String[] args) throws DAOException {

    DAO dao = new CastorDAO();
    GWESM gwesm = dao.loadData("testsData" + File.separator
        + "GWESM-Sample.xml");
    Account account = gwesm.getAccounts().get(0);

    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    AccountPresenter presenter = new AccountPresenter(gwesm, account);

    WelcomeView view = new WelcomeView(shell, presenter);
    presenter.refresh();

    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }

  private void selectBtnClicked(final Event event) {
    String accountName = this.accountsCombo.getItem(this.accountsCombo
        .getSelectionIndex());
  }

  private void createBtnClicked(final Event event) {
    String accountName = this.input.getText();
    this.presenter.createAccount(accountName);
  }

}
TOP

Related Classes of com.gwesm.ui.WelcomeView

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.