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:
public class ExampleModifierData extends ModifierData {
private String what;
private String with;
public ExampleModifierData(String what, String with) {
this.what = what;
this.with = with;
}
public String getWhat() {
return what;
}
public void setWhat(String what) {
this.what = what;
}
public String getWith() {
return with;
}
public void setWith(String with) {
this.with = with;
}
}
To let user configure what/with data, we have to create configurator component:
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 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;
}
@Override
public void setConfig(ModifierData data) {
whatInput.setText(((ExampleModifierData) data).getWhat());
withInput.setText(((ExampleModifierData) data).getWith());
}
}
Simple FXML template:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<fx:root prefHeight="69.0" prefWidth="249.0" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/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:
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;
}
ExamplePdu examplePdu = ((ExamplePdu) pdu);
examplePdu.setStringParam(
examplePdu
.getStringParam()
.replace(
((ExampleModifierData) data).getWhat(), ((ExampleModifierData) data).getWith()));
return true;
}
}
In order to add our modifier type to the Modifier extension, we need to create modifier factory:
public class ExampleModifierFactory extends ModifierFactory {
@Override
public ModifierConfigurator createConfigPane() throws IOException {
return new ExampleModifierConfigurator();
}
@Override
public Modifier createModifier(ModifierData data) {
return new ExampleModifier(this, data);
}
@Override
public String getCode() {
return "example-replace";
}
@Override
public Type getConfigType() {
return ExampleModifierData.class;
}
@Override
public String getName() {
return "Example - Replace in StringParam";
}
}
Great, everything is ready, so we can register our modifier factory inside beforeInit method (implement ExtensionInitListener into PetepExtension).
Extension modifier = helper.getExtension("modifier");
if (modifier == null) {
return;
}
if (((ModifierApi) modifier).registerModifierFactory(new ExampleModifierFactory())) {
Logger.getGlobal().info("Example modifier registered!");
}