Example usage for javax.swing.text Highlighter getHighlights

List of usage examples for javax.swing.text Highlighter getHighlights

Introduction

In this page you can find the example usage for javax.swing.text Highlighter getHighlights.

Prototype

public Highlight[] getHighlights();

Source Link

Document

Fetches the current list of highlights.

Usage

From source file:org.zaproxy.zap.extension.ascan.CustomScanDialog.java

private JButton getRemoveCustomButton() {
    if (removeCustomButton == null) {
        removeCustomButton = new JButton(Constant.messages.getString("ascan.custom.button.pt.rem"));
        removeCustomButton.setEnabled(false);

        removeCustomButton.addActionListener(new java.awt.event.ActionListener() {
            @Override//  w  w w. j  a v  a2  s .c  o  m
            public void actionPerformed(java.awt.event.ActionEvent e) {
                // Remove any selected injection points
                int userDefStart = getRequestField().getSelectionStart();
                if (userDefStart >= 0) {
                    int userDefEnd = getRequestField().getSelectionEnd();
                    Highlighter hltr = getRequestField().getHighlighter();
                    Highlight[] hls = hltr.getHighlights();

                    if (hls != null && hls.length > 0) {
                        for (Highlight hl : hls) {
                            if (selectionIncludesHighlight(userDefStart, userDefEnd, hl)) {
                                hltr.removeHighlight(hl);
                                injectionPointModel.removeElement(hl);
                            }
                        }
                    }

                    // Unselect the text
                    getRequestField().setSelectionStart(userDefEnd);
                    getRequestField().setSelectionEnd(userDefEnd);
                    getRequestField().getCaret().setVisible(true);
                }
            }
        });
    }

    return removeCustomButton;
}

From source file:org.zaproxy.zap.extension.customFire.CustomFireDialog.java

/**
 * /*from   w  w w  .j av a 2 s  . co  m*/
 * @param highlightList
 * @param httpReq
 * @return JList<Highlight> 
 * Request field-HTTPMsg is highlighted based on saved list of injection points and add them to new JList<Highlight>
 */
private JList<Highlight> getSavedInjectionPointList(ArrayList highlightList, String httpReq) {

    getRequestField().setText(httpReq);

    //If list instance is null then add elements from highlightList to list or remove everything from list and add from saved highlightList
    list.clear();

    //Highlight the coords
    Highlighter highLighter = getRequestField().getHighlighter();
    highLighter.removeAllHighlights();
    HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);

    for (int i = 0; i < highlightList.size(); i++) {
        String s = highlightList.get(i).toString();

        try {
            //Find co ords from string
            String[] s1 = s.split(",");
            int start = Integer.parseInt(s1[0].replaceAll("[\\D]", ""));
            int end = ((Number) NumberFormat.getInstance().parse(s1[1])).intValue();

            headerLength = httpReq.trim().length();
            urlPathStart = httpReq.indexOf("/", httpReq.indexOf("://") + 2) + 1;
            if (!(start < urlPathStart || (start < headerLength && end > headerLength))) {
                highLighter.addHighlight(start, end, painter);
                list.add(s);
            }

        } catch (BadLocationException | ParseException e) {
            e.printStackTrace();
        }

    }
    Highlight[] hls = highLighter.getHighlights();
    if (hls != null && hls.length > 0) {
        for (Highlight hl : hls) {
            injectionPointModel.addElement(hl);
        }
    }
    injectionPointList = new JList<>(injectionPointModel);
    ListCellRendererEx listCellrender = new ListCellRendererEx(getRequestField());
    injectionPointList.setCellRenderer(listCellrender);

    return injectionPointList;
}

From source file:org.zaproxy.zap.extension.customFire.CustomFireDialog.java

private JButton getRemoveCustomButton() {
    if (removeCustomButton == null) {
        removeCustomButton = new JButton(Constant.messages.getString("customFire.custom.button.pt.rem"));
        removeCustomButton.setEnabled(false);

        removeCustomButton.addActionListener(new java.awt.event.ActionListener() {
            @Override/*ww w .jav  a 2 s .c o  m*/
            public void actionPerformed(java.awt.event.ActionEvent e) {
                // Remove any selected injection points
                int userDefStart = getRequestField().getSelectionStart();
                if (userDefStart >= 0) {
                    int userDefEnd = getRequestField().getSelectionEnd();
                    Highlighter hltr = getRequestField().getHighlighter();
                    Highlight[] hls = hltr.getHighlights();

                    if (hls != null && hls.length > 0) {
                        for (Highlight hl : hls) {
                            if (selectionIncludesHighlight(userDefStart, userDefEnd, hl)) {
                                try {
                                    int i = list.indexOf(
                                            "[" + userDefStart + "," + userDefEnd + "]:" + getRequestField()
                                                    .getText(userDefStart, userDefEnd - userDefStart));
                                    list.remove(i);
                                } catch (BadLocationException e1) {
                                    e1.printStackTrace();
                                }
                                hltr.removeHighlight(hl);
                                injectionPointModel.removeElement(hl);
                            }
                        }
                    }

                    // Unselect the text
                    getRequestField().setSelectionStart(userDefEnd);
                    getRequestField().setSelectionEnd(userDefEnd);
                    getRequestField().getCaret().setVisible(true);
                }
            }
        });
    }

    return removeCustomButton;
}