From: jsimomaa Date: Fri, 7 Jun 2019 07:34:01 +0000 (+0300) Subject: Add CSV table view for copy/pasting consumer information before creation X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F35%2F2935%2F2;p=simantics%2Fdistrict.git Add CSV table view for copy/pasting consumer information before creation gitlab #51 APROS-15209 Change-Id: I5307000c29fd93292cb6c41ad527a83abc8f9f6f --- diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/ColumnHeaderTableDataProvider.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/ColumnHeaderTableDataProvider.java new file mode 100644 index 00000000..530870b2 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/ColumnHeaderTableDataProvider.java @@ -0,0 +1,31 @@ +package org.simantics.district.network.ui.table; + +import org.eclipse.nebula.widgets.nattable.data.IDataProvider; + +public class ColumnHeaderTableDataProvider implements IDataProvider { + + @Override + public Object getDataValue(int columnIndex, int rowIndex) { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setDataValue(int columnIndex, int rowIndex, Object newValue) { + // TODO Auto-generated method stub + + } + + @Override + public int getColumnCount() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getRowCount() { + // TODO Auto-generated method stub + return 0; + } + +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/CustomPasteDataAction.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/CustomPasteDataAction.java new file mode 100644 index 00000000..dc8c684a --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/CustomPasteDataAction.java @@ -0,0 +1,80 @@ +package org.simantics.district.network.ui.table; + +import java.util.Set; + +import org.eclipse.nebula.widgets.nattable.NatTable; +import org.eclipse.nebula.widgets.nattable.coordinate.Range; +import org.eclipse.nebula.widgets.nattable.ui.action.IKeyAction; +import org.eclipse.swt.dnd.TextTransfer; +import org.eclipse.swt.events.KeyEvent; + +public class CustomPasteDataAction implements IKeyAction { + + private DistrictCSVTable table; + + public CustomPasteDataAction(DistrictCSVTable table) { + this.table = table; + } + + @Override + public void run(NatTable natTable, KeyEvent event) { + Object data = table.cpb.getContents(TextTransfer.getInstance()); + if (data instanceof String) { + String textData = (String) data; + String[][] fullData = computeData(textData); + + int[] cols = table.selectionLayer.getSelectedColumnPositions(); + int firstCol = cols[0]; + int column = table.selectionLayer.getColumnIndexByPosition(firstCol); + Set ranges = table.selectionLayer.getSelectedRowPositions(); + if (!ranges.isEmpty()) { + int rowPosition = ranges.iterator().next().start; + int[] columns = new int[fullData.length]; + columns[0] = column; + for (int i = 1; i < fullData.length; i++) + columns[i] = table.selectionLayer.getColumnIndexByPosition(firstCol + i); + table.bodyDataLayer.doCommand(new CustomPasteDataCommand(table.bodyDataLayer, columns, rowPosition, fullData)); + } + } + } + + private static String[][] computeData(String textData) { + String separator; + if (textData.contains(",") && !textData.contains(";")) { + separator = ","; + } else if (textData.contains(";") && !textData.contains("\t")) { + separator = ";"; + } else { + separator = "\\t"; + } + + textData = textData.replaceAll("\\r", ""); + + String[] rows = textData.split("\\n"); + + String[][] cells = new String[rows.length][]; + for(int i=0;i diagrams = new HashMap<>(); + private Map vertexMappings = new HashMap<>(); + + private Composite composite; + private Combo networkDiagramSelectionCombo; + private Combo sourceCRSCombo; + private Combo vertexMappingCombo; + private String sourceCRS; + private Resource diagram; + private Resource mapping; + + public SelectionDialog(Shell parent) { + super(parent); + } + + public Resource getMappingType() { + return mapping; + } + + public Resource getTargetDiagram() { + return diagram; + } + + public String getSourceCRS() { + return "EPSG:" + sourceCRS; + } + + @Override + protected Control createDialogArea(Composite parent) { + composite = (Composite) super.createDialogArea(parent); + createMappingsGroup(composite); + computeContent(); + return composite; + } + + private void computeContent() { + Simantics.getSession().asyncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + + Resource indexRoot = graph.sync(new IndexRoot(graph.sync(new PossibleActiveModel(Simantics.getProjectResource())))); + vertexMappings = Functions.getVertexMappings(graph, indexRoot); + + Collection diagramss = Functions.getDistrictDiagrams(graph); + ModelingResources MOD = ModelingResources.getInstance(graph); + Resource projectResource = Simantics.getProjectResource(); + String projectURI = graph.getURI(projectResource); + for (Resource diagram : diagramss) { + Resource composite = graph.getSingleObject(diagram, MOD.DiagramToComposite); + String compositeURI = graph.getURI(composite); + String path = compositeURI.replace(projectURI, ""); + diagrams.put(path, diagram); + } + + composite.getDisplay().asyncExec(() -> { + + vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()])); + + networkDiagramSelectionCombo.setItems(diagrams.keySet().toArray(new String[diagrams.size()])); + if (diagrams.size() > 0) { + networkDiagramSelectionCombo.select(0); + } + Set codes = CRS.getSupportedCodes("EPSG"); + sourceCRSCombo.setItems(codes.toArray(new String[codes.size()])); + sourceCRSCombo.addModifyListener(new ModifyListener() { + + @Override + public void modifyText(ModifyEvent e) { + String currentText = sourceCRSCombo.getText(); + if (codes.contains(currentText)) { + // Select this + String[] items = sourceCRSCombo.getItems(); + int i; + for (i = 0; i < items.length; i++) { + String item = items[i]; + if (currentText.equals(item)) { + break; + } + } + if (i != 0) { + sourceCRSCombo.select(i); + } else { + System.err.println("Should not happen"); + } + } + } + }); + }); + } + }); + } + + private void createMappingsGroup(Composite parent) { + Group group= new Group(parent, SWT.NONE); + group.setFont(parent.getFont()); + group.setText("Select Diagram & CRS"); + GridDataFactory.fillDefaults().grab(true, false).applyTo(group); + group.setLayout(new GridLayout(1, false)); + + Composite cmposite = new Composite(group, SWT.NONE); + cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); + cmposite.setLayout(new GridLayout(2, false)); + + Label vertexMappingLabel = new Label(cmposite, SWT.NONE); + vertexMappingLabel.setText("Select Vertex Mapping"); + + vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); + GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo); + + Label selectNetworkDiagramLabel = new Label(cmposite, SWT.NONE); + selectNetworkDiagramLabel.setText("Select Network Diagram"); + + networkDiagramSelectionCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); + GridDataFactory.fillDefaults().grab(true, false).applyTo(networkDiagramSelectionCombo); + + Label label = new Label(cmposite, SWT.NONE); + label.setText("Select Source Coordinate Reference System"); + + sourceCRSCombo = new Combo(cmposite, SWT.NONE); + sourceCRSCombo.setToolTipText("Select the coordinate reference system that is used in the source material for possible transformation to target coordinate reference system (EPSG:4326)"); + + GridDataFactory.fillDefaults().grab(true, false).applyTo(sourceCRSCombo); + } + + @Override + protected void computeResult() { + mapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex())); + diagram = diagrams.get(networkDiagramSelectionCombo.getItem(networkDiagramSelectionCombo.getSelectionIndex())); + sourceCRS = sourceCRSCombo.getItem(sourceCRSCombo.getSelectionIndex()); + } + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/PasteDataCommandHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/PasteDataCommandHandler.java new file mode 100644 index 00000000..06e9fbd5 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/PasteDataCommandHandler.java @@ -0,0 +1,47 @@ +package org.simantics.district.network.ui.table; + +import java.util.Collection; + +import org.eclipse.nebula.widgets.nattable.command.AbstractLayerCommandHandler; +import org.eclipse.nebula.widgets.nattable.layer.DataLayer; +import org.eclipse.nebula.widgets.nattable.layer.event.ColumnVisualUpdateEvent; +import org.eclipse.nebula.widgets.nattable.layer.event.StructuralRefreshEvent; +import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer; +import org.eclipse.swt.dnd.Clipboard; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PasteDataCommandHandler extends AbstractLayerCommandHandler { + + private static final Logger LOGGER = LoggerFactory.getLogger(PasteDataCommandHandler.class); + + private final SelectionLayer selectionLayer; + private final DataLayer dataLayer; + private final Clipboard cpb; + private final TableDataProvider provider; + + public PasteDataCommandHandler(TableDataProvider provider, DataLayer dataLayer, SelectionLayer selectionLayer, Clipboard cpb) { + this.provider = provider; + this.dataLayer = dataLayer; + this.selectionLayer = selectionLayer; + this.cpb = cpb; + } + + @Override + public Class getCommandClass() { + return CustomPasteDataCommand.class; + } + + @Override + protected boolean doCommand(CustomPasteDataCommand command) { + String[][] fullData = command.data; + int pasteRow = command.pasteRow; + Collection pasteColumn = command.getColumnPositions(); + if (pasteRow > -1) { + provider.setDataValues(pasteColumn, pasteRow, fullData); + dataLayer.fireLayerEvent(new StructuralRefreshEvent(dataLayer)); + } + return true; + } +} + diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/RowHeaderTableDataProvider.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/RowHeaderTableDataProvider.java new file mode 100644 index 00000000..b85f6e37 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/RowHeaderTableDataProvider.java @@ -0,0 +1,33 @@ +package org.simantics.district.network.ui.table; + +import org.eclipse.nebula.widgets.nattable.data.IDataProvider; + +public class RowHeaderTableDataProvider implements IDataProvider { + + protected final IDataProvider bodyDataProvider; + + public RowHeaderTableDataProvider(IDataProvider bodyDataProvider) { + this.bodyDataProvider = bodyDataProvider; + } + + @Override + public int getColumnCount() { + return 1; + } + + @Override + public int getRowCount() { + return this.bodyDataProvider.getRowCount(); + } + + @Override + public Object getDataValue(int columnIndex, int rowIndex) { + return Integer.valueOf(rowIndex + 1); + } + + @Override + public void setDataValue(int columnIndex, int rowIndex, Object newValue) { + throw new UnsupportedOperationException(); + } + +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/TableDataProvider.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/TableDataProvider.java new file mode 100644 index 00000000..a8e2a016 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/TableDataProvider.java @@ -0,0 +1,56 @@ +package org.simantics.district.network.ui.table; + +import java.util.Collection; + +import org.eclipse.nebula.widgets.nattable.data.IDataProvider; + +public class TableDataProvider implements IDataProvider { + + private String[][] data = null; + + @Override + public Object getDataValue(int columnIndex, int rowIndex) { + if (data == null) { + return null; + } else { + return data[rowIndex][columnIndex]; + } + } + + @Override + public void setDataValue(int columnIndex, int rowIndex, Object newValue) { + + } + + @Override + public int getColumnCount() { + if (data == null) { + return 10; + } else { + return data[0].length; + } + } + + @Override + public int getRowCount() { + if (data == null) { + return 10; + } else { + return data.length; + } + } + + public boolean isEditable(int columnIndex, int rowIndex) { + return false; + } + + public void setDataValues(Collection pasteColumn, int pasteRow, String[][] fullData) { + // start always from row index 0 and column index 0 + this.data = fullData; + } + + public String[][] getCurrentData() { + return data; + } + +}