Search in sources :

Example 26 with PluginInteraction

use of au.gov.asd.tac.constellation.plugins.PluginInteraction in project constellation by constellation-app.

the class CopyCustomMarkersToGraphPlugin method edit.

@Override
protected void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    final int vertexIdentifierAttributeId = VisualConcept.VertexAttribute.IDENTIFIER.ensure(graph);
    final int vertexTypeAttributeId = AnalyticConcept.VertexAttribute.TYPE.ensure(graph);
    final int vertexLatitudeAttributeId = SpatialConcept.VertexAttribute.LATITUDE.ensure(graph);
    final int vertexLongitudeAttributeId = SpatialConcept.VertexAttribute.LONGITUDE.ensure(graph);
    final int vertexPrecisionAttributeId = SpatialConcept.VertexAttribute.PRECISION.ensure(graph);
    final int vertexShapeAttributeId = SpatialConcept.VertexAttribute.SHAPE.ensure(graph);
    final MarkerCache markerCache = MarkerCache.getDefault();
    for (final ConstellationAbstractMarker marker : markerCache.getCustomMarkers()) {
        final String markerId = marker.getId() == null ? marker.toString() : marker.getId();
        final int vertexId = graph.addVertex();
        graph.setStringValue(vertexIdentifierAttributeId, vertexId, markerId);
        graph.setObjectValue(vertexTypeAttributeId, vertexId, AnalyticConcept.VertexType.LOCATION);
        graph.setFloatValue(vertexLatitudeAttributeId, vertexId, marker.getLocation().getLat());
        graph.setFloatValue(vertexLongitudeAttributeId, vertexId, marker.getLocation().getLon());
        graph.setFloatValue(vertexPrecisionAttributeId, vertexId, (float) marker.getRadius());
        try {
            final Shape.GeometryType geometryType;
            if (marker instanceof ConstellationMultiMarker) {
                geometryType = Shape.GeometryType.MULTI_POLYGON;
            } else if (marker instanceof ConstellationPolygonMarker) {
                geometryType = Shape.GeometryType.POLYGON;
            } else if (marker instanceof ConstellationLineMarker) {
                geometryType = Shape.GeometryType.LINE;
            } else {
                geometryType = Shape.GeometryType.POINT;
            }
            final List<Tuple<Double, Double>> coordinates = marker.getLocations().stream().map(location -> Tuple.create((double) location.getLon(), (double) location.getLat())).collect(Collectors.toList());
            final String shape = Shape.generateShape(markerId, geometryType, coordinates);
            graph.setStringValue(vertexShapeAttributeId, vertexId, shape);
        } catch (final IOException ex) {
            throw new PluginException(PluginNotificationLevel.ERROR, ex);
        }
    }
    PluginExecution.withPlugin(VisualSchemaPluginRegistry.COMPLETE_SCHEMA).executeNow(graph);
    PluginExecution.withPlugin(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
}
Also used : ConstellationAbstractMarker(au.gov.asd.tac.constellation.views.mapview.markers.ConstellationAbstractMarker) GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) Tuple(au.gov.asd.tac.constellation.utilities.datastructure.Tuple) SimpleEditPlugin(au.gov.asd.tac.constellation.plugins.templates.SimpleEditPlugin) SpatialConcept(au.gov.asd.tac.constellation.graph.schema.analytic.concept.SpatialConcept) VisualConcept(au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept) MarkerCache(au.gov.asd.tac.constellation.views.mapview.utilities.MarkerCache) ConstellationLineMarker(au.gov.asd.tac.constellation.views.mapview.markers.ConstellationLineMarker) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) Shape(au.gov.asd.tac.constellation.utilities.geospatial.Shape) ServiceProvider(org.openide.util.lookup.ServiceProvider) PluginTags(au.gov.asd.tac.constellation.plugins.templates.PluginTags) PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) ConstellationPolygonMarker(au.gov.asd.tac.constellation.views.mapview.markers.ConstellationPolygonMarker) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) InteractiveGraphPluginRegistry(au.gov.asd.tac.constellation.graph.interaction.InteractiveGraphPluginRegistry) IOException(java.io.IOException) ConstellationAbstractMarker(au.gov.asd.tac.constellation.views.mapview.markers.ConstellationAbstractMarker) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Collectors(java.util.stream.Collectors) PluginNotificationLevel(au.gov.asd.tac.constellation.plugins.PluginNotificationLevel) PluginInfo(au.gov.asd.tac.constellation.plugins.PluginInfo) List(java.util.List) AnalyticConcept(au.gov.asd.tac.constellation.graph.schema.analytic.concept.AnalyticConcept) ConstellationMultiMarker(au.gov.asd.tac.constellation.views.mapview.markers.ConstellationMultiMarker) VisualSchemaPluginRegistry(au.gov.asd.tac.constellation.graph.schema.visual.VisualSchemaPluginRegistry) NbBundle(org.openide.util.NbBundle) Shape(au.gov.asd.tac.constellation.utilities.geospatial.Shape) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) ConstellationMultiMarker(au.gov.asd.tac.constellation.views.mapview.markers.ConstellationMultiMarker) IOException(java.io.IOException) ConstellationLineMarker(au.gov.asd.tac.constellation.views.mapview.markers.ConstellationLineMarker) ConstellationPolygonMarker(au.gov.asd.tac.constellation.views.mapview.markers.ConstellationPolygonMarker) MarkerCache(au.gov.asd.tac.constellation.views.mapview.utilities.MarkerCache) Tuple(au.gov.asd.tac.constellation.utilities.datastructure.Tuple)

