Skip to content

Commit

Permalink
Add Event handlers for KeycodeCombinations to copy information from t…
Browse files Browse the repository at this point in the history
…ables
  • Loading branch information
patschuh committed Mar 28, 2023
1 parent 55565c9 commit d15e3db
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 70 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -11,7 +11,7 @@ plugins {
}

group = 'at.esque.kafka'
version = '2.5.0'
version = '2.5.1'

repositories {
mavenCentral()
Expand Down
42 changes: 41 additions & 1 deletion src/main/java/at/esque/kafka/Controller.java
Expand Up @@ -51,6 +51,7 @@
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
Expand All @@ -72,7 +73,12 @@
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.HBox;
import javafx.stage.DirectoryChooser;
Expand Down Expand Up @@ -109,6 +115,7 @@
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
Expand All @@ -126,6 +133,7 @@
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -162,6 +170,8 @@ public class Controller {

//FXML
@FXML
private Tooltip helpIconToolTip;
@FXML
private KafkaEsqueCodeArea keyTextArea;
@FXML
private Tab valueTab;
Expand Down Expand Up @@ -305,6 +315,9 @@ public void setup(Stage controlledStage) {
}
});

headerTableView.setOnKeyPressed(generateHeaderTableEventHandler());
metdataTableView.setOnKeyPressed(generateMetadataTableEventHandler());

configHandler.configureKafkaEsqueCodeArea(keyTextArea);
configHandler.configureKafkaEsqueCodeArea(valueTextArea);

Expand All @@ -319,6 +332,7 @@ public void setup(Stage controlledStage) {
ClusterConfig dummycluster = new ClusterConfig();
dummycluster.setIdentifier("Empty");
messageTabPane.getTabs().add(createTab(dummycluster, "Tab"));
helpIconToolTip.setText(buildToolTip());

versionInfoHandler.showDialogIfUpdateIsAvailable(hostServices);
}
Expand Down Expand Up @@ -1409,7 +1423,8 @@ public void playMessageBook(ActionEvent event) {
});
producerHandler.sendMessage(producerId, message.getTargetTopic(), message.getPartition() == -1 ? null : message.getPartition(), message.getKey(), message.getValue(), message.getKeyType(), message.getValueType());
Platform.runLater(() -> backGroundTaskHolder.setProgressMessage("published " + counter.incrementAndGet() + " messages"));
} catch (InterruptedException | ExecutionException | TimeoutException | IOException | RestClientException e) {
} catch (InterruptedException | ExecutionException | TimeoutException | IOException |
RestClientException e) {
throw new RuntimeException(e);
}
});
Expand Down Expand Up @@ -1555,7 +1570,32 @@ public void aboutClick(ActionEvent event) {
}
}

private EventHandler<? super KeyEvent> generateHeaderTableEventHandler() {
Map<KeyCodeCombination, Function<Header, String>> copyCombinations = Map.of(
new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN), header -> new String(header.value(), StandardCharsets.UTF_8),
new KeyCodeCombination(KeyCode.K, KeyCombination.SHORTCUT_DOWN), Header::key
);

return SystemUtils.generateTableCopySelectedItemCopyEventHandler(headerTableView, copyCombinations);
}

private EventHandler<? super KeyEvent> generateMetadataTableEventHandler() {
Map<KeyCodeCombination, Function<MessageMetaData, String>> copyCombinations = Map.of(
new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN), metadata -> metadata.valueAsString().getValue(),
new KeyCodeCombination(KeyCode.K, KeyCombination.SHORTCUT_DOWN), metadata -> metadata.nameProperty().getName()
);

return SystemUtils.generateTableCopySelectedItemCopyEventHandler(metdataTableView, copyCombinations);
}


public void setHostServices(HostServices hostServices) {
this.hostServices = hostServices;
}

private String buildToolTip() {
return String.format("The following KeyCombinations let you copy data from the selected element in the metadata and header table%n" +
new KeyCodeCombination(KeyCode.K, KeyCombination.SHORTCUT_DOWN).getDisplayText() + " - copy Key/Name%n" +
new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN).getDisplayText() + " - copy Value%n");
}
}
102 changes: 59 additions & 43 deletions src/main/java/at/esque/kafka/KafkaConnectBrowserController.java
Expand Up @@ -25,8 +25,13 @@
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.Tooltip;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;
Expand All @@ -35,14 +40,18 @@
import org.kordamp.ikonli.javafx.FontIcon;

import java.util.Map;
import java.util.function.Function;


