Search in sources :

Example 1 with UpdateDataTask

use of au.gov.asd.tac.constellation.views.tableview.tasks.UpdateDataTask in project constellation by constellation-app.

the class TableNGTest method testUpdateData.

/**
 * Tests the update data method. If the initial state's element type is
 * vertex, then the parameters transaction row 1 and 2 can be null. And vice
 * versa.
 *
 * @param stateElementType the initial element type in the table state
 * @param isSelectedOnlyMode true if the table's initial state is in
 * selected only mode, false otherwise
 * @param transactionRow1 row 1 that represents a transaction element in the
 * graph
 * @param transactionRow2 row 2 that represents a transaction element in the
 * graph
 * @param vertexRow1 row 1 that represents a vertex element in the graph
 * @param vertexRow2 row 2 that represents a vertex element in the graph
 * @param expectedRows the expected rows that will be added to the table
 */
private void testUpdateData(final GraphElementType stateElementType, final boolean isSelectedOnlyMode, final ObservableList<String> transactionRow1, final ObservableList<String> transactionRow2, final ObservableList<String> vertexRow1, final ObservableList<String> vertexRow2, final List<ObservableList<String>> expectedRows) {
    final TableViewState tableViewState = new TableViewState();
    tableViewState.setElementType(stateElementType);
    tableViewState.setSelectedOnly(isSelectedOnlyMode);
    final BorderPane progressPane = mock(BorderPane.class);
    final ProgressBar progressBar = mock(ProgressBar.class);
    when(progressBar.getProgressPane()).thenReturn(progressPane);
    final ReadableGraph readableGraph = mock(ReadableGraph.class);
    when(graph.getReadableGraph()).thenReturn(readableGraph);
    // Initialize row and element ID mappers and place some fake data in
    // to ensure that it is cleared during the update
    final Map<Integer, ObservableList<String>> elementIdToRowIndex = new HashMap<>();
    elementIdToRowIndex.put(42, FXCollections.observableArrayList());
    final Map<ObservableList<String>, Integer> rowToElementIdIndex = new HashMap<>();
    rowToElementIdIndex.put(FXCollections.observableArrayList(), 42);
    doReturn(elementIdToRowIndex).when(activeTableReference).getElementIdToRowIndex();
    doReturn(rowToElementIdIndex).when(activeTableReference).getRowToElementIdIndex();
    // Mock graph to extract transaction element IDs
    when(readableGraph.getAttribute(stateElementType, "selected")).thenReturn(22);
    when(readableGraph.getTransactionCount()).thenReturn(2);
    when(readableGraph.getVertexCount()).thenReturn(2);
    when(readableGraph.getTransaction(0)).thenReturn(101);
    when(readableGraph.getTransaction(1)).thenReturn(102);
    when(readableGraph.getVertex(0)).thenReturn(201);
    when(readableGraph.getVertex(1)).thenReturn(202);
    when(readableGraph.getBooleanValue(22, 101)).thenReturn(false);
    when(readableGraph.getBooleanValue(22, 102)).thenReturn(true);
    when(readableGraph.getBooleanValue(22, 201)).thenReturn(false);
    when(readableGraph.getBooleanValue(22, 202)).thenReturn(true);
    // Mock the transaction row creation
    doReturn(transactionRow1).when(table).getRowDataForTransaction(readableGraph, 101);
    doReturn(transactionRow2).when(table).getRowDataForTransaction(readableGraph, 102);
    doReturn(vertexRow1).when(table).getRowDataForVertex(readableGraph, 201);
    doReturn(vertexRow2).when(table).getRowDataForVertex(readableGraph, 202);
    try (final MockedStatic<Platform> platformMockedStatic = Mockito.mockStatic(Platform.class)) {
        platformMockedStatic.when(() -> Platform.runLater(any(Runnable.class))).then(mockitoInvocation -> {
            final Runnable runnable = (Runnable) mockitoInvocation.getArgument(0);
            if (runnable instanceof UpdateDataTask) {
                final UpdateDataTask updateDataTask = (UpdateDataTask) runnable;
                // If this is not called then the test will halt forever
                updateDataTask.getUpdateDataLatch().countDown();
                assertEquals(expectedRows, updateDataTask.getRows());
            } else {
                // Progress Bar
                runnable.run();
            }
            return null;
        });
        table.updateData(graph, tableViewState, progressBar);
    }
    assertTrue(elementIdToRowIndex.isEmpty());
    assertTrue(rowToElementIdIndex.isEmpty());
    verify(tablePane).setCenter(progressPane);
    verify(readableGraph).release();
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) BorderPane(javafx.scene.layout.BorderPane) Platform(javafx.application.Platform) HashMap(java.util.HashMap) ObservableList(javafx.collections.ObservableList) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) UpdateDataTask(au.gov.asd.tac.constellation.views.tableview.tasks.UpdateDataTask)

