use of javax.swing.text.Highlighter.HighlightPainter in project zaproxy by zaproxy.
the class CustomScanDialog method getAddCustomButton.
private JButton getAddCustomButton() {
if (addCustomButton == null) {
addCustomButton = new JButton(Constant.messages.getString("ascan.custom.button.pt.add"));
addCustomButton.setEnabled(false);
addCustomButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
// Add the selected injection point
int userDefStart = getRequestField().getSelectionStart();
if (userDefStart >= 0) {
int userDefEnd = getRequestField().getSelectionEnd();
Highlighter hl = getRequestField().getHighlighter();
HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
try {
Highlight hlt = (Highlight) hl.addHighlight(userDefStart, userDefEnd, painter);
injectionPointModel.addElement(hlt);
// Unselect the text
getRequestField().setSelectionStart(userDefEnd);
getRequestField().setSelectionEnd(userDefEnd);
getRequestField().getCaret().setVisible(true);
} catch (BadLocationException e1) {
logger.error(e1.getMessage(), e1);
}
}
}
});
}
return addCustomButton;
}
use of javax.swing.text.Highlighter.HighlightPainter in project zaproxy by zaproxy.
the class HttpPanelSyntaxHighlightTextArea method highlight.
protected void highlight(int start, int end) {
Highlighter hilite = this.getHighlighter();
HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(DisplayUtils.getHighlightColor());
try {
removeAllHighlights();
hilite.addHighlight(start, end, painter);
this.setCaretPosition(start);
} catch (BadLocationException e) {
log.error(e.getMessage(), e);
}
}
use of javax.swing.text.Highlighter.HighlightPainter in project java-swing-tips by aterai.
the class ComboKeyHandler method makeComboBox.
private static JComboBox<String> makeComboBox(String... model) {
HighlightPainter highlightPainter = new DefaultHighlightPainter(Color.YELLOW);
return new JComboBox<String>(model) {
@Override
public void updateUI() {
super.updateUI();
JTextField field = new JTextField(" ");
field.setOpaque(true);
field.setBorder(BorderFactory.createEmptyBorder());
ListCellRenderer<? super String> renderer = getRenderer();
setRenderer((list, value, index, isSelected, cellHasFocus) -> {
String pattern = ((JTextField) getEditor().getEditorComponent()).getText();
if (index >= 0 && !pattern.isEmpty()) {
Highlighter highlighter = field.getHighlighter();
highlighter.removeAllHighlights();
String txt = Objects.toString(value, "");
field.setText(txt);
addHighlight(highlighter, Pattern.compile(pattern).matcher(txt));
field.setBackground(isSelected ? new Color(0xAA_64_AA_FF, true) : Color.WHITE);
return field;
}
return renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
});
}
private void addHighlight(Highlighter highlighter, Matcher matcher) {
int pos = 0;
try {
while (matcher.find(pos) && !matcher.group().isEmpty()) {
int start = matcher.start();
int end = matcher.end();
highlighter.addHighlight(start, end, highlightPainter);
pos = end;
}
} catch (BadLocationException ex) {
// should never happen
RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
wrap.initCause(ex);
throw wrap;
}
}
};
}
use of javax.swing.text.Highlighter.HighlightPainter in project omegat by omegat-org.
the class ProtectedPartsMarker method getMarksForEntry.
@Override
public List<Mark> getMarksForEntry(SourceTextEntry ste, String sourceText, String translationText, boolean isActive) throws Exception {
HighlightPainter painter;
AttributeSet attrs;
if (((EditorController) Core.getEditor()).getOrientation() == Document3.ORIENTATION.ALL_LTR) {
attrs = ATTRIBUTES_LTR;
painter = null;
} else {
attrs = null;
painter = PAINTER_RTL;
}
if (ste.getProtectedParts().length == 0) {
return null;
}
if (sourceText == null && translationText == null) {
return null;
}
List<Mark> r = new ArrayList<Mark>();
// find protected parts
for (ProtectedPart pp : ste.getProtectedParts()) {
if (sourceText != null) {
int pos = -1;
while ((pos = sourceText.indexOf(pp.getTextInSourceSegment(), pos + 1)) >= 0) {
Mark m = new Mark(Mark.ENTRY_PART.SOURCE, pos, pos + pp.getTextInSourceSegment().length());
m.painter = painter;
m.attributes = attrs;
m.toolTipText = escapeHtml(pp.getDetailsFromSourceFile());
r.add(m);
}
}
if (translationText != null) {
int pos = -1;
while ((pos = translationText.indexOf(pp.getTextInSourceSegment(), pos + 1)) >= 0) {
Mark m = new Mark(Mark.ENTRY_PART.TRANSLATION, pos, pos + pp.getTextInSourceSegment().length());
m.painter = painter;
m.attributes = attrs;
m.toolTipText = escapeHtml(pp.getDetailsFromSourceFile());
r.add(m);
}
}
}
return r;
}
use of javax.swing.text.Highlighter.HighlightPainter in project zaproxy by zaproxy.
the class HttpPanelSyntaxHighlightTextArea method highlightEntryParser.
// Parse the TextArea data and search the HighlightEntry strings
// Highlight all found strings
private void highlightEntryParser(HighlightSearchEntry entry) {
String text;
int lastPos = 0;
text = this.getText();
Highlighter hilite = this.getHighlighter();
HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(entry.getColor());
while ((lastPos = text.indexOf(entry.getToken(), lastPos)) > -1) {
try {
hilite.addHighlight(lastPos, lastPos + entry.getToken().length(), painter);
lastPos += entry.getToken().length();
} catch (BadLocationException e) {
log.warn("Could not highlight entry", e);
}
}
}
Aggregations