Search in sources :

Example 1 with UserTablePreferences

use of au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences in project constellation by constellation-app.

the class TableViewTopComponentNGTest method handleGraphClosed.

@Test
public void handleGraphClosed() throws InterruptedException {
    final TableViewTopComponent tableViewTopComponent = mock(TableViewTopComponent.class);
    final TablePane tablePane = mock(TablePane.class);
    final ActiveTableReference activeTableReference = mock(ActiveTableReference.class);
    final Pagination pagination = mock(Pagination.class);
    final UserTablePreferences userTablePreferences = new UserTablePreferences();
    userTablePreferences.setMaxRowsPerPage(42);
    when(tableViewTopComponent.getTablePane()).thenReturn(tablePane);
    when(tablePane.getActiveTableReference()).thenReturn(activeTableReference);
    when(activeTableReference.getUserTablePreferences()).thenReturn(userTablePreferences);
    when(activeTableReference.getPagination()).thenReturn(pagination);
    doCallRealMethod().when(tableViewTopComponent).handleGraphClosed(any(Graph.class));
    tableViewTopComponent.handleGraphClosed(mock(Graph.class));
    verify(activeTableReference).updatePagination(42, null, tablePane);
}
Also used : Pagination(javafx.scene.control.Pagination) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) UserTablePreferences(au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences) ActiveTableReference(au.gov.asd.tac.constellation.views.tableview.api.ActiveTableReference) TablePane(au.gov.asd.tac.constellation.views.tableview.panes.TablePane) Test(org.testng.annotations.Test)

Example 2 with UserTablePreferences

use of au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences in project constellation by constellation-app.

the class TableViewPreferencesIoProvider method savePreferences.

/**
 * Saves details of the table's current preferences. Persists the
 * preferences as JSON to the local disk.
 *
 * @param tableElementType the current table setting specifying which
 * element type to display
 * @param table the current table
 * @param pageSize the current table page size
 */
public static void savePreferences(final GraphElementType tableElementType, final TableView<ObservableList<String>> table, final int pageSize) {
    final String filePrefix = tableElementType == GraphElementType.VERTEX ? VERTEX_FILE_PREFIX : TRANSACTION_FILE_PREFIX;
    final UserTablePreferences tablePreferences = new UserTablePreferences();
    tablePreferences.setColumnOrder(table.getColumns().stream().filter(column -> column.isVisible()).map(column -> column.getText()).collect(Collectors.toList()));
    tablePreferences.setMaxRowsPerPage(pageSize);
    if (!table.getSortOrder().isEmpty()) {
        tablePreferences.setSortByColumn(ImmutablePair.of(table.getSortOrder().get(0).getText(), table.getSortOrder().get(0).getSortType()));
    }
    JsonIO.saveJsonPreferences(Optional.of(TABLE_VIEW_PREF_DIR), Optional.of(filePrefix), List.of(tablePreferences));
}
Also used : List(java.util.List) JsonIO(au.gov.asd.tac.constellation.utilities.genericjsonio.JsonIO) GraphElementType(au.gov.asd.tac.constellation.graph.GraphElementType) UserTablePreferences(au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences) Optional(java.util.Optional) ObservableList(javafx.collections.ObservableList) TypeReference(com.fasterxml.jackson.core.type.TypeReference) TableView(javafx.scene.control.TableView) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) UserTablePreferences(au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences)

Example 3 with UserTablePreferences

use of au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences in project constellation by constellation-app.

the class TableViewPreferencesIoProvider method getPreferences.

/**
 * Load in the preferences from the JSON file into the
 * {@link UserTablePreferences} POJO.
 *
 * @param tableElementType the current table setting specifying which
 * element type to display
 *
 * @return a populated {@link UserTablePreferences} from the local file
 */
public static UserTablePreferences getPreferences(final GraphElementType tableElementType) {
    final String filePrefix = tableElementType == GraphElementType.VERTEX ? VERTEX_FILE_PREFIX : TRANSACTION_FILE_PREFIX;
    final List<UserTablePreferences> root = JsonIO.loadJsonPreferences(Optional.of(TABLE_VIEW_PREF_DIR), Optional.of(filePrefix), new TypeReference<List<UserTablePreferences>>() {
    });
    return root == null ? new UserTablePreferences() : root.get(0);
}
Also used : UserTablePreferences(au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences) List(java.util.List) ObservableList(javafx.collections.ObservableList)

