Example usage for javax.swing.text DefaultHighlighter DefaultHighlighter

List of usage examples for javax.swing.text DefaultHighlighter DefaultHighlighter

Introduction

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

Prototype

public DefaultHighlighter() 

Source Link

Document

Creates a new DefaultHighlighther object.

Usage

From source file:TextFieldDemo.java

public TextFieldDemo() {
    initComponents();//from  w w  w  .  j a  v a2s  . co m

    InputStream in = getClass().getResourceAsStream("content.txt");
    try {
        textArea.read(new InputStreamReader(in), null);
    } catch (IOException e) {
        e.printStackTrace();
    }

    hilit = new DefaultHighlighter();
    painter = new DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
    textArea.setHighlighter(hilit);

    entryBg = entry.getBackground();
    entry.getDocument().addDocumentListener(this);

    InputMap im = entry.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = entry.getActionMap();
    im.put(KeyStroke.getKeyStroke("ESCAPE"), CANCEL_ACTION);
    am.put(CANCEL_ACTION, new CancelAction());
}

From source file:org.owasp.jbrofuzz.ui.viewers.WindowViewerFrame.java

/**
 * <p>/*from   w  w  w .  j  a  v a2s. co m*/
 * The window viewer that gets launched for each request within the
 * corresponding panel.
 * </p>
 * 
 * @param parent The parent panel that the frame will belong to
 * @param name The full file name of the file location to be opened
 * 
 * @author subere@uncon.org
 * @version 2.0
 * @since 2.0
 */
public WindowViewerFrame(final AbstractPanel parent, final String name) {

    super("JBroFuzz - File Viewer - " + name);

    setIconImage(ImageCreator.IMG_FRAME.getImage());

    // The container pane
    final Container pane = getContentPane();
    pane.setLayout(new BorderLayout());

    // Define the Panel
    final JPanel listPanel = new JPanel();
    listPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(name),
            BorderFactory.createEmptyBorder(1, 1, 1, 1)));
    listPanel.setLayout(new BorderLayout());

    // Get the preferences for wrapping lines of text
    final boolean wrapText = JBroFuzz.PREFS.getBoolean(JBroFuzzPrefs.FUZZING[3].getId(), false);

    if (wrapText) {

        listTextArea = new JTextPane();

    } else {

        listTextArea = new NonWrappingTextPane();

    }

    // Refine the Text Area
    listTextArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
    listTextArea.setEditable(false);

    // Define the search area
    entry = new JTextField(10);
    status = new JLabel("Enter text to search:");

    // Initialise the highlighter on the text area
    hilit = new DefaultHighlighter();
    painter = new DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
    listTextArea.setHighlighter(hilit);

    entryBg = entry.getBackground();
    entry.getDocument().addDocumentListener(this);

    final InputMap im = entry.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    final ActionMap am = entry.getActionMap();
    im.put(KeyStroke.getKeyStroke("ESCAPE"), CANCEL_ACTION);
    am.put(CANCEL_ACTION, new CancelAction());

    // Right click: Cut, Copy, Paste, Select All
    AbstractPanel.popupText(listTextArea, false, true, false, true);

    // Define the Scroll Pane for the Text Area
    final JScrollPane listTextScrollPane = new JScrollPane(listTextArea);
    listTextScrollPane.setVerticalScrollBarPolicy(20);
    listTextScrollPane.setHorizontalScrollBarPolicy(30);

    // Define the progress bar
    final JProgressBar progressBar = new JProgressBar();
    progressBar.setString("   ");
    progressBar.setStringPainted(true);

    // Define the bottom panel with the progress bar
    final JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 15, 15));
    bottomPanel.add(status);
    bottomPanel.add(entry);
    bottomPanel.add(progressBar);

    listTextArea.setCaretPosition(0);
    // doSyntaxHighlight();
    /*      listTextArea.setEditorKit(new StyledEditorKit() {
            
             private static final long serialVersionUID = -6085642347022880064L;
            
             @Override
             public Document createDefaultDocument() {
    return new TextHighlighter();
             }
            
          });
    */

    listPanel.add(listTextScrollPane);

    // Global Frame Issues
    pane.add(listPanel, BorderLayout.CENTER);
    pane.add(bottomPanel, BorderLayout.SOUTH);

    this.setLocation(parent.getLocationOnScreen().x + 100, parent.getLocationOnScreen().y + 20);
    this.setSize(SIZE_X, SIZE_Y);

    setResizable(true);
    setVisible(true);
    setMinimumSize(new Dimension(SIZE_X, SIZE_Y));
    setDefaultCloseOperation(2);

    listTextArea.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(final KeyEvent ke) {
            if (ke.getKeyCode() == 27) {
                WindowViewerFrame.this.dispose();
            }
            if (ke.getKeyCode() == 10) {
                search();
            }
        }
    });

    entry.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(final KeyEvent ke) {
            if (ke.getKeyCode() == 10) {
                search();
            }
        }
    });

    class FileLoader extends SwingWorker<String, Object> { // NO_UCD

        @Override
        public String doInBackground() {

            progressBar.setIndeterminate(true);

            String dbType = JBroFuzz.PREFS.get(JBroFuzzPrefs.DBSETTINGS[11].getId(), "-1");

            if (dbType.equals("SQLite") || dbType.equals("CouchDB")) {

                String sessionId = parent.getFrame().getJBroFuzz().getWindow().getPanelFuzzing()
                        .getSessionName();

                if (sessionId == null || sessionId.equals("null")) {
                    sessionId = JBroFuzz.PREFS.get("sessionId", "");
                }

                Logger.log("Reading Session: " + sessionId + " with name: " + name, 3);

                MessageContainer mc = parent.getFrame().getJBroFuzz().getStorageHandler()
                        .readFuzzFile(name, sessionId, parent.getFrame().getJBroFuzz().getWindow()).get(0);

                listTextArea.setText("Date: " + mc.getEndDateFull() + "\n" + "FileName: " + mc.getFileName()
                        + "\n" + "URL: " + mc.getTextURL() + "\n" + "Payload: " + mc.getPayload() + "\n"
                        + "EncodedPayload: " + mc.getEncodedPayload() + "\n" + "TextRequest:"
                        + mc.getTextRequest() + "\n" + "Message: " + mc.getMessage() + "\n" + "Status: "
                        + mc.getStatus() + "\n"

                );

            } else {
                Logger.log("Loading data from file", 3);
                final File inputFile = new File(parent.getFrame().getJBroFuzz().getWindow().getPanelFuzzing()
                        .getFrame().getJBroFuzz().getStorageHandler().getLocationURIString(), name + ".html");

                listTextArea.setText(

                        FileHandler.readFile(inputFile)

                );
            }
            return "done";
        }

        @Override
        protected void done() {
            progressBar.setIndeterminate(false);
            progressBar.setValue(100);
            listTextArea.repaint();
        }
    }

    (new FileLoader()).execute();

}

