|
| 1 | +package software.coley.recaf.ui.pane.search; |
| 2 | + |
| 3 | +import jakarta.annotation.Nonnull; |
| 4 | +import jakarta.annotation.Nullable; |
| 5 | +import jakarta.enterprise.context.Dependent; |
| 6 | +import jakarta.inject.Inject; |
| 7 | +import javafx.beans.property.SimpleStringProperty; |
| 8 | +import javafx.beans.property.StringProperty; |
| 9 | +import javafx.collections.FXCollections; |
| 10 | +import javafx.collections.ListChangeListener; |
| 11 | +import javafx.collections.ObservableList; |
| 12 | +import javafx.geometry.Insets; |
| 13 | +import javafx.geometry.Pos; |
| 14 | +import javafx.scene.Node; |
| 15 | +import javafx.scene.control.Button; |
| 16 | +import javafx.scene.control.ComboBox; |
| 17 | +import javafx.scene.control.TextField; |
| 18 | +import javafx.scene.layout.ColumnConstraints; |
| 19 | +import javafx.scene.layout.GridPane; |
| 20 | +import javafx.scene.layout.Priority; |
| 21 | +import org.kordamp.ikonli.carbonicons.CarbonIcons; |
| 22 | +import org.reactfx.EventStreams; |
| 23 | +import software.coley.collections.Lists; |
| 24 | +import software.coley.recaf.services.cell.CellConfigurationService; |
| 25 | +import software.coley.recaf.services.navigation.Actions; |
| 26 | +import software.coley.recaf.services.search.SearchService; |
| 27 | +import software.coley.recaf.services.search.match.StringPredicate; |
| 28 | +import software.coley.recaf.services.search.match.StringPredicateProvider; |
| 29 | +import software.coley.recaf.services.search.query.InstructionQuery; |
| 30 | +import software.coley.recaf.services.search.query.Query; |
| 31 | +import software.coley.recaf.services.workspace.WorkspaceManager; |
| 32 | +import software.coley.recaf.ui.control.ActionButton; |
| 33 | +import software.coley.recaf.ui.control.BoundBiDiComboBox; |
| 34 | +import software.coley.recaf.ui.control.richtext.Editor; |
| 35 | +import software.coley.recaf.util.Lang; |
| 36 | +import software.coley.recaf.util.RegexUtil; |
| 37 | +import software.coley.recaf.util.ToStringConverter; |
| 38 | + |
| 39 | +import java.time.Duration; |
| 40 | +import java.util.Collections; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Objects; |
| 43 | +import java.util.UUID; |
| 44 | + |
| 45 | +import static software.coley.recaf.services.search.match.StringPredicateProvider.KEY_ANYTHING; |
| 46 | +import static software.coley.recaf.services.search.match.StringPredicateProvider.KEY_NOTHING; |
| 47 | + |
| 48 | +/** |
| 49 | + * Instruction disassembly search pane. |
| 50 | + * |
| 51 | + * @author Matt Coley |
| 52 | + */ |
| 53 | +@Dependent |
| 54 | +public class InstructionSearchPane extends AbstractSearchPane { |
| 55 | + private final StringPredicateProvider stringPredicateProvider; |
| 56 | + private final ObservableList<Line> lines = FXCollections.observableArrayList(); |
| 57 | + |
| 58 | + @Inject |
| 59 | + public InstructionSearchPane(@Nonnull WorkspaceManager workspaceManager, |
| 60 | + @Nonnull SearchService searchService, |
| 61 | + @Nonnull CellConfigurationService configurationService, |
| 62 | + @Nonnull Actions actions, |
| 63 | + @Nonnull StringPredicateProvider stringPredicateProvider) { |
| 64 | + super(workspaceManager, searchService, configurationService, actions); |
| 65 | + |
| 66 | + this.stringPredicateProvider = stringPredicateProvider; |
| 67 | + |
| 68 | + GridPane input = new GridPane(); |
| 69 | + ColumnConstraints colTexts = new ColumnConstraints(); |
| 70 | + ColumnConstraints colCombos = new ColumnConstraints(); |
| 71 | + colTexts.setFillWidth(true); |
| 72 | + input.setAlignment(Pos.CENTER); |
| 73 | + input.getColumnConstraints().addAll(colTexts, colCombos); |
| 74 | + input.setHgap(10); |
| 75 | + input.setPadding(new Insets(10)); |
| 76 | + |
| 77 | + List<String> stringPredicates = stringPredicateProvider.getBiStringMatchers().keySet().stream() |
| 78 | + .filter(s -> !KEY_ANYTHING.equals(s) && !KEY_NOTHING.equals(s)) |
| 79 | + .sorted().toList(); |
| 80 | + lines.addListener((ListChangeListener<Line>) change -> { |
| 81 | + while (change.next()) { |
| 82 | + for (Line line : change.getAddedSubList()) { |
| 83 | + StringProperty stringValue = line.text(); |
| 84 | + StringProperty stringPredicateId = line.predicateId(); |
| 85 | + TextField textField = new TextField(); |
| 86 | + textField.setId(line.uuid().toString()); |
| 87 | + stringValue.bind(textField.textProperty()); |
| 88 | + ComboBox<String> modeCombo = new BoundBiDiComboBox<>(stringPredicateId, stringPredicates, |
| 89 | + ToStringConverter.from(s -> Lang.get(StringPredicate.TRANSLATION_PREFIX + s))); |
| 90 | + modeCombo.getSelectionModel().select(StringPredicateProvider.KEY_CONTAINS); |
| 91 | + EventStreams.changesOf(stringValue) |
| 92 | + .or(EventStreams.changesOf(stringPredicateId)) |
| 93 | + .reduceSuccessions(Collections::singletonList, Lists::add, Duration.ofMillis(Editor.MEDIUM_DELAY_MS)) |
| 94 | + .addObserver(unused -> search()); |
| 95 | + GridPane.setHgrow(textField, Priority.ALWAYS); |
| 96 | + input.addRow(input.getRowCount(), textField, modeCombo, new ActionButton(CarbonIcons.TRASH_CAN, () -> lines.remove(line))); |
| 97 | + } |
| 98 | + for (Line line : change.getRemoved()) { |
| 99 | + for (Node child : input.getChildrenUnmodifiable()) { |
| 100 | + if (line.uuid().toString().equals(child.getId())) { |
| 101 | + Integer nodeRow = GridPane.getRowIndex(child); |
| 102 | + if (nodeRow != null) { |
| 103 | + input.getRowConstraints().remove(nodeRow.intValue()); |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + // Add an initial line, and a button to facilitate adding additional lines |
| 113 | + lines.add(new Line(UUID.randomUUID(), new SimpleStringProperty(), new SimpleStringProperty())); |
| 114 | + Button addLine = new ActionButton(CarbonIcons.ADD_ALT, Lang.getBinding("dialog.search.add-instruction-line"), () -> { |
| 115 | + lines.add(new Line(UUID.randomUUID(), new SimpleStringProperty(), new SimpleStringProperty())); |
| 116 | + }); |
| 117 | + input.addRow(0, addLine); |
| 118 | + setInputs(input); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return List of models to match against lines of disassembled instructions. |
| 123 | + */ |
| 124 | + @Nonnull |
| 125 | + public ObservableList<Line> getLinePredicates() { |
| 126 | + return lines; |
| 127 | + } |
| 128 | + |
| 129 | + @Nullable |
| 130 | + @Override |
| 131 | + protected Query buildQuery() { |
| 132 | + List<StringPredicate> predicates = lines.stream().map(line -> { |
| 133 | + String search = line.text().get(); |
| 134 | + String id = line.predicateId().get(); |
| 135 | + |
| 136 | + // Skip for blank input |
| 137 | + if (search.isBlank()) |
| 138 | + return null; |
| 139 | + |
| 140 | + // Validate regex input |
| 141 | + if (id.contains("regex") && !RegexUtil.validate(search).valid()) |
| 142 | + return null; |
| 143 | + |
| 144 | + // May be null if no such id exists as a predicate, but we filter out nulls anyways. |
| 145 | + return stringPredicateProvider.newBiStringPredicate(id, search); |
| 146 | + }).filter(Objects::nonNull).toList(); |
| 147 | + return new InstructionQuery(predicates); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Model of a single line of text to search against disassembled code. |
| 152 | + * |
| 153 | + * @param uuid |
| 154 | + * Unique identifier for this input. |
| 155 | + * @param text |
| 156 | + * Text to match on the line. |
| 157 | + * @param predicateId |
| 158 | + * Predicate identifier for {@link StringPredicateProvider} lookups. |
| 159 | + */ |
| 160 | + public record Line(@Nonnull UUID uuid, @Nonnull StringProperty text, @Nonnull StringProperty predicateId) {} |
| 161 | +} |
0 commit comments