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);
}
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);
}
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);
}
}
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();
}
}
}
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();
}
}
}
Aggregations