public class KafkaConnectBrowserController {

private static final String RIGHT_PANE_CONNECTOR_ACTION_PAUSE = "pause connector";
private static final String RIGHT_PANE_CONNECTOR_ACTION_RESUME = "resume connector";
private static final String RIGHT_PANE_CONNECTOR_ACTION_PAUSE = "pause connector";
private static final String RIGHT_PANE_CONNECTOR_ACTION_RESUME = "resume connector";
private static final String RIGHT_PANE_CONNECTOR_ACTION_RESTART = "restart connector";

@FXML
private Tooltip helpIconToolTip;

@FXML
private FilterableListView<String> connectorListView;
@FXML
Expand Down Expand Up @@ -114,8 +123,7 @@ public void handle(ActionEvent event) {
}

updateRightPane(selectedConnectorInRightPane);
} catch (Exception e)
{
} catch (Exception e) {
ErrorAlert.show(e, getWindow());
}

Expand All @@ -127,20 +135,19 @@ public void handle(ActionEvent event) {
}
});



taskTableView.setOnKeyPressed(generateMessageTableCopyEventHandler());
helpIconToolTip.setText(buildToolTip());
}

private Window getWindow() {
return connectorConfigTextArea.getScene().getWindow();
}

public void updateRightPane(String selectedConnector)
{
public void updateRightPane(String selectedConnector) {
try {
if(selectedConnector != null) {
if (selectedConnector != null) {
Map<String, String> connectorConfig = kafkaesqueConnectClient.getConnectorConfig(selectedConnector);
connectorConfigTextArea.setText(ConnectUtil.buildConfigString(connectorConfig,ConnectUtil.PARAM_BLACK_LIST_VIEW));
connectorConfigTextArea.setText(ConnectUtil.buildConfigString(connectorConfig, ConnectUtil.PARAM_BLACK_LIST_VIEW));

Status status = kafkaesqueConnectClient.getConnectorStatus(selectedConnector);
connectorStatus.setText(status.getStatus());
Expand All @@ -149,9 +156,7 @@ public void updateRightPane(String selectedConnector)
selectedConnectorInRightPane = selectedConnector;

taskTableView.setItems(FXCollections.observableArrayList(status.getTaskStatusList()));
}
else
{
} else {
connectorConfigTextArea.setText("");
connectorStatus.setText("");
updateDisableFlagOfConnectorActionButtonStatus(null);
Expand Down Expand Up @@ -180,8 +185,7 @@ public void refreshConnectors(ActionEvent actionEvent) {
}
}

private void showConnectConfigDialog(String selectedConnector)
{
private void showConnectConfigDialog(String selectedConnector) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/createConnector.fxml"));
Parent root1 = fxmlLoader.load();
Expand All @@ -208,21 +212,20 @@ private ListCell<String> connectorListCellFactory() {
deleteItem.setGraphic(new FontIcon(FontAwesome.TRASH));
deleteItem.textProperty().set("delete");
deleteItem.setOnAction(event -> {
if (ConfirmationAlert.show("Delete Connector", "Connector [" + cell.itemProperty().get() + "] will be deleted.", "Are you sure you want to delete this connector", getWindow())) {
try {
boolean result = kafkaesqueConnectClient.deleteConnector(cell.itemProperty().get());

if(result == true) {
SuccessAlert.show("Delete Connector", null, "Connector [" + cell.itemProperty().get() + "] deleted.", getWindow());
} else
{
WarningAlert.show( "Delete Connector", null, "It wasn't possible to delete the connector", getWindow());
}
} catch (Exception e) {
ErrorAlert.show(e, getWindow());
if (ConfirmationAlert.show("Delete Connector", "Connector [" + cell.itemProperty().get() + "] will be deleted.", "Are you sure you want to delete this connector", getWindow())) {
try {
boolean result = kafkaesqueConnectClient.deleteConnector(cell.itemProperty().get());

if (result == true) {
SuccessAlert.show("Delete Connector", null, "Connector [" + cell.itemProperty().get() + "] deleted.", getWindow());
} else {
WarningAlert.show("Delete Connector", null, "It wasn't possible to delete the connector", getWindow());
}
} catch (Exception e) {
ErrorAlert.show(e, getWindow());
}
});
}
});

MenuItem configItem = new MenuItem();
configItem.setGraphic(new FontIcon(FontAwesome.COG));
Expand All @@ -247,12 +250,11 @@ private ListCell<String> connectorListCellFactory() {
}


private void updateDisableFlagOfConnectorActionButtonStatus(String connectorStatus)
{
private void updateDisableFlagOfConnectorActionButtonStatus(String connectorStatus) {
if (connectorStatus == null)
connectorStatus = "";

switch(connectorStatus){
switch (connectorStatus) {
case "RUNNING":
pauseButton.setDisable(false);
resumeButton.setDisable(true);
Expand All @@ -268,33 +270,29 @@ private void updateDisableFlagOfConnectorActionButtonStatus(String connectorStat
}

@FXML
public void pauseConnectorClick(ActionEvent actionEvent)
{
public void pauseConnectorClick(ActionEvent actionEvent) {
rightPaneConnectorAction(RIGHT_PANE_CONNECTOR_ACTION_PAUSE);
}

@FXML
public void resumeConnectorClick(ActionEvent actionEvent)
{
public void resumeConnectorClick(ActionEvent actionEvent) {
rightPaneConnectorAction(RIGHT_PANE_CONNECTOR_ACTION_RESUME);
}

@FXML
public void restartConnectorClick(ActionEvent actionEvent)
{
public void restartConnectorClick(ActionEvent actionEvent) {
rightPaneConnectorAction(RIGHT_PANE_CONNECTOR_ACTION_RESTART);
}

private void rightPaneConnectorAction(String action)
{
private void rightPaneConnectorAction(String action) {
try {

if (selectedConnectorInRightPane == null)
return;

boolean result = false;

switch(action){
switch (action) {
case RIGHT_PANE_CONNECTOR_ACTION_PAUSE:
result = kafkaesqueConnectClient.pauseConnector(selectedConnectorInRightPane);
break;
Expand All @@ -306,9 +304,8 @@ private void rightPaneConnectorAction(String action)
break;
}

if (result != true)
{
WarningAlert.show("Connector Action", null, String.format("Connector action '%s' returned fales from API!",action), getWindow());
if (result != true) {
WarningAlert.show("Connector Action", null, String.format("Connector action '%s' returned fales from API!", action), getWindow());
}

//Refresh view
Expand All @@ -317,4 +314,23 @@ private void rightPaneConnectorAction(String action)
ErrorAlert.show(e, getWindow());
}
}

private EventHandler<? super KeyEvent> generateMessageTableCopyEventHandler() {
Map<KeyCodeCombination, Function<Status.TaskStatus, String>> copyCombinations = Map.of(
new KeyCodeCombination(KeyCode.I, KeyCombination.SHORTCUT_DOWN), taskStatus -> Integer.toString(taskStatus.getId()),
new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN), Status.TaskStatus::getStatus,
new KeyCodeCombination(KeyCode.W, KeyCombination.SHORTCUT_DOWN), Status.TaskStatus::getWorkerId,
new KeyCodeCombination(KeyCode.T, KeyCombination.SHORTCUT_DOWN), Status.TaskStatus::getTrace
);

return SystemUtils.generateTableCopySelectedItemCopyEventHandler(taskTableView, copyCombinations);
}

private String buildToolTip() {
return String.format("The following KeyCombinations let you copy data from the selected element in the tasks table%n" +
new KeyCodeCombination(KeyCode.I, KeyCombination.SHORTCUT_DOWN).getDisplayText() + " - copy task id%n" +
new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN).getDisplayText() + " - copy task status%n" +
new KeyCodeCombination(KeyCode.W, KeyCombination.SHORTCUT_DOWN).getDisplayText() + " - copy worker id%n" +
new KeyCodeCombination(KeyCode.T, KeyCombination.SHORTCUT_DOWN).getDisplayText() + " - copy trace");
}
}
18 changes: 18 additions & 0 deletions src/main/java/at/esque/kafka/SystemUtils.java
Expand Up @@ -2,17 +2,23 @@

import at.esque.kafka.alerts.ErrorAlert;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.control.TableView;
import javafx.scene.control.TextInputDialog;
import javafx.scene.image.Image;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.function.Function;

public class SystemUtils {

Expand Down Expand Up @@ -52,4 +58,16 @@ public static Optional<String> showInputDialog(String defaultValue, String title
return Optional.empty();
}
}

public static <T> EventHandler<? super KeyEvent> generateTableCopySelectedItemCopyEventHandler(TableView<T> targetTableView, Map<KeyCodeCombination, Function<T, String>> copyCombinationMap) {
return keyEvent -> {
if (targetTableView.equals(keyEvent.getSource()) && targetTableView.getSelectionModel().getSelectedItem() != null) {
copyCombinationMap.entrySet()
.stream()
.filter(keyCombinationFunctionEntry -> keyCombinationFunctionEntry.getKey().match(keyEvent))
.findFirst()
.ifPresent(keyCombinationFunctionEntry -> SystemUtils.copyStringSelectionToClipboard(() -> keyCombinationFunctionEntry.getValue().apply(targetTableView.getSelectionModel().getSelectedItem())));
}
};
}
}

0 comments on commit d15e3db

Please sign in to comment.