Search in sources :

Example 6 with TableViewState

use of au.gov.asd.tac.constellation.views.tableview.state.TableViewState in project constellation by constellation-app.

the class TableViewStateIoProvider method writeObject.

@Override
public void writeObject(final Attribute attribute, final int elementId, final JsonGenerator jsonGenerator, final GraphReadMethods graph, final GraphByteWriter byteWriter, final boolean verbose) throws IOException {
    if (verbose || !graph.isDefaultValue(attribute.getId(), elementId)) {
        final TableViewState state = (TableViewState) graph.getObjectValue(attribute.getId(), elementId);
        if (state == null) {
            jsonGenerator.writeNullField(attribute.getName());
        } else {
            jsonGenerator.writeObjectFieldStart(attribute.getName());
            jsonGenerator.writeBooleanField("selectedOnly", state.isSelectedOnly());
            jsonGenerator.writeStringField("elementType", state.getElementType().name());
            if (state.getTransactionColumnAttributes() == null) {
                jsonGenerator.writeNullField(TRANSACTION_COLUMN_ATTRIBUTES);
            } else {
                jsonGenerator.writeArrayFieldStart(TRANSACTION_COLUMN_ATTRIBUTES);
                for (final Tuple<String, Attribute> columnAttribute : state.getTransactionColumnAttributes()) {
                    jsonGenerator.writeStartObject();
                    jsonGenerator.writeStringField(ATTRIBUTE_PREFIX, columnAttribute.getFirst());
                    jsonGenerator.writeStringField(ATTRIBUTE_ELEMENT_TYPE, columnAttribute.getSecond().getElementType().name());
                    jsonGenerator.writeStringField(ATTRIBUTE_NAME, columnAttribute.getSecond().getName());
                    jsonGenerator.writeEndObject();
                }
                jsonGenerator.writeEndArray();
            }
            if (state.getVertexColumnAttributes() == null) {
                jsonGenerator.writeNullField(VERTEX_COLUMN_ATTRIBUTES);
            } else {
                jsonGenerator.writeArrayFieldStart(VERTEX_COLUMN_ATTRIBUTES);
                for (final Tuple<String, Attribute> columnAttribute : state.getVertexColumnAttributes()) {
                    jsonGenerator.writeStartObject();
                    jsonGenerator.writeStringField(ATTRIBUTE_PREFIX, columnAttribute.getFirst());
                    jsonGenerator.writeStringField(ATTRIBUTE_ELEMENT_TYPE, columnAttribute.getSecond().getElementType().name());
                    jsonGenerator.writeStringField(ATTRIBUTE_NAME, columnAttribute.getSecond().getName());
                    jsonGenerator.writeEndObject();
                }
                jsonGenerator.writeEndArray();
            }
            jsonGenerator.writeEndObject();
        }
    }
}
Also used : GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) Attribute(au.gov.asd.tac.constellation.graph.Attribute) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState)

Example 7 with TableViewState

use of au.gov.asd.tac.constellation.views.tableview.state.TableViewState in project constellation by constellation-app.

the class TableViewTopComponent method showSelected.

/**
 * Copy's the existing table view state and sets the new state's element
 * type to the passed value. Also ensures the new state is in "Selected
 * Only" mode. The graph table view state attribute is updated with the new
 * state and then the table's selection is updated.
 *
 * @param elementType the element type to set to the new state
 * @param elementId can be anything, not used
 */