Example 27 with PluginInteraction

use of au.gov.asd.tac.constellation.plugins.PluginInteraction in project constellation by constellation-app.

the class PluginReportPane method saveToClipboard.

/**
 * Saves a text representation of this PluginReport to the clipboard.
 */
private void saveToClipboard() {
    CharArrayWriter writer = new CharArrayWriter();
    try (PrintWriter out = new PrintWriter(writer)) {
        out.append("Name: " + pluginReport.getPluginName() + SeparatorConstants.NEWLINE);
        out.append("Description: " + pluginReport.getPluginDescription() + SeparatorConstants.NEWLINE);
        out.append("Message: " + pluginReport.getMessage() + SeparatorConstants.NEWLINE);
        out.append("Tags: " + Arrays.toString(pluginReport.getTags()) + SeparatorConstants.NEWLINE);
        out.append("Start: " + dateFormat.format(new Date(pluginReport.getStartTime())) + SeparatorConstants.NEWLINE);
        out.append("Stop: " + dateFormat.format(new Date(pluginReport.getStopTime())) + SeparatorConstants.NEWLINE);
        if (pluginReport.getError() != null) {
            out.append("Error: " + pluginReport.getError().getMessage() + "\n\n");
            pluginReport.getError().printStackTrace(out);
        }
    }
    final Clipboard clipboard = Clipboard.getSystemClipboard();
    ClipboardContent content = new ClipboardContent();
    content.putString(writer.toString());
    clipboard.setContent(content);
    // TODO: can't do this because of circular dependancy
    // ClipboardUtilities.copyToClipboard(writer.toString());
    PluginExecution.withPlugin(new SimplePlugin("Copy To Clipboard") {

        @Override
        protected void execute(PluginGraphs graphs, PluginInteraction interaction, PluginParameters parameters) throws InterruptedException, PluginException {
            ConstellationLoggerHelper.copyPropertyBuilder(this, writer.toString().length(), ConstellationLoggerHelper.SUCCESS);
        }
    }).executeLater(null);
}
Also used : PluginGraphs(au.gov.asd.tac.constellation.plugins.PluginGraphs) ClipboardContent(javafx.scene.input.ClipboardContent) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) SimplePlugin(au.gov.asd.tac.constellation.plugins.templates.SimplePlugin) Clipboard(javafx.scene.input.Clipboard) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) CharArrayWriter(java.io.CharArrayWriter) Date(java.util.Date) PrintWriter(java.io.PrintWriter)

Example 28 with PluginInteraction

use of au.gov.asd.tac.constellation.plugins.PluginInteraction in project constellation by constellation-app.

the class ExportToExcelFilePlugin method execute.

