In this example, we are going to create our own modifier type that will allow user to define modifier "Replace 'what' 'with' in ExamplePdus".
Our rule has to know what to replace and with what it should be replaced:
/**
* Configuration of example modifier.
*/
@Value
@EqualsAndHashCode(callSuper=true)
public class ExampleModifierData extends ModifierData {
String what;
String with;
}
To let user configure what/with data, we have to create configurator component:
/**
* Configurator for setting up example modifier.
*/
public class ExampleModifierConfigurator extends ModifierConfigurator {
@FXML
private TextField whatInput;
@FXML
private TextField withInput;
public ExampleModifierConfigurator() throws IOException {
super("/fxml/ExampleModifierConfigurator.fxml");
}
@Override
public ModifierData getConfig() {
return new ExampleModifierData(whatInput.getText(), withInput.getText());
}
@Override
public void setConfig(ModifierData data) {
whatInput.setText(((ExampleModifierData) data).getWhat());
withInput.setText(((ExampleModifierData) data).getWith());
}
@Override
public boolean isValid() {
if (whatInput.getText().isEmpty()) {
Dialogs.createErrorDialog(
"What required", "You have to enter what you want to find in stringParam.");
return false;
}
return true;
}
}
Simple FXML template:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.*?>
<fx:root xmlns:fx="http://javafx.com/fxml/1" prefHeight="69.0" prefWidth="249.0" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/11.0.1">
<children>
<Label layoutX="7.0" layoutY="13.0" styleClass="input-label" text="What:" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="13.0"/>
<TextField fx:id="whatInput" layoutX="90.0" layoutY="2.0" text="client" AnchorPane.leftAnchor="85.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0"/>
<Label layoutX="10.0" layoutY="43.0" styleClass="input-label" text="With:" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="43.0"/>
<TextField fx:id="withInput" layoutX="85.0" layoutY="39.0" prefHeight="25.0" prefWidth="154.0" text="client xyz" AnchorPane.leftAnchor="85.0" AnchorPane.rightAnchor="10.0"/>
</children>
</fx:root>
Now, let's create modifier that will procees PDUs and provide the replace functionality:
/**
* Example modifier that replaces string params (metadata) in example PDUs.
*/
public class ExampleModifier extends Modifier {
public ExampleModifier(ModifierFactory factory, ModifierData data) {
super(factory, data);
}
@Override
public boolean process(PDU pdu) {
if (!(pdu instanceof ExamplePdu)) {
return true;
}
var examplePdu = (ExamplePdu) pdu;
var exampleModifierData = (ExampleModifierData) data;
var currentStringParam = examplePdu.getStringParam();
var newStringParam = currentStringParam.replace(exampleModifierData.getWhat(), exampleModifierData.getWith());
examplePdu.setStringParam(newStringParam);
return true;
}
}
In order to add our modifier type to the Modifier extension, we need to create modifier factory:
/**
* Example modifier factory for creating example modifiers.
*/
public class ExampleModifierFactory extends ModifierFactory {
@Override
public String getCode() {
return "example-modifier";
}
@Override
public String getName() {
return "Example - Replace in StringParam";
}
@Override
public Modifier createModifier(ModifierData data) {
return new ExampleModifier(this, data);
}
@Override
public Optional<Type> getConfigType() {
return Optional.of(ExampleModifierData.class);
}
@Override
public Optional<ModifierConfigurator> createConfigPane() throws IOException {
return Optional.of(new ExampleModifierConfigurator());
}
}
Great, everything is ready, so we can register our modifier factory inside beforeInit method (implement ExtensionInitListener into PetepExtension).
var maybeModifier = helper.getExtension("modifier");
if (maybeModifier.isEmpty()) {
return;
}
var modifier = (ModifierApi) maybeModifier.get();
if (modifier.registerModifierFactory(new ExampleModifierFactory())) {
Logger.getGlobal().info("Example modifier registered!");
}