From source file:volker.streaming.music.gui.FormatPanel.java

private void initComponents() {
    formatLabel = new JLabel("How should your track info be displayed:");
    formatArea = new JTextArea(config.getFormat());
    formatLighter = new DefaultHighlighter();
    // TODO allow configuration of this color
    formatPainter = new DefaultHighlighter.DefaultHighlightPainter(new Color(200, 200, 255));
    formatArea.setHighlighter(formatLighter);
    formatArea.getDocument().addDocumentListener(new DocumentListener() {
        @Override//from   www. ja  v  a2  s. c  o  m
        public void removeUpdate(DocumentEvent e) {
            formatUpdated();
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            formatUpdated();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            formatUpdated();
        }
    });

    ImageIcon infoIcon = null;
    try {
        InputStream is = getClass().getResourceAsStream("info.png");
        if (is == null) {
            LOG.error("Couldn't find the info image.");
        } else {
            infoIcon = new ImageIcon(ImageIO.read(is));
            is.close();
        }
    } catch (IOException e1) {
        LOG.error("Couldn't find the info image.", e1);
    }
    if (infoIcon == null) {
        formatInfoButton = new JButton("?");
    } else {
        formatInfoButton = new JButton(infoIcon);
        formatInfoButton.setBorder(BorderFactory.createEmptyBorder());
    }

    formatInfoButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            showFormatHelp();
        }
    });

    templateLabel = new JLabel("Your template:");

    tagLabel = new JLabel("Available tags:");
    tagList = new JList<String>(new AbstractListModel<String>() {
        private static final long serialVersionUID = -8886588605378873151L;

        @Override
        public int getSize() {
            return properTags.size();
        }

        @Override
        public String getElementAt(int index) {
            return properTags.get(index);
        }
    });
    tagScrollPane = new JScrollPane(tagList);

    previewLabel = new JLabel("Preview:");
    previewField = new JTextField();
    previewField.setEditable(false);
    previewField.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    previewField.setBackground(new Color(255, 255, 150));

    formatUpdated();
    highlightTags();

    nullMessageLabel = new JLabel("Message to display when no song is found:");
    nullMessageField = new JTextField(config.getNoTrackMessage() == null ? "" : config.getNoTrackMessage());
    nullMessageField.getDocument().addDocumentListener(new DocumentListener() {
        public void action() {
            config.setNoTrackMessage(nullMessageField.getText());
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            action();
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            action();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            action();
        }
    });

    hline = new JSeparator(SwingConstants.HORIZONTAL);
    fileLabel = new JLabel("Location of text file:");
    fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fileField = new JTextField(15);
    if (config.getOutputFile() != null) {
        fileField.setText(config.getOutputFile().getAbsolutePath());
    }
    fileField.getDocument().addDocumentListener(new DocumentListener() {
        public void action() {
            config.setOutputFile(new File(fileField.getText()));
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            action();
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            action();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            action();
        }
    });
    fileButton = new JButton("Browse");
    fileButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            browseFile();
        }
    });
}