GP-3329 - Search Highlights - Major update and refactor of the way Listing highlights work. Highlights are now more closely coupled with the fields that will paint them.

This commit is contained in:
dragonmacher
2023-04-21 12:43:42 -04:00
parent a21c2c4347
commit 84650030d0
130 changed files with 1096 additions and 919 deletions

View File

@@ -24,7 +24,7 @@ import docking.util.GraphicsUtils;
import docking.widgets.fieldpanel.field.SimpleTextField;
import docking.widgets.fieldpanel.internal.FieldBackgroundColorManager;
import docking.widgets.fieldpanel.internal.PaintContext;
import docking.widgets.fieldpanel.support.HighlightFactory;
import docking.widgets.fieldpanel.support.FieldHighlightFactory;
import docking.widgets.fieldpanel.support.RowColLocation;
import ghidra.util.ColorUtils;
@@ -52,7 +52,7 @@ public class ByteField extends SimpleTextField {
*/
public ByteField(String text, FontMetrics fontMetrics, int startX, int width,
boolean allowCursorAtEnd, int fieldOffset, BigInteger index,
HighlightFactory hlFactory) {
FieldHighlightFactory hlFactory) {
super(text, fontMetrics, startX, width, allowCursorAtEnd, hlFactory);
this.fieldOffset = fieldOffset;
@@ -64,7 +64,7 @@ public class ByteField extends SimpleTextField {
public void paint(JComponent c, Graphics g, PaintContext context,
Rectangle clip, FieldBackgroundColorManager colorManager, RowColLocation cursorLoc, int rowHeight) {
paintSelection(g, colorManager, 0);
paintHighlights(g, hlFactory.getHighlights(this, text, -1));
paintHighlights(g, hlFactory.createHighlights(this, text, -1));
g.setFont(metrics.getFont());
if (foregroundColor == null) {
foregroundColor = context.getForeground();

View File

@@ -65,7 +65,7 @@ public class ByteViewerComponent extends FieldPanel implements FieldMouseListene
private boolean indexUpdate = true;
private FieldLocation lastFieldLoc;
private ByteViewerHighlightProvider highlightProvider;
private ByteViewerHighlighter highlightProvider = new ByteViewerHighlighter();
private int highlightButton = MouseEvent.BUTTON2;
/**
@@ -86,7 +86,6 @@ public class ByteViewerComponent extends FieldPanel implements FieldMouseListene
this.bytesPerLine = bytesPerLine;
this.fm = fm;
this.layoutModel = layoutModel;
highlightProvider = new ByteViewerHighlightProvider();
setName(model.getName());
initialize();

View File

@@ -19,17 +19,14 @@ import java.awt.Color;
import docking.widgets.fieldpanel.support.Highlight;
import generic.theme.GColor;
import ghidra.app.util.HighlightProvider;
import ghidra.app.util.viewer.field.FieldFactory;
public class ByteViewerHighlightProvider implements HighlightProvider {
class ByteViewerHighlighter {
private static Highlight[] NO_HIGHLIGHTS = new Highlight[0];
private String highlightText;
private Color highlightColor = new GColor("color.bg.byteviewer.highlight");
@Override
public Highlight[] getHighlights(String text, Object obj,
Class<? extends FieldFactory> fieldFactoryClass, int cursorTextOffset) {
public Highlight[] createHighlights(String text) {
if (text.equals(highlightText)) {
return new Highlight[] { new Highlight(0, text.length() - 1, highlightColor) };
@@ -48,5 +45,4 @@ public class ByteViewerHighlightProvider implements HighlightProvider {
void setHighlightColor(Color color) {
this.highlightColor = color;
}
}

View File

@@ -21,9 +21,8 @@ import java.math.BigInteger;
import docking.widgets.fieldpanel.field.Field;
import docking.widgets.fieldpanel.support.Highlight;
import docking.widgets.fieldpanel.support.HighlightFactory;
import docking.widgets.fieldpanel.support.FieldHighlightFactory;
import ghidra.app.plugin.core.format.*;
import ghidra.app.util.HighlightProvider;
import ghidra.program.model.address.AddressOutOfBoundsException;
/**
@@ -45,7 +44,7 @@ class FieldFactory {
private Color editColor;
private Color separatorColor;
private int unitByteSize;
private HighlightFactory highlightFactory;
private FieldHighlightFactory highlightFactory;
/**
* Constructor
@@ -54,7 +53,7 @@ class FieldFactory {
* @param label label that is used as a renderer in the field viewer
*/
FieldFactory(DataFormatModel model, int bytesPerLine, int fieldOffset, FontMetrics fm,
HighlightProvider highlightProvider) {
ByteViewerHighlighter highlightProvider) {
this.model = model;
this.fieldOffset = fieldOffset;
this.fm = fm;
@@ -204,7 +203,6 @@ class FieldFactory {
separatorColor = c;
}
///////////////////////////////////////////////////////////////////
/**
* Get the padded string that has the given char value.
*/
@@ -221,16 +219,16 @@ class FieldFactory {
return new ByteField(value, fm, startX, width, false, fieldOffset, index, highlightFactory);
}
static class SimpleHighlightFactory implements HighlightFactory {
private final HighlightProvider provider;
static class SimpleHighlightFactory implements FieldHighlightFactory {
private final ByteViewerHighlighter provider;
public SimpleHighlightFactory(HighlightProvider provider) {
public SimpleHighlightFactory(ByteViewerHighlighter provider) {
this.provider = provider;
}
@Override
public Highlight[] getHighlights(Field field, String text, int cursorTextOffset) {
return provider.getHighlights(text, null, null, -1);
public Highlight[] createHighlights(Field field, String text, int cursorTextOffset) {
return provider.createHighlights(text);
}
}
}

View File

@@ -22,7 +22,7 @@ import java.math.BigInteger;
import docking.widgets.fieldpanel.field.Field;
import docking.widgets.fieldpanel.field.SimpleTextField;
import docking.widgets.fieldpanel.support.Highlight;
import docking.widgets.fieldpanel.support.HighlightFactory;
import docking.widgets.fieldpanel.support.FieldHighlightFactory;
import ghidra.app.plugin.core.format.ByteBlockInfo;
/**
@@ -37,7 +37,7 @@ class IndexFieldFactory {
private String noValueStr;
private int startX;
private Color missingValueColor;
private HighlightFactory highlightFactory = new DummyHighlightFactory();
private FieldHighlightFactory highlightFactory = new DummyHighlightFactory();
/**
* Constructor
@@ -136,14 +136,14 @@ class IndexFieldFactory {
missingValueColor = c;
}
static class DummyHighlightFactory implements HighlightFactory {
static class DummyHighlightFactory implements FieldHighlightFactory {
private final Highlight[] NO_HIGHLIGHTS = new Highlight[0];
public DummyHighlightFactory() {
}
@Override
public Highlight[] getHighlights(Field field, String text, int cursorTextOffset) {
public Highlight[] createHighlights(Field field, String text, int cursorTextOffset) {
return NO_HIGHLIGHTS;
}
}

View File

@@ -31,7 +31,7 @@ import ghidra.app.nav.*;
import ghidra.app.plugin.core.format.*;
import ghidra.app.services.ClipboardService;
import ghidra.app.services.ProgramManager;
import ghidra.app.util.HighlightProvider;
import ghidra.app.util.ListingHighlightProvider;
import ghidra.framework.model.*;
import ghidra.framework.options.SaveState;
import ghidra.framework.plugintool.PluginEvent;
@@ -787,12 +787,12 @@ public class ProgramByteViewerComponentProvider extends ByteViewerComponentProvi
}
@Override
public void removeHighlightProvider(HighlightProvider highlightProvider, Program p) {
public void removeHighlightProvider(ListingHighlightProvider highlightProvider, Program p) {
// currently unsupported
}
@Override
public void setHighlightProvider(HighlightProvider highlightProvider, Program p) {
public void setHighlightProvider(ListingHighlightProvider highlightProvider, Program p) {
// currently unsupported
}

View File

@@ -1404,8 +1404,8 @@ public class ByteViewerPlugin2Test extends AbstractGhidraHeadedIntegrationTest {
env.showTool();
ByteViewerComponent component = panel.getCurrentComponent();
ByteViewerHighlightProvider highlightProvider =
(ByteViewerHighlightProvider) getInstanceField("highlightProvider", component);
ByteViewerHighlighter highlightProvider =
(ByteViewerHighlighter) getInstanceField("highlightProvider", component);
String currentHighlightText = highlightProvider.getText();
assertNull(currentHighlightText);