@Override
public void execute(final PluginGraphs graphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    try (final SXSSFWorkbook workbook = new SXSSFWorkbook(SXSSFWorkbook.DEFAULT_WINDOW_SIZE)) {
        final Sheet sheet = workbook.createSheet(sheetName);
        // get the indexes of all visible columns
        final List<Integer> visibleIndices = table.getColumns().stream().filter(column -> column.isVisible()).map(column -> table.getColumns().indexOf(column)).collect(Collectors.toList());
        // iterate through the visible columns and print each ones name to the sheet
        final Row headerRow = sheet.createRow(0);
        visibleIndices.forEach(index -> {
            final TableColumn<ObservableList<String>, ?> column = table.getColumns().get(index);
            final Cell headerCell = headerRow.createCell(visibleIndices.indexOf(index));
            headerCell.setCellValue(column.getText());
        });
        // store the current page so we can reset it after the export
        final int currentPage = pagination.getCurrentPageIndex();
        if (selectedOnly) {
            // iterate through all the pages in the table and write the selected rows to the sheet
            for (int i = 0; i < pagination.getPageCount(); i++) {
                pagination.getPageFactory().call(i);
                // Calculates the start index in the sheet based on the current table page
                // Because only selected rows are included this could leave a gap in the sheet
                // + 1 to skip the header
                // + 1 to skip the header
                final int startIndex = rowsPerPage * i + 1;
                final Thread writeSheetThread = new Thread("Export to Excel File: Writing Sheet") {

                    @Override
                    public void run() {
                        // get a copy of the table data so that users can continue working
                        final List<ObservableList<String>> data = getTable().getSelectionModel().getSelectedItems();
                        writeRecords(sheet, visibleIndices, data, startIndex);
                    }
                };
                writeSheetThread.start();
                writeSheetThread.join();
            }
        } else {
            // iterate through all the pages in the table and write their rows to the sheet
            for (int i = 0; i < pagination.getPageCount(); i++) {
                pagination.getPageFactory().call(i);
                // Calculates the start index in the sheet based on the current table page
                // + 1 to skip the header
                final int startIndex = rowsPerPage * i + 1;
                final Thread writeSheetThread = new Thread("Export to Excel File: Writing Sheet") {

                    @Override
                    public void run() {
                        // get a copy of the table data so that users can continue working
                        final List<ObservableList<String>> data = getTable().getItems();
                        writeRecords(sheet, visibleIndices, data, startIndex);
                    }
                };
                writeSheetThread.start();
                writeSheetThread.join();
            }
        }
        // call the page factory function once more to go back to the original page index
        pagination.getPageFactory().call(currentPage);
        // The sheet has now been created. Time to write it to the file
        final Thread outputThread = new Thread("Export to Excel File: Writing File") {

            @Override
            public void run() {
                try (final FileOutputStream fileStream = new FileOutputStream(getFile())) {
                    workbook.write(fileStream);
                    LOGGER.log(Level.INFO, "Table View data written to Excel file");
                } catch (final IOException ex) {
                    interaction.notify(PluginNotificationLevel.ERROR, ex.getLocalizedMessage());
                }
                workbook.dispose();
            }
        };
        outputThread.start();
        outputThread.join();
    } catch (final IOException ex) {
        throw new PluginException(PluginNotificationLevel.ERROR, ex);
    }
}
Also used : PluginType(au.gov.asd.tac.constellation.plugins.PluginType) Level(java.util.logging.Level) TableColumn(javafx.scene.control.TableColumn) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) Cell(org.apache.poi.ss.usermodel.Cell) PluginTags(au.gov.asd.tac.constellation.plugins.templates.PluginTags) TableView(javafx.scene.control.TableView) Sheet(org.apache.poi.ss.usermodel.Sheet) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) PluginGraphs(au.gov.asd.tac.constellation.plugins.PluginGraphs) Logger(java.util.logging.Logger) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Collectors(java.util.stream.Collectors) File(java.io.File) PluginNotificationLevel(au.gov.asd.tac.constellation.plugins.PluginNotificationLevel) PluginInfo(au.gov.asd.tac.constellation.plugins.PluginInfo) List(java.util.List) Row(org.apache.poi.ss.usermodel.Row) ObservableList(javafx.collections.ObservableList) SimplePlugin(au.gov.asd.tac.constellation.plugins.templates.SimplePlugin) Pagination(javafx.scene.control.Pagination) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObservableList(javafx.collections.ObservableList) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) FileOutputStream(java.io.FileOutputStream) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Example 29 with PluginInteraction

use of au.gov.asd.tac.constellation.plugins.PluginInteraction in project constellation by constellation-app.

the class ExportToCsvFilePluginNGTest method exportCSV.

