de.hska.ruja.stockquotes.application.parts.view.StockToolView.java Source code

Java tutorial

Introduction

Here is the source code for de.hska.ruja.stockquotes.application.parts.view.StockToolView.java

Source

/*******************************************************************************
 * Copyright (c) 2010 - 2013 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *     Lars Vogel <lars.Vogel@gmail.com> - Bug 419770
 *******************************************************************************/
package de.hska.ruja.stockquotes.application.parts.view;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Singleton;

import org.eclipse.e4.core.di.annotations.Creatable;
import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.di.Persist;
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
import org.eclipse.jface.layout.TableColumnLayout;
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.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;

import de.hska.iwii.stockquotes.net.StockData;
import de.hska.ruja.stockquotes.application.parts.controller.StockToolController;
import de.hska.ruja.stockquotes.application.parts.model.Model;

@Creatable
@Singleton
public class StockToolView implements Observer {

    @Inject
    private MDirtyable dirty;
    private Text tbFilter;
    private Table table;
    private Combo cobIndizes;
    private Combo cobDatasource;
    @Inject
    private Model model;
    @Inject
    private StockToolController controller;
    private Composite tableComposite;

    private ArrayList<StockData> stockData = new ArrayList<>();
    private TableViewer tableViewer;

    static final int SHORTNAME = 0;
    static final int COMPANY = 1;
    static final int QUOT_CURRENT = 2;
    static final int QUOT_DAYMAX = 3;
    static final int QUOT_DAYMIN = 4;

    public StockToolView() {

        StockData bla = new StockData();
        bla.setCompanyShortName("ZINTEL");
        bla.setCompanyName("Zintel COrp");
        bla.setCurrentPrice(100.0);
        bla.setDayHighPrice(105.0);
        bla.setDayLowPrice(95.2);

        stockData.add(bla);

        //stockData.add(new Stock("ZINTEL", "ZIntel Corp.", 100.0, 105.0, 95.0));
        //stockData.add(new Stock("PORSC", "Porsche AG", 200.0, 205.0, 195.0));
    }

    private void doTabeStuff() {

        //tableComposite.setLayout(new FillLayout());
        TableColumnLayout tcl = new TableColumnLayout();
        tableComposite.setLayout(tcl);

        tableViewer = new TableViewer(tableComposite, SWT.FULL_SELECTION);
        tableViewer.setContentProvider(new StockContentProvider());
        //tableViewer.setLabelProvider(new StockColumnLabelProvider());

        table = tableViewer.getTable();

        //      FormData fd_table = new FormData();
        //      fd_table.top = new FormAttachment(0);
        //      fd_table.bottom = new FormAttachment(0);
        //      fd_table.left = new FormAttachment(0);
        //      fd_table.right = new FormAttachment(0);
        //table.setLayoutData(fd_table);

        TableViewerColumn tableColumnShortname = new TableViewerColumn(tableViewer, SWT.LEFT);
        //tableColumnShortname.setEditingSupport(new PersonNameEditingSupport(tableViewer));
        tableColumnShortname.setLabelProvider(new StockShortnameLabelProvider());
        tableColumnShortname.getColumn().setText("Shortname");
        tableColumnShortname.getColumn().setWidth(130);
        // Dynamisches Spaltenlayout: 65% der Breite
        tcl.setColumnData(tableColumnShortname.getColumn(), new ColumnWeightData(20, true));

        TableViewerColumn tableColumnCompany = new TableViewerColumn(tableViewer, SWT.LEFT);
        //tableColumnShortname.setEditingSupport(new PersonNameEditingSupport(tableViewer));
        tableColumnCompany.setLabelProvider(new StockCompanyLabelProvider());
        tableColumnCompany.getColumn().setText("Company");
        tableColumnCompany.getColumn().setWidth(130);
        // Dynamisches Spaltenlayout: 65% der Breite
        tcl.setColumnData(tableColumnCompany.getColumn(), new ColumnWeightData(20, true));

        TableViewerColumn tableColumnQuotCurrent = new TableViewerColumn(tableViewer, SWT.LEFT);
        //tableColumnQuotCurrent.setEditingSupport(new PersonNameEditingSupport(tableViewer));
        tableColumnQuotCurrent.setLabelProvider(new StockQuotCurrentLabelProvider());
        tableColumnQuotCurrent.getColumn().setText("QuotCurrent");
        tableColumnQuotCurrent.getColumn().setWidth(130);
        // Dynamisches Spaltenlayout: 65% der Breite
        tcl.setColumnData(tableColumnQuotCurrent.getColumn(), new ColumnWeightData(20, true));
        tableColumnQuotCurrent.setLabelProvider(new StockColumnLabelProvider());

        TableViewerColumn tableColumnQuotDayMax = new TableViewerColumn(tableViewer, SWT.LEFT);
        //tableColumnQuotDayMax.setEditingSupport(new PersonNameEditingSupport(tableViewer));
        tableColumnQuotDayMax.setLabelProvider(new StockQuotDayMaxLabelProvider());
        tableColumnQuotDayMax.getColumn().setText("QuotDayMax");
        tableColumnQuotDayMax.getColumn().setWidth(130);
        // Dynamisches Spaltenlayout: 65% der Breite
        tcl.setColumnData(tableColumnQuotDayMax.getColumn(), new ColumnWeightData(20, true));

        TableViewerColumn tableColumnQuotDayMin = new TableViewerColumn(tableViewer, SWT.LEFT);
        //tableColumnQuotDayMin.setEditingSupport(new PersonNameEditingSupport(tableViewer));
        tableColumnQuotDayMin.setLabelProvider(new StockQuotDayMinLabelProvider());
        tableColumnQuotDayMin.getColumn().setText("QuotDayMin");
        tableColumnQuotDayMin.getColumn().setWidth(130);
        // Dynamisches Spaltenlayout: 65% der Breite
        tcl.setColumnData(tableColumnQuotDayMin.getColumn(), new ColumnWeightData(20, true));

        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        tableViewer.setInput(model.getStockdata()); //model.getStockdata()
        model.addObserver(this);

    }

