Search in sources :

Example 41 with Element

use of javax.swing.text.Element in project java-swing-tips by aterai.

the class NoWrapEditorKit method processChangedLines.

private void processChangedLines(int offset, int length) throws BadLocationException {
    Element root = getDefaultRootElement();
    String content = getText(0, getLength());
    int startLine = root.getElementIndex(offset);
    int endLine = root.getElementIndex(offset + length);
    for (int i = startLine; i <= endLine; i++) {
        applyHighlighting(content, i);
    }
}
Also used : Element(javax.swing.text.Element)

Example 42 with Element

use of javax.swing.text.Element in project vcell by virtualcell.

the class BNGLDebugger method initialize.

@Deprecated
public void initialize() {
    // -------------------------------------------------- bngl panel
    JScrollPane bnglPanel = new JScrollPane();
    bnglTextArea = new JTextArea();
    bnglTextArea.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    // bnglTextArea.setFont(new Font("monospaced", Font.PLAIN, 14));
    lineNumberArea = new JTextArea("1");
    lineNumberArea.setBackground(Color.LIGHT_GRAY);
    lineNumberArea.setEditable(false);
    lineNumberArea.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    bnglTextArea.getDocument().addDocumentListener(new DocumentListener() {

        public String getNr() {
            lines = 1;
            int caretPosition = bnglTextArea.getDocument().getLength();
            Element root = bnglTextArea.getDocument().getDefaultRootElement();
            String nr = "1" + System.getProperty("line.separator");
            for (int i = 2; i < root.getElementIndex(caretPosition) + 2; i++) {
                nr += i + System.getProperty("line.separator");
                lines++;
            }
            return nr;
        }

        @Override
        public void changedUpdate(DocumentEvent de) {
            int oldLines = lines;
            String numbers = getNr();
            if (oldLines != lines) {
                lineNumberArea.setText(numbers);
            }
            lineNumberArea.getHighlighter().removeAllHighlights();
        }

        @Override
        public void insertUpdate(DocumentEvent de) {
            int oldLines = lines;
            String numbers = getNr();
            if (oldLines != lines) {
                lineNumberArea.setText(numbers);
            }
            lineNumberArea.getHighlighter().removeAllHighlights();
        }

        @Override
        public void removeUpdate(DocumentEvent de) {
            int oldLines = lines;
            String numbers = getNr();
            if (oldLines != lines) {
                lineNumberArea.setText(numbers);
            }
            lineNumberArea.getHighlighter().removeAllHighlights();
        }
    });
    bnglPanel.getViewport().add(bnglTextArea);
    bnglPanel.setRowHeaderView(lineNumberArea);
    bnglPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    JPanel upperPanel = new JPanel();
    upperPanel.setLayout(new BorderLayout());
    upperPanel.add(bnglPanel, BorderLayout.CENTER);
    // ---------------------------------------------------- exception panel
    JScrollPane exceptionPanel = new JScrollPane();
    exceptionTextArea = new JTextArea();
    exceptionTextArea.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    exceptionTextArea.setFont(new Font("monospaced", Font.PLAIN, 14));
    exceptionPanel.getViewport().add(exceptionTextArea);
    exceptionPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    JPanel lowerPanel = new JPanel();
    lowerPanel.setLayout(new BorderLayout());
    lowerPanel.add(exceptionPanel, BorderLayout.CENTER);
    // ---------------------------------------------------- all together now  :)
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setOneTouchExpandable(true);
    splitPane.setDividerLocation(350);
    splitPane.setResizeWeight(0.9);
    splitPane.setTopComponent(upperPanel);
    splitPane.setBottomComponent(lowerPanel);
    // ... Create menubar
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = menuBar.add(new JMenu("File"));
    // fileMenu.setMnemonic('F');
    // fileMenu.add(openAction);       // Note use of actions, not text.
    // fileMenu.add(saveAction);
    // fileMenu.addSeparator();
    fileMenu.add(exitAction);
    JPanel buttonPane = new JPanel();
    buttonParse.addActionListener(this);
    buttonSave.addActionListener(this);
    buttonExit.addActionListener(this);
    buttonPane.add(buttonParse);
    buttonPane.add(buttonSave);
    buttonPane.add(buttonExit);
    JPanel framePanel = new JPanel();
    framePanel.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.BOTH;
    framePanel.add(splitPane, gbc);
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.weightx = 0;
    gbc.weighty = 0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    framePanel.add(buttonPane, gbc);
    frame.add(framePanel);
    frame.setJMenuBar(menuBar);
    frame.pack();
    frame.setName("BnglDebugger");
    frame.setSize(900, 650);
    frame.setVisible(true);
}
Also used : JScrollPane(javax.swing.JScrollPane) DocumentListener(javax.swing.event.DocumentListener) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) JTextArea(javax.swing.JTextArea) GridBagLayout(java.awt.GridBagLayout) Element(javax.swing.text.Element) DocumentEvent(javax.swing.event.DocumentEvent) Font(java.awt.Font) BorderLayout(java.awt.BorderLayout) JSplitPane(javax.swing.JSplitPane) JMenuBar(javax.swing.JMenuBar) JMenu(javax.swing.JMenu)