@Test
public void exportCSV() throws IOException, InterruptedException, PluginException {
    final TableView<ObservableList<String>> table = mock(TableView.class);
    final Pagination pagination = mock(Pagination.class);
    final PluginInteraction pluginInteraction = mock(PluginInteraction.class);
    File tmpFile = null;
    try (MockedStatic<TableViewUtilities> tableViewUtilsMockedStatic = Mockito.mockStatic(TableViewUtilities.class)) {
        tmpFile = File.createTempFile("constellationTest", ".csv");
        final String csv = "COLUMN_1,COLUMN_2\nrow1Column1,row1Column2\nrow2Column1,row2Column2\n";
        tableViewUtilsMockedStatic.when(() -> TableViewUtilities.getTableData(table, pagination, true, true)).thenReturn(csv);
        final ExportToCsvFilePlugin plugin = new ExportToCsvFilePlugin(tmpFile, table, pagination, true);
        plugin.execute(null, pluginInteraction, null);
        final String outputtedFile = new String(IOUtils.toByteArray(new FileInputStream(tmpFile)), StandardCharsets.UTF_8);
        assertEquals(csv, outputtedFile);
        assertEquals(plugin.getName(), "Table View: Export to Delimited File");
    } finally {
        if (tmpFile != null) {
            tmpFile.delete();
        }
    }
}
Also used : Pagination(javafx.scene.control.Pagination) ObservableList(javafx.collections.ObservableList) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) TableViewUtilities(au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 30 with PluginInteraction

use of au.gov.asd.tac.constellation.plugins.PluginInteraction in project constellation by constellation-app.

the class ExportToExcelFilePluginNGTest method exportToExcelFileSelectedOnly.

@Test
public void exportToExcelFileSelectedOnly() throws IOException, InterruptedException, PluginException {
    final boolean selectedOnly = true;
    final String sheetName = "My Sheet";
    final TableView<ObservableList<String>> table = mock(TableView.class);
    final TableView.TableViewSelectionModel<ObservableList<String>> selectionModel = mock(TableView.TableViewSelectionModel.class);
    final Pagination pagination = mock(Pagination.class);
    final PluginInteraction pluginInteraction = mock(PluginInteraction.class);
    final Callback<Integer, Node> callback = mock(Callback.class);
    File tmpFile = null;
    try {
        tmpFile = File.createTempFile("constellationTest", ".xls");
        when(pagination.getPageFactory()).thenReturn(callback);
        when(callback.call(anyInt())).thenReturn(table);
        when(table.getSelectionModel()).thenReturn(selectionModel);
        when(pagination.getCurrentPageIndex()).thenReturn(42);
        when(pagination.getPageCount()).thenReturn(2);
        final TableColumn<ObservableList<String>, ? extends Object> column1 = mock(TableColumn.class);
        final TableColumn<ObservableList<String>, ? extends Object> column2 = mock(TableColumn.class);
        when(column1.getText()).thenReturn("COLUMN_1");
        when(column2.getText()).thenReturn("COLUMN_2");
        when(column1.isVisible()).thenReturn(true);
        when(column2.isVisible()).thenReturn(true);
        when(table.getColumns()).thenReturn(FXCollections.observableArrayList(column1, column2));
        // Page 1
        doReturn(FXCollections.observableList(List.of(FXCollections.observableList(List.of("row1Column1", "row1Column2", "row1InvisibleColumn3"))))).doReturn(FXCollections.observableList(List.of(FXCollections.observableList(List.of("row2Column1", "row2Column2", "row2InvisibleColumn3"))))).when(selectionModel).getSelectedItems();
        final ExportToExcelFilePlugin exportToExcel = new ExportToExcelFilePlugin(tmpFile, table, pagination, 1, selectedOnly, sheetName);
        exportToExcel.execute(null, pluginInteraction, null);
        // Due to date/times etc. no two files are the same at the byte level
        // So open the saved file, iterating over it, generating a CSV that can
        // be verified.
        final String csvInFile = generateCsvFromExcelFile(tmpFile, sheetName);
        final String expected = "COLUMN_1,COLUMN_2\nrow1Column1,row1Column2\nrow2Column1,row2Column2\n";
        assertEquals(expected, csvInFile);
        // Verifies that it resets to the current page
        verify(callback).call(42);
        assertEquals("Table View: Export to Excel File", exportToExcel.getName());
    } finally {
        if (tmpFile != null) {
            tmpFile.delete();
        }
    }
}
Also used : Node(javafx.scene.Node) Pagination(javafx.scene.control.Pagination) ObservableList(javafx.collections.ObservableList) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) File(java.io.File) TableView(javafx.scene.control.TableView) Test(org.testng.annotations.Test)

Aggregations

PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)51 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)44 Test (org.testng.annotations.Test)33 PluginParameter (au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)16 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)15 ArrayList (java.util.ArrayList)15 TextPluginInteraction (au.gov.asd.tac.constellation.plugins.text.TextPluginInteraction)14 GraphWriteMethods (au.gov.asd.tac.constellation.graph.GraphWriteMethods)13 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)12 PluginGraphs (au.gov.asd.tac.constellation.plugins.PluginGraphs)10 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)9 Graph (au.gov.asd.tac.constellation.graph.Graph)8 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)8 RecordStore (au.gov.asd.tac.constellation.graph.processing.RecordStore)8 List (java.util.List)8 VisualConcept (au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept)7 PluginInfo (au.gov.asd.tac.constellation.plugins.PluginInfo)7 PluginTags (au.gov.asd.tac.constellation.plugins.templates.PluginTags)7 SimpleEditPlugin (au.gov.asd.tac.constellation.plugins.templates.SimpleEditPlugin)7 PluginExecution (au.gov.asd.tac.constellation.plugins.PluginExecution)6