use of au.gov.asd.tac.constellation.views.tableview.plugins.UpdateStatePlugin in project constellation by constellation-app.
the class TableViewTopComponentNGTest method updateStateNotPresentInGraphAttributes.
@Test
public void updateStateNotPresentInGraphAttributes() {
final TableViewTopComponent tableViewTopComponent = mock(TableViewTopComponent.class);
doCallRealMethod().when(tableViewTopComponent).updateState(any(Graph.class));
doCallRealMethod().when(tableViewTopComponent).updateState(isNull());
doCallRealMethod().when(tableViewTopComponent).getCurrentState();
final Graph graph = mock(Graph.class);
final Graph currentGraph = mock(Graph.class);
final ReadableGraph readableGraph = mock(ReadableGraph.class);
when(graph.getReadableGraph()).thenReturn(readableGraph);
when(readableGraph.getAttribute(GraphElementType.META, "table_view_state")).thenReturn(42);
when(readableGraph.getObjectValue(42, 0)).thenReturn(null);
when(tableViewTopComponent.getCurrentGraph()).thenReturn(currentGraph);
try (final MockedStatic<PluginExecution> pluginExecutionMockedStatic = Mockito.mockStatic(PluginExecution.class)) {
final PluginExecution pluginExecution = mock(PluginExecution.class);
pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(any(UpdateStatePlugin.class))).thenAnswer(mockitoInvocation -> {
final UpdateStatePlugin plugin = (UpdateStatePlugin) mockitoInvocation.getArgument(0);
assertEquals(new TableViewState(), plugin.getTableViewState());
return pluginExecution;
});
tableViewTopComponent.updateState(graph);
verify(pluginExecution).executeLater(currentGraph);
}
assertEquals(new TableViewState(), tableViewTopComponent.getCurrentState());
verify(graph).getReadableGraph();
verify(readableGraph).release();
}
use of au.gov.asd.tac.constellation.views.tableview.plugins.UpdateStatePlugin in project constellation by constellation-app.
the class TableViewTopComponentNGTest method showSelected.
@Test
public void showSelected() {
final TableViewTopComponent tableViewTopComponent = mock(TableViewTopComponent.class);
final TableViewState currentState = new TableViewState();
final Graph currentGraph = mock(Graph.class);
final TableViewState expectedNewState = new TableViewState();
expectedNewState.setElementType(GraphElementType.META);
expectedNewState.setSelectedOnly(true);
final TablePane tablePane = mock(TablePane.class);
final Table table = mock(Table.class);
doCallRealMethod().when(tableViewTopComponent).showSelected(any(GraphElementType.class), anyInt());
when(tableViewTopComponent.getCurrentState()).thenReturn(currentState);
when(tableViewTopComponent.getCurrentGraph()).thenReturn(currentGraph);
when(tableViewTopComponent.getTablePane()).thenReturn(tablePane);
when(tableViewTopComponent.getExecutorService()).thenReturn(Executors.newSingleThreadExecutor());
when(tablePane.getTable()).thenReturn(table);
try (final MockedStatic<PluginExecution> pluginExecutionMockedStatic = Mockito.mockStatic(PluginExecution.class)) {
final PluginExecution pluginExecution = mock(PluginExecution.class);
when(pluginExecution.executeLater(any(Graph.class))).thenReturn(CompletableFuture.completedFuture(null));
pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(any(UpdateStatePlugin.class))).thenAnswer(mockitoInvocation -> {
final UpdateStatePlugin plugin = (UpdateStatePlugin) mockitoInvocation.getArgument(0);
assertEquals(expectedNewState, plugin.getTableViewState());
// Change the mock now that the "Update Plugin" has run
when(tableViewTopComponent.getCurrentState()).thenReturn(expectedNewState);
return pluginExecution;
});
final Future<?> updateTask = tableViewTopComponent.showSelected(GraphElementType.META, 42);
// Ensure that the update selection task is completed
try {
updateTask.get(5, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
fail("The submitted task in show selected did not complete in the " + "allowed time. Something is probably wrong.");
}
verify(pluginExecution).executeLater(currentGraph);
verify(table).updateSelection(currentGraph, expectedNewState);
}
}
use of au.gov.asd.tac.constellation.views.tableview.plugins.UpdateStatePlugin in project constellation by constellation-app.
the class TableToolbar method init.
/**
* Initializes the export menu. Until this method is called, all menu UI
* components will be null.
*/
public void init() {
columnVisibilityButton = createButton(COLUMNS_ICON, COLUMN_VISIBILITY, e -> {
final ColumnVisibilityContextMenu columnVisibilityMenu = createColumnVisibilityContextMenu();
columnVisibilityMenu.getContextMenu().show(columnVisibilityButton, Side.RIGHT, 0, 0);
e.consume();
});
selectedOnlyButton = createToggleButton(getSelectedOnlyInitialIcon(), SELECTED_ONLY, e -> {
if (getTableViewTopComponent().getCurrentState() != null) {
getActiveTableReference().getSelectedOnlySelectedRows().clear();
final TableViewState newState = new TableViewState(getTableViewTopComponent().getCurrentState());
newState.setSelectedOnly(!getTableViewTopComponent().getCurrentState().isSelectedOnly());
selectedOnlyButton.setGraphic(newState.isSelectedOnly() ? SELECTED_VISIBLE_ICON : ALL_VISIBLE_ICON);
PluginExecution.withPlugin(new UpdateStatePlugin(newState)).executeLater(getTableViewTopComponent().getCurrentGraph());
}
e.consume();
});
elementTypeButton = createButton(getElementTypeInitialIcon(), ELEMENT_TYPE, e -> {
if (getTableViewTopComponent().getCurrentState() != null) {
final TableViewState newState = new TableViewState(getTableViewTopComponent().getCurrentState());
newState.setElementType(getTableViewTopComponent().getCurrentState().getElementType() == GraphElementType.TRANSACTION ? GraphElementType.VERTEX : GraphElementType.TRANSACTION);
elementTypeButton.setGraphic(newState.getElementType() == GraphElementType.TRANSACTION ? TRANSACTION_ICON : VERTEX_ICON);
PluginExecution.withPlugin(new UpdateStatePlugin(newState)).executeLater(getTableViewTopComponent().getCurrentGraph());
}
e.consume();
});
copyMenu = createCopyMenu();
exportMenu = createExportMenu();
preferencesMenu = createPreferencesMenu();
helpButton = createButton(HELP_ICON, HELP, e -> {
getHelpContext().display();
e.consume();
});
toolbar = new ToolBar(columnVisibilityButton, selectedOnlyButton, elementTypeButton, new Separator(), copyMenu.getCopyButton(), exportMenu.getExportButton(), preferencesMenu.getPreferencesButton(), helpButton);
toolbar.setOrientation(Orientation.VERTICAL);
toolbar.setPadding(new Insets(5));
}
use of au.gov.asd.tac.constellation.views.tableview.plugins.UpdateStatePlugin 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());
});
}
use of au.gov.asd.tac.constellation.views.tableview.plugins.UpdateStatePlugin 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;
}
Aggregations