Example 4 with UserTablePreferences

use of au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences in project constellation by constellation-app.

the class ExportMenuNGTest method verifyExportExcelAction.

/**
 * Verify that the passed event handler exports to Excel either the whole
 * table or just the selected rows.
 *
 * @param eventHandler the handler to test
 * @param expectedCopyOnlySelectedRows true if only the selected rows are
 *     expected to be exported, false otherwise
 * @param userCancelsRequest true if the user is meant to cancel the export when
 *     picking a file in the file chooser
 */
private void verifyExportExcelAction(final EventHandler<ActionEvent> eventHandler, final boolean userCancelsRequest, final boolean expectedExportOnlySelectedRows) throws InterruptedException, PluginException, ExecutionException {
    final ExportMenuItemActionHandler exportActionHandler = (ExportMenuItemActionHandler) eventHandler;
    final ExportMenuItemActionHandler spiedExportActionHandler = spy(exportActionHandler);
    final FileChooserBuilder exportFileChooser = mock(FileChooserBuilder.class);
    final File exportFile = userCancelsRequest ? null : new File("test.xlsx");
    final Optional<File> optionalExportFile = Optional.ofNullable(exportFile);
    doReturn(exportFileChooser).when(spiedExportActionHandler).getExportFileChooser();
    try (final MockedStatic<PluginExecution> pluginExecutionMockedStatic = Mockito.mockStatic(PluginExecution.class);
        final MockedStatic<FileChooser> fileChooserMockedStatic = Mockito.mockStatic(FileChooser.class);
        final MockedStatic<Platform> platformMockedStatic = Mockito.mockStatic(Platform.class)) {
        // This is added so that the mocked static that we would otherwise be
        // trying to run in the fx thread is actually invoked properly
        platformMockedStatic.when(() -> Platform.runLater(any(Runnable.class))).thenAnswer(iom -> {
            ((Runnable) iom.getArgument(0)).run();
            return null;
        });
        fileChooserMockedStatic.when(() -> FileChooser.openSaveDialog(exportFileChooser)).thenReturn(CompletableFuture.completedFuture(optionalExportFile));
        final ActionEvent actionEvent = mock(ActionEvent.class);
        final Pagination pagination = mock(Pagination.class);
        final TableView<ObservableList<String>> tableView = mock(TableView.class);
        final int maxRowsPerPage = 42;
        final UserTablePreferences userTablePreferences = new UserTablePreferences();
        userTablePreferences.setMaxRowsPerPage(maxRowsPerPage);
        when(activeTableReference.getPagination()).thenReturn(pagination);
        when(table.getTableView()).thenReturn(tableView);
        when(activeTableReference.getUserTablePreferences()).thenReturn(userTablePreferences);
        final PluginExecution pluginExecution = mock(PluginExecution.class);
        pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(any(Plugin.class))).thenAnswer(mockitoInvocation -> {
            final ExportToExcelFilePlugin plugin = (ExportToExcelFilePlugin) mockitoInvocation.getArgument(0);
            if (exportFile != null) {
                assertEquals(exportFile.getAbsolutePath(), plugin.getFile().getAbsolutePath());
            } else {
                assertEquals(exportFile, plugin.getFile());
            }
            assertEquals(pagination, plugin.getPagination());
            assertEquals(tableView, plugin.getTable());
            assertEquals(42, plugin.getRowsPerPage());
            assertEquals(GRAPH_ID, plugin.getSheetName());
            assertEquals(expectedExportOnlySelectedRows, plugin.isSelectedOnly());
            return pluginExecution;
        });
        spiedExportActionHandler.handle(actionEvent);
        // Wait for the export job to complete
        spiedExportActionHandler.getLastExport().get();
        fileChooserMockedStatic.verify(() -> FileChooser.openSaveDialog(exportFileChooser));
        if (userCancelsRequest) {
            verifyNoInteractions(pluginExecution);
        } else {
            verify(pluginExecution).executeNow((Graph) null);
        }
        verify(actionEvent).consume();
    }
}
Also used : PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) Platform(javafx.application.Platform) ActionEvent(javafx.event.ActionEvent) ExportMenuItemActionHandler(au.gov.asd.tac.constellation.views.tableview.components.ExportMenu.ExportMenuItemActionHandler) ExportToExcelFilePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.ExportToExcelFilePlugin) Pagination(javafx.scene.control.Pagination) UserTablePreferences(au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences) ObservableList(javafx.collections.ObservableList) JFileChooser(javax.swing.JFileChooser) FileChooser(au.gov.asd.tac.constellation.utilities.gui.filechooser.FileChooser) FileChooserBuilder(org.openide.filesystems.FileChooserBuilder) File(java.io.File) ExportToExcelFilePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.ExportToExcelFilePlugin) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) ExportToCsvFilePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.ExportToCsvFilePlugin)

