Search in sources :

Example 1 with ByteArrayResource

use of com.revolsys.spring.resource.ByteArrayResource in project com.revolsys.open by revolsys.

the class AbstractRecordLayer method getPasteRecordGeometry.

protected Geometry getPasteRecordGeometry(final LayerRecord record, final boolean alert) {
    try {
        if (record == null) {
            return null;
        } else {
            final RecordDefinition recordDefinition = getRecordDefinition();
            final FieldDefinition geometryField = recordDefinition.getGeometryField();
            if (geometryField != null) {
                final MapPanel parentComponent = getMapPanel();
                Geometry geometry = null;
                DataType geometryDataType = null;
                Class<?> layerGeometryClass = null;
                final GeometryFactory geometryFactory = getGeometryFactory();
                geometryDataType = geometryField.getDataType();
                layerGeometryClass = geometryDataType.getJavaClass();
                RecordReader reader = ClipboardUtil.getContents(RecordReaderTransferable.DATA_OBJECT_READER_FLAVOR);
                if (reader == null) {
                    final String string = ClipboardUtil.getContents(DataFlavor.stringFlavor);
                    if (Property.hasValue(string)) {
                        try {
                            geometry = geometryFactory.geometry(string);
                            geometry = geometryFactory.geometry(layerGeometryClass, geometry);
                            if (geometry != null) {
                                return geometry;
                            }
                        } catch (final Throwable e) {
                        }
                        final Resource resource = new ByteArrayResource("t.csv", string);
                        reader = RecordReader.newRecordReader(resource);
                    } else {
                        return null;
                    }
                }
                if (reader != null) {
                    try {
                        for (final Record sourceRecord : reader) {
                            if (geometry == null) {
                                final Geometry sourceGeometry = sourceRecord.getGeometry();
                                if (sourceGeometry == null) {
                                    if (alert) {
                                        JOptionPane.showMessageDialog(parentComponent, "Clipboard does not contain a record with a geometry.", "Paste Geometry", JOptionPane.ERROR_MESSAGE);
                                    }
                                    return null;
                                }
                                geometry = geometryFactory.geometry(layerGeometryClass, sourceGeometry);
                                if (geometry == null) {
                                    if (alert) {
                                        JOptionPane.showMessageDialog(parentComponent, "Clipboard should contain a record with a " + geometryDataType + " not a " + sourceGeometry.getGeometryType() + ".", "Paste Geometry", JOptionPane.ERROR_MESSAGE);
                                    }
                                    return null;
                                }
                            } else {
                                if (alert) {
                                    JOptionPane.showMessageDialog(parentComponent, "Clipboard contains more than one record. Copy a single record.", "Paste Geometry", JOptionPane.ERROR_MESSAGE);
                                }
                                return null;
                            }
                        }
                    } finally {
                        FileUtil.closeSilent(reader);
                    }
                    if (geometry == null) {
                        if (alert) {
                            JOptionPane.showMessageDialog(parentComponent, "Clipboard does not contain a record with a geometry.", "Paste Geometry", JOptionPane.ERROR_MESSAGE);
                        }
                    } else if (geometry.isEmpty()) {
                        if (alert) {
                            JOptionPane.showMessageDialog(parentComponent, "Clipboard contains an empty geometry.", "Paste Geometry", JOptionPane.ERROR_MESSAGE);
                        }
                        return null;
                    } else {
                        return geometry;
                    }
                }
            }
            return null;
        }
    } catch (final Throwable t) {
        return null;
    }
}
Also used : MapPanel(com.revolsys.swing.map.MapPanel) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) FieldDefinition(com.revolsys.record.schema.FieldDefinition) RecordReader(com.revolsys.record.io.RecordReader) ListRecordReader(com.revolsys.record.io.ListRecordReader) ByteArrayResource(com.revolsys.spring.resource.ByteArrayResource) Resource(com.revolsys.spring.resource.Resource) LineString(com.revolsys.geometry.model.LineString) ByteArrayResource(com.revolsys.spring.resource.ByteArrayResource) RecordDefinition(com.revolsys.record.schema.RecordDefinition) Geometry(com.revolsys.geometry.model.Geometry) DataType(com.revolsys.datatype.DataType) ArrayRecord(com.revolsys.record.ArrayRecord) Record(com.revolsys.record.Record)

Example 2 with ByteArrayResource