Example 2 with UpdateDataTask

use of au.gov.asd.tac.constellation.views.tableview.tasks.UpdateDataTask in project constellation by constellation-app.

the class Table method updateData.

/**
 * Update the data in the table using the graph and state.
 * <p/>
 * If the table is in "Selection Only" mode then only the elements on the
 * graph that are selected will be loaded into the table, otherwise they all
 * will.
 * <p/>
 * Which elements are loaded also depends on which element type the table
 * state is currently set to, vertex or transaction.
 * <p/>
 * The entire method is synchronized so it should be thread safe and keeps
 * the locking logic simpler. Maybe this method could be broken out further.
 *
 * @param graph the graph to retrieve data from.
 * @param state the current table view state.
 */
public void updateData(final Graph graph, final TableViewState state, final ProgressBar progressBar) {
    synchronized (TABLE_LOCK) {
        if (graph != null && state != null) {
            if (Platform.isFxApplicationThread()) {
                throw new IllegalStateException(ATTEMPT_PROCESS_JAVAFX);
            }
            if (SwingUtilities.isEventDispatchThread()) {
                throw new IllegalStateException(ATTEMPT_PROCESS_EDT);
            }
            // Set progress indicator
            Platform.runLater(() -> getParentComponent().setCenter(progressBar.getProgressPane()));
            // Clear the current row and element mappings
            getActiveTableReference().getElementIdToRowIndex().clear();
            getActiveTableReference().getRowToElementIdIndex().clear();
            // Build table data based on attribute values on the graph
            final List<ObservableList<String>> rows = new ArrayList<>();
            final ReadableGraph readableGraph = graph.getReadableGraph();
            try {
                if (state.getElementType() == GraphElementType.TRANSACTION) {
                    final int selectedAttributeId = VisualConcept.TransactionAttribute.SELECTED.get(readableGraph);
                    final int transactionCount = readableGraph.getTransactionCount();
                    IntStream.range(0, transactionCount).forEach(transactionPosition -> {
                        final int transactionId = readableGraph.getTransaction(transactionPosition);
                        boolean isSelected = false;
                        if (selectedAttributeId != Graph.NOT_FOUND) {
                            isSelected = readableGraph.getBooleanValue(selectedAttributeId, transactionId);
                        }
                        // in selected only mode, only add the ones that are selected in the graph
                        if (!state.isSelectedOnly() || isSelected) {
                            rows.add(getRowDataForTransaction(readableGraph, transactionId));
                        }
                    });
                } else {
                    final int selectedAttributeId = VisualConcept.VertexAttribute.SELECTED.get(readableGraph);
                    final int vertexCount = readableGraph.getVertexCount();
                    IntStream.range(0, vertexCount).forEach(vertexPosition -> {
                        final int vertexId = readableGraph.getVertex(vertexPosition);
                        boolean isSelected = false;
                        if (selectedAttributeId != Graph.NOT_FOUND) {
                            isSelected = readableGraph.getBooleanValue(selectedAttributeId, vertexId);
                        }
                        // in selected only mode, only add the ones that are selected in the graph
                        if (!state.isSelectedOnly() || isSelected) {
                            rows.add(getRowDataForVertex(readableGraph, vertexId));
                        }
                    });
                }
            } finally {
                readableGraph.release();
            }
            // bar.
            if (!Thread.currentThread().isInterrupted()) {
                final UpdateDataTask updateDataTask = new UpdateDataTask(this, rows);
                Platform.runLater(updateDataTask);
                try {
                    updateDataTask.getUpdateDataLatch().await();
                } catch (final InterruptedException ex) {
                    LOGGER.log(Level.WARNING, "InterruptedException encountered while updating table data");
                    updateDataTask.setInterrupted(true);
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) ObservableList(javafx.collections.ObservableList) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) UpdateDataTask(au.gov.asd.tac.constellation.views.tableview.tasks.UpdateDataTask)

Aggregations

ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)2 UpdateDataTask (au.gov.asd.tac.constellation.views.tableview.tasks.UpdateDataTask)2 ObservableList (javafx.collections.ObservableList)2 TableViewState (au.gov.asd.tac.constellation.views.tableview.state.TableViewState)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Platform (javafx.application.Platform)1 BorderPane (javafx.scene.layout.BorderPane)1