Example 5 with UserTablePreferences

use of au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences in project constellation by constellation-app.

the class PreferenceMenuNGTest method verifyPageSizeAction.

/**
 * Verifies that when a page size menu item is clicked, the preferences will
 * be updated as well as the pagination and the table will be refreshed. If
 * the page size that was clicked is the current page size, then no actions
 * should be taken.
 *
 * @param pageSizeMenuItem a page size menu item
 * @param pageSize the page size that the menu item will trigger
 * @throws InterruptedException if there is a an issue waiting for the
 * JavaFX thread work to complete
 */
private void verifyPageSizeAction(final MenuItem pageSizeMenuItem, final int pageSize) throws InterruptedException {
    clearInvocations(activeTableReference, tablePane);
    final ActionEvent actionEvent = mock(ActionEvent.class);
    final Pagination pagination = mock(Pagination.class);
    final UserTablePreferences userTablePreferences = new UserTablePreferences();
    userTablePreferences.setMaxRowsPerPage(42);
    when(activeTableReference.getUserTablePreferences()).thenReturn(userTablePreferences);
    when(activeTableReference.getPagination()).thenReturn(pagination);
    pageSizeMenuItem.getOnAction().handle(actionEvent);
    pageSizeMenuItem.getOnAction().handle(actionEvent);
    final CountDownLatch latch = new CountDownLatch(1);
    Platform.runLater(() -> latch.countDown());
    latch.await();
    // The action was called twice but the update should only happen once as
    // it was the same page size. No change for the second action
    assertEquals(pageSize, userTablePreferences.getMaxRowsPerPage());
    verify(activeTableReference).updatePagination(pageSize, tablePane);
}
Also used : Pagination(javafx.scene.control.Pagination) UserTablePreferences(au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences) ActionEvent(javafx.event.ActionEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

UserTablePreferences (au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences)21 ObservableList (javafx.collections.ObservableList)15 Test (org.testng.annotations.Test)14 List (java.util.List)9 Pagination (javafx.scene.control.Pagination)6 Graph (au.gov.asd.tac.constellation.graph.Graph)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)5 ActionEvent (javafx.event.ActionEvent)5 TableViewPreferencesIoProvider (au.gov.asd.tac.constellation.views.tableview.io.TableViewPreferencesIoProvider)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 FileInputStream (java.io.FileInputStream)4 GraphManager (au.gov.asd.tac.constellation.graph.manager.GraphManager)3 TableViewState (au.gov.asd.tac.constellation.views.tableview.state.TableViewState)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 TableColumn (javafx.scene.control.TableColumn)3 Attribute (au.gov.asd.tac.constellation.graph.Attribute)2 ActiveTableReference (au.gov.asd.tac.constellation.views.tableview.api.ActiveTableReference)2 Column (au.gov.asd.tac.constellation.views.tableview.api.Column)2 UpdateMethod (au.gov.asd.tac.constellation.views.tableview.api.UpdateMethod)2 TablePane (au.gov.asd.tac.constellation.views.tableview.panes.TablePane)2