use of com.revolsys.spring.resource.ByteArrayResource in project com.revolsys.open by revolsys.

the class EsriGdbXmlParser method parse.

@SuppressWarnings("unchecked")
public static <T> T parse(final String text) {
    final byte[] bytes = text.getBytes();
    final ByteArrayResource resource = new ByteArrayResource(bytes);
    return (T) parse(resource);
}
Also used : ByteArrayResource(com.revolsys.spring.resource.ByteArrayResource)

Example 3 with ByteArrayResource

use of com.revolsys.spring.resource.ByteArrayResource in project com.revolsys.open by revolsys.

the class AbstractRecordLayer method pasteRecords.

public void pasteRecords() {
    final List<LayerRecord> newRecords = new ArrayList<>();
    try (BaseCloseable eventsEnabled = eventsDisabled()) {
        RecordReader reader = ClipboardUtil.getContents(RecordReaderTransferable.DATA_OBJECT_READER_FLAVOR);
        if (reader == null) {
            final String string = ClipboardUtil.getContents(DataFlavor.stringFlavor);
            if (Property.hasValue(string)) {
                if (string.contains("\t")) {
                    final Resource tsvResource = new ByteArrayResource("t.tsv", string);
                    reader = RecordReader.newRecordReader(tsvResource);
                } else {
                    final Resource resource = new ByteArrayResource("t.csv", string);
                    reader = RecordReader.newRecordReader(resource);
                }
            }
        }
        if (reader != null) {
            for (final Record sourceRecord : reader) {
                final Map<String, Object> newValues = getPasteNewValues(sourceRecord);
                if (!newValues.isEmpty()) {
                    final LayerRecord newRecord = newLayerRecord(newValues);
                    if (newRecord != null) {
                        newRecords.add(newRecord);
                    }
                }
            }
        }
    } catch (final Throwable e) {
        LoggingEventPanel.showDialog(getMapPanel(), "Unexpected error pasting records", e);
        return;
    }
    RecordValidationDialog.validateRecords("Pasting Records", this, newRecords, (validator) -> {
        // Success
        // Save the valid records
        final List<LayerRecord> validRecords = validator.getValidRecords();
        if (!validRecords.isEmpty()) {
            saveChanges(validRecords);
            addSelectedRecords(validRecords);
            zoomToRecords(validRecords);
            showRecordsTable(RecordLayerTableModel.MODE_RECORDS_SELECTED);
            firePropertyChange(RECORDS_INSERTED, null, validRecords);
        }
        // Delete any invalid records
        final List<LayerRecord> invalidRecords = validator.getInvalidRecords();
        if (!invalidRecords.isEmpty()) {
            deleteRecords(invalidRecords);
        }
    }, (validator) -> {
        // Cancel, delete all the records
        deleteRecords(newRecords);
    });
}
Also used : BaseCloseable(com.revolsys.io.BaseCloseable) RecordReader(com.revolsys.record.io.RecordReader) ListRecordReader(com.revolsys.record.io.ListRecordReader) ArrayList(java.util.ArrayList) ByteArrayResource(com.revolsys.spring.resource.ByteArrayResource) Resource(com.revolsys.spring.resource.Resource) ArrayRecord(com.revolsys.record.ArrayRecord) Record(com.revolsys.record.Record) LineString(com.revolsys.geometry.model.LineString) ByteArrayResource(com.revolsys.spring.resource.ByteArrayResource)

Aggregations

ByteArrayResource (com.revolsys.spring.resource.ByteArrayResource)3 LineString (com.revolsys.geometry.model.LineString)2 ArrayRecord (com.revolsys.record.ArrayRecord)2 Record (com.revolsys.record.Record)2 ListRecordReader (com.revolsys.record.io.ListRecordReader)2 RecordReader (com.revolsys.record.io.RecordReader)2 Resource (com.revolsys.spring.resource.Resource)2 DataType (com.revolsys.datatype.DataType)1 Geometry (com.revolsys.geometry.model.Geometry)1 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 BaseCloseable (com.revolsys.io.BaseCloseable)1 FieldDefinition (com.revolsys.record.schema.FieldDefinition)1 RecordDefinition (com.revolsys.record.schema.RecordDefinition)1 MapPanel (com.revolsys.swing.map.MapPanel)1 ArrayList (java.util.ArrayList)1