    /**
     * Hier wird der Tabelle ein Sortierer hinzugefuegt. 
     * @param tableColumn1 Erste Spalte, die einen Sortierer erhalten soll.
     * @param tableColumn2 Zweite Spalte, die einen Sortierer erhalten soll.
     */
    private void createSorter(TableViewerColumn tableColumn1, TableViewerColumn tableColumn2) {
        // Sorter erzeugen, um die Tabellenzellen bei einem Klick auf
        // einen Header sortiert anzuzeigen 
        final StockSorter sorter = new StockSorter();

        // Sorter am TableViewer registrieren.
        tableViewer.setSorter(sorter);

        // Als Listener registrieren, um das Sortieren zu starten
        tableColumn1.getColumn().addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                sorter.doSort(SHORTNAME);
                tableViewer.refresh();
            }
        });

        tableColumn2.getColumn().addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                sorter.doSort(COMPANY);
                tableViewer.refresh();
            }
        });
    }

    public Text getTbFilter() {
        return tbFilter;
    }

    public void setTbFilter(Text tbFilter) {
        this.tbFilter = tbFilter;
    }

    public Table getTable() {
        return table;
    }

    public Combo getCobIndizes() {
        return cobIndizes;
    }

    public Combo getCobDatasource() {
        return cobDatasource;
    }

    @Override
    public void update(Observable o, Object arg) {
        System.out.println("View: notified - " + (String) arg);
        switch ((String) arg) {
        case "ChangedStockData":
            tableViewer.setInput(model.getStockdata());
            tableViewer.refresh();
            break;

        default:
            break;
        }
    }

    @PostConstruct
    public void createComposite(Composite parent, Shell shell) {

        shell.setMinimumSize(500, 400);

        FormLayout plFormLayout = new FormLayout();
        plFormLayout.marginLeft = 10;
        plFormLayout.marginRight = 10;
        plFormLayout.marginTop = 10;
        plFormLayout.marginBottom = 10;

        parent.setLayout(plFormLayout);

        Label lblIndex = new Label(parent, SWT.NONE);
        FormData fd_lblIndex = new FormData();
        lblIndex.setLayoutData(fd_lblIndex);
        lblIndex.setText("Index:");

        cobIndizes = new Combo(parent, SWT.READ_ONLY);
        String[] indizes = { "IXIC (NASDAQ)", "DJI (Dow Jones)", "FTSE (FTSE100)" };
        cobIndizes.setItems(new String[] { "IXIC (NASDAQ)", "DJI (Dow Jones)", "FTSE (FTSE100)" });
        fd_lblIndex.top = new FormAttachment(cobIndizes, 3, SWT.TOP);
        fd_lblIndex.left = new FormAttachment(0);

        FormData fd_cobIndizes = new FormData();
        fd_cobIndizes.top = new FormAttachment(0, 10);
        fd_cobIndizes.left = new FormAttachment(lblIndex, 6);
        cobIndizes.setLayoutData(fd_cobIndizes);

        Label lblFilter = new Label(parent, SWT.NONE);
        FormData fd_lblFilter = new FormData();
        fd_lblFilter.left = new FormAttachment(cobIndizes, 10);
        fd_lblFilter.top = new FormAttachment(lblIndex, 0, SWT.TOP);
        lblFilter.setLayoutData(fd_lblFilter);
        lblFilter.setText("Filter:");

        tbFilter = new Text(parent, SWT.BORDER);
        FormData fd_tbFilter = new FormData();
        fd_tbFilter.top = new FormAttachment(cobIndizes, 0, SWT.TOP);
        fd_tbFilter.left = new FormAttachment(lblFilter, 6);
        tbFilter.setLayoutData(fd_tbFilter);

        cobDatasource = new Combo(parent, SWT.READ_ONLY);
        cobDatasource.setItems(new String[] { "Yahoo Data", "Simulation" });
        FormData fd_cobDatenquelle = new FormData();
        fd_cobDatenquelle.top = new FormAttachment(cobIndizes, 0, SWT.TOP);
        fd_cobDatenquelle.right = new FormAttachment(100, -10);
        cobDatasource.setLayoutData(fd_cobDatenquelle);

        Label lblDatenquelle = new Label(parent, SWT.NONE);
        fd_tbFilter.right = new FormAttachment(lblDatenquelle, -20);
        FormData fd_lblDatenquelle = new FormData();
        fd_lblDatenquelle.bottom = new FormAttachment(lblIndex, 0, SWT.BOTTOM);
        fd_lblDatenquelle.right = new FormAttachment(cobDatasource, -6);
        lblDatenquelle.setLayoutData(fd_lblDatenquelle);
        lblDatenquelle.setText("Datenquelle:");

        FormData fd_tableComposite = new FormData();
        fd_tableComposite.top = new FormAttachment(cobIndizes, 30);
        fd_tableComposite.left = new FormAttachment(0);
        fd_tableComposite.right = new FormAttachment(100);
        tableComposite = new Composite(parent, SWT.NONE);
        tableComposite.setLayoutData(fd_tableComposite);

        ProgressBar progressBar = new ProgressBar(parent, SWT.NONE);
        FormData fd_progressBar = new FormData();
        fd_progressBar.left = new FormAttachment(0);
        fd_progressBar.right = new FormAttachment(0, 175);
        fd_progressBar.bottom = new FormAttachment(100);
        progressBar.setLayoutData(fd_progressBar);

        Label lblProgressInfo = new Label(parent, SWT.NONE);
        fd_tableComposite.bottom = new FormAttachment(lblProgressInfo, -6);

        FormData fd_lblProgressInfo = new FormData();
        fd_lblProgressInfo.bottom = new FormAttachment(100, -2);
        fd_lblProgressInfo.left = new FormAttachment(0, 181);
        lblProgressInfo.setLayoutData(fd_lblProgressInfo);
        lblProgressInfo.setText("Daten abholen");

        doTabeStuff();

        // Do MVC stuff
        // controller = new StockToolController(this,model);
        controller.setView(this);
    }

    @Focus
    public void setFocus() {
        // tableViewer.getTable().setFocus();
    }

    @Persist
    public void save() {
        dirty.setDirty(false);
    }
}