public Future<?> showSelected(final GraphElementType elementType, final int elementId) {
    final TableViewState stateSnapshot = getCurrentState();
    final Future<?> stateLock;
    if (getCurrentState() != null && getCurrentState().getElementType() != elementType) {
        final TableViewState newState = new TableViewState(getCurrentState());
        newState.setElementType(elementType);
        newState.setSelectedOnly(true);
        stateLock = PluginExecution.withPlugin(new UpdateStatePlugin(newState)).executeLater(getCurrentGraph());
    } else {
        stateLock = null;
    }
    if (stateLock != null) {
        try {
            stateLock.get();
        } catch (final ExecutionException ex) {
        // DO NOTHING
        } catch (final InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
    return getExecutorService().submit(() -> {
        while (stateLock != null && getCurrentState() == stateSnapshot) {
            try {
                // TODO: remove sleep
                // ...but there is an async issue which needs to be
                // resolved first. When showSelected() is called, the
                // order of operations is to update the Table View
                // state (if required) and then to select the rows in
                // the table based on the current graph selection. The
                // issue is that the state is updated by writing a
                // TableViewState object to the graph and letting a
                // Table View listener respond to that. Unfortunately,
                // there is no obvious way for this operation to know
                // when the Table View listener has finished responding,
                // so for now we just wait until the currentState object
                // matches the state object we updated it to.
                Thread.sleep(10);
            } catch (final InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }
        getTablePane().getTable().updateSelection(getCurrentGraph(), getCurrentState());
    });
}
Also used : TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) UpdateStatePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.UpdateStatePlugin) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with TableViewState

use of au.gov.asd.tac.constellation.views.tableview.state.TableViewState in project constellation by constellation-app.

the class TableViewTopComponent method updateState.

/**
 * Update the current table state with the table state stored in the passed
 * graph attributes. If a table state does not exist in the graph attribute
 * then it will crate and new state and set it to the current state in the
 * table.
 *
 * @param graph the graph that the new state will be extracted from
 */
protected void updateState(final Graph graph) {
    TableViewState state = null;
    boolean newState = false;
    if (graph == null) {
        currentState = state;
    } else {
        final ReadableGraph readableGraph = graph.getReadableGraph();
        try {
            final int stateAttribute = TableViewConcept.MetaAttribute.TABLE_VIEW_STATE.get(readableGraph);
            if (stateAttribute == Graph.NOT_FOUND) {
                state = new TableViewState();
                newState = true;
            } else {
                state = readableGraph.getObjectValue(stateAttribute, 0);
                if (state == null) {
                    state = new TableViewState();
                    newState = true;
                }
            }
            if (newState) {
                PluginExecution.withPlugin(new UpdateStatePlugin(state)).executeLater(getCurrentGraph());
            }
        } finally {
            readableGraph.release();
        }
    }
    currentState = state;
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) UpdateStatePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.UpdateStatePlugin)

Example 9 with TableViewState

use of au.gov.asd.tac.constellation.views.tableview.state.TableViewState 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 10 with TableViewState

use of au.gov.asd.tac.constellation.views.tableview.state.TableViewState in project constellation by constellation-app.

the class TableNGTest method updateSelectionOnFxThread.

@Test(expectedExceptions = IllegalStateException.class)
public void updateSelectionOnFxThread() {
    try (final MockedStatic<Platform> platformMockedStatic = Mockito.mockStatic(Platform.class)) {
        platformMockedStatic.when(Platform::isFxApplicationThread).thenReturn(true);
        table.updateSelection(graph, new TableViewState());
    }
}
Also used : Platform(javafx.application.Platform) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) Test(org.testng.annotations.Test)

Aggregations

TableViewState (au.gov.asd.tac.constellation.views.tableview.state.TableViewState)65 Test (org.testng.annotations.Test)50 Graph (au.gov.asd.tac.constellation.graph.Graph)24 Platform (javafx.application.Platform)19 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)14 ObservableList (javafx.collections.ObservableList)14 Attribute (au.gov.asd.tac.constellation.graph.Attribute)13 UpdateStatePlugin (au.gov.asd.tac.constellation.views.tableview.plugins.UpdateStatePlugin)12 PluginExecution (au.gov.asd.tac.constellation.plugins.PluginExecution)10 TableColumn (javafx.scene.control.TableColumn)10 GraphAttribute (au.gov.asd.tac.constellation.graph.GraphAttribute)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 TablePane (au.gov.asd.tac.constellation.views.tableview.panes.TablePane)7 ListChangeListener (javafx.collections.ListChangeListener)7 Tuple (au.gov.asd.tac.constellation.utilities.datastructure.Tuple)6 TableViewUtilities (au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities)6 GraphElementType (au.gov.asd.tac.constellation.graph.GraphElementType)5 Column (au.gov.asd.tac.constellation.views.tableview.api.Column)5 Table (au.gov.asd.tac.constellation.views.tableview.components.Table)5 ArrayList (java.util.ArrayList)5