package com.daehonges.soilsieve;
import java.util.Vector;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
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.layout.FillLayout;
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.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.swtchart.Chart;
import org.swtchart.IAxis;
import org.swtchart.IAxisSet;
import org.swtchart.ISeries;
import org.swtchart.ISeries.SeriesType;
import org.swtchart.ISeriesSet;
import org.swtchart.ITitle;
import org.swtchart.Range;
import com.daehonges.soilsieve.lib.Sieve;
import com.daehonges.soilsieve.lib.SieveData;
public class View extends ViewPart {
private SieveData sd;
public View() {
}
public static final String ID = "com.daehonges.soilsieve.view";
private Text textTotal;
private Text textReminded;
private Table table;
private Composite compositeChart;
private TableViewer tableViewer;
private Button buttonAdd;
private Combo comboSieve;
private Chart chart;
private TabFolder tabFolder;
/**
* The content provider class is responsible for providing objects to the
* view. It can wrap existing objects in adapters or simply return objects
* as-is. These objects may be sensitive to the current input of the view,
* or ignore it and always show the same content (like Task List, for
* example).
*/
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
public void dispose() {
}
public Object[] getElements(Object parent) {
if (parent instanceof Object[]) {
return (Object[]) parent;
}
return new Object[0];
}
}
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
tabFolder = new TabFolder(parent, SWT.NONE);
TabItem tbtmSieveData = new TabItem(tabFolder, SWT.NONE);
tbtmSieveData.setText("Sieve Data");
Composite composite = new Composite(tabFolder, SWT.NONE);
tbtmSieveData.setControl(composite);
composite.setLayout(new GridLayout(7, false));
new Label(composite, SWT.NONE);
new Label(composite, SWT.NONE);
Label labelTotal = new Label(composite, SWT.NONE);
labelTotal.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
labelTotal.setText("Total Soil Mass : ");
new Label(composite, SWT.NONE);
textTotal = new Text(composite, SWT.BORDER);
textTotal.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
new Label(composite, SWT.NONE);
Button buttonStart = new Button(composite, SWT.NONE);
buttonStart.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
buttonStartWidgetSelected(e);
}
});
buttonStart.setText("Start");
Label labelSieveNo = new Label(composite, SWT.NONE);
labelSieveNo.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
labelSieveNo.setText("Sieve NO. : ");
comboSieve = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
String[] sievesNOs = Sieve.getSieveNOs();
comboSieve.setItems(sievesNOs);
comboSieve.select(0);
comboSieve.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
Label lblMassOfSoil = new Label(composite, SWT.NONE);
lblMassOfSoil.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblMassOfSoil.setText("Mass Of Soil Reminded : ");
new Label(composite, SWT.NONE);
textReminded = new Text(composite, SWT.BORDER);
textReminded.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
new Label(composite, SWT.NONE);
buttonAdd = new Button(composite, SWT.NONE);
buttonAdd.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
buttonAddWidgetSelected(e);
}
});
buttonAdd.setEnabled(false);
buttonAdd.setText("Add");
table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 7, 1));
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn tableColumnSieveNO = new TableColumn(table, SWT.RIGHT);
tableColumnSieveNO.setWidth(100);
tableColumnSieveNO.setText("Sieve NO.");
TableColumn tableColumn1 = new TableColumn(table, SWT.NONE);
tableColumn1.setWidth(100);
tableColumn1.setText("D ( mm )");
TableColumn tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setWidth(100);
tableColumn.setText("Reminded( gr )");
TableColumn tableColumn2 = new TableColumn(table, SWT.NONE);
tableColumn2.setWidth(100);
tableColumn2.setText("% Reminded");
TableColumn tableColumn3 = new TableColumn(table, SWT.NONE);
tableColumn3.setWidth(60);
tableColumn3.setText("% Finer");
TabItem tbtmChart = new TabItem(tabFolder, SWT.NONE);
tbtmChart.setText("Chart");
compositeChart = new Composite(tabFolder, SWT.NONE);
tbtmChart.setControl(compositeChart);
renderChart(compositeChart);
}
protected void buttonAddWidgetSelected(SelectionEvent e) {
Sieve s = Sieve.getSieve(comboSieve.getText());
double reminded = Double.parseDouble(textReminded.getText());
sd.add(s, reminded);
tableViewer.refresh();
updateChart();
}
protected void buttonStartWidgetSelected(SelectionEvent e) {
sd = new SieveData(Double.parseDouble(textTotal.getText()));
createTableViewer();
}
private void createTableViewer() {
tableViewer = new TableViewer(table);
tableViewer.setContentProvider(new IStructuredContentProvider() {
@Override
public void inputChanged(Viewer viewer, Object oldInput,
Object newInput) {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public Object[] getElements(Object inputElement) {
@SuppressWarnings("unchecked")
Vector<Sieve> v = (Vector<Sieve>) inputElement;
return v.toArray();
}
});
tableViewer.setInput(sd.getSieves());
tableViewer.setLabelProvider(new ITableLabelProvider() {
@Override
public void removeListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
@Override
public boolean isLabelProperty(Object element, String property) {
// TODO Auto-generated method stub
return false;
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void addListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
@Override
public String getColumnText(Object element, int columnIndex) {
Sieve s = (Sieve) element;
switch (columnIndex) {
case 0:
return s.getNo();
case 1:
return Double.toString(s.getSize());
case 2:
return Double.toString(s.getReminded());
case 3:
return Double.toString(s.getPercentReminded());
case 4:
return Double.toString(s.getPercentFiner());
}
return "--";
}
@Override
public Image getColumnImage(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}
});
final String[] colNames = new String[] { "Sieve NO.", "D ( mm )",
"Reminded( gr )", "% Reminded", "% Finer" };
tableViewer.setColumnProperties(colNames);
CellEditor[] cellEditors = new CellEditor[5];
cellEditors[0] = new TextCellEditor();
cellEditors[1] = new TextCellEditor();
cellEditors[2] = new TextCellEditor();
cellEditors[3] = new TextCellEditor();
cellEditors[4] = new TextCellEditor();
buttonAdd.setEnabled(true);
}
private void renderChart(Composite composite) {
compositeChart.setLayout(new FillLayout(SWT.HORIZONTAL));
chart = new Chart(composite, SWT.NONE);
chart.setVisible(true);
ITitle title = chart.getTitle();
title.setText("Sieve Test Chart");
}
private void updateChart() {
double[] xSeries = new double[sd.getSieves().size()];
double[] ySeries = new double[sd.getSieves().size()];
Vector<Sieve> s = sd.getSieves();
for(int i = 0; i < s.size(); i++) {
Sieve sieve = s.elementAt(i);
xSeries[i] = sieve.getSize();
ySeries[i] = sieve.getPercentFiner();
}
ISeriesSet seriesSet = chart.getSeriesSet();
ISeries series = seriesSet.createSeries(SeriesType.LINE, "Particle Size Curve");
series.setXSeries(xSeries);
series.setYSeries(ySeries);
IAxisSet axisSet = chart.getAxisSet();
IAxis xAxis = axisSet.getXAxis(0);
// xAxis.adjustRange();
xAxis.setRange(new Range(0.001, 10));
xAxis.getTitle().setText("Grain Size ( mm )");
xAxis.enableLogScale(true);
IAxis yAxis = axisSet.getYAxis(0);
yAxis.getTitle().setText("Percent Finer");
// yAxis.setPosition(Position.Secondary);
yAxis.setRange(new Range(0, 100));
// axisSet.adjustRange();
chart.update();
}
public SieveData getSieveData() {
return sd;
}
public void exportChart( String filename, int format ) {
tabFolder.setSelection(1);
chart.setFocus();
chart.save(filename, format);
}
public void setSieveData( SieveData sieveData ) {
this.sd = sieveData;
textTotal.setText(Double.toString(sd.getTotal()));
createTableViewer();
updateChart();
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
}
}