Commit 1ca24a1b authored by Matija Obreza's avatar Matija Obreza
Browse files

Columns databinding

parent 89f45299
......@@ -121,6 +121,16 @@
<artifactId>com.ibm.icu</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.maven</groupId>
<artifactId>org.eclipse.osgi.services</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.maven</groupId>
<artifactId>org.eclipse.osgi</artifactId>
<version>3.8.0</version>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -20,31 +20,29 @@ import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public abstract class AbstractModelObject {
private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
this);
private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
// System.out.println("Adding listener to " + this.getClass() + " of " + listener);
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
// System.out.println("Adding property listener '" + propertyName + "' to " + this.getClass() + " of " + listener);
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
// System.out.println("Removing property listener from " + this.getClass() + " of " + listener);
propertyChangeSupport.removePropertyChangeListener(listener);
}
public void removePropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(propertyName,
listener);
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
// System.out.println("Removing property listener '" + propertyName + "' from " + this.getClass() + " of " + listener);
propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
}
protected void firePropertyChange(String propertyName, Object oldValue,
Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue,
newValue);
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
}
......@@ -418,9 +418,6 @@ public class AppWindow {
CTabItem newSheetTab = SheetTabFactory.createComposite(
tabFolder, currentSheet);
final SheetDisplay sheetDisplay = (SheetDisplay) newSheetTab
.getControl();
// Load stuff
threadPool.execute(new Runnable() {
......@@ -429,17 +426,7 @@ public class AppWindow {
try {
final List<Object[]> rows = dataSourceLoader
.loadRows(currentSheet, 100);
// for (Object[] row : rows) {
// System.err.println(ArrayUtils.toString(row));
// }
// Need to update sheetDisplay
display.asyncExec(new Runnable() {
public void run() {
sheetDisplay.setRows(rows);
}
});
currentSheet.updateData(rows);
} catch (UnsupportedDataFormatException e) {
e.printStackTrace();
......
......@@ -22,6 +22,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.genesys2.anno.model.Column;
public class DataSourceSheet extends AbstractModelObject {
private File sourceFile;
private String sheetName;
......@@ -29,6 +31,8 @@ public class DataSourceSheet extends AbstractModelObject {
private Map<String, String> columnTerms = new HashMap<String, String>();
private boolean headersIncluded = true;
private int headerRowIndex = 0;
private List<Object[]> sampleData;
private List<Column> columns = new ArrayList<Column>();
public DataSourceSheet(File sourceFile) {
this.sourceFile = new File(sourceFile.getAbsolutePath());
......@@ -84,4 +88,73 @@ public class DataSourceSheet extends AbstractModelObject {
this.headerRowIndex = headerRowIndex;
firePropertyChange("headerRowIndex", null, headerRowIndex);
}
public void updateData(List<Object[]> rows) {
int columnCount = 0;
for (Object[] row : rows) {
if (row != null)
columnCount = Math.max(columnCount, row.length);
}
Object[] headers = null;
if (headersIncluded) {
this.sampleData = rows.subList(headerRowIndex + 1, rows.size());
try {
Object[] headerRow = rows.get(headerRowIndex);
headers = headerRow;
} catch (IndexOutOfBoundsException e) {
}
} else {
this.sampleData = rows;
}
// Make sure we have the Columns in the list
ensureColumns(columnCount, headers);
System.err.println("Firing sampleData change");
firePropertyChange("sampleData", null, this.sampleData);
}
private void ensureColumns(int columnCount, Object[] headerRow) {
boolean changed = false;
for (int i = columns.size() - 1; i >= 0 && i > columnCount; i--) {
System.err.println("Removing extra columns");
columns.remove(i);
changed = true;
}
for (int i = columns.size(); i < columnCount; i++) {
System.err.println("Adding column " + i);
columns.add(new Column());
changed = true;
}
for (int i = columns.size() - 1; i >= 0; i--) {
String headerColumnName = (headerRow != null && headerRow.length > i ? String.valueOf(headerRow[i]) : null);
Column c = columns.get(i);
c.setPreferredName(headerColumnName == null ? "Column " + i : headerColumnName);
}
// fire change
if (changed)
firePropertyChange("columns", null, columns);
}
public List<Object[]> getSampleData() {
return sampleData;
}
public List<Column> getColumns() {
return this.columns;
}
public void setColumns(List<Column> columns) {
this.columns = columns;
firePropertyChange("columns", null, this.columns);
}
public Column getColumn(int columnIndex) {
return this.columns.get(columnIndex);
}
}
......@@ -19,9 +19,13 @@ package org.genesys2.anno.gui;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.util.LocalSelectionTransfer;
......@@ -39,6 +43,8 @@ import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
......@@ -58,11 +64,13 @@ 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.genesys2.anno.model.Column;
import swing2swt.layout.BorderLayout;
public class SheetDisplay extends Composite {
private DataBindingContext m_bindingContext;
private static class ContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
if (inputElement instanceof List) {
......@@ -78,8 +86,7 @@ public class SheetDisplay extends Composite {
}
}
private class TableLabelProvider extends LabelProvider implements
ITableLabelProvider {
private class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
......@@ -98,33 +105,28 @@ public class SheetDisplay extends Composite {
}
private Table table;
private Text txtSomeThing;
private Text txtColumnName;
private Text txtRdfTerm;
private Text txtColumnIndex;
private DataSourceSheet dataSourceSheet;
private DSW dsw=new DSW();
private DSW dsw = new DSW();
private TableViewer tableViewer;
private Text txtHeaderRowIndex;
private Text txtCharset;
private Text txtQuoteChar;
private Text text_1;
private Text txtCsvCharset;
private Text txtCsvQuoteChar;
private Text txtCsvSeparator;
private Button chkbxContainsHeaders;
private TabFolder tabFolder;
private Object[] headerRow;
private Text text;
private Text txtColumnDescription;
private Button btnContainsMultipleValues;
private IObservableValue sampleDataDswObserveValue;
public void setDataSourceSheet(DataSourceSheet dataSourceSheet) {
this.dataSourceSheet = dataSourceSheet;
this.dsw.setDataSourceSheet(dataSourceSheet);
System.err.println("Updating targets");
m_bindingContext.updateTargets();
// m_bindingContext.updateModels();
// chkbxContainsHeaders.setSelection(dataSourceSheet.isHeadersIncluded());
// txtHeaderRowIndex.setText(String.valueOf(dataSourceSheet
// .getHeaderRowIndex()));
}
// System.err.println("Updating targets");
// m_bindingContext.updateTargets();
}
/**
* Create the composite.
*
......@@ -133,6 +135,22 @@ public class SheetDisplay extends Composite {
*/
public SheetDisplay(Composite parent, int style) {
super(parent, SWT.NONE);
addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
// UNBIND ALL STUFF
sampleDataDswObserveValue.dispose();
ArrayList<Object> bindings = new ArrayList<Object>(m_bindingContext.getBindings());
for (Object b : bindings) {
System.out.println("Remove binfing " +b);
if (b instanceof Binding) {
((Binding) b).getModel().dispose();
}
m_bindingContext.removeBinding((Binding) b);
}
m_bindingContext.dispose();
}
});
setLayout(new BorderLayout(0, 0));
SashForm sashForm = new SashForm(this, SWT.VERTICAL);
......@@ -148,79 +166,66 @@ public class SheetDisplay extends Composite {
TabItem tbtmColumnProperties = new TabItem(tabFolder, SWT.NONE);
tbtmColumnProperties.setText("Column");
ScrolledComposite scrolledComposite = new ScrolledComposite(tabFolder,
SWT.V_SCROLL);
ScrolledComposite scrolledComposite = new ScrolledComposite(tabFolder, SWT.V_SCROLL);
scrolledComposite.setExpandVertical(true);
tbtmColumnProperties.setControl(scrolledComposite);
scrolledComposite.setExpandHorizontal(true);
Composite composite = new Composite(scrolledComposite,
SWT.NO_BACKGROUND);
Composite composite = new Composite(scrolledComposite, SWT.NO_BACKGROUND);
composite.setLayout(new GridLayout(3, false));
scrolledComposite.setContent(composite);
scrolledComposite.setMinSize(new Point(10, 200));
new Label(composite, SWT.NONE);
Label lblColumn = new Label(composite, SWT.NONE);
lblColumn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
false, 1, 1));
lblColumn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblColumn.setText("Column");
txtColumnIndex = new Text(composite, SWT.BORDER);
txtColumnIndex.setEnabled(false);
txtColumnIndex.setEditable(false);
txtColumnIndex.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
false, 1, 1));
txtColumnIndex.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(composite, SWT.NONE);
Label lblThisIsA = new Label(composite, SWT.NONE);
lblThisIsA.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
false, 1, 1));
lblThisIsA.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblThisIsA.setText("Column name");
txtSomeThing = new Text(composite, SWT.BORDER);
txtSomeThing.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
false, 1, 1));
txtColumnName = new Text(composite, SWT.BORDER);
txtColumnName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(composite, SWT.NONE);
Label lblColumnDescription = new Label(composite, SWT.NONE);
lblColumnDescription.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP,
false, false, 1, 1));
lblColumnDescription.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
lblColumnDescription.setText("Column description");
text = new Text(composite, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI);
GridData gd_text = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
gd_text.minimumHeight = 30;
gd_text.heightHint = 40;
text.setLayoutData(gd_text);
txtColumnDescription = new Text(composite, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI);
GridData gd_txtColumnDescription = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
gd_txtColumnDescription.minimumHeight = 30;
gd_txtColumnDescription.heightHint = 40;
txtColumnDescription.setLayoutData(gd_txtColumnDescription);
new Label(composite, SWT.NONE);
new Label(composite, SWT.NONE);
Button btnContainsMultipleValues = new Button(composite, SWT.CHECK);
btnContainsMultipleValues
.setText("Contains multiple values, separated");
btnContainsMultipleValues = new Button(composite, SWT.CHECK);
btnContainsMultipleValues.setText("Contains multiple values, separated");
new Label(composite, SWT.NONE);
Label lblNewLabel = new Label(composite, SWT.NONE);
lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
false, 1, 1));
lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblNewLabel.setText("RDF Term");
txtRdfTerm = new Text(composite, SWT.BORDER);
txtRdfTerm.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
false, 1, 1));
txtRdfTerm.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(composite, SWT.NONE);
Label lblData = new Label(composite, SWT.NONE);
lblData.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
1, 1));
lblData.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblData.setText("Data type");
Combo combo = new Combo(composite, SWT.NONE);
combo.setItems(new String[] { "Text (general blah-blah)", "Number",
"Date, time, timestamp" });
combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
1));
Combo comboDataType = new Combo(composite, SWT.NONE);
comboDataType.setItems(new String[] { "Text (general blah-blah)", "Number", "Date, time, timestamp" });
comboDataType.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(composite, SWT.NONE);
new Label(composite, SWT.NONE);
......@@ -233,20 +238,19 @@ public class SheetDisplay extends Composite {
});
btnAnalyzeColumnData.setText("Analyze column data");
DropTarget dropTarget = new DropTarget(composite, DND.DROP_MOVE
| DND.DROP_COPY | DND.DROP_DEFAULT | DND.DROP_LINK);
DropTarget dropTarget = new DropTarget(composite, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT | DND.DROP_LINK);
dropTarget.setTransfer(new Transfer[] { LocalSelectionTransfer.getTransfer() });
dropTarget.addDropListener(new DropTargetAdapter() {
@Override
public void drop(DropTargetEvent event) {
if (LocalSelectionTransfer.getTransfer().isSupportedType(event.currentDataType)) {
ISelection sel = LocalSelectionTransfer.getTransfer().getSelection();
System.err.println("Dropped " + sel);
System.err.println("Dropped class " + sel.getClass());
ISelection sel = LocalSelectionTransfer.getTransfer().getSelection();
System.err.println("Dropped " + sel);
System.err.println("Dropped class " + sel.getClass());
}
}
@Override
public void dragEnter(DropTargetEvent event) {
System.err.println("dragenter " + event.detail);
......@@ -266,31 +270,29 @@ public class SheetDisplay extends Composite {
chkbxContainsHeaders.setText("Contains headers");
Label lblRowContainingHeaders = new Label(compoHeaders, SWT.NONE);
lblRowContainingHeaders.setLayoutData(new GridData(SWT.RIGHT,
SWT.CENTER, false, false, 1, 1));
lblRowContainingHeaders.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblRowContainingHeaders.setText("Header row index");
txtHeaderRowIndex = new Text(compoHeaders, SWT.BORDER);
txtHeaderRowIndex.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
// try {
// int idx = Integer.parseInt(txtHeaderRowIndex.getText());
// if (dataSourceSheet.getHeaderRowIndex() != idx) {
// dataSourceSheet.setHeaderRowIndex(idx);
// System.err.println("Modify headerRowIndex: " + idx);
// }
// } catch (NumberFormatException e1) {
// txtHeaderRowIndex.setText(String.valueOf(dataSourceSheet
// .getHeaderRowIndex()));
// }
// try {
// int idx = Integer.parseInt(txtHeaderRowIndex.getText());
// if (dataSourceSheet.getHeaderRowIndex() != idx) {
// dataSourceSheet.setHeaderRowIndex(idx);
// System.err.println("Modify headerRowIndex: " + idx);
// }
// } catch (NumberFormatException e1) {
// txtHeaderRowIndex.setText(String.valueOf(dataSourceSheet
// .getHeaderRowIndex()));
// }
System.err.println("Updating model...");
m_bindingContext.updateModels();
}
});
txtHeaderRowIndex.setEnabled(false);
txtHeaderRowIndex.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
true, false, 1, 1));
txtHeaderRowIndex.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
txtHeaderRowIndex.addListener(SWT.Verify, new Listener() {
public void handleEvent(Event e) {
......@@ -306,15 +308,15 @@ public class SheetDisplay extends Composite {
}
});
// chkbxContainsHeaders.addSelectionListener(new SelectionAdapter() {
// @Override
// public void widgetSelected(SelectionEvent e) {
// txtHeaderRowIndex.setEnabled(chkbxContainsHeaders
// .getSelection());
// dataSourceSheet.setHeadersIncluded(chkbxContainsHeaders
// .getSelection());
// }
// });
// chkbxContainsHeaders.addSelectionListener(new SelectionAdapter() {
// @Override
// public void widgetSelected(SelectionEvent e) {
// txtHeaderRowIndex.setEnabled(chkbxContainsHeaders
// .getSelection());
// dataSourceSheet.setHeadersIncluded(chkbxContainsHeaders
// .getSelection());
// }
// });
TabItem tbtmCSV = new TabItem(tabFolder, SWT.NONE);
tbtmCSV.setText("CSV");
......@@ -324,40 +326,46 @@ public class SheetDisplay extends Composite {
compoCSV.setLayout(new GridLayout(2, false));
Label lblCharacterSet = new Label(compoCSV, SWT.NONE);
lblCharacterSet.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
false, false, 1, 1));
lblCharacterSet.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblCharacterSet.setText("Character set");
txtCharset = new Text(compoCSV, SWT.BORDER);
txtCharset.setText("UTF-8");
txtCharset.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false,
1, 1));
txtCsvCharset = new Text(compoCSV, SWT.BORDER);
txtCsvCharset.setText("UTF-8");
txtCsvCharset.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
Label lblSeparator = new Label(compoCSV, SWT.NONE);
lblSeparator.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
false, 1, 1));
lblSeparator.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblSeparator.setText("Separator");
text_1 = new Text(compoCSV, SWT.BORDER);
text_1.setText(",");
text_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
1));
txtCsvSeparator = new Text(compoCSV, SWT.BORDER);
txtCsvSeparator.setText(",");
txtCsvSeparator.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Label lblQuoteCharacter = new Label(compoCSV, SWT.NONE);
lblQuoteCharacter.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
false, false, 1, 1));
lblQuoteCharacter.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblQuoteCharacter.setText("Quote character");
txtQuoteChar = new Text(compoCSV, SWT.BORDER);
txtQuoteChar.setText("\"");
txtQuoteChar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
false, 1, 1));
txtCsvQuoteChar = new Text(compoCSV, SWT.BORDER);
txtCsvQuoteChar.setText("\"");
txtCsvQuoteChar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
sashForm.setWeights(new int[] { 5, 2 });
tableViewer.setContentProvider(new ContentProvider());
tableViewer.setLabelProvider(new TableLabelProvider());
m_bindingContext = initDataBindings();
// Listen to rows change
sampleDataDswObserveValue = BeanProperties.value("dataSourceSheet.sampleData").observe(dsw);
sampleDataDswObserveValue.addChangeListener(new IChangeListener() {
@Override
public void handleChange(ChangeEvent arg0) {
getDisplay().asyncExec(new Runnable() {
public void run() {
updateTable();
}
});
}
});
}
@Override
......@@ -369,90 +377,71 @@ public class SheetDisplay extends Composite {
return tableViewer;
}
public void setRows(List<Object[]> rows) {
private void updateTable() {
System.err.println("Rebuilding table...");
table.clearAll();
int columnCount = 0;
for (Object[] row : rows) {
if (row != null)
columnCount = Math.max(columnCount, row.length);
}
ArrayList<Object[]> clone = null;
Object[] headerRow = null;
DataSourceSheet dataSourceSheet = dsw.getDataSourceSheet();
if (dataSourceSheet.isHeadersIncluded()) {
clone = new ArrayList<Object[]>(rows.size());
int headerRowIndex = dataSourceSheet.getHeaderRowIndex();
try {
headerRow = rows.get(headerRowIndex);
System.err
.println("HEADERS: " + ArrayUtils.toString(headerRow));
for (int i = headerRowIndex + 1; i < rows.size(); i++) {
clone.add(rows.get(i));
}
} catch (IndexOutOfBoundsException e) {
// No data
}