Example 43 with Element

use of javax.swing.text.Element in project sonarqube by SonarSource.

the class TextLineNumber method getOffsetY.

/*
   * Determine the Y offset for the current row
   */
private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics) throws BadLocationException {
    // Get the bounding rectangle of the row
    Rectangle r = component.modelToView(rowStartOffset);
    int lineHeight = fontMetrics.getHeight();
    int y = r.y + r.height;
    int descent = 0;
    if (// default font is being used
    r.height == lineHeight) {
        descent = fontMetrics.getDescent();
    } else // We need to check all the attributes for font changes
    {
        if (fonts == null)
            fonts = new HashMap<>();
        Element root = component.getDocument().getDefaultRootElement();
        int index = root.getElementIndex(rowStartOffset);
        Element line = root.getElement(index);
        for (int i = 0; i < line.getElementCount(); i++) {
            Element child = line.getElement(i);
            AttributeSet as = child.getAttributes();
            String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily);
            Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize);
            String key = fontFamily + fontSize;
            FontMetrics fm = fonts.get(key);
            if (fm == null) {
                Font font = new Font(fontFamily, Font.PLAIN, fontSize);
                fm = component.getFontMetrics(font);
                fonts.put(key, fm);
            }
            descent = Math.max(descent, fm.getDescent());
        }
    }
    return y - descent;
}
Also used : HashMap(java.util.HashMap) AttributeSet(javax.swing.text.AttributeSet) FontMetrics(java.awt.FontMetrics) Element(javax.swing.text.Element) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Font(java.awt.Font)

Example 44 with Element

use of javax.swing.text.Element in project sonarqube by SonarSource.

the class TextLineNumber method isCurrentLine.

/*
   * We need to know if the caret is currently positioned on the line we
   * are about to paint so the line number can be highlighted.
   */
private boolean isCurrentLine(int rowStartOffset) {
    int caretPosition = component.getCaretPosition();
    Element root = component.getDocument().getDefaultRootElement();
    return root.getElementIndex(rowStartOffset) == root.getElementIndex(caretPosition);
}
Also used : Element(javax.swing.text.Element) Point(java.awt.Point)

Example 45 with Element

use of javax.swing.text.Element in project sonarqube by SonarSource.

the class TextLineNumber method getTextLineNumber.

/*
   * Get the line number to be drawn. The empty string will be returned
   * when a line of text has wrapped.
   */
protected String getTextLineNumber(int rowStartOffset) {
    Element root = component.getDocument().getDefaultRootElement();
    int index = root.getElementIndex(rowStartOffset);
    Element line = root.getElement(index);
    if (line.getStartOffset() == rowStartOffset)
        return String.valueOf(index + 1);
    else
        return "";
}
Also used : Element(javax.swing.text.Element) Point(java.awt.Point)

Aggregations

Element (javax.swing.text.Element)100 BadLocationException (javax.swing.text.BadLocationException)35 Point (java.awt.Point)19 AttributeSet (javax.swing.text.AttributeSet)15 Document (javax.swing.text.Document)15 HTMLDocument (javax.swing.text.html.HTMLDocument)11 FontMetrics (java.awt.FontMetrics)6 Dimension (java.awt.Dimension)5 Rectangle (java.awt.Rectangle)5 View (javax.swing.text.View)5 Font (java.awt.Font)4 Insets (java.awt.Insets)4 IOException (java.io.IOException)4 AbstractDocument (javax.swing.text.AbstractDocument)4 StyledDocument (javax.swing.text.StyledDocument)4 java.awt (java.awt)3 Objects (java.util.Objects)3 javax.swing (javax.swing)3 AbstractElement (javax.swing.text.AbstractDocument.AbstractElement)3 HTML (javax.swing.